]> git.sesse.net Git - vlc/blob - modules/video_output/x11/glx.c
Revert the so-called whitelisting commits that are actually blacklisting
[vlc] / modules / video_output / x11 / glx.c
1 /*****************************************************************************
2  * glx.c: GLX OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29
30 #include <vlc/vlc.h>
31 #include <vlc_interface.h>
32 #include <vlc_vout.h>
33
34 #ifdef HAVE_SYS_SHM_H
35 #   include <sys/shm.h>                                /* shmget(), shmctl() */
36 #endif
37
38 #include <X11/Xlib.h>
39 #include <X11/Xmd.h>
40 #include <X11/Xutil.h>
41 #include <X11/keysym.h>
42 #ifdef HAVE_SYS_SHM_H
43 #   include <X11/extensions/XShm.h>
44 #endif
45 #ifdef DPMSINFO_IN_DPMS_H
46 #   include <X11/extensions/dpms.h>
47 #endif
48
49 #include <GL/glx.h>
50
51 #include "xcommon.h"
52
53 /* RV16 */
54 //#define VLCGL_RGB_FORMAT GL_RGB
55 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
56
57 /* RV24 */
58 //#define VLCGL_RGB_FORMAT GL_RGB
59 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
60
61 /* RV32 */
62 #define VLCGL_RGB_FORMAT GL_RGBA
63 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
64
65
66 /*****************************************************************************
67  * OpenGL provider interface
68  *****************************************************************************/
69 static int  CreateOpenGL ( vlc_object_t * );
70 static void DestroyOpenGL( vlc_object_t * );
71 static int  InitOpenGL   ( vout_thread_t * );
72 static void SwapBuffers  ( vout_thread_t * );
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77 static int  InitGLX12    ( vout_thread_t * );
78 static int  InitGLX13    ( vout_thread_t * );
79 static void SwitchContext( vout_thread_t * );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 #define ADAPTOR_TEXT N_("XVideo adaptor number")
85 #define ADAPTOR_LONGTEXT N_( \
86     "If your graphics card provides several adaptors, you have " \
87     "to choose which one will be used (you shouldn't have to change this).")
88
89 #define ALT_FS_TEXT N_("Alternate fullscreen method")
90 #define ALT_FS_LONGTEXT N_( \
91     "There are two ways to make a fullscreen window, unfortunately each one " \
92     "has its drawbacks.\n" \
93     "1) Let the window manager handle your fullscreen window (default), but " \
94     "things like taskbars will likely show on top of the video.\n" \
95     "2) Completely bypass the window manager, but then nothing will be able " \
96     "to show on top of the video.")
97
98 #define DISPLAY_TEXT N_("X11 display")
99 #define DISPLAY_LONGTEXT N_( \
100     "X11 hardware display to use. By default VLC will " \
101     "use the value of the DISPLAY environment variable.")
102
103 #define SCREEN_TEXT N_("Screen for fullscreen mode.")
104 #define SCREEN_LONGTEXT N_( \
105     "Screen to use in fullscreen mode. For instance " \
106     "set it to 0 for first screen, 1 for the second.")
107
108 vlc_module_begin();
109     set_shortname( "OpenGL(GLX)" );
110     set_category( CAT_VIDEO );
111     set_subcategory( SUBCAT_VIDEO_VOUT );
112     set_description( _("OpenGL(GLX) provider") );
113     set_capability( "opengl provider", 50 );
114     set_callbacks( CreateOpenGL, DestroyOpenGL );
115
116     add_string( "glx-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, VLC_TRUE );
117     add_integer( "glx-adaptor", -1, NULL, ADAPTOR_TEXT, ADAPTOR_LONGTEXT, VLC_TRUE );
118     add_bool( "glx-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT, VLC_TRUE );
119 #ifdef HAVE_XINERAMA
120     add_integer ( "glx-xineramascreen", 0, NULL, SCREEN_TEXT, SCREEN_LONGTEXT, VLC_TRUE );
121 #endif
122 vlc_module_end();
123
124 /*****************************************************************************
125  * Exported prototypes
126  *****************************************************************************/
127 extern int  E_(Activate)   ( vlc_object_t * );
128 extern void E_(Deactivate) ( vlc_object_t * );
129
130 /*****************************************************************************
131  * CreateOpenGL: initialize an OpenGL provider
132  *****************************************************************************/
133 static int CreateOpenGL( vlc_object_t *p_this )
134 {
135     vout_thread_t *p_vout = (vout_thread_t *)p_this;
136
137     if( E_(Activate)( p_this ) != VLC_SUCCESS )
138     {
139         return VLC_EGENERIC;
140     }
141
142     /* Set the function pointer */
143     p_vout->pf_init = InitOpenGL;
144     p_vout->pf_swap = SwapBuffers;
145
146     return VLC_SUCCESS;
147 }
148
149 /*****************************************************************************
150  * DestroyOpenGL: destroys an OpenGL provider
151  *****************************************************************************/
152 static void DestroyOpenGL( vlc_object_t *p_this )
153 {
154     vout_thread_t *p_vout = (vout_thread_t *)p_this;
155     vout_sys_t *p_sys = p_vout->p_sys;
156
157     glXDestroyContext( p_sys->p_display, p_sys->gwctx );
158     if( p_sys->b_glx13 )
159     {
160         glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
161     }
162
163     E_(Deactivate)( p_this );
164 }
165
166 /*****************************************************************************
167  * InitOpenGL: initializes OpenGL provider
168  *****************************************************************************/
169 static int InitOpenGL( vout_thread_t *p_vout )
170 {
171     /* Initialize GLX */
172     if( !p_vout->p_sys->b_glx13 )
173     {
174         if( InitGLX12( p_vout ) != VLC_SUCCESS )
175         {
176             return VLC_EGENERIC;
177         }
178     }
179     else
180     {
181         if( InitGLX13( p_vout ) != VLC_SUCCESS )
182         {
183             return VLC_EGENERIC;
184         }
185     }
186
187     /* Set the OpenGL context _for the current thread_ */
188     SwitchContext( p_vout );
189
190     return VLC_SUCCESS;
191 }
192
193 int InitGLX12( vout_thread_t *p_vout )
194 {
195     vout_sys_t *p_sys = p_vout->p_sys;
196     XVisualInfo *p_vi;
197     int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
198                      GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
199
200     p_vi = glXChooseVisual( p_sys->p_display,
201                             DefaultScreen( p_sys->p_display), p_attr );
202     if(! p_vi )
203     {
204         msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
205         return VLC_EGENERIC;
206     }
207
208     /* Create an OpenGL context */
209     p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
210     XFree( p_vi );
211     if( !p_sys->gwctx )
212     {
213         msg_Err( p_vout, "Cannot create OpenGL context");
214         return VLC_EGENERIC;
215     }
216
217     return VLC_SUCCESS;
218 }
219
220 int InitGLX13( vout_thread_t *p_vout )
221 {
222     vout_sys_t *p_sys = p_vout->p_sys;
223     int i_nbelem;
224     GLXFBConfig *p_fbconfs, fbconf;
225     XVisualInfo *p_vi;
226     int p_attr[] = { GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
227                      GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, True,
228                      GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, 0 };
229
230     /* Get the FB configuration */
231     p_fbconfs = glXChooseFBConfig( p_sys->p_display, 0, p_attr, &i_nbelem );
232     if( (i_nbelem <= 0) || !p_fbconfs )
233     {
234         msg_Err( p_vout, "Cannot get FB configurations");
235         if( p_fbconfs ) XFree( p_fbconfs );
236         return VLC_EGENERIC;
237     }
238     fbconf = p_fbconfs[0];
239
240     /* Get the X11 visual */
241     p_vi = glXGetVisualFromFBConfig( p_sys->p_display, fbconf );
242     if( !p_vi )
243     {
244         msg_Err( p_vout, "Cannot get X11 visual" );
245         XFree( p_fbconfs );
246         return VLC_EGENERIC;
247     }
248     XFree( p_vi );
249
250     /* Create the GLX window */
251     p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
252                                    p_sys->p_win->video_window, NULL );
253     if( p_sys->gwnd == None )
254     {
255         msg_Err( p_vout, "Cannot create GLX window" );
256         return VLC_EGENERIC;
257     }
258
259     /* Create an OpenGL context */
260     p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
261                                         GLX_RGBA_TYPE, NULL, True );
262     XFree( p_fbconfs );
263     if( !p_sys->gwctx )
264     {
265         msg_Err( p_vout, "Cannot create OpenGL context");
266         return VLC_EGENERIC;
267     }
268
269     return VLC_SUCCESS;
270 }
271
272 /*****************************************************************************
273  * SwapBuffers: swap front/back buffers
274  *****************************************************************************/
275 static void SwapBuffers( vout_thread_t *p_vout )
276 {
277     vout_sys_t *p_sys = p_vout->p_sys;
278     unsigned int i_width, i_height, i_x, i_y;
279
280     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
281                        p_vout->p_sys->p_win->i_height,
282                        &i_x, &i_y, &i_width, &i_height );
283
284     glViewport( 0, 0, (GLint)i_width, (GLint)i_height );
285
286     if( p_sys->b_glx13 )
287     {
288         glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
289     }
290     else
291     {
292         glXSwapBuffers( p_sys->p_display, p_sys->p_win->video_window );
293     }
294 }
295
296 void SwitchContext( vout_thread_t *p_vout )
297 {
298     vout_sys_t *p_sys = p_vout->p_sys;
299
300     /* Change the current OpenGL context */
301     if( p_sys->b_glx13 )
302     {
303         glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
304                                p_sys->gwnd, p_sys->gwctx );
305     }
306     else
307     {
308         glXMakeCurrent( p_sys->p_display, p_sys->p_win->video_window,
309                         p_sys->gwctx );
310     }
311 }