1 /*****************************************************************************
2 * intf_mga.c: MGA interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
31 #include <sys/types.h> /* on BSD, uio.h needs types.h */
32 #include <sys/uio.h> /* for input.h */
35 #include <X11/Xutil.h>
36 #include <X11/keysym.h>
44 #include "stream_control.h"
45 #include "input_ext-intf.h"
48 #include "video_output.h"
51 #include "interface.h"
55 /*****************************************************************************
56 * intf_sys_t: description and status of X11 interface
57 *****************************************************************************/
58 typedef struct intf_sys_s
60 /* X11 generic properties */
61 Display * p_display; /* X11 display pointer */
62 int i_screen; /* X11 screen */
64 Atom wm_delete_window;
66 /* Main window properties */
67 Window window; /* main window */
68 GC gc; /* graphic context for main window */
69 int i_width; /* width of main window */
70 int i_height; /* height of main window */
72 /* Screen saver properties */
73 int i_ss_count; /* enabling/disabling count */
74 int i_ss_timeout; /* timeout */
75 int i_ss_interval; /* interval between changes */
76 int i_ss_blanking; /* blanking mode */
77 int i_ss_exposure; /* exposure mode */
79 /* Mouse pointer properties */
80 boolean_t b_mouse; /* is the mouse pointer displayed ? */
84 /*****************************************************************************
86 *****************************************************************************/
87 static int X11CreateWindow ( intf_thread_t *p_intf );
88 static void X11DestroyWindow ( intf_thread_t *p_intf );
89 static void X11ManageWindow ( intf_thread_t *p_intf );
90 static void X11EnableScreenSaver ( intf_thread_t *p_intf );
91 static void X11DisableScreenSaver ( intf_thread_t *p_intf );
92 static void X11TogglePointer ( intf_thread_t *p_intf );
94 /*****************************************************************************
95 * intf_MGACreate: initialize and create window
96 *****************************************************************************/
97 int intf_MGACreate( intf_thread_t *p_intf )
101 /* Allocate instance and initialize some members */
102 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
103 if( p_intf->p_sys == NULL )
105 intf_ErrMsg("error: %s", strerror(ENOMEM));
109 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
110 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
111 p_intf->p_sys->p_display = XOpenDisplay( psz_display );
112 if( !p_intf->p_sys->p_display ) /* error */
114 intf_ErrMsg("error: can't open display %s", psz_display );
115 free( p_intf->p_sys );
118 p_intf->p_sys->i_screen = DefaultScreen( p_intf->p_sys->p_display );
120 /* Spawn base window - this window will include the video output window,
121 * but also command buttons, subtitles and other indicators */
122 if( X11CreateWindow( p_intf ) )
124 intf_ErrMsg("error: can't create interface window" );
125 XCloseDisplay( p_intf->p_sys->p_display );
126 free( p_intf->p_sys );
130 /* Spawn video output thread */
131 if( p_main->b_video )
133 p_intf->p_vout = vout_CreateThread( psz_display, p_intf->p_sys->window,
134 p_intf->p_sys->i_width,
135 p_intf->p_sys->i_height, NULL, 0, NULL );
136 if( p_intf->p_vout == NULL ) /* error */
138 intf_ErrMsg("error: can't create video output thread" );
139 X11DestroyWindow( p_intf );
140 XCloseDisplay( p_intf->p_sys->p_display );
141 free( p_intf->p_sys );
146 p_intf->p_sys->b_mouse = 1;
149 intf_AssignNormalKeys( p_intf );
152 /* Disable screen saver and return */
153 p_intf->p_sys->i_ss_count = 1;
154 X11DisableScreenSaver( p_intf );
158 /*****************************************************************************
159 * intf_MGADestroy: destroy interface window
160 *****************************************************************************/
161 void intf_MGADestroy( intf_thread_t *p_intf )
163 /* Enable screen saver */
164 X11EnableScreenSaver( p_intf );
166 /* Close input thread, if any (blocking) */
167 if( p_intf->p_input )
169 input_DestroyThread( p_intf->p_input, NULL );
172 /* Close video output thread, if any (blocking) */
175 vout_DestroyThread( p_intf->p_vout, NULL );
178 /* Close main window and display */
179 X11DestroyWindow( p_intf );
180 XCloseDisplay( p_intf->p_sys->p_display );
182 /* Destroy structure */
183 free( p_intf->p_sys );
187 /*****************************************************************************
188 * intf_MGAManage: event loop
189 *****************************************************************************/
190 void intf_MGAManage( intf_thread_t *p_intf )
192 /* Manage main window */
193 X11ManageWindow( p_intf );
196 /* following functions are local */
198 /*****************************************************************************
199 * X11CreateWindow: open and set-up X11 main window
200 *****************************************************************************/
201 static int X11CreateWindow( intf_thread_t *p_intf )
203 XSizeHints xsize_hints;
204 XSetWindowAttributes xwindow_attributes;
208 boolean_t b_configure_notify;
209 boolean_t b_map_notify;
211 /* Set main window's size */
212 p_intf->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR, VOUT_WIDTH_DEFAULT );
213 p_intf->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR, VOUT_HEIGHT_DEFAULT );
215 /* Prepare window manager hints and properties */
216 xsize_hints.base_width = p_intf->p_sys->i_width;
217 xsize_hints.base_height = p_intf->p_sys->i_height;
218 xsize_hints.flags = PSize;
219 p_intf->p_sys->wm_protocols = XInternAtom( p_intf->p_sys->p_display,
220 "WM_PROTOCOLS", True );
221 p_intf->p_sys->wm_delete_window = XInternAtom( p_intf->p_sys->p_display,
222 "WM_DELETE_WINDOW", True );
224 /* Prepare window attributes */
225 xwindow_attributes.backing_store = Always; /* save the hidden part */
226 xwindow_attributes.background_pixel = WhitePixel( p_intf->p_sys->p_display,
227 p_intf->p_sys->i_screen );
229 /* Create the window and set hints - the window must receive ConfigureNotify
230 * events, and, until it is displayed, Expose and MapNotify events. */
231 p_intf->p_sys->window = XCreateSimpleWindow( p_intf->p_sys->p_display,
232 DefaultRootWindow( p_intf->p_sys->p_display ),
234 p_intf->p_sys->i_width, p_intf->p_sys->i_height,
236 XSelectInput( p_intf->p_sys->p_display, p_intf->p_sys->window,
237 ExposureMask | StructureNotifyMask );
238 XChangeWindowAttributes( p_intf->p_sys->p_display, p_intf->p_sys->window,
239 CWBackingStore | CWBackPixel, &xwindow_attributes);
241 /* Set window manager hints and properties: size hints, command, window's name,
242 * and accepted protocols */
243 XSetWMNormalHints( p_intf->p_sys->p_display, p_intf->p_sys->window, &xsize_hints );
244 XSetCommand( p_intf->p_sys->p_display, p_intf->p_sys->window,
245 p_main->ppsz_argv, p_main->i_argc );
246 XStoreName( p_intf->p_sys->p_display, p_intf->p_sys->window, VOUT_TITLE );
247 if( (p_intf->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
248 || (p_intf->p_sys->wm_delete_window == None)
249 || !XSetWMProtocols( p_intf->p_sys->p_display, p_intf->p_sys->window,
250 &p_intf->p_sys->wm_delete_window, 1 ) )
252 /* WM_DELETE_WINDOW is not supported by window manager */
253 intf_Msg("error: missing or bad window manager - please exit program kindly.");
256 /* Creation of a graphic context that doesn't generate a GraphicsExpose event
257 when using functions like XCopyArea */
258 xgcvalues.graphics_exposures = False;
259 p_intf->p_sys->gc = XCreateGC( p_intf->p_sys->p_display, p_intf->p_sys->window,
260 GCGraphicsExposures, &xgcvalues);
262 /* Send orders to server, and wait until window is displayed - three events
263 * must be received: a MapNotify event, an Expose event allowing drawing in the
264 * window, and a ConfigureNotify to get the window dimensions. Once those events
265 * have been received, only ConfigureNotify events need to be received. */
267 b_configure_notify = 0;
269 XMapWindow( p_intf->p_sys->p_display, p_intf->p_sys->window);
272 XNextEvent( p_intf->p_sys->p_display, &xevent);
273 if( (xevent.type == Expose)
274 && (xevent.xexpose.window == p_intf->p_sys->window) )
278 else if( (xevent.type == MapNotify)
279 && (xevent.xmap.window == p_intf->p_sys->window) )
283 else if( (xevent.type == ConfigureNotify)
284 && (xevent.xconfigure.window == p_intf->p_sys->window) )
286 b_configure_notify = 1;
287 p_intf->p_sys->i_width = xevent.xconfigure.width;
288 p_intf->p_sys->i_height = xevent.xconfigure.height;
291 while( !( b_expose && b_configure_notify && b_map_notify ) );
292 XSelectInput( p_intf->p_sys->p_display, p_intf->p_sys->window,
293 StructureNotifyMask | KeyPressMask | ButtonPressMask );
295 /* At this stage, the window is openned, displayed, and ready to receive data */
299 /*****************************************************************************
300 * X11DestroyWindow: destroy X11 main window
301 *****************************************************************************/
302 static void X11DestroyWindow( intf_thread_t *p_intf )
304 XUnmapWindow( p_intf->p_sys->p_display, p_intf->p_sys->window );
305 XFreeGC( p_intf->p_sys->p_display, p_intf->p_sys->gc );
306 XDestroyWindow( p_intf->p_sys->p_display, p_intf->p_sys->window );
309 /*****************************************************************************
310 * X11ManageWindow: manage X11 main window
311 *****************************************************************************/
312 static void X11ManageWindow( intf_thread_t *p_intf )
314 XEvent xevent; /* X11 event */
315 boolean_t b_resized; /* window has been resized */
316 char i_key; /* ISO Latin-1 key */
318 /* Handle X11 events: ConfigureNotify events are parsed to know if the
319 * output window's size changed, MapNotify and UnmapNotify to know if the
320 * window is mapped (and if the display is useful), and ClientMessages
321 * to intercept window destruction requests */
323 while( XCheckWindowEvent( p_intf->p_sys->p_display, p_intf->p_sys->window,
324 StructureNotifyMask | KeyPressMask |
325 ButtonPressMask, &xevent ) == True )
327 /* ConfigureNotify event: prepare */
328 if( (xevent.type == ConfigureNotify)
329 && ((xevent.xconfigure.width != p_intf->p_sys->i_width)
330 || (xevent.xconfigure.height != p_intf->p_sys->i_height)) )
332 /* Update dimensions */
334 p_intf->p_sys->i_width = xevent.xconfigure.width;
335 p_intf->p_sys->i_height = xevent.xconfigure.height;
337 /* MapNotify event: change window status and disable screen saver */
338 else if( xevent.type == MapNotify)
340 if( (p_intf->p_vout != NULL) && !p_intf->p_vout->b_active )
342 X11DisableScreenSaver( p_intf );
343 p_intf->p_vout->b_active = 1;
346 /* UnmapNotify event: change window status and enable screen saver */
347 else if( xevent.type == UnmapNotify )
349 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_active )
351 X11EnableScreenSaver( p_intf );
352 p_intf->p_vout->b_active = 0;
355 /* DestroyNotify event: window has been destroyed */
356 else if( xevent.type == DestroyNotify )
358 intf_ErrMsg( "vout: window destroyed !");
361 else if( xevent.type == KeyPress )
363 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
365 if( intf_ProcessKey( p_intf, i_key ) )
367 intf_DbgMsg("unhandled key '%c' (%i)", (char) i_key, i_key );
372 else if( xevent.type == ButtonPress )
374 switch( ((XButtonEvent *)&xevent)->button )
377 /* in this part we will eventually manage
378 * clicks for DVD navigation for instance */
382 X11TogglePointer( p_intf );
386 vlc_mutex_lock( &p_intf->p_vout->change_lock );
387 p_intf->p_vout->b_interface = !p_intf->p_vout->b_interface;
388 p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
389 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
393 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
394 * are handled - according to the man pages, the format is always 32
396 else if( (xevent.type == ClientMessage)
397 && (xevent.xclient.message_type == p_intf->p_sys->wm_protocols)
398 && (xevent.xclient.data.l[0] == p_intf->p_sys->wm_delete_window ) )
400 /* FIXME: this never happens :( how to receive wm messages ?? */
401 intf_DbgMsg("ClientMessage received");
407 intf_DbgMsg("%p -> unhandled event type %d received", p_intf, xevent.type );
413 * Handle vout or interface windows resizing
415 if( p_intf->p_vout != NULL )
419 /* If interface window has been resized, change vout size */
420 intf_DbgMsg("resizing output window");
421 vlc_mutex_lock( &p_intf->p_vout->change_lock );
422 p_intf->p_vout->i_width = p_intf->p_sys->i_width;
423 p_intf->p_vout->i_height = p_intf->p_sys->i_height;
424 p_intf->p_vout->i_changes |= VOUT_SIZE_CHANGE;
425 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
427 else if( (p_intf->p_vout->i_width != p_intf->p_sys->i_width) ||
428 (p_intf->p_vout->i_height != p_intf->p_sys->i_height) )
430 /* If video output size has changed, change interface window size */
431 intf_DbgMsg("resizing interface window");
432 p_intf->p_sys->i_width = p_intf->p_vout->i_width;
433 p_intf->p_sys->i_height = p_intf->p_vout->i_height;
434 XResizeWindow( p_intf->p_sys->p_display, p_intf->p_sys->window,
435 p_intf->p_sys->i_width, p_intf->p_sys->i_height );
440 /*****************************************************************************
441 * X11EnableScreenSaver: enable screen saver
442 *****************************************************************************
443 * This function enable the screen saver on a display after it had been
444 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
445 * know wether the screen saver can be activated or not: if n successive calls
446 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
447 * will be required before the screen saver could effectively be activated.
448 *****************************************************************************/
449 void X11EnableScreenSaver( intf_thread_t *p_intf )
451 if( p_intf->p_sys->i_ss_count++ == 0 )
453 intf_Msg("Enabling screen saver");
454 XSetScreenSaver( p_intf->p_sys->p_display, p_intf->p_sys->i_ss_timeout,
455 p_intf->p_sys->i_ss_interval, p_intf->p_sys->i_ss_blanking,
456 p_intf->p_sys->i_ss_exposure );
460 /*****************************************************************************
461 * X11DisableScreenSaver: disable screen saver
462 *****************************************************************************
463 * See XEnableScreenSaver
464 *****************************************************************************/
465 void X11DisableScreenSaver( intf_thread_t *p_intf )
467 if( --p_intf->p_sys->i_ss_count == 0 )
469 /* Save screen saver informations */
470 XGetScreenSaver( p_intf->p_sys->p_display, &p_intf->p_sys->i_ss_timeout,
471 &p_intf->p_sys->i_ss_interval, &p_intf->p_sys->i_ss_blanking,
472 &p_intf->p_sys->i_ss_exposure );
474 /* Disable screen saver */
475 intf_Msg("Disabling screen saver");
476 XSetScreenSaver( p_intf->p_sys->p_display, 0,
477 p_intf->p_sys->i_ss_interval, p_intf->p_sys->i_ss_blanking,
478 p_intf->p_sys->i_ss_exposure );
482 /*****************************************************************************
483 * X11TogglePointer: hide or show the mouse pointer
484 *****************************************************************************
485 * This function hides the X pointer if it is visible by putting it at
486 * coordinates (32,32) and setting the pointer sprite to a blank one. To
487 * show it again, we disable the sprite and restore the original coordinates.
488 *****************************************************************************/
489 void X11TogglePointer( intf_thread_t *p_intf )
491 static Cursor cursor;
492 static boolean_t b_cursor = 0;
494 if( p_intf->p_sys->b_mouse )
496 p_intf->p_sys->b_mouse = 0;
501 Pixmap blank = XCreatePixmap( p_intf->p_sys->p_display,
502 DefaultRootWindow(p_intf->p_sys->p_display),
505 XParseColor( p_intf->p_sys->p_display,
506 XCreateColormap( p_intf->p_sys->p_display,
508 p_intf->p_sys->p_display ),
510 p_intf->p_sys->p_display,
511 p_intf->p_sys->i_screen ),
515 cursor = XCreatePixmapCursor( p_intf->p_sys->p_display,
516 blank, blank, &color, &color, 1, 1 );
520 XDefineCursor( p_intf->p_sys->p_display,
521 p_intf->p_sys->window, cursor );
525 p_intf->p_sys->b_mouse = 1;
527 XUndefineCursor( p_intf->p_sys->p_display, p_intf->p_sys->window );