]> git.sesse.net Git - vlc/blob - modules/gui/gtk2/gtk2.c
* Mac OS X: intercept and respond to user-configured VLC hotkeys, rather
[vlc] / modules / gui / gtk2 / gtk2.c
1 /*****************************************************************************
2  * gtk2.c : Gtk2 plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: gtk2.c,v 1.3 2004/03/03 20:39:52 gbazin Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #include <gtk/gtk.h>
36
37 #include "gtk2_callbacks.h"
38 #include "gtk2_interface.h"
39 #include "gtk2_support.h"
40
41 /*****************************************************************************
42  * Local prototypes.
43  *****************************************************************************/
44 static int  Open         ( vlc_object_t * );
45 static void Close        ( vlc_object_t * );
46
47 static void Run          ( intf_thread_t * );
48 static int  Manage       ( intf_thread_t * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();
54     int i = getenv( "DISPLAY" ) == NULL ? 15 : 95;
55     set_description( _("Gtk2 interface") );
56     set_capability( "interface", i );
57     set_callbacks( Open, Close );
58     set_program( "gvlc" );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * intf_sys_t
63  *****************************************************************************/
64 struct intf_sys_t
65 {
66     module_t *p_gui_helper;
67
68     GtkWidget *p_window;
69 };
70
71 /*****************************************************************************
72  * Open: initialize and create window
73  *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
75 {
76     intf_thread_t *p_intf = (intf_thread_t *)p_this;
77
78     /* Allocate instance and initialize some members */
79     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
80     if( p_intf->p_sys == NULL )
81     {
82         msg_Err( p_intf, "out of memory" );
83         return VLC_ENOMEM;
84     }
85
86 #ifdef NEED_GTK2_MAIN
87     p_intf->p_sys->p_gui_helper =
88         module_Need( p_this, "gui-helper", "gtk2", VLC_TRUE );
89     if( p_intf->p_sys->p_gui_helper == NULL )
90     {
91         free( p_intf->p_sys );
92         return VLC_ENOMOD;
93     }
94 #endif
95
96     p_intf->pf_run = Run;
97
98     return VLC_SUCCESS;
99 }
100
101 /*****************************************************************************
102  * Close: destroy interface window
103  *****************************************************************************/
104 static void Close( vlc_object_t *p_this )
105 {
106     intf_thread_t *p_intf = (intf_thread_t *)p_this;
107
108 #ifdef NEED_GTK2_MAIN
109     module_Unneed( p_intf, p_intf->p_sys->p_gui_helper );
110 #endif
111
112     /* Destroy structure */
113     free( p_intf->p_sys );
114 }
115
116 /*****************************************************************************
117  * Run: Gtk2 thread
118  *****************************************************************************
119  * this part of the interface is in a separate thread so that we can call
120  * gtk_main() from within it without annoying the rest of the program.
121  *****************************************************************************/
122 static void Run( intf_thread_t *p_intf )
123 {
124 #ifdef NEED_GTK2_MAIN
125     gdk_threads_enter();
126 #else
127     /* gtk_init needs to know the command line. We don't care, so we
128      * give it an empty one */
129     char  *p_args[] = { "", NULL };
130     char **pp_args  = p_args;
131     int    i_args   = 1;
132     int    i_dummy;
133
134     gtk_set_locale();
135     gtk_init( &i_args, &pp_args );
136 #endif
137
138     /* Create some useful widgets that will certainly be used */
139     p_intf->p_sys->p_window = create_window1();
140
141     /* Set the title of the main window */
142     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
143                           VOUT_TITLE " (Gtk2 interface)");
144
145     /* Show the control window */
146     gtk_widget_show( p_intf->p_sys->p_window );
147
148 #ifdef NEED_GTK2_MAIN
149     while( !p_intf->b_die )
150     {
151         Manage( p_intf );
152
153         /* Sleep to avoid using all CPU - since some interfaces need to
154          * access keyboard events, a 100ms delay is a good compromise */
155         gdk_threads_leave();
156         msleep( INTF_IDLE_SLEEP );
157         gdk_threads_enter();
158     }
159 #else
160     /* Sleep to avoid using all CPU - since some interfaces needs to access
161      * keyboard events, a 100ms delay is a good compromise */
162     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
163                                p_intf );
164     /* Enter Gtk mode */
165     gtk_main();
166     /* Remove the timeout */
167     gtk_timeout_remove( i_dummy );
168 #endif
169
170     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
171
172 #ifdef NEED_GTK2_MAIN
173     gdk_threads_leave();
174 #endif
175 }
176
177 /* following functions are local */
178
179 /*****************************************************************************
180  * Manage: manage main thread messages
181  *****************************************************************************
182  * In this function, called approx. 10 times a second, we check what the
183  * main program wanted to tell us.
184  *****************************************************************************/
185 static int Manage( intf_thread_t *p_intf )
186 {
187 #ifndef NEED_GTK2_MAIN
188     if( p_intf->b_die )
189     {
190         gtk_main_quit();
191
192         return FALSE;
193     }
194 #endif
195
196     return TRUE;
197 }