]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/skin_main.cpp
* modules/gui/wxwindows/*: don't forget to delete the timer.
[vlc] / modules / gui / skins / src / skin_main.cpp
1 /*****************************************************************************
2  * skin-main.cpp: skins plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: skin_main.cpp,v 1.48 2003/10/14 22:41:41 gbazin Exp $
6  *
7  * Authors: Olivier Teulière <ipkiss@via.ecp.fr>
8  *          Emmanuel Puig    <karibu@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111,
23  * USA.
24  *****************************************************************************/
25
26 //--- VLC -------------------------------------------------------------------
27 #include <vlc/vlc.h>
28 #include <vlc/intf.h>
29 #include <vlc/aout.h>
30
31 //--- SKIN ------------------------------------------------------------------
32 #include "../os_api.h"
33 #include "event.h"
34 #include "banks.h"
35 #include "window.h"
36 #include "theme.h"
37 #include "../os_theme.h"
38 #include "themeloader.h"
39 #include "vlcproc.h"
40 #include "skin_common.h"
41 #include "dialogs.h"
42
43 #ifdef X11_SKINS
44 #include <X11/Xlib.h>
45 #include <Imlib2.h>
46 #endif
47
48 //---------------------------------------------------------------------------
49 // Interface thread
50 // It is a global variable because we have C code for the parser, and we
51 // need to access C++ objects from there
52 //---------------------------------------------------------------------------
53 intf_thread_t *g_pIntf;
54
55 //---------------------------------------------------------------------------
56 // Exported interface functions.
57 //---------------------------------------------------------------------------
58 #ifdef WIN32
59 extern "C" __declspec( dllexport )
60     int __VLC_SYMBOL( vlc_entry ) ( module_t *p_module );
61 #endif
62
63 //---------------------------------------------------------------------------
64 // Local prototypes.
65 //---------------------------------------------------------------------------
66 static int  Open   ( vlc_object_t * );
67 static void Close  ( vlc_object_t * );
68 static void Run    ( intf_thread_t * );
69
70 int  SkinManage( intf_thread_t *p_intf );
71 void OSRun( intf_thread_t *p_intf );
72
73 //---------------------------------------------------------------------------
74 // Open: initialize interface
75 //---------------------------------------------------------------------------
76 static int Open ( vlc_object_t *p_this )
77 {
78     intf_thread_t *p_intf = (intf_thread_t *)p_this;
79     g_pIntf = p_intf;
80
81     // Allocate instance and initialize some members
82     p_intf->p_sys = (intf_sys_t *) malloc( sizeof( intf_sys_t ) );
83     if( p_intf->p_sys == NULL )
84     {
85         msg_Err( p_intf, "out of memory" );
86         return( 1 );
87     };
88
89     p_intf->pf_run = Run;
90
91     // Suscribe to messages bank
92     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
93
94     // Set no new theme when opening file
95     p_intf->p_sys->p_new_theme_file = NULL;
96
97     // Initialize info on playlist
98     p_intf->p_sys->i_index        = -1;
99     p_intf->p_sys->i_size         = 0;
100
101     p_intf->p_sys->i_close_status = VLC_NOTHING;
102
103     p_intf->p_sys->p_input = NULL;
104     p_intf->p_sys->p_playlist = (playlist_t *)vlc_object_find( p_intf,
105         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
106
107 #if defined X11_SKINS
108     // Initialize X11
109     Display *display = XOpenDisplay( NULL );
110     p_intf->p_sys->display = display;
111     vlc_mutex_init( p_intf, &p_intf->p_sys->xlock );
112     // Fake window to receive broadcast events
113     Window root = DefaultRootWindow( display );
114     p_intf->p_sys->mainWin = XCreateSimpleWindow( display, root, 0, 0, 
115                                                   1, 1, 0, 0, 0 );
116     XStoreName( display, p_intf->p_sys->mainWin, "VLC Media Player" );
117
118     // Load the vlc icon
119     int screen = DefaultScreen( display );
120     Screen *screenptr = DefaultScreenOfDisplay( display );
121     Visual *visual = DefaultVisualOfScreen( screenptr );
122     imlib_context_set_display( display );
123     imlib_context_set_visual( visual );
124     imlib_context_set_drawable( root );
125     imlib_context_set_colormap( DefaultColormap( display, screen ) );
126     imlib_context_set_dither( 1 );
127     imlib_context_set_blend( 1 );
128     Imlib_Image img = imlib_load_image_immediately( DATA_PATH"/vlc32x32.png" );
129     if( img == NULL )
130     {
131         // for developers ;)
132         img = imlib_load_image_immediately( "./share/vlc32x32.png" );
133     }
134     if( img == NULL )
135     {
136         msg_Err( p_intf, "loading vlc icon failed" );
137         p_intf->p_sys->iconPixmap = None;
138         p_intf->p_sys->iconMask = None;
139     }
140     else
141     {
142         imlib_context_set_image( img );
143         imlib_render_pixmaps_for_whole_image( &p_intf->p_sys->iconPixmap,
144                                               &p_intf->p_sys->iconMask );
145         imlib_free_image();
146     }
147
148
149 #elif defined WIN32
150     // Interface thread id used to post broadcast messages
151     p_intf->p_sys->dwThreadId = GetCurrentThreadId();
152
153     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
154     p_intf->p_sys->h_msimg32_dll = LoadLibrary("msimg32.dll");
155     if( !p_intf->p_sys->h_msimg32_dll ||
156         !( p_intf->p_sys->TransparentBlt =
157            (BOOL (WINAPI*)(HDC,int,int,int,int,HDC,
158                            int,int,int,int,unsigned int))
159            GetProcAddress( p_intf->p_sys->h_msimg32_dll, "TransparentBlt" ) ) )
160     {
161         p_intf->p_sys->TransparentBlt = NULL;
162         msg_Dbg( p_intf, "Couldn't find TransparentBlt(), "
163                  "falling back to BitBlt()" );
164     }
165
166     // idem for user32.dll and SetLayeredWindowAttributes()
167     p_intf->p_sys->h_user32_dll = LoadLibrary("user32.dll");
168     if( !p_intf->p_sys->h_user32_dll ||
169         !( p_intf->p_sys->SetLayeredWindowAttributes =
170            (BOOL (WINAPI *)(HWND,COLORREF,BYTE,DWORD))
171            GetProcAddress( p_intf->p_sys->h_user32_dll,
172                            "SetLayeredWindowAttributes" ) ) )
173     {
174         p_intf->p_sys->SetLayeredWindowAttributes = NULL;
175         msg_Dbg( p_intf, "Couldn't find SetLayeredWindowAttributes()" );
176     }
177
178 #endif
179
180     p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
181
182     return( 0 );
183 }
184
185 //---------------------------------------------------------------------------
186 // Close: destroy interface
187 //---------------------------------------------------------------------------
188 static void Close ( vlc_object_t *p_this )
189 {
190     intf_thread_t *p_intf = (intf_thread_t *)p_this;
191
192     if( p_intf->p_sys->p_input )
193     {
194         vlc_object_release( p_intf->p_sys->p_input );
195     }
196
197     if( p_intf->p_sys->p_playlist )
198     {
199         vlc_object_release( p_intf->p_sys->p_playlist );
200     }
201
202     // Delete theme, it's important to do it correctly
203     delete (OSTheme *)p_intf->p_sys->p_theme;
204
205 #if defined X11_SKINS
206     XDestroyWindow( p_intf->p_sys->display, p_intf->p_sys->mainWin );
207
208     // There is a bug in imlib2 which prevents us from closing the display
209     // (__imlib_RenderImage() can free old GC with already closed display)
210     //XCloseDisplay( p_intf->p_sys->display );
211 #endif
212
213     // Unsuscribe to messages bank
214     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
215
216 #ifdef WIN32
217     // Unload msimg32.dll and user32.dll
218     if( p_intf->p_sys->h_msimg32_dll )
219         FreeLibrary( p_intf->p_sys->h_msimg32_dll );
220     if( p_intf->p_sys->h_user32_dll )
221         FreeLibrary( p_intf->p_sys->h_user32_dll );
222 #elif defined X11_SKINS
223     vlc_mutex_destroy( &p_intf->p_sys->xlock );
224 #endif
225
226     // Destroy structure
227     free( p_intf->p_sys );
228 }
229
230
231 //---------------------------------------------------------------------------
232 // Run: main loop
233 //---------------------------------------------------------------------------
234 static void Run( intf_thread_t *p_intf )
235 {
236
237     int a = OSAPI_GetTime();
238
239     // Initialize the dialog boxes
240     p_intf->p_sys->p_dialogs = new Dialogs( p_intf );
241     if( !p_intf->p_sys->p_dialogs ) return;
242
243     // Load a theme
244     char *skin_last = config_GetPsz( p_intf, "skin_last" );
245     ThemeLoader *Loader = new ThemeLoader( p_intf );
246
247     if( skin_last == NULL || ! Loader->Load( skin_last ) )
248     {
249         // Too bad, it failed. Let's try with the default theme
250 //        if( ! Loader->Load( DEFAULT_SKIN_FILE ) )
251 #ifdef WIN32
252         string default_dir = (string)p_intf->p_libvlc->psz_vlcpath +
253                              DIRECTORY_SEPARATOR + "skins" +
254                              DIRECTORY_SEPARATOR + "default" +
255                              DIRECTORY_SEPARATOR + "theme.xml";
256         if( ! Loader->Load( default_dir ) )
257         {
258             // Last chance: the user can  select a new theme file
259 #else
260         string user_skin = (string)p_intf->p_vlc->psz_homedir +
261                               DIRECTORY_SEPARATOR + CONFIG_DIR +
262                               DIRECTORY_SEPARATOR + "skins" +
263                               DIRECTORY_SEPARATOR + "default" +
264                               DIRECTORY_SEPARATOR + "theme.xml";
265
266         string default_skin = (string)DATA_PATH +
267                               DIRECTORY_SEPARATOR + "skins" +
268                               DIRECTORY_SEPARATOR + "default" +
269                               DIRECTORY_SEPARATOR + "theme.xml";
270         if( !Loader->Load( user_skin ) && !Loader->Load( default_skin ) )
271         {
272 #endif
273             p_intf->p_sys->p_dialogs->ShowOpenSkin( 1 /* block */ );
274
275             // try to load selected file
276             if( !p_intf->p_sys->p_new_theme_file ||
277                 !Loader->Load( (string)p_intf->p_sys->p_new_theme_file ) )
278             {
279                 // He, he, what the hell is he doing ?
280                 delete Loader;
281                 delete p_intf->p_sys->p_dialogs;
282                 if( skin_last ) free( skin_last );
283                 return;
284             }
285         }
286     }
287
288     // Show the theme
289     p_intf->p_sys->p_theme->InitTheme();
290     p_intf->p_sys->p_theme->ShowTheme();
291
292     if( skin_last ) free( skin_last );
293     delete Loader;
294
295     msg_Dbg( p_intf, "Load theme time : %i ms", OSAPI_GetTime() - a );
296
297     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (int)true );
298
299     OSRun( p_intf );
300
301     // clean up the dialog boxes
302     delete p_intf->p_sys->p_dialogs;
303 }
304
305 //---------------------------------------------------------------------------
306 // Module descriptor
307 //---------------------------------------------------------------------------
308 #define DEFAULT_SKIN        N_("Last skin actually used")
309 #define DEFAULT_SKIN_LONG   N_("Last skin actually used")
310 #define SKIN_CONFIG         N_("Config of last used skin")
311 #define SKIN_CONFIG_LONG    N_("Config of last used skin")
312 #define SKIN_TRAY           N_("Show application in system tray")
313 #define SKIN_TRAY_LONG      N_("Show application in system tray")
314 #define SKIN_TASKBAR        N_("Show application in taskbar")
315 #define SKIN_TASKBAR_LONG   N_("Show application in taskbar")
316
317 vlc_module_begin();
318     add_string( "skin_last", "", NULL, DEFAULT_SKIN, DEFAULT_SKIN_LONG,
319                 VLC_TRUE );
320     add_string( "skin_config", "", NULL, SKIN_CONFIG, SKIN_CONFIG_LONG,
321                 VLC_TRUE );
322     add_bool( "show_in_tray", VLC_FALSE, NULL, SKIN_TRAY, SKIN_TRAY_LONG,
323               VLC_FALSE );
324     add_bool( "show_in_taskbar", VLC_TRUE, NULL, SKIN_TASKBAR,
325               SKIN_TASKBAR_LONG, VLC_FALSE );
326     set_description( _("Skinnable Interface") );
327     set_capability( "interface", 30 );
328     set_callbacks( Open, Close );
329     set_program( "svlc" );
330 vlc_module_end();
331
332
333 //---------------------------------------------------------------------------
334 // Refresh procedure
335 //---------------------------------------------------------------------------
336 int SkinManage( intf_thread_t *p_intf )
337 {
338     vlc_mutex_lock( &p_intf->change_lock );
339
340     // Update the input
341     if( p_intf->p_sys->p_input == NULL )
342     {
343         p_intf->p_sys->p_input = (input_thread_t *)
344                     vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
345     }
346     else if( p_intf->p_sys->p_input->b_dead )
347     {
348         vlc_object_release( p_intf->p_sys->p_input );
349         p_intf->p_sys->p_input = NULL;
350     }
351
352     //-------------------------------------------------------------------------
353     if( p_intf->p_sys->p_input != NULL && !p_intf->p_sys->p_input->b_die )
354     {
355         input_thread_t  * p_input = p_intf->p_sys->p_input;
356
357         vlc_mutex_lock( &p_input->stream.stream_lock );
358
359         // Refresh sound volume
360         audio_volume_t volume;
361
362         // Get sound volume from VLC
363         aout_VolumeGet( p_intf, &volume);
364
365         // Update sliders
366         OSAPI_PostMessage( NULL, CTRL_SET_SLIDER,
367             (unsigned int)
368             p_intf->p_sys->p_theme->EvtBank->Get( "volume_refresh" ),
369             (long)( volume * SLIDER_RANGE / AOUT_VOLUME_MAX ) );
370
371         // Refresh slider
372         // if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
373         if( p_input->stream.b_seekable )
374         {
375 #define p_area p_input->stream.p_selected_area
376
377             // Set value of sliders
378             long Value = SLIDER_RANGE *
379                 p_input->stream.p_selected_area->i_tell /
380                 p_input->stream.p_selected_area->i_size;
381
382             // Update sliders
383             OSAPI_PostMessage( NULL, CTRL_SET_SLIDER, (unsigned int)
384                 p_intf->p_sys->p_theme->EvtBank->Get( "time" ), (long)Value );
385
386             // Text char * for updating text controls
387             char *text = new char[OFFSETTOTIME_MAX_SIZE];
388
389             // Create end time text
390             input_OffsetToTime( p_intf->p_sys->p_input, &text[1],
391                                 p_area->i_size - p_area->i_tell );
392             text[0] = '-';
393             p_intf->p_sys->p_theme->EvtBank->Get( "left_time" )
394                 ->PostTextMessage( text );
395
396             // Create time text and update
397             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_tell );
398             p_intf->p_sys->p_theme->EvtBank->Get( "time" )
399                 ->PostTextMessage( text );
400
401             // Create total time text
402             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_size );
403             p_intf->p_sys->p_theme->EvtBank->Get( "total_time" )
404                 ->PostTextMessage( text );
405
406             // Free memory
407             delete[] text;
408
409 #undef p_area
410         }
411         vlc_mutex_unlock( &p_input->stream.stream_lock );
412     }
413     //-------------------------------------------------------------------------
414     vlc_mutex_unlock( &p_intf->change_lock );
415
416     return( VLC_TRUE );
417 }