]> git.sesse.net Git - vlc/blob - modules/video_output/x11/glx.c
Remove most stray semi-colons in module descriptions
[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
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <errno.h>                                                 /* ENOMEM */
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
38 #include <vlc_vout.h>
39
40 #ifdef HAVE_SYS_SHM_H
41 #   include <sys/shm.h>                                /* shmget(), shmctl() */
42 #endif
43
44 #include <X11/Xlib.h>
45 #include <X11/Xmd.h>
46 #include <X11/Xutil.h>
47 #include <X11/keysym.h>
48 #ifdef HAVE_SYS_SHM_H
49 #   include <X11/extensions/XShm.h>
50 #endif
51 #ifdef DPMSINFO_IN_DPMS_H
52 #   include <X11/extensions/dpms.h>
53 #endif
54
55 #include <GL/glx.h>
56
57 #include "xcommon.h"
58
59 /* RV16 */
60 //#define VLCGL_RGB_FORMAT GL_RGB
61 //#define VLCGL_RGB_TYPE GL_UNSIGNED_SHORT_5_6_5
62
63 /* RV24 */
64 //#define VLCGL_RGB_FORMAT GL_RGB
65 //#define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
66
67 /* RV32 */
68 #define VLCGL_RGB_FORMAT GL_RGBA
69 #define VLCGL_RGB_TYPE GL_UNSIGNED_BYTE
70
71
72 /*****************************************************************************
73  * OpenGL provider interface
74  *****************************************************************************/
75 static int  CreateOpenGL ( vlc_object_t * );
76 static void DestroyOpenGL( vlc_object_t * );
77 static int  InitOpenGL   ( vout_thread_t * );
78 static void SwapBuffers  ( vout_thread_t * );
79
80 /*****************************************************************************
81  * Local prototypes
82  *****************************************************************************/
83 static int  InitGLX12    ( vout_thread_t * );
84 static int  InitGLX13    ( vout_thread_t * );
85 static void SwitchContext( vout_thread_t * );
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90 #define ADAPTOR_TEXT N_("XVideo adaptor number")
91 #define ADAPTOR_LONGTEXT N_( \
92     "If your graphics card provides several adaptors, you have " \
93     "to choose which one will be used (you shouldn't have to change this).")
94
95 #define ALT_FS_TEXT N_("Alternate fullscreen method")
96 #define ALT_FS_LONGTEXT N_( \
97     "There are two ways to make a fullscreen window, unfortunately each one " \
98     "has its drawbacks.\n" \
99     "1) Let the window manager handle your fullscreen window (default), but " \
100     "things like taskbars will likely show on top of the video.\n" \
101     "2) Completely bypass the window manager, but then nothing will be able " \
102     "to show on top of the video.")
103
104 #define DISPLAY_TEXT N_("X11 display")
105 #define DISPLAY_LONGTEXT N_( \
106     "X11 hardware display to use. By default VLC will " \
107     "use the value of the DISPLAY environment variable.")
108
109 #define SHM_TEXT N_("Use shared memory")
110 #define SHM_LONGTEXT N_( \
111     "Use shared memory to communicate between VLC and the X server.")
112
113 #define SCREEN_TEXT N_("Screen for fullscreen mode.")
114 #define SCREEN_LONGTEXT N_( \
115     "Screen to use in fullscreen mode. For instance " \
116     "set it to 0 for first screen, 1 for the second.")
117
118 vlc_module_begin ()
119     set_shortname( "OpenGL(GLX)" )
120     set_category( CAT_VIDEO )
121     set_subcategory( SUBCAT_VIDEO_VOUT )
122     set_description( N_("OpenGL(GLX) provider") )
123     set_capability( "opengl provider", 50 )
124     set_callbacks( CreateOpenGL, DestroyOpenGL )
125
126     add_string( "glx-display", NULL, NULL, DISPLAY_TEXT, DISPLAY_LONGTEXT, true )
127     add_integer( "glx-adaptor", -1, NULL, ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true )
128     add_bool( "glx-altfullscreen", 0, NULL, ALT_FS_TEXT, ALT_FS_LONGTEXT, true )
129 #ifdef HAVE_SYS_SHM_H
130     add_bool( "glx-shm", 1, NULL, SHM_TEXT, SHM_LONGTEXT, true )
131 #endif
132 #ifdef HAVE_XINERAMA
133     add_integer ( "glx-xineramascreen", -1, NULL, SCREEN_TEXT, SCREEN_LONGTEXT, true )
134 #endif
135 vlc_module_end ()
136
137 /*****************************************************************************
138  * Exported prototypes
139  *****************************************************************************/
140 extern int  Activate   ( vlc_object_t * );
141 extern void Deactivate ( vlc_object_t * );
142
143 /*****************************************************************************
144  * CreateOpenGL: initialize an OpenGL provider
145  *****************************************************************************/
146 static int CreateOpenGL( vlc_object_t *p_this )
147 {
148     vout_thread_t *p_vout = (vout_thread_t *)p_this;
149
150     if( Activate( p_this ) != VLC_SUCCESS )
151     {
152         return VLC_EGENERIC;
153     }
154
155     /* Set the function pointer */
156     p_vout->pf_init = InitOpenGL;
157     p_vout->pf_swap = SwapBuffers;
158
159     return VLC_SUCCESS;
160 }
161
162 /*****************************************************************************
163  * DestroyOpenGL: destroys an OpenGL provider
164  *****************************************************************************/
165 static void DestroyOpenGL( vlc_object_t *p_this )
166 {
167     vout_thread_t *p_vout = (vout_thread_t *)p_this;
168     vout_sys_t *p_sys = p_vout->p_sys;
169
170     glXDestroyContext( p_sys->p_display, p_sys->gwctx );
171     if( p_sys->b_glx13 )
172     {
173         glXDestroyWindow( p_sys->p_display, p_sys->gwnd );
174     }
175
176     Deactivate( p_this );
177 }
178
179 /*****************************************************************************
180  * InitOpenGL: initializes OpenGL provider
181  *****************************************************************************/
182 static int InitOpenGL( vout_thread_t *p_vout )
183 {
184     /* Initialize GLX */
185     if( !p_vout->p_sys->b_glx13 )
186     {
187         if( InitGLX12( p_vout ) != VLC_SUCCESS )
188         {
189             return VLC_EGENERIC;
190         }
191     }
192     else
193     {
194         if( InitGLX13( p_vout ) != VLC_SUCCESS )
195         {
196             return VLC_EGENERIC;
197         }
198     }
199
200     /* Set the OpenGL context _for the current thread_ */
201     SwitchContext( p_vout );
202
203     return VLC_SUCCESS;
204 }
205
206 int InitGLX12( vout_thread_t *p_vout )
207 {
208     vout_sys_t *p_sys = p_vout->p_sys;
209     XVisualInfo *p_vi;
210     int p_attr[] = { GLX_RGBA, GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
211                      GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, 0 };
212
213     p_vi = glXChooseVisual( p_sys->p_display,
214                             DefaultScreen( p_sys->p_display), p_attr );
215     if(! p_vi )
216     {
217         msg_Err( p_vout, "Cannot get GLX 1.2 visual" );
218         return VLC_EGENERIC;
219     }
220
221     /* Create an OpenGL context */
222     p_sys->gwctx = glXCreateContext( p_sys->p_display, p_vi, 0, True );
223     XFree( p_vi );
224     if( !p_sys->gwctx )
225     {
226         msg_Err( p_vout, "Cannot create OpenGL context");
227         return VLC_EGENERIC;
228     }
229
230     return VLC_SUCCESS;
231 }
232
233 int InitGLX13( vout_thread_t *p_vout )
234 {
235     vout_sys_t *p_sys = p_vout->p_sys;
236     int i_nbelem;
237     GLXFBConfig *p_fbconfs, fbconf;
238     XVisualInfo *p_vi;
239     int p_attr[] = { GLX_RED_SIZE, 5, GLX_GREEN_SIZE, 5,
240                      GLX_BLUE_SIZE, 5, GLX_DOUBLEBUFFER, True,
241                      GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT, 0 };
242
243     /* Get the FB configuration */
244     p_fbconfs = glXChooseFBConfig( p_sys->p_display, 0, p_attr, &i_nbelem );
245     if( (i_nbelem <= 0) || !p_fbconfs )
246     {
247         msg_Err( p_vout, "Cannot get FB configurations");
248         if( p_fbconfs ) XFree( p_fbconfs );
249         return VLC_EGENERIC;
250     }
251     fbconf = p_fbconfs[0];
252
253     /* Get the X11 visual */
254     p_vi = glXGetVisualFromFBConfig( p_sys->p_display, fbconf );
255     if( !p_vi )
256     {
257         msg_Err( p_vout, "Cannot get X11 visual" );
258         XFree( p_fbconfs );
259         return VLC_EGENERIC;
260     }
261     XFree( p_vi );
262
263     /* Create the GLX window */
264     p_sys->gwnd = glXCreateWindow( p_sys->p_display, fbconf,
265                                    p_sys->p_win->video_window, NULL );
266     if( p_sys->gwnd == None )
267     {
268         msg_Err( p_vout, "Cannot create GLX window" );
269         return VLC_EGENERIC;
270     }
271
272     /* Create an OpenGL context */
273     p_sys->gwctx = glXCreateNewContext( p_sys->p_display, fbconf,
274                                         GLX_RGBA_TYPE, NULL, True );
275     XFree( p_fbconfs );
276     if( !p_sys->gwctx )
277     {
278         msg_Err( p_vout, "Cannot create OpenGL context");
279         return VLC_EGENERIC;
280     }
281
282     return VLC_SUCCESS;
283 }
284
285 /*****************************************************************************
286  * SwapBuffers: swap front/back buffers
287  *****************************************************************************/
288 static void SwapBuffers( vout_thread_t *p_vout )
289 {
290     vout_sys_t *p_sys = p_vout->p_sys;
291     unsigned int i_width, i_height, i_x, i_y;
292
293     vout_PlacePicture( p_vout, p_vout->p_sys->p_win->i_width,
294                        p_vout->p_sys->p_win->i_height,
295                        &i_x, &i_y, &i_width, &i_height );
296
297     glViewport( 0, 0, (GLint)i_width, (GLint)i_height );
298
299     if( p_sys->b_glx13 )
300     {
301         glXSwapBuffers( p_sys->p_display, p_sys->gwnd );
302     }
303     else
304     {
305         glXSwapBuffers( p_sys->p_display, p_sys->p_win->video_window );
306     }
307 }
308
309 void SwitchContext( vout_thread_t *p_vout )
310 {
311     vout_sys_t *p_sys = p_vout->p_sys;
312
313     /* Change the current OpenGL context */
314     if( p_sys->b_glx13 )
315     {
316         glXMakeContextCurrent( p_sys->p_display, p_sys->gwnd,
317                                p_sys->gwnd, p_sys->gwctx );
318     }
319     else
320     {
321         glXMakeCurrent( p_sys->p_display, p_sys->p_win->video_window,
322                         p_sys->gwctx );
323     }
324 }