]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/skin_main.cpp
c016944354aa43eaaf11f31a3ffe5f20b810a4d2
[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.55 2003/12/22 02:24:52 sam 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 // Local prototypes.
57 //---------------------------------------------------------------------------
58 static int  Open   ( vlc_object_t * );
59 static void Close  ( vlc_object_t * );
60 static void Run    ( intf_thread_t * );
61
62 int  SkinManage( intf_thread_t *p_intf );
63 void OSRun( intf_thread_t *p_intf );
64
65 //---------------------------------------------------------------------------
66 // Open: initialize interface
67 //---------------------------------------------------------------------------
68 static int Open ( vlc_object_t *p_this )
69 {
70     intf_thread_t *p_intf = (intf_thread_t *)p_this;
71     g_pIntf = p_intf;
72
73 #if defined X11_SKINS
74     // Open/Test for an X11 display
75     Display *display = XOpenDisplay( NULL );
76     if( !display ) return VLC_EGENERIC;
77 #endif
78
79     // Allocate instance and initialize some members
80     p_intf->p_sys = (intf_sys_t *) malloc( sizeof( intf_sys_t ) );
81     if( p_intf->p_sys == NULL )
82     {
83         msg_Err( p_intf, "out of memory" );
84         return( 1 );
85     };
86
87     p_intf->pf_run = Run;
88
89     // Suscribe to messages bank
90     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
91
92     // Set no new theme when opening file
93     p_intf->p_sys->p_new_theme_file = NULL;
94
95     // Initialize info on playlist
96     p_intf->p_sys->i_index        = -1;
97     p_intf->p_sys->i_size         = 0;
98
99     p_intf->p_sys->b_on_top = false;
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     p_intf->p_sys->display = display;
110     vlc_mutex_init( p_intf, &p_intf->p_sys->xlock );
111     // Fake window to receive broadcast events
112     Window root = DefaultRootWindow( display );
113     p_intf->p_sys->mainWin = XCreateSimpleWindow( display, root, 0, 0,
114                                                   1, 1, 0, 0, 0 );
115     XStoreName( display, p_intf->p_sys->mainWin, "VLC Media Player" );
116
117     // Load the vlc icon
118     int screen = DefaultScreen( display );
119     Screen *screenptr = DefaultScreenOfDisplay( display );
120     Visual *visual = DefaultVisualOfScreen( screenptr );
121     imlib_context_set_display( display );
122     imlib_context_set_visual( visual );
123     imlib_context_set_drawable( root );
124     imlib_context_set_colormap( DefaultColormap( display, screen ) );
125     imlib_context_set_dither( 1 );
126     imlib_context_set_blend( 1 );
127     Imlib_Image img = imlib_load_image_immediately( DATA_PATH"/vlc32x32.png" );
128     if( img == NULL )
129     {
130         // for developers ;)
131         img = imlib_load_image_immediately( "./share/vlc32x32.png" );
132     }
133     if( img == NULL )
134     {
135         msg_Err( p_intf, "loading vlc icon failed" );
136         p_intf->p_sys->iconPixmap = None;
137         p_intf->p_sys->iconMask = None;
138     }
139     else
140     {
141         imlib_context_set_image( img );
142         imlib_render_pixmaps_for_whole_image( &p_intf->p_sys->iconPixmap,
143                                               &p_intf->p_sys->iconMask );
144         imlib_free_image();
145     }
146
147
148 #elif defined WIN32
149     // Interface thread id used to post broadcast messages
150     p_intf->p_sys->dwThreadId = GetCurrentThreadId();
151
152     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
153     p_intf->p_sys->h_msimg32_dll = LoadLibrary("msimg32.dll");
154     if( !p_intf->p_sys->h_msimg32_dll ||
155         !( p_intf->p_sys->TransparentBlt =
156            (BOOL (WINAPI*)(HDC,int,int,int,int,HDC,
157                            int,int,int,int,unsigned int))
158            GetProcAddress( p_intf->p_sys->h_msimg32_dll, "TransparentBlt" ) ) )
159     {
160         p_intf->p_sys->TransparentBlt = NULL;
161         msg_Dbg( p_intf, "Couldn't find TransparentBlt(), "
162                  "falling back to BitBlt()" );
163     }
164
165     // idem for user32.dll and SetLayeredWindowAttributes()
166     p_intf->p_sys->h_user32_dll = LoadLibrary("user32.dll");
167     if( !p_intf->p_sys->h_user32_dll ||
168         !( p_intf->p_sys->SetLayeredWindowAttributes =
169            (BOOL (WINAPI *)(HWND,COLORREF,BYTE,DWORD))
170            GetProcAddress( p_intf->p_sys->h_user32_dll,
171                            "SetLayeredWindowAttributes" ) ) )
172     {
173         p_intf->p_sys->SetLayeredWindowAttributes = NULL;
174         msg_Dbg( p_intf, "Couldn't find SetLayeredWindowAttributes()" );
175     }
176
177 #endif
178
179     p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
180
181     return( 0 );
182 }
183
184 //---------------------------------------------------------------------------
185 // Close: destroy interface
186 //---------------------------------------------------------------------------
187 static void Close ( vlc_object_t *p_this )
188 {
189     intf_thread_t *p_intf = (intf_thread_t *)p_this;
190
191     if( p_intf->p_sys->p_input )
192     {
193         vlc_object_release( p_intf->p_sys->p_input );
194     }
195
196     if( p_intf->p_sys->p_playlist )
197     {
198         vlc_object_release( p_intf->p_sys->p_playlist );
199     }
200
201     // Delete theme, it's important to do it correctly
202     delete (OSTheme *)p_intf->p_sys->p_theme;
203
204 #if defined X11_SKINS
205     XDestroyWindow( p_intf->p_sys->display, p_intf->p_sys->mainWin );
206
207     // There is a bug in imlib2 which prevents us from closing the display
208     // (__imlib_RenderImage() can free old GC with already closed display)
209     //XCloseDisplay( p_intf->p_sys->display );
210 #endif
211
212     // Unsuscribe to messages bank
213     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
214
215 #ifdef WIN32
216     // Unload msimg32.dll and user32.dll
217     if( p_intf->p_sys->h_msimg32_dll )
218         FreeLibrary( p_intf->p_sys->h_msimg32_dll );
219     if( p_intf->p_sys->h_user32_dll )
220         FreeLibrary( p_intf->p_sys->h_user32_dll );
221 #elif defined X11_SKINS
222     vlc_mutex_destroy( &p_intf->p_sys->xlock );
223 #endif
224
225     // Destroy structure
226     free( p_intf->p_sys );
227 }
228
229
230 //---------------------------------------------------------------------------
231 // Run: main loop
232 //---------------------------------------------------------------------------
233 static void Run( intf_thread_t *p_intf )
234 {
235
236     int a = OSAPI_GetTime();
237
238     // Initialize the dialog boxes
239     p_intf->p_sys->p_dialogs = new Dialogs( p_intf );
240     if( !p_intf->p_sys->p_dialogs ) return;
241
242     // Load a theme
243     char *skin_last = config_GetPsz( p_intf, "skin_last" );
244     ThemeLoader *Loader = new ThemeLoader( p_intf );
245
246     if( skin_last == NULL || ! Loader->Load( skin_last ) )
247     {
248         // Too bad, it failed. Let's try with the default theme
249 //        if( ! Loader->Load( DEFAULT_SKIN_FILE ) )
250 #ifdef WIN32
251         string default_dir = (string)p_intf->p_libvlc->psz_vlcpath +
252                              DIRECTORY_SEPARATOR + "skins" +
253                              DIRECTORY_SEPARATOR + "default" +
254                              DIRECTORY_SEPARATOR + "theme.xml";
255         if( ! Loader->Load( default_dir ) )
256         {
257             // Last chance: the user can  select a new theme file
258 #else
259         string user_skin = (string)p_intf->p_vlc->psz_homedir +
260                               DIRECTORY_SEPARATOR + CONFIG_DIR +
261                               DIRECTORY_SEPARATOR + "skins" +
262                               DIRECTORY_SEPARATOR + "default" +
263                               DIRECTORY_SEPARATOR + "theme.xml";
264
265         string default_skin = (string)DATA_PATH +
266                               DIRECTORY_SEPARATOR + "skins" +
267                               DIRECTORY_SEPARATOR + "default" +
268                               DIRECTORY_SEPARATOR + "theme.xml";
269         if( !Loader->Load( user_skin ) && !Loader->Load( default_skin ) )
270         {
271 #endif
272             p_intf->p_sys->p_dialogs->ShowOpenSkin( 1 /* block */ );
273
274             // try to load selected file
275             if( !p_intf->p_sys->p_new_theme_file ||
276                 !Loader->Load( (string)p_intf->p_sys->p_new_theme_file ) )
277             {
278                 // He, he, what the hell is he doing ?
279                 delete Loader;
280                 delete p_intf->p_sys->p_dialogs;
281                 if( skin_last ) free( skin_last );
282                 return;
283             }
284         }
285     }
286
287     // Show the theme
288     p_intf->p_sys->p_theme->InitTheme();
289     p_intf->p_sys->p_theme->ShowTheme();
290
291     if( skin_last ) free( skin_last );
292     delete Loader;
293
294     msg_Dbg( p_intf, "Load theme time : %i ms", OSAPI_GetTime() - a );
295
296     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (int)true );
297
298     OSRun( p_intf );
299
300     // clean up the dialog boxes
301     delete p_intf->p_sys->p_dialogs;
302 }
303
304 //---------------------------------------------------------------------------
305 // Module descriptor
306 //---------------------------------------------------------------------------
307 #define DEFAULT_SKIN        N_("Last skin actually used")
308 #define DEFAULT_SKIN_LONG   N_("Last skin actually used")
309 #define SKIN_CONFIG         N_("Config of last used skin")
310 #define SKIN_CONFIG_LONG    N_("Config of last used skin")
311 #define SKIN_TRAY           N_("Show application in system tray")
312 #define SKIN_TRAY_LONG      N_("Show application in system tray")
313 #define SKIN_TASKBAR        N_("Show application in taskbar")
314 #define SKIN_TASKBAR_LONG   N_("Show application in taskbar")
315
316 vlc_module_begin();
317     add_string( "skin_last", "", NULL, DEFAULT_SKIN, DEFAULT_SKIN_LONG,
318                 VLC_TRUE );
319     add_string( "skin_config", "", NULL, SKIN_CONFIG, SKIN_CONFIG_LONG,
320                 VLC_TRUE );
321     add_bool( "show_in_tray", VLC_FALSE, NULL, SKIN_TRAY, SKIN_TRAY_LONG,
322               VLC_FALSE );
323     add_bool( "show_in_taskbar", VLC_TRUE, NULL, SKIN_TASKBAR,
324               SKIN_TASKBAR_LONG, VLC_FALSE );
325     set_description( _("Skinnable interface") );
326     set_capability( "interface", 30 );
327     set_callbacks( Open, Close );
328     set_program( "svlc" );
329 vlc_module_end();
330
331
332 //---------------------------------------------------------------------------
333 // Refresh procedure
334 //---------------------------------------------------------------------------
335 int SkinManage( intf_thread_t *p_intf )
336 {
337     vlc_mutex_lock( &p_intf->change_lock );
338
339     // Update the input
340     if( p_intf->p_sys->p_input == NULL )
341     {
342         p_intf->p_sys->p_input = (input_thread_t *)
343                     vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
344     }
345     else if( p_intf->p_sys->p_input->b_dead )
346     {
347         vlc_object_release( p_intf->p_sys->p_input );
348         p_intf->p_sys->p_input = NULL;
349     }
350
351     //-------------------------------------------------------------------------
352     if( p_intf->p_sys->p_input != NULL && !p_intf->p_sys->p_input->b_die )
353     {
354         input_thread_t  * p_input = p_intf->p_sys->p_input;
355
356         vlc_mutex_lock( &p_input->stream.stream_lock );
357
358         // Refresh sound volume
359         audio_volume_t volume;
360
361         // Get sound volume from VLC
362         aout_VolumeGet( p_intf, &volume);
363
364         // Update sliders
365         OSAPI_PostMessage( NULL, CTRL_SET_SLIDER,
366             (unsigned int)
367             p_intf->p_sys->p_theme->EvtBank->Get( "volume_refresh" ),
368             (long)( volume * SLIDER_RANGE / (AOUT_VOLUME_DEFAULT * 2) ) );
369
370         // Refresh slider
371         // if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
372 #define p_area p_input->stream.p_selected_area
373         if( p_input->stream.b_seekable && p_area->i_size )
374         {
375
376             // Set value of sliders
377             long Value = SLIDER_RANGE * p_area->i_tell / p_area->i_size;
378
379             // Update sliders
380             OSAPI_PostMessage( NULL, CTRL_SET_SLIDER, (unsigned int)
381                 p_intf->p_sys->p_theme->EvtBank->Get( "time" ), (long)Value );
382
383             // Text char * for updating text controls
384             char *text = new char[MSTRTIME_MAX_SIZE];
385
386             // Create end time text
387             input_OffsetToTime( p_intf->p_sys->p_input, &text[1],
388                                 p_area->i_size - p_area->i_tell );
389             text[0] = '-';
390             p_intf->p_sys->p_theme->EvtBank->Get( "left_time" )
391                 ->PostTextMessage( text );
392
393             // Create time text and update
394             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_tell );
395             p_intf->p_sys->p_theme->EvtBank->Get( "time" )
396                 ->PostTextMessage( text );
397
398             // Create total time text
399             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_size );
400             p_intf->p_sys->p_theme->EvtBank->Get( "total_time" )
401                 ->PostTextMessage( text );
402
403             // Free memory
404             delete[] text;
405
406 #undef p_area
407         }
408         vlc_mutex_unlock( &p_input->stream.stream_lock );
409     }
410     //-------------------------------------------------------------------------
411     vlc_mutex_unlock( &p_intf->change_lock );
412
413     return( VLC_TRUE );
414 }