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 /* create basic key bindings */
140 intf_AssignNormalKeys( p_intf );
143 /* Disable screen saver and return */
144 p_intf->p_sys->i_ss_count = 1;
145 GnomeDisableScreenSaver( p_intf );
149 /*****************************************************************************
150 * intf_GnomeDestroy: destroy interface window
151 *****************************************************************************/
152 void intf_GnomeDestroy( intf_thread_t *p_intf )
154 /* Enable screen saver */
155 GnomeEnableScreenSaver( p_intf );
157 /* Close input thread, if any (blocking) */
158 if( p_intf->p_input )
160 input_DestroyThread( p_intf->p_input, NULL );
163 /* Close video output thread, if any (blocking) */
166 vout_DestroyThread( p_intf->p_vout, NULL );
169 /* Close gnome thread, if any (blocking) */
170 if( p_intf->p_sys->p_gnome->thread_id )
172 p_intf->p_sys->p_gnome->b_die = 1;
173 intf_Msg( "waiting for Gnome thread to terminate\n" );
174 vlc_thread_join( p_intf->p_sys->p_gnome->thread_id );
175 intf_Msg( "Gnome thread terminated\n" );
178 /* Close main window and display */
179 GnomeDestroyWindow( p_intf );
180 XCloseDisplay( p_intf->p_sys->p_display );
182 /* Destroy structures */
183 free( p_intf->p_sys->p_gnome );
184 free( p_intf->p_sys );
187 /*****************************************************************************
188 * intf_GnomeManage: event loop
189 *****************************************************************************/
190 void intf_GnomeManage( intf_thread_t *p_intf )
192 /* Manage main window */
193 GnomeManageWindow( p_intf );
195 /* Manage messages from the Gnome interface */
196 GnomeManageInterface( p_intf );
199 /* following functions are local */
201 /*****************************************************************************
202 * GnomeCreateWindow: open and set-up X11 main window
203 *****************************************************************************/
204 static int GnomeCreateWindow( intf_thread_t *p_intf )
206 XSizeHints xsize_hints;
207 XSetWindowAttributes xwindow_attributes;
211 boolean_t b_configure_notify;
212 boolean_t b_map_notify;
214 /* Set main window's size */
215 p_intf->p_sys->i_width = main_GetIntVariable( VOUT_WIDTH_VAR,
216 VOUT_WIDTH_DEFAULT );
217 p_intf->p_sys->i_height = main_GetIntVariable( VOUT_HEIGHT_VAR,
218 VOUT_HEIGHT_DEFAULT );
220 /* Prepare window manager hints and properties */
221 xsize_hints.base_width = p_intf->p_sys->i_width;
222 xsize_hints.base_height = p_intf->p_sys->i_height;
223 xsize_hints.flags = PSize;
224 p_intf->p_sys->wm_protocols = XInternAtom( p_intf->p_sys->p_display,
225 "WM_PROTOCOLS", True );
226 p_intf->p_sys->wm_delete_window = XInternAtom( p_intf->p_sys->p_display,
227 "WM_DELETE_WINDOW", True );
229 /* Prepare window attributes */
230 xwindow_attributes.backing_store = Always; /* save the hidden part */
231 xwindow_attributes.background_pixel = WhitePixel( p_intf->p_sys->p_display,
232 p_intf->p_sys->i_screen );
234 xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
236 /* Create the window and set hints - the window must receive ConfigureNotify
237 * events, and, until it is displayed, Expose and MapNotify events. */
238 p_intf->p_sys->window =
239 XCreateWindow( p_intf->p_sys->p_display,
240 DefaultRootWindow( p_intf->p_sys->p_display ),
242 p_intf->p_sys->i_width, p_intf->p_sys->i_height, 1,
244 CWBackingStore | CWBackPixel | CWEventMask,
245 &xwindow_attributes );
247 /* Set window manager hints and properties: size hints, command,
248 * window's name, and accepted protocols */
249 XSetWMNormalHints( p_intf->p_sys->p_display, p_intf->p_sys->window,
251 XSetCommand( p_intf->p_sys->p_display, p_intf->p_sys->window,
252 p_main->ppsz_argv, p_main->i_argc );
253 XStoreName( p_intf->p_sys->p_display, p_intf->p_sys->window, VOUT_TITLE );
254 if( (p_intf->p_sys->wm_protocols == None) /* use WM_DELETE_WINDOW */
255 || (p_intf->p_sys->wm_delete_window == None)
256 || !XSetWMProtocols( p_intf->p_sys->p_display, p_intf->p_sys->window,
257 &p_intf->p_sys->wm_delete_window, 1 ) )
259 /* WM_DELETE_WINDOW is not supported by window manager */
260 intf_Msg("error: missing or bad window manager - please exit program kindly.\n");
263 /* Creation of a graphic context that doesn't generate a GraphicsExpose
264 * event when using functions like XCopyArea */
265 xgcvalues.graphics_exposures = False;
266 p_intf->p_sys->gc = XCreateGC( p_intf->p_sys->p_display, p_intf->p_sys->window,
267 GCGraphicsExposures, &xgcvalues);
269 /* Send orders to server, and wait until window is displayed - three
270 * events must be received: a MapNotify event, an Expose event allowing
271 * drawing in the window, and a ConfigureNotify to get the window
272 * dimensions. Once those events have been received, only ConfigureNotify
273 * events need to be received. */
275 b_configure_notify = 0;
277 XMapWindow( p_intf->p_sys->p_display, p_intf->p_sys->window);
280 XNextEvent( p_intf->p_sys->p_display, &xevent);
281 if( (xevent.type == Expose)
282 && (xevent.xexpose.window == p_intf->p_sys->window) )
286 else if( (xevent.type == MapNotify)
287 && (xevent.xmap.window == p_intf->p_sys->window) )
291 else if( (xevent.type == ConfigureNotify)
292 && (xevent.xconfigure.window == p_intf->p_sys->window) )
294 b_configure_notify = 1;
295 p_intf->p_sys->i_width = xevent.xconfigure.width;
296 p_intf->p_sys->i_height = xevent.xconfigure.height;
298 } while( !( b_expose && b_configure_notify && b_map_notify ) );
300 XSelectInput( p_intf->p_sys->p_display, p_intf->p_sys->window,
301 StructureNotifyMask | KeyPressMask | ButtonPressMask );
303 if( XDefaultDepth(p_intf->p_sys->p_display, p_intf->p_sys->i_screen) == 8 )
305 /* Allocate a new palette */
306 p_intf->p_sys->colormap = XCreateColormap( p_intf->p_sys->p_display,
307 DefaultRootWindow( p_intf->p_sys->p_display ),
308 DefaultVisual( p_intf->p_sys->p_display,
309 p_intf->p_sys->i_screen ),
312 xwindow_attributes.colormap = p_intf->p_sys->colormap;
313 XChangeWindowAttributes( p_intf->p_sys->p_display,
314 p_intf->p_sys->window,
315 CWColormap, &xwindow_attributes );
318 /* At this stage, the window is open, displayed, and ready to receive data */
322 /*****************************************************************************
323 * GnomeDestroyWindow: destroy X11 main window
324 *****************************************************************************/
325 static void GnomeDestroyWindow( intf_thread_t *p_intf )
327 XUnmapWindow( p_intf->p_sys->p_display, p_intf->p_sys->window );
328 XFreeGC( p_intf->p_sys->p_display, p_intf->p_sys->gc );
329 XDestroyWindow( p_intf->p_sys->p_display, p_intf->p_sys->window );
332 /*****************************************************************************
333 * GnomeManageWindow: manage X11 main window
334 *****************************************************************************/
335 static void GnomeManageWindow( intf_thread_t *p_intf )
337 XEvent xevent; /* X11 event */
338 boolean_t b_resized; /* window has been resized */
339 char i_key; /* ISO Latin-1 key */
341 /* Handle X11 events: ConfigureNotify events are parsed to know if the
342 * output window's size changed, MapNotify and UnmapNotify to know if the
343 * window is mapped (and if the display is useful), and ClientMessages
344 * to intercept window destruction requests */
346 while( XCheckWindowEvent( p_intf->p_sys->p_display, p_intf->p_sys->window,
347 StructureNotifyMask | KeyPressMask |
348 ButtonPressMask, &xevent ) == True )
350 /* ConfigureNotify event: prepare */
351 if( (xevent.type == ConfigureNotify)
352 && ((xevent.xconfigure.width != p_intf->p_sys->i_width)
353 || (xevent.xconfigure.height != p_intf->p_sys->i_height)) )
355 /* Update dimensions */
357 p_intf->p_sys->i_width = xevent.xconfigure.width;
358 p_intf->p_sys->i_height = xevent.xconfigure.height;
360 /* MapNotify event: change window status and disable screen saver */
361 else if( xevent.type == MapNotify)
363 if( (p_intf->p_vout != NULL) && !p_intf->p_vout->b_active )
365 GnomeDisableScreenSaver( p_intf );
366 p_intf->p_vout->b_active = 1;
369 /* UnmapNotify event: change window status and enable screen saver */
370 else if( xevent.type == UnmapNotify )
372 if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_active )
374 GnomeEnableScreenSaver( p_intf );
375 p_intf->p_vout->b_active = 0;
379 else if( xevent.type == KeyPress )
381 if( XLookupString( &xevent.xkey, &i_key, 1, NULL, NULL ) )
383 if( intf_ProcessKey( p_intf, i_key ) )
385 intf_DbgMsg( "unhandled key '%c' (%i)\n", (char) i_key, i_key );
390 else if( xevent.type == ButtonPress )
392 switch( ((XButtonEvent *)&xevent)->button )
395 /* in this part we will eventually manage
396 * clicks for DVD navigation for instance */
400 GnomeTogglePointer( p_intf );
404 /* toggle the menu display */
405 vlc_mutex_lock( &p_intf->p_sys->p_gnome->change_lock );
406 p_intf->p_sys->p_gnome->b_popup_changed = 1;
407 vlc_mutex_unlock( &p_intf->p_sys->p_gnome->change_lock );
416 intf_DbgMsg( "%p -> unhandled event type %d received\n",
417 p_intf, xevent.type );
422 /* ClientMessage event - only WM_PROTOCOLS with WM_DELETE_WINDOW data
423 * are handled - according to the man pages, the format is always 32
425 while( XCheckTypedEvent( p_intf->p_sys->p_display,
426 ClientMessage, &xevent ) )
428 if( (xevent.xclient.message_type == p_intf->p_sys->wm_protocols)
429 && (xevent.xclient.data.l[0] == p_intf->p_sys->wm_delete_window ) )
435 intf_DbgMsg( "%p -> unhandled ClientMessage received\n", p_intf );
440 * Handle vout or interface windows resizing
442 if( p_intf->p_vout != NULL )
446 /* If interface window has been resized, change vout size */
447 intf_DbgMsg( "resizing output window\n" );
448 vlc_mutex_lock( &p_intf->p_vout->change_lock );
449 p_intf->p_vout->i_width = p_intf->p_sys->i_width;
450 p_intf->p_vout->i_height = p_intf->p_sys->i_height;
451 p_intf->p_vout->i_changes |= VOUT_SIZE_CHANGE;
452 vlc_mutex_unlock( &p_intf->p_vout->change_lock );
454 else if( (p_intf->p_vout->i_width != p_intf->p_sys->i_width) ||
455 (p_intf->p_vout->i_height != p_intf->p_sys->i_height) )
457 /* If video output size has changed, change interface window size */
458 intf_DbgMsg( "resizing output window\n" );
459 p_intf->p_sys->i_width = p_intf->p_vout->i_width;
460 p_intf->p_sys->i_height = p_intf->p_vout->i_height;
461 XResizeWindow( p_intf->p_sys->p_display, p_intf->p_sys->window,
462 p_intf->p_sys->i_width, p_intf->p_sys->i_height );
467 /*****************************************************************************
468 * GnomeEnableScreenSaver: enable screen saver
469 *****************************************************************************
470 * This function enable the screen saver on a display after it had been
471 * disabled by XDisableScreenSaver. Both functions use a counter mechanism to
472 * know wether the screen saver can be activated or not: if n successive calls
473 * are made to XDisableScreenSaver, n successive calls to XEnableScreenSaver
474 * will be required before the screen saver could effectively be activated.
475 *****************************************************************************/
476 void GnomeEnableScreenSaver( intf_thread_t *p_intf )
478 if( p_intf->p_sys->i_ss_count++ == 0 )
480 intf_Msg( "Enabling screen saver\n" );
481 XSetScreenSaver( p_intf->p_sys->p_display, p_intf->p_sys->i_ss_timeout,
482 p_intf->p_sys->i_ss_interval, p_intf->p_sys->i_ss_blanking,
483 p_intf->p_sys->i_ss_exposure );
487 /*****************************************************************************
488 * GnomeDisableScreenSaver: disable screen saver
489 *****************************************************************************
490 * See XEnableScreenSaver
491 *****************************************************************************/
492 void GnomeDisableScreenSaver( intf_thread_t *p_intf )
494 if( --p_intf->p_sys->i_ss_count == 0 )
496 /* Save screen saver informations */
497 XGetScreenSaver( p_intf->p_sys->p_display, &p_intf->p_sys->i_ss_timeout,
498 &p_intf->p_sys->i_ss_interval, &p_intf->p_sys->i_ss_blanking,
499 &p_intf->p_sys->i_ss_exposure );
501 /* Disable screen saver */
502 intf_Msg("Disabling screen saver\n");
503 XSetScreenSaver( p_intf->p_sys->p_display, 0,
504 p_intf->p_sys->i_ss_interval, p_intf->p_sys->i_ss_blanking,
505 p_intf->p_sys->i_ss_exposure );
509 /*****************************************************************************
510 * GnomeTogglePointer: hide or show the mouse pointer
511 *****************************************************************************
512 * This function hides the X pointer if it is visible by putting it at
513 * coordinates (32,32) and setting the pointer sprite to a blank one. To
514 * show it again, we disable the sprite and restore the original coordinates.
515 *****************************************************************************/
516 void GnomeTogglePointer( intf_thread_t *p_intf )
518 static Cursor cursor;
519 static boolean_t b_cursor = 0;
521 if( p_intf->p_sys->b_mouse )
523 p_intf->p_sys->b_mouse = 0;
528 Pixmap blank = XCreatePixmap( p_intf->p_sys->p_display,
529 DefaultRootWindow(p_intf->p_sys->p_display),
532 XParseColor( p_intf->p_sys->p_display,
533 XCreateColormap( p_intf->p_sys->p_display,
535 p_intf->p_sys->p_display ),
537 p_intf->p_sys->p_display,
538 p_intf->p_sys->i_screen ),
542 cursor = XCreatePixmapCursor( p_intf->p_sys->p_display,
543 blank, blank, &color, &color, 1, 1 );
547 XDefineCursor( p_intf->p_sys->p_display,
548 p_intf->p_sys->window, cursor );
552 p_intf->p_sys->b_mouse = 1;
554 XUndefineCursor( p_intf->p_sys->p_display, p_intf->p_sys->window );
558 /*****************************************************************************
559 * GnomeManageInterface: manage messages from the Gnome interface
561 *****************************************************************************
562 * In this function, called approx. 10 times a second, we check what the
563 * Gnome interface wanted to tell us.
564 *****************************************************************************/
565 static void GnomeManageInterface( intf_thread_t *p_intf )
567 gnome_thread_t *p_gnome = p_intf->p_sys->p_gnome;
569 /* lock the change structure */
570 vlc_mutex_lock( &p_gnome->change_lock );
572 /* you killed my father, prepare to die */
578 if( p_gnome->b_activity_changed )
580 vlc_mutex_lock( &p_intf->p_vout->picture_lock );
581 p_intf->p_vout->b_active = p_gnome->b_activity;
582 /* having to access p_main sucks */
583 p_main->p_aout->b_active = p_gnome->b_activity;
584 vlc_mutex_unlock( &p_intf->p_vout->picture_lock );
586 p_gnome->b_activity_changed = 0;
589 /* unlock the change structure */
590 vlc_mutex_unlock( &p_gnome->change_lock );
593 /*****************************************************************************
594 * GnomeManageMain: manage main thread messages
595 *****************************************************************************
596 * In this function, called approx. 10 times a second, we check what the
597 * main program wanted to tell us.
598 *****************************************************************************/
599 static gint GnomeManageMain( gpointer p_data )
601 gnome_thread_t *p_gnome = (void *)p_data;
603 /* lock the change structure */
604 vlc_mutex_lock( &p_gnome->change_lock );
608 /* unlock the change structure */
609 vlc_mutex_unlock( &p_gnome->change_lock );
611 /* prepare to die, young man */
616 /* if the "display popup" flag has changed */
617 if( p_gnome->b_popup_changed )
619 gnome_popup_menu_do_popup( p_gnome->p_popup,
620 NULL, NULL, NULL, NULL );
621 p_gnome->b_popup_changed = 0;
624 /* unlock the change structure */
625 vlc_mutex_unlock( &p_gnome->change_lock );
630 /*****************************************************************************
631 * GnomeThread: special Gnome thread
632 *****************************************************************************
633 * this part of the interface is in a separate thread so that we can call
634 * gtk_main() from within it without annoying the rest of the program.
635 * XXX: the approach may look kludgy, and probably is, but I could not find
636 * a better way to dynamically load a Gnome interface at runtime.
637 *****************************************************************************/
638 void GnomeThread( gnome_thread_t *p_gnome )
640 /* gnome_init needs to know the command line. We don't care, so we
641 * give it an empty one */
642 char *p_args[] = { };
644 /* Sleep to avoid using all CPU - since some interfaces needs to access
645 * keyboard events, a 100ms delay is a good compromise */
646 gtk_timeout_add( INTF_IDLE_SLEEP / 1000, GnomeManageMain, p_gnome );
648 gnome_init( "vlc", VERSION, 1, p_args );
650 /* create some useful widgets that will certainly be used */
651 p_gnome->p_window = create_intf_window();
652 p_gnome->p_popup = create_intf_popup( );
654 /* we don't create these ones yet because we perhaps won't need them */
655 p_gnome->p_about = NULL;
656 p_gnome->p_playlist = NULL;
658 /* store p_sys to keep an eye on it */
659 gtk_object_set_data( GTK_OBJECT(p_gnome->p_window), "p_gnome", p_gnome );
660 gtk_object_set_data( GTK_OBJECT(p_gnome->p_popup), "p_gnome", p_gnome );
662 /* show the control window */
663 //gtk_widget_show( p_gnome->p_window );
665 /* enter gnome mode */