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