]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/glx.c
XCB: reset the X11 screen saver when displaying a picture
[vlc] / modules / video_output / xcb / glx.c
1 /**
2  * @file glx.c
3  * @brief GLX video output module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2004 the VideoLAN team
7  * Copyright © 2009 Rémi Denis-Courmont
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  ****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <assert.h>
30
31 #include <xcb/xcb.h>
32 #include <X11/Xlib-xcb.h>
33 #include <GL/glx.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_xlib.h>
38 #include <vlc_vout_display.h>
39 #include <vlc_vout_opengl.h>
40 #include "../opengl.h"
41
42 #include "xcb_vlc.h"
43
44 static int  Open (vlc_object_t *);
45 static void Close (vlc_object_t *);
46
47 /*
48  * Module descriptor
49  */
50 vlc_module_begin ()
51     set_shortname (N_("GLX"))
52     set_description (N_("GLX video output (XCB)"))
53     set_category (CAT_VIDEO)
54     set_subcategory (SUBCAT_VIDEO_VOUT)
55     set_capability ("vout display", 50)
56     set_callbacks (Open, Close)
57
58     add_shortcut ("xcb-glx", "glx", "opengl", "xid")
59 vlc_module_end ()
60
61 struct vout_display_sys_t
62 {
63     Display *display; /* Xlib instance */
64     vout_window_t *embed; /* VLC window (when windowed) */
65
66     xcb_cursor_t cursor; /* blank cursor */
67     xcb_window_t window; /* drawable X window */
68     xcb_window_t glwin; /* GLX window */
69     bool visible; /* whether to draw */
70     bool v1_3; /* whether GLX >= 1.3 is available */
71
72     GLXContext ctx;
73     vout_opengl_t gl;
74     vout_display_opengl_t vgl;
75     picture_pool_t *pool; /* picture pool */
76 };
77
78 static picture_pool_t *Pool (vout_display_t *, unsigned);
79 static void PictureRender (vout_display_t *, picture_t *);
80 static void PictureDisplay (vout_display_t *, picture_t *);
81 static int Control (vout_display_t *, int, va_list);
82 static void Manage (vout_display_t *);
83
84 static void SwapBuffers (vout_opengl_t *gl);
85
86 static vout_window_t *MakeWindow (vout_display_t *vd)
87 {
88     vout_window_cfg_t wnd_cfg;
89
90     memset (&wnd_cfg, 0, sizeof (wnd_cfg));
91     wnd_cfg.type = VOUT_WINDOW_TYPE_XID;
92     wnd_cfg.x = var_InheritInteger (vd, "video-x");
93     wnd_cfg.y = var_InheritInteger (vd, "video-y");
94     wnd_cfg.width  = vd->cfg->display.width;
95     wnd_cfg.height = vd->cfg->display.height;
96
97     vout_window_t *wnd = vout_display_NewWindow (vd, &wnd_cfg);
98     if (wnd == NULL)
99         msg_Err (vd, "parent window not available");
100     return wnd;
101 }
102
103 static const xcb_screen_t *
104 FindWindow (vout_display_t *vd, xcb_connection_t *conn,
105             unsigned *restrict pnum, uint8_t *restrict pdepth,
106             uint16_t *restrict pwidth, uint16_t *restrict pheight)
107 {
108     vout_display_sys_t *sys = vd->sys;
109
110     xcb_get_geometry_reply_t *geo =
111         xcb_get_geometry_reply (conn,
112             xcb_get_geometry (conn, sys->embed->handle.xid), NULL);
113     if (geo == NULL)
114     {
115         msg_Err (vd, "parent window not valid");
116         return NULL;
117     }
118
119     xcb_window_t root = geo->root;
120     *pdepth = geo->depth;
121     *pwidth = geo->width;
122     *pheight = geo->height;
123     free (geo);
124
125     /* Find the selected screen */
126     const xcb_setup_t *setup = xcb_get_setup (conn);
127     const xcb_screen_t *screen = NULL;
128     unsigned num = 0;
129
130     for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
131          i.rem > 0;
132          xcb_screen_next (&i))
133     {
134         if (i.data->root == root)
135         {
136             screen = i.data;
137             break;
138         }
139         num++;
140     }
141
142     if (screen == NULL)
143     {
144         msg_Err (vd, "parent window screen not found");
145         return NULL;
146     }
147     msg_Dbg (vd, "using screen 0x%"PRIx32 " (number: %u)", root, num);
148     *pnum = num;
149     return screen;
150 }
151
152 static bool CheckGLX (vout_display_t *vd, Display *dpy, bool *restrict pv13)
153 {
154     int major, minor;
155     bool ok = false;
156
157     if (!glXQueryVersion (dpy, &major, &minor))
158         msg_Dbg (vd, "GLX extension not available");
159     else
160     if (major != 1)
161         msg_Dbg (vd, "GLX extension version %d.%d unknown", major, minor);
162     else
163     if (minor < 2)
164         msg_Dbg (vd, "GLX extension version %d.%d too old", major, minor);
165     else
166     {
167         msg_Dbg (vd, "using GLX extension version %d.%d", major, minor);
168         ok = true;
169         *pv13 = minor >= 3;
170     }
171     return ok;
172 }
173
174 static int CreateWindow (vout_display_t *vd, xcb_connection_t *conn,
175                          uint_fast8_t depth, xcb_visualid_t vid,
176                          uint_fast16_t width, uint_fast16_t height)
177 {
178     vout_display_sys_t *sys = vd->sys;
179     const uint32_t mask = XCB_CW_EVENT_MASK;
180     const uint32_t values[] = {
181         /* XCB_CW_EVENT_MASK */
182         XCB_EVENT_MASK_VISIBILITY_CHANGE,
183     };
184     xcb_void_cookie_t cc, cm;
185
186     cc = xcb_create_window_checked (conn, depth, sys->window,
187                                     sys->embed->handle.xid, 0, 0,
188                                     width, height, 0,
189                                     XCB_WINDOW_CLASS_INPUT_OUTPUT,
190                                     vid, mask, values);
191     cm = xcb_map_window_checked (conn, sys->window);
192     if (CheckError (vd, conn, "cannot create X11 window", cc)
193      || CheckError (vd, conn, "cannot map X11 window", cm))
194         return VLC_EGENERIC;
195
196     msg_Dbg (vd, "using X11 window %08"PRIx32, sys->window);
197     return VLC_SUCCESS;
198 }
199
200 /**
201  * Probe the X server.
202  */
203 static int Open (vlc_object_t *obj)
204 {
205     if (!vlc_xlib_init (obj))
206         return VLC_EGENERIC;
207
208     vout_display_t *vd = (vout_display_t *)obj;
209     vout_display_sys_t *sys = malloc (sizeof (*sys));
210
211     if (sys == NULL)
212         return VLC_ENOMEM;
213
214     vd->sys = sys;
215     sys->pool = NULL;
216     sys->gl.sys = NULL;
217
218     /* Get window */
219     sys->embed = MakeWindow (vd);
220     if (sys->embed == NULL)
221     {
222         free (sys);
223         return VLC_EGENERIC;
224     }
225
226     /* Connect to X server */
227     Display *dpy = XOpenDisplay (sys->embed->display.x11);
228     if (dpy == NULL)
229     {
230         vout_display_DeleteWindow (vd, sys->embed);
231         free (sys);
232         return VLC_EGENERIC;
233     }
234     sys->display = dpy;
235     sys->ctx = NULL;
236     XSetEventQueueOwner (dpy, XCBOwnsEventQueue);
237
238     if (!CheckGLX (vd, dpy, &sys->v1_3))
239         goto error;
240
241     xcb_connection_t *conn = XGetXCBConnection (dpy);
242     assert (conn);
243     RegisterMouseEvents (obj, conn, sys->embed->handle.xid);
244
245     /* Find window parameters */
246     unsigned snum;
247     uint8_t depth;
248     uint16_t width, height;
249     const xcb_screen_t *scr = FindWindow (vd, conn, &snum, &depth,
250                                           &width, &height);
251     if (scr == NULL)
252         goto error;
253
254     sys->window = xcb_generate_id (conn);
255
256     /* Determine our pixel format */
257     if (sys->v1_3)
258     {   /* GLX 1.3 */
259         static const int attr[] = {
260             GLX_RED_SIZE, 5,
261             GLX_GREEN_SIZE, 5,
262             GLX_BLUE_SIZE, 5,
263             GLX_DOUBLEBUFFER, True,
264             GLX_X_RENDERABLE, True,
265             GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
266             None };
267
268         xcb_get_window_attributes_reply_t *wa =
269             xcb_get_window_attributes_reply (conn,
270                 xcb_get_window_attributes (conn, sys->embed->handle.xid),
271                 NULL);
272         if (wa == NULL)
273             goto error;
274         xcb_visualid_t visual = wa->visual;
275         free (wa);
276
277         int nelem;
278         GLXFBConfig *confs = glXChooseFBConfig (dpy, snum, attr, &nelem);
279         if (confs == NULL)
280         {
281             msg_Err (vd, "no GLX frame bufer configurations");
282             goto error;
283         }
284
285         GLXFBConfig conf;
286         bool found = false;
287
288         for (int i = 0; i < nelem && !found; i++)
289         {
290             conf = confs[i];
291
292             XVisualInfo *vi = glXGetVisualFromFBConfig (dpy, conf);
293             if (vi == NULL)
294                 continue;
295
296             if (vi->visualid == visual)
297                 found = true;
298             XFree (vi);
299         }
300         XFree (confs);
301
302         if (!found)
303         {
304             msg_Err (vd, "no matching GLX frame buffer configuration");
305             goto error;
306         }
307
308         sys->glwin = None;
309         if (!CreateWindow (vd, conn, depth, 0 /* ??? */, width, height))
310             sys->glwin = glXCreateWindow (dpy, conf, sys->window, NULL );
311         if (sys->glwin == None)
312         {
313             msg_Err (vd, "cannot create GLX window");
314             goto error;
315         }
316
317         /* Create an OpenGL context */
318         sys->ctx = glXCreateNewContext (dpy, conf, GLX_RGBA_TYPE, NULL,
319                                         True);
320         if (sys->ctx == NULL)
321         {
322             msg_Err (vd, "cannot create GLX context");
323             goto error;
324         }
325
326         if (!glXMakeContextCurrent (dpy, sys->glwin, sys->glwin, sys->ctx))
327             goto error;
328     }
329     else
330     {   /* GLX 1.2 */
331         int attr[] = {
332             GLX_RGBA,
333             GLX_RED_SIZE, 5,
334             GLX_GREEN_SIZE, 5,
335             GLX_BLUE_SIZE, 5,
336             GLX_DOUBLEBUFFER,
337             None };
338
339         XVisualInfo *vi = glXChooseVisual (dpy, snum, attr);
340         if (vi == NULL)
341         {
342             msg_Err (vd, "cannot find GLX 1.2 visual" );
343             goto error;
344         }
345         msg_Dbg (vd, "using GLX visual ID 0x%"PRIx32, (uint32_t)vi->visualid);
346
347         if (CreateWindow (vd, conn, depth, 0 /* ??? */, width, height) == 0)
348             sys->ctx = glXCreateContext (dpy, vi, 0, True);
349         XFree (vi);
350         if (sys->ctx == NULL)
351         {
352             msg_Err (vd, "cannot create GLX context");
353             goto error;
354         }
355
356         if (glXMakeCurrent (dpy, sys->window, sys->ctx) == False)
357             goto error;
358         sys->glwin = sys->window;
359     }
360
361     /* Initialize common OpenGL video display */
362     sys->gl.lock = NULL;
363     sys->gl.unlock = NULL;
364     sys->gl.swap = SwapBuffers;
365     sys->gl.sys = sys;
366
367     if (vout_display_opengl_Init (&sys->vgl, &vd->fmt, &sys->gl))
368     {
369         sys->gl.sys = NULL;
370         goto error;
371     }
372
373     sys->cursor = CreateBlankCursor (conn, scr);
374     sys->visible = false;
375
376     /* */
377     vout_display_info_t info = vd->info;
378     info.has_pictures_invalid = false;
379     info.has_event_thread = true;
380
381     /* Setup vout_display_t once everything is fine */
382     vd->info = info;
383
384     vd->pool = Pool;
385     vd->prepare = PictureRender;
386     vd->display = PictureDisplay;
387     vd->control = Control;
388     vd->manage = Manage;
389
390     /* */
391     bool is_fullscreen = vd->cfg->is_fullscreen;
392     if (is_fullscreen && vout_window_SetFullScreen (sys->embed, true))
393         is_fullscreen = false;
394     vout_display_SendEventFullscreen (vd, is_fullscreen);
395     vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
396
397     return VLC_SUCCESS;
398
399 error:
400     Close (obj);
401     return VLC_EGENERIC;
402 }
403
404
405 /**
406  * Disconnect from the X server.
407  */
408 static void Close (vlc_object_t *obj)
409 {
410     vout_display_t *vd = (vout_display_t *)obj;
411     vout_display_sys_t *sys = vd->sys;
412     Display *dpy = sys->display;
413
414     if (sys->gl.sys != NULL)
415         vout_display_opengl_Clean (&sys->vgl);
416
417     if (sys->ctx != NULL)
418     {
419         if (sys->v1_3)
420             glXMakeContextCurrent (dpy, None, None, NULL);
421         else
422             glXMakeCurrent (dpy, None, NULL);
423         glXDestroyContext (dpy, sys->ctx);
424         if (sys->v1_3)
425             glXDestroyWindow (dpy, sys->glwin);
426     }
427
428     /* show the default cursor */
429     xcb_change_window_attributes (XGetXCBConnection (sys->display),
430                                   sys->embed->handle.xid, XCB_CW_CURSOR,
431                                   &(uint32_t) { XCB_CURSOR_NONE });
432     xcb_flush (XGetXCBConnection (sys->display));
433
434     XCloseDisplay (dpy);
435     vout_display_DeleteWindow (vd, sys->embed);
436     free (sys);
437 }
438
439 static void SwapBuffers (vout_opengl_t *gl)
440 {
441     vout_display_sys_t *sys = gl->sys;
442
443     glXSwapBuffers (sys->display, sys->glwin);
444 }
445
446 /**
447  * Return a direct buffer
448  */
449 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
450 {
451     vout_display_sys_t *sys = vd->sys;
452     (void)requested_count;
453
454     if (!sys->pool)
455         sys->pool = vout_display_opengl_GetPool (&sys->vgl);
456     return sys->pool;
457 }
458
459 static void PictureRender (vout_display_t *vd, picture_t *pic)
460 {
461     vout_display_sys_t *sys = vd->sys;
462
463     vout_display_opengl_Prepare (&sys->vgl, pic);
464 }
465
466 static void PictureDisplay (vout_display_t *vd, picture_t *pic)
467 {
468     vout_display_sys_t *sys = vd->sys;
469     xcb_connection_t *conn = XGetXCBConnection (sys->display);
470
471     xcb_force_screen_saver (conn, XCB_SCREEN_SAVER_RESET);
472     vout_display_opengl_Display (&sys->vgl, &vd->source);
473     xcb_flush (conn);
474     picture_Release (pic);
475 }
476
477 static int Control (vout_display_t *vd, int query, va_list ap)
478 {
479     vout_display_sys_t *sys = vd->sys;
480
481     switch (query)
482     {
483     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
484     {
485         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
486         return vout_window_SetFullScreen (sys->embed, c->is_fullscreen);
487     }
488
489     case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
490     {
491         unsigned state = va_arg (ap, unsigned);
492         return vout_window_SetState (sys->embed, state);
493     }
494
495     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
496     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
497     case VOUT_DISPLAY_CHANGE_ZOOM:
498     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
499     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
500     {
501         xcb_connection_t *conn = XGetXCBConnection (sys->display);
502         const vout_display_cfg_t *cfg;
503         const video_format_t *source;
504         bool is_forced = false;
505
506         if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
507          || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
508         {
509             source = (const video_format_t *)va_arg (ap, const video_format_t *);
510             cfg = vd->cfg;
511         }
512         else
513         {
514             source = &vd->source;
515             cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
516             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
517                 is_forced = (bool)va_arg (ap, int);
518         }
519
520         /* */
521         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
522          && is_forced
523          && (cfg->display.width  != vd->cfg->display.width
524            ||cfg->display.height != vd->cfg->display.height)
525          && vout_window_SetSize (sys->embed,
526                                  cfg->display.width, cfg->display.height))
527             return VLC_EGENERIC;
528
529         vout_display_place_t place;
530         vout_display_PlacePicture (&place, source, cfg, false);
531
532         /* Move the picture within the window */
533         const uint32_t values[] = { place.x, place.y,
534                                     place.width, place.height, };
535         xcb_void_cookie_t ck =
536             xcb_configure_window_checked (conn, sys->window,
537                             XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
538                           | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
539                               values);
540         if (CheckError (vd, conn, "cannot resize X11 window", ck))
541             return VLC_EGENERIC;
542
543         glViewport (0, 0, place.width, place.height);
544         return VLC_SUCCESS;
545     }
546
547     /* Hide the mouse. It will be send when
548      * vout_display_t::info.b_hide_mouse is false */
549     case VOUT_DISPLAY_HIDE_MOUSE:
550         xcb_change_window_attributes (XGetXCBConnection (sys->display),
551                                       sys->embed->handle.xid,
552                                     XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
553         return VLC_SUCCESS;
554
555     case VOUT_DISPLAY_GET_OPENGL:
556     {
557         vout_opengl_t **gl = va_arg (ap, vout_opengl_t **);
558         *gl = &sys->gl;
559         return VLC_SUCCESS;
560     }
561
562     case VOUT_DISPLAY_RESET_PICTURES:
563         assert (0);
564     default:
565         msg_Err (vd, "Unknown request in XCB vout display");
566         return VLC_EGENERIC;
567     }
568 }
569
570 static void Manage (vout_display_t *vd)
571 {
572     vout_display_sys_t *sys = vd->sys;
573     xcb_connection_t *conn = XGetXCBConnection (sys->display);
574
575     ManageEvent (vd, conn, &sys->visible);
576 }