1 /*****************************************************************************
2 * intf_gnome.c: Gnome 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>
46 #include "video_output.h"
48 #include "audio_output.h" /* needed for mute */
51 #include "interface.h"
59 #include "intf_gnome_thread.h"
60 #include "intf_gnome.h"
61 #include "intf_gnome_interface.h"
62 #include "intf_gnome_support.h"
64 /*****************************************************************************
65 * intf_GnomeCreate: initialize and create window
66 *****************************************************************************/
67 int intf_GnomeCreate( intf_thread_t *p_intf )
71 /* Allocate instance and initialize some members */
72 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
73 if( p_intf->p_sys == NULL )
75 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
79 p_intf->p_sys->p_gnome = malloc( sizeof( gnome_thread_t ) );
80 if( p_intf->p_sys->p_gnome == NULL )
82 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
83 free( p_intf->p_sys );
87 /* Open display, unsing 'vlc_display' or DISPLAY environment variable */
88 psz_display = XDisplayName( main_GetPszVariable( VOUT_DISPLAY_VAR, NULL ) );
89 p_intf->p_sys->p_display = XOpenDisplay( psz_display );
90 if( !p_intf->p_sys->p_display ) /* error */
92 intf_ErrMsg("error: can't open display %s\n", psz_display );
93 free( p_intf->p_sys->p_gnome );
94 free( p_intf->p_sys );
97 p_intf->p_sys->i_screen = DefaultScreen( p_intf->p_sys->p_display );
99 /* Spawn base window - this window will include the video output window */
100 if( GnomeCreateWindow( p_intf ) )
102 intf_ErrMsg( "error: can't create output window\n" );
103 XCloseDisplay( p_intf->p_sys->p_display );
104 free( p_intf->p_sys->p_gnome );
105 free( p_intf->p_sys );
109 /* Spawn video output thread */
110 if( p_main->b_video )
112 p_intf->p_vout = vout_CreateThread( psz_display, p_intf->p_sys->window,
113 p_intf->p_sys->i_width,
114 p_intf->p_sys->i_height, NULL, 0,
115 (void *)&p_intf->p_sys->colormap );
117 if( p_intf->p_vout == NULL ) /* error */
119 intf_ErrMsg("error: can't create video output thread\n" );
120 GnomeDestroyWindow( p_intf );
121 XCloseDisplay( p_intf->p_sys->p_display );
122 free( p_intf->p_sys->p_gnome );
123 free( p_intf->p_sys );
128 /* Spawn Gnome thread */
129 p_intf->p_sys->p_gnome->b_die = 0;
130 p_intf->p_sys->p_gnome->b_error = 0;
132 p_intf->p_sys->p_gnome->b_popup_changed = 0;
133 p_intf->p_sys->p_gnome->b_window_changed = 0;
134 p_intf->p_sys->p_gnome->b_playlist_changed = 0;
136 vlc_thread_create( &p_intf->p_sys->p_gnome->thread_id, "gnome",
137 (void *)GnomeThread, p_intf->p_sys->p_gnome );
139 /* Disable screen saver and return */
140 p_intf->p_sys->i_ss_count = 1;
141 GnomeDisableScreenSaver( p_intf );
145 /*****************************************************************************
146 * intf_GnomeDestroy: destroy interface window
147 *****************************************************************************/
148 void intf_GnomeDestroy( intf_thread_t *p_intf )
150 /* Enable screen saver */
151 GnomeEnableScreenSaver( p_intf );
153 /* Close input thread, if any (blocking) */
154 if( p_intf->p_input )
156 input_DestroyThread( p_intf->p_input, NULL );
159 /* Close video output thread, if any (blocking) */
162 vout_DestroyThread( p_intf->p_vout, NULL );
165 /* Close gnome thread, if any (blocking) */
166 if( p_intf->p_sys->p_gnome->thread_id )
168 p_intf->p_sys->p_gnome->b_die = 1;
169 intf_Msg( "waiting for Gnome thread to terminate\n" );
170 vlc_thread_join( p_intf->p_sys->p_gnome->thread_id );
171 intf_Msg( "Gnome thread terminated\n" );
174 /* Close main window and display */
175 GnomeDestroyWindow( p_intf );
176 XCloseDisplay( p_intf->p_sys->p_display );
178 /* Destroy structures */
179 free( p_intf->p_sys->p_gnome );
180 free( p_intf->p_sys );
183 /*****************************************************************************
184 * intf_GnomeManage: event loop
185 *****************************************************************************/
186 void intf_GnomeManage( intf_thread_t *p_intf )
188 /* Manage main window */
189 GnomeManageWindow( p_intf );
191 /* Manage messages from the Gnome interface */
192 GnomeManageInterface( p_intf );
195 /* following functions are local */
197 /*****************************************************************************
198 * GnomeCreateWindow: open and set-up X11 main window
199 *****************************************************************************/
200 static int GnomeCreateWindow( intf_thread_t *p_intf )
202 XSizeHints xsize_hints;
203 XSetWindowAttributes xwindow_attributes;
207 boolean_t b_configure_notify;
208 boolean_t b_map_notify;
210 /* Set main window's size */
211 p_intf->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
212 VOUT_WIDTH_DEFAULT );
213 p_intf->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
214 VOUT_HEIGHT_DEFAULT );
216 /* Prepare window manager hints and properties */
217 xsize_hints.base_width = p_intf->p_sys->i_width;
218 xsize_hints.base_height = p_intf->p_sys->i_height;
219 xsize_hints.flags = PSize;
220 p_intf->p_sys->wm_protocols = XInternAtom( p_intf->p_sys->p_display,
221 "WM_PROTOCOLS", True );
222 p_intf->p_sys->wm_delete_window = XInternAtom( p_intf->p_sys->p_display,
223 "WM_DELETE_WINDOW", True );
225 /* Prepare window attributes */
226 xwindow_attributes.backing_store = Always; /* save the hidden part */
227 xwindow_attributes.background_pixel = WhitePixel( p_intf->p_sys->p_display,
228 p_intf->p_sys->i_screen );
230 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
232 /* Create the window and set hints - the window must receive ConfigureNotify
233 * events, and, until it is displayed, Expose and MapNotify events. */
234 p_intf->p_sys->window =
235 XCreateWindow( p_intf->p_sys->p_display,
236 DefaultRootWindow( p_intf->p_sys->p_display ),
238 p_intf->p_sys->i_width, p_intf->p_sys->i_height, 1,
240 CWBackingStore | CWBackPixel | CWEventMask,
241 &xwindow_attributes );
243 /* Set window manager hints and properties: size hints, command,
244 * window's name, and accepted protocols */
245 XSetWMNormalHints( p_intf->p_sys->p_display, p_intf->p_sys->window,
247 XSetCommand( p_intf->p_sys->p_display, p_intf->p_sys->window,
248 p_main->ppsz_argv, p_main->i_argc );
249 XStoreName( p_intf->p_sys->p_display, p_intf->p_sys->window, VOUT_TITLE );
250 if( (p_intf->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
251 || (p_intf->p_sys->wm_delete_window == None)
252 || !XSetWMProtocols( p_intf->p_sys->p_display, p_intf->p_sys->window,
253 &p_intf->p_sys->wm_delete_window, 1 ) )
255 /* WM_DELETE_WINDOW is not supported by window manager */
256 intf_Msg("error: missing or bad window manager - please exit program kindly.\n");
259 /* Creation of a graphic context that doesn't generate a GraphicsExpose
260 * event when using functions like XCopyArea */
261 xgcvalues.graphics_exposures = False;
262 p_intf->p_sys->gc = XCreateGC( p_intf->p_sys->p_display, p_intf->p_sys->window,
263 GCGraphicsExposures, &xgcvalues);
265 /* Send orders to server, and wait until window is displayed - three
266 * events must be received: a MapNotify event, an Expose event allowing
267 * drawing in the window, and a ConfigureNotify to get the window
268 * dimensions. Once those events have been received, only ConfigureNotify
269 * events need to be received. */
271 b_configure_notify = 0;
273 XMapWindow( p_intf->p_sys->p_display, p_intf->p_sys->window);
276 XNextEvent( p_intf->p_sys->p_display, &xevent);
277 if( (xevent.type == Expose)
278 && (xevent.xexpose.window == p_intf->p_sys->window) )
282 else if( (xevent.type == MapNotify)
283 && (xevent.xmap.window == p_intf->p_sys->window) )
287 else if( (xevent.type == ConfigureNotify)
288 && (xevent.xconfigure.window == p_intf->p_sys->window) )
290 b_configure_notify = 1;
291 p_intf->p_sys->i_width = xevent.xconfigure.width;
292 p_intf->p_sys->i_height = xevent.xconfigure.height;
294 } while( !( b_expose && b_configure_notify && b_map_notify ) );
296 XSelectInput( p_intf->p_sys->p_display, p_intf->p_sys->window,
297 StructureNotifyMask | KeyPressMask | ButtonPressMask );
299 if( XDefaultDepth(p_intf->p_sys->p_display, p_intf->p_sys->i_screen) == 8 )
301 /* Allocate a new palette */
302 p_intf->p_sys->colormap = XCreateColormap( p_intf->p_sys->p_display,
303 DefaultRootWindow( p_intf->p_sys->p_display ),
304 DefaultVisual( p_intf->p_sys->p_display,
305 p_intf->p_sys->i_screen ),
308 xwindow_attributes.colormap = p_intf->p_sys->colormap;
309 XChangeWindowAttributes( p_intf->p_sys->p_display,
310 p_intf->p_sys->window,
311 CWColormap, &xwindow_attributes );
314 /* At this stage, the window is open, displayed, and ready to receive data */
318 /*****************************************************************************
319 * GnomeDestroyWindow: destroy X11 main window
320 *****************************************************************************/
321 static void GnomeDestroyWindow( intf_thread_t *p_intf )
323 XUnmapWindow( p_intf->p_sys->p_display, p_intf->p_sys->window );
324 XFreeGC( p_intf->p_sys->p_display, p_intf->p_sys->gc );
325 XDestroyWindow( p_intf->p_sys->p_display, p_intf->p_sys->window );
328 /*****************************************************************************
329 * GnomeManageWindow: manage X11 main window
330 *****************************************************************************/
331 static void GnomeManageWindow( intf_thread_t *p_intf )
333 XEvent xevent; /* X11 event */
334 boolean_t b_resized; /* window has been resized */
335 char i_key; /* ISO Latin-1 key */
337 /* Handle X11 events: ConfigureNotify events are parsed to know if the
338 * output window's size changed, MapNotify and UnmapNotify to know if the
339 * window is mapped (and if the display is useful), and ClientMessages
340 * to intercept window destruction requests */
342 while( XCheckWindowEvent( p_intf->p_sys->p_display, p_intf->p_sys->window,
343 StructureNotifyMask | KeyPressMask |
344 ButtonPressMask, &xevent ) == True )
346 /* ConfigureNotify event: prepare */
347 if( (xevent.type == ConfigureNotify)
348 && ((xevent.xconfigure.width != p_intf->p_sys->i_width)
349 || (xevent.xconfigure.height != p_intf->p_sys->i_height)) )
351 /* Update dimensions */
353 p_intf->p_sys->i_width = xevent.xconfigure.width;
354 p_intf->p_sys->i_height = xevent.xconfigure.height;
356 /* MapNotify event: change window status and disable screen saver */
357 else if( xevent.type == MapNotify)
359 if( (p_intf->p_vout != NULL) && !p_intf->p_vout->b_active )
361 GnomeDisableScreenSaver( p_intf );
362 p_intf->p_vout->b_active = 1;
365 /* UnmapNotify event: change window status and enable screen saver */
366 else if( xevent.type == UnmapNotify )
368 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_active )
370 GnomeEnableScreenSaver( p_intf );
371 p_intf->p_vout->b_active = 0;
375 else if( xevent.type == KeyPress )
377 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
379 if( intf_ProcessKey( p_intf, i_key ) )
381 intf_DbgMsg( "unhandled key '%c' (%i)\n", (char) i_key, i_key );
386 else if( xevent.type == ButtonPress )
388 switch( ((XButtonEvent *)&xevent)->button )
391 /* in this part we will eventually manage
392 * clicks for DVD navigation for instance */
396 GnomeTogglePointer( p_intf );
400 /* toggle the menu display */
401 vlc_mutex_lock( &p_intf->p_sys->p_gnome->change_lock );
402 p_intf->p_sys->p_gnome->b_popup_changed = 1;
403 vlc_mutex_unlock( &p_intf->p_sys->p_gnome->change_lock );
412 intf_DbgMsg( "%p -> unhandled event type %d received\n",
413 p_intf, xevent.type );
418 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
419 * are handled - according to the man pages, the format is always 32
421 while( XCheckTypedEvent( p_intf->p_sys->p_display,
422 ClientMessage, &xevent ) )
424 if( (xevent.xclient.message_type == p_intf->p_sys->wm_protocols)
425 && (xevent.xclient.data.l[0] == p_intf->p_sys->wm_delete_window ) )
431 intf_DbgMsg( "%p -> unhandled ClientMessage received\n", p_intf );
436 * Handle vout or interface windows resizing
438 if( p_intf->p_vout != NULL )
442 /* If interface window has been resized, change vout size */
443 intf_DbgMsg( "resizing output window\n" );
444 vlc_mutex_lock( &p_intf->p_vout->change_lock );
445 p_intf->p_vout->i_width = p_intf->p_sys->i_width;
446 p_intf->p_vout->i_height = p_intf->p_sys->i_height;
447 p_intf->p_vout->i_changes |= VOUT_SIZE_CHANGE;
448 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
450 else if( (p_intf->p_vout->i_width != p_intf->p_sys->i_width) ||
451 (p_intf->p_vout->i_height != p_intf->p_sys->i_height) )
453 /* If video output size has changed, change interface window size */
454 intf_DbgMsg( "resizing output window\n" );
455 p_intf->p_sys->i_width = p_intf->p_vout->i_width;
456 p_intf->p_sys->i_height = p_intf->p_vout->i_height;
457 XResizeWindow( p_intf->p_sys->p_display, p_intf->p_sys->window,
458 p_intf->p_sys->i_width, p_intf->p_sys->i_height );
463 /*****************************************************************************
464 * GnomeEnableScreenSaver: enable screen saver
465 *****************************************************************************
466 * This function enable the screen saver on a display after it had been
467 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
468 * know wether the screen saver can be activated or not: if n successive calls
469 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
470 * will be required before the screen saver could effectively be activated.
471 *****************************************************************************/
472 void GnomeEnableScreenSaver( intf_thread_t *p_intf )
474 if( p_intf->p_sys->i_ss_count++ == 0 )
476 intf_Msg( "Enabling screen saver\n" );
477 XSetScreenSaver( p_intf->p_sys->p_display, p_intf->p_sys->i_ss_timeout,
478 p_intf->p_sys->i_ss_interval, p_intf->p_sys->i_ss_blanking,
479 p_intf->p_sys->i_ss_exposure );
483 /*****************************************************************************
484 * GnomeDisableScreenSaver: disable screen saver
485 *****************************************************************************
486 * See XEnableScreenSaver
487 *****************************************************************************/
488 void GnomeDisableScreenSaver( intf_thread_t *p_intf )
490 if( --p_intf->p_sys->i_ss_count == 0 )
492 /* Save screen saver informations */
493 XGetScreenSaver( p_intf->p_sys->p_display, &p_intf->p_sys->i_ss_timeout,
494 &p_intf->p_sys->i_ss_interval, &p_intf->p_sys->i_ss_blanking,
495 &p_intf->p_sys->i_ss_exposure );
497 /* Disable screen saver */
498 intf_Msg("Disabling screen saver\n");
499 XSetScreenSaver( p_intf->p_sys->p_display, 0,
500 p_intf->p_sys->i_ss_interval, p_intf->p_sys->i_ss_blanking,
501 p_intf->p_sys->i_ss_exposure );
505 /*****************************************************************************
506 * GnomeTogglePointer: hide or show the mouse pointer
507 *****************************************************************************
508 * This function hides the X pointer if it is visible by putting it at
509 * coordinates (32,32) and setting the pointer sprite to a blank one. To
510 * show it again, we disable the sprite and restore the original coordinates.
511 *****************************************************************************/
512 void GnomeTogglePointer( intf_thread_t *p_intf )
514 static Cursor cursor;
515 static boolean_t b_cursor = 0;
517 if( p_intf->p_sys->b_mouse )
519 p_intf->p_sys->b_mouse = 0;
524 Pixmap blank = XCreatePixmap( p_intf->p_sys->p_display,
525 DefaultRootWindow(p_intf->p_sys->p_display),
528 XParseColor( p_intf->p_sys->p_display,
529 XCreateColormap( p_intf->p_sys->p_display,
531 p_intf->p_sys->p_display ),
533 p_intf->p_sys->p_display,
534 p_intf->p_sys->i_screen ),
538 cursor = XCreatePixmapCursor( p_intf->p_sys->p_display,
539 blank, blank, &color, &color, 1, 1 );
543 XDefineCursor( p_intf->p_sys->p_display,
544 p_intf->p_sys->window, cursor );
548 p_intf->p_sys->b_mouse = 1;
550 XUndefineCursor( p_intf->p_sys->p_display, p_intf->p_sys->window );
554 /*****************************************************************************
555 * GnomeManageInterface: manage messages from the Gnome interface
557 *****************************************************************************
558 * In this function, called approx. 10 times a second, we check what the
559 * Gnome interface wanted to tell us.
560 *****************************************************************************/
561 static void GnomeManageInterface( intf_thread_t *p_intf )
563 gnome_thread_t *p_gnome = p_intf->p_sys->p_gnome;
565 /* lock the change structure */
566 vlc_mutex_lock( &p_gnome->change_lock );
568 /* you killed my father, prepare to die */
574 if( p_gnome->b_activity_changed )
576 vlc_mutex_lock( &p_intf->p_vout->picture_lock );
577 p_intf->p_vout->b_active = p_gnome->b_activity;
578 /* having to access p_main sucks */
579 p_main->p_aout->b_active = p_gnome->b_activity;
580 vlc_mutex_unlock( &p_intf->p_vout->picture_lock );
582 p_gnome->b_activity_changed = 0;
585 /* unlock the change structure */
586 vlc_mutex_unlock( &p_gnome->change_lock );
589 /*****************************************************************************
590 * GnomeManageMain: manage main thread messages
591 *****************************************************************************
592 * In this function, called approx. 10 times a second, we check what the
593 * main program wanted to tell us.
594 *****************************************************************************/
595 static gint GnomeManageMain( gpointer p_data )
597 gnome_thread_t *p_gnome = (void *)p_data;
599 /* lock the change structure */
600 vlc_mutex_lock( &p_gnome->change_lock );
604 /* unlock the change structure */
605 vlc_mutex_unlock( &p_gnome->change_lock );
607 /* prepare to die, young man */
612 /* if the "display popup" flag has changed */
613 if( p_gnome->b_popup_changed )
615 gnome_popup_menu_do_popup( p_gnome->p_popup,
616 NULL, NULL, NULL, NULL );
617 p_gnome->b_popup_changed = 0;
620 /* unlock the change structure */
621 vlc_mutex_unlock( &p_gnome->change_lock );
626 /*****************************************************************************
627 * GnomeThread: special Gnome thread
628 *****************************************************************************
629 * this part of the interface is in a separate thread so that we can call
630 * gtk_main() from within it without annoying the rest of the program.
631 * XXX: the approach may look kludgy, and probably is, but I could not find
632 * a better way to dynamically load a Gnome interface at runtime.
633 *****************************************************************************/
634 void GnomeThread( gnome_thread_t *p_gnome )
636 /* gnome_init needs to know the command line. We don't care, so we
637 * give it an empty one */
638 char *p_args[] = { };
640 /* Sleep to avoid using all CPU - since some interfaces needs to access
641 * keyboard events, a 100ms delay is a good compromise */
642 gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManageMain, p_gnome );
644 gnome_init( "vlc", VERSION, 1, p_args );
646 /* create some useful widgets that will certainly be used */
647 p_gnome->p_window = create_intf_window();
648 p_gnome->p_popup = create_intf_popup( );
650 /* we don't create these ones yet because we perhaps won't need them */
651 p_gnome->p_about = NULL;
652 p_gnome->p_playlist = NULL;
654 /* store p_sys to keep an eye on it */
655 gtk_object_set_data( GTK_OBJECT(p_gnome->p_window), "p_gnome", p_gnome );
656 gtk_object_set_data( GTK_OBJECT(p_gnome->p_popup), "p_gnome", p_gnome );
658 /* show the control window */
659 //gtk_widget_show( p_gnome->p_window );
661 /* enter gnome mode */