]> git.sesse.net Git - vlc/blob - modules/gui/gtk2/gtk2.c
Improvements to preferences
[vlc] / modules / gui / gtk2 / gtk2.c
1 /*****************************************************************************
2  * gtk2.c : Gtk2 plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
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_category( CAT_INTERFACE );
56     set_subcategory( SUBCAT_INTERFACE_GENERAL );
57     set_description( _("Gtk2 interface") );
58     set_capability( "interface", i );
59     set_callbacks( Open, Close );
60     set_program( "gvlc" );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * intf_sys_t
65  *****************************************************************************/
66 struct intf_sys_t
67 {
68     module_t *p_gui_helper;
69
70     GtkWidget *p_window;
71 };
72
73 /*****************************************************************************
74  * Open: initialize and create window
75  *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
77 {
78     intf_thread_t *p_intf = (intf_thread_t *)p_this;
79
80     /* Allocate instance and initialize some members */
81     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
82     if( p_intf->p_sys == NULL )
83     {
84         msg_Err( p_intf, "out of memory" );
85         return VLC_ENOMEM;
86     }
87
88 #ifdef NEED_GTK2_MAIN
89     p_intf->p_sys->p_gui_helper =
90         module_Need( p_this, "gui-helper", "gtk2", VLC_TRUE );
91     if( p_intf->p_sys->p_gui_helper == NULL )
92     {
93         free( p_intf->p_sys );
94         return VLC_ENOMOD;
95     }
96 #endif
97
98     p_intf->pf_run = Run;
99
100     return VLC_SUCCESS;
101 }
102
103 /*****************************************************************************
104  * Close: destroy interface window
105  *****************************************************************************/
106 static void Close( vlc_object_t *p_this )
107 {
108     intf_thread_t *p_intf = (intf_thread_t *)p_this;
109
110 #ifdef NEED_GTK2_MAIN
111     module_Unneed( p_intf, p_intf->p_sys->p_gui_helper );
112 #endif
113
114     /* Destroy structure */
115     free( p_intf->p_sys );
116 }
117
118 /*****************************************************************************
119  * Run: Gtk2 thread
120  *****************************************************************************
121  * this part of the interface is in a separate thread so that we can call
122  * gtk_main() from within it without annoying the rest of the program.
123  *****************************************************************************/
124 static void Run( intf_thread_t *p_intf )
125 {
126 #ifdef NEED_GTK2_MAIN
127     gdk_threads_enter();
128 #else
129     /* gtk_init needs to know the command line. We don't care, so we
130      * give it an empty one */
131     char  *p_args[] = { "", NULL };
132     char **pp_args  = p_args;
133     int    i_args   = 1;
134     int    i_dummy;
135
136     gtk_set_locale();
137     gtk_init( &i_args, &pp_args );
138 #endif
139
140     /* Create some useful widgets that will certainly be used */
141     p_intf->p_sys->p_window = create_window1();
142
143     /* Set the title of the main window */
144     gtk_window_set_title( GTK_WINDOW(p_intf->p_sys->p_window),
145                           VOUT_TITLE " (Gtk2 interface)");
146
147     /* Show the control window */
148     gtk_widget_show( p_intf->p_sys->p_window );
149
150 #ifdef NEED_GTK2_MAIN
151     while( !p_intf->b_die )
152     {
153         Manage( p_intf );
154
155         /* Sleep to avoid using all CPU - since some interfaces need to
156          * access keyboard events, a 100ms delay is a good compromise */
157         gdk_threads_leave();
158         msleep( INTF_IDLE_SLEEP );
159         gdk_threads_enter();
160     }
161 #else
162     /* Sleep to avoid using all CPU - since some interfaces needs to access
163      * keyboard events, a 100ms delay is a good compromise */
164     i_dummy = gtk_timeout_add( INTF_IDLE_SLEEP / 1000, (GtkFunction)Manage,
165                                p_intf );
166     /* Enter Gtk mode */
167     gtk_main();
168     /* Remove the timeout */
169     gtk_timeout_remove( i_dummy );
170 #endif
171
172     gtk_object_destroy( GTK_OBJECT(p_intf->p_sys->p_window) );
173
174 #ifdef NEED_GTK2_MAIN
175     gdk_threads_leave();
176 #endif
177 }
178
179 /* following functions are local */
180
181 /*****************************************************************************
182  * Manage: manage main thread messages
183  *****************************************************************************
184  * In this function, called approx. 10 times a second, we check what the
185  * main program wanted to tell us.
186  *****************************************************************************/
187 static int Manage( intf_thread_t *p_intf )
188 {
189 #ifndef NEED_GTK2_MAIN
190     if( p_intf->b_die )
191     {
192         gtk_main_quit();
193
194         return FALSE;
195     }
196 #endif
197
198     return TRUE;
199 }