]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/skin_main.cpp
142ecea06f9c2af820279358d6f85d7fd5ee85b2
[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.46 2003/07/18 20:06:00 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     p_intf->p_sys->pf_showdialog = Dialogs::ShowDialog;
91
92
93     // Suscribe to messages bank
94     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
95
96     // Set no new theme when opening file
97     p_intf->p_sys->p_new_theme_file = NULL;
98
99     // Initialize info on playlist
100     p_intf->p_sys->i_index        = -1;
101     p_intf->p_sys->i_size         = 0;
102
103     p_intf->p_sys->i_close_status = VLC_NOTHING;
104
105     p_intf->p_sys->p_input = NULL;
106     p_intf->p_sys->p_playlist = (playlist_t *)vlc_object_find( p_intf,
107         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
108
109 #if defined X11_SKINS
110     // Initialize X11
111     Display *display = XOpenDisplay( NULL );
112     p_intf->p_sys->display = display;
113     vlc_mutex_init( p_intf, &p_intf->p_sys->xlock );
114     // Fake window to receive broadcast events
115     Window root = DefaultRootWindow( display );
116     p_intf->p_sys->mainWin = XCreateSimpleWindow( display, root, 0, 0, 
117                                                   1, 1, 0, 0, 0 );
118     XStoreName( display, p_intf->p_sys->mainWin, "VLC Media Player" );
119
120     // Load the vlc icon
121     int screen = DefaultScreen( display );
122     Screen *screenptr = DefaultScreenOfDisplay( display );
123     Visual *visual = DefaultVisualOfScreen( screenptr );
124     imlib_context_set_display( display );
125     imlib_context_set_visual( visual );
126     imlib_context_set_drawable( root );
127     imlib_context_set_colormap( DefaultColormap( display, screen ) );
128     imlib_context_set_dither( 1 );
129     imlib_context_set_blend( 1 );
130     Imlib_Image img = imlib_load_image_immediately( DATA_PATH"/vlc32x32.png" );
131     if( img == NULL )
132     {
133         // for developers ;)
134         img = imlib_load_image_immediately( "./share/vlc32x32.png" );
135     }
136     if( img == NULL )
137     {
138         msg_Err( p_intf, "loading vlc icon failed" );
139         p_intf->p_sys->iconPixmap = None;
140         p_intf->p_sys->iconMask = None;
141     }
142     else
143     {
144         imlib_context_set_image( img );
145         imlib_render_pixmaps_for_whole_image( &p_intf->p_sys->iconPixmap,
146                                               &p_intf->p_sys->iconMask );
147         imlib_free_image();
148     }
149
150
151 #elif defined WIN32
152     // Interface thread id used to post broadcast messages
153     p_intf->p_sys->dwThreadId = GetCurrentThreadId();
154
155     // We dynamically load msimg32.dll to get a pointer to TransparentBlt()
156     p_intf->p_sys->h_msimg32_dll = LoadLibrary("msimg32.dll");
157     if( !p_intf->p_sys->h_msimg32_dll ||
158         !( p_intf->p_sys->TransparentBlt =
159            (BOOL (WINAPI*)(HDC,int,int,int,int,HDC,
160                            int,int,int,int,unsigned int))
161            GetProcAddress( p_intf->p_sys->h_msimg32_dll, "TransparentBlt" ) ) )
162     {
163         p_intf->p_sys->TransparentBlt = NULL;
164         msg_Dbg( p_intf, "Couldn't find TransparentBlt(), "
165                  "falling back to BitBlt()" );
166     }
167
168     // idem for user32.dll and SetLayeredWindowAttributes()
169     p_intf->p_sys->h_user32_dll = LoadLibrary("user32.dll");
170     if( !p_intf->p_sys->h_user32_dll ||
171         !( p_intf->p_sys->SetLayeredWindowAttributes =
172            (BOOL (WINAPI *)(HWND,COLORREF,BYTE,DWORD))
173            GetProcAddress( p_intf->p_sys->h_user32_dll,
174                            "SetLayeredWindowAttributes" ) ) )
175     {
176         p_intf->p_sys->SetLayeredWindowAttributes = NULL;
177         msg_Dbg( p_intf, "Couldn't find SetLayeredWindowAttributes()" );
178     }
179
180 #endif
181
182     p_intf->p_sys->p_theme = (Theme *)new OSTheme( p_intf );
183
184     return( 0 );
185 }
186
187 //---------------------------------------------------------------------------
188 // Close: destroy interface
189 //---------------------------------------------------------------------------
190 static void Close ( vlc_object_t *p_this )
191 {
192     intf_thread_t *p_intf = (intf_thread_t *)p_this;
193
194     if( p_intf->p_sys->p_input )
195     {
196         vlc_object_release( p_intf->p_sys->p_input );
197     }
198
199     if( p_intf->p_sys->p_playlist )
200     {
201         vlc_object_release( p_intf->p_sys->p_playlist );
202     }
203
204     // Delete theme, it's important to do it correctly
205     delete (OSTheme *)p_intf->p_sys->p_theme;
206
207 #if defined X11_SKINS
208     XDestroyWindow( p_intf->p_sys->display, p_intf->p_sys->mainWin );
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 #if 0
273 #if !defined(MODULE_NAME_IS_basic_skins)
274             wxMutexGuiEnter();
275             wxFileDialog dialog( NULL,
276                 wxU(_("Open a skin file")), wxT(""), wxT(""),
277                 wxT("Skin files (*.vlt)|*.vlt|Skin files (*.xml)|*.xml|"
278                     "All files|*.*"), wxOPEN );
279
280             if( dialog.ShowModal() == wxID_OK )
281             {
282                 // try to load selected file
283                 if( ! Loader->Load( (string)dialog.GetPath().mb_str() ) )
284                 {
285                     // He, he, what the hell is he doing ?
286                     delete Loader;
287                     wxMutexGuiLeave();
288                     return;
289                 }
290                 wxMutexGuiLeave();
291             }
292             else
293 #endif
294 #endif
295             {
296                 delete Loader;
297 #if 0
298 #if !defined(MODULE_NAME_IS_basic_skins)
299                 wxMutexGuiLeave();
300 #endif
301 #endif
302                 return;
303             }
304         }
305     }
306
307     // Show the theme
308     p_intf->p_sys->p_theme->InitTheme();
309     p_intf->p_sys->p_theme->ShowTheme();
310
311     delete Loader;
312
313     msg_Dbg( p_intf, "Load theme time : %i ms", OSAPI_GetTime() - a );
314
315     OSAPI_PostMessage( NULL, VLC_INTF_REFRESH, 0, (int)true );
316
317     OSRun( p_intf );
318
319     // clean up the dialog boxes
320     delete p_intf->p_sys->p_dialogs;
321 }
322
323 //---------------------------------------------------------------------------
324 // Module descriptor
325 //---------------------------------------------------------------------------
326 #define DEFAULT_SKIN        N_("Last skin actually used")
327 #define DEFAULT_SKIN_LONG   N_("Last skin actually used")
328 #define SKIN_CONFIG         N_("Config of last used skin")
329 #define SKIN_CONFIG_LONG    N_("Config of last used skin")
330 #define SKIN_TRAY           N_("Show application in system tray")
331 #define SKIN_TRAY_LONG      N_("Show application in system tray")
332 #define SKIN_TASKBAR        N_("Show application in taskbar")
333 #define SKIN_TASKBAR_LONG   N_("Show application in taskbar")
334
335 vlc_module_begin();
336     add_string( "skin_last", "", NULL, DEFAULT_SKIN, DEFAULT_SKIN_LONG,
337                 VLC_TRUE );
338     add_string( "skin_config", "", NULL, SKIN_CONFIG, SKIN_CONFIG_LONG,
339                 VLC_TRUE );
340     add_bool( "show_in_tray", VLC_FALSE, NULL, SKIN_TRAY, SKIN_TRAY_LONG,
341               VLC_FALSE );
342     add_bool( "show_in_taskbar", VLC_TRUE, NULL, SKIN_TASKBAR,
343               SKIN_TASKBAR_LONG, VLC_FALSE );
344     set_description( _("Skinnable Interface") );
345     set_capability( "interface", 30 );
346     set_callbacks( Open, Close );
347     set_program( "svlc" );
348 vlc_module_end();
349
350
351 //---------------------------------------------------------------------------
352 // Refresh procedure
353 //---------------------------------------------------------------------------
354 int SkinManage( intf_thread_t *p_intf )
355 {
356     vlc_mutex_lock( &p_intf->change_lock );
357
358     // Update the input
359     if( p_intf->p_sys->p_input == NULL )
360     {
361         p_intf->p_sys->p_input = (input_thread_t *)
362                     vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
363     }
364     else if( p_intf->p_sys->p_input->b_dead )
365     {
366         vlc_object_release( p_intf->p_sys->p_input );
367         p_intf->p_sys->p_input = NULL;
368     }
369
370     //-------------------------------------------------------------------------
371     if( p_intf->p_sys->p_input != NULL && !p_intf->p_sys->p_input->b_die )
372     {
373         input_thread_t  * p_input = p_intf->p_sys->p_input;
374
375         vlc_mutex_lock( &p_input->stream.stream_lock );
376
377         // Refresh sound volume
378         audio_volume_t volume;
379
380         // Get sound volume from VLC
381         aout_VolumeGet( p_intf, &volume);
382
383         // Update sliders
384         OSAPI_PostMessage( NULL, CTRL_SET_SLIDER,
385             (unsigned int)
386             p_intf->p_sys->p_theme->EvtBank->Get( "volume_refresh" ),
387             (long)( volume * SLIDER_RANGE / AOUT_VOLUME_MAX ) );
388
389         // Refresh slider
390         // if( p_input->stream.b_seekable && p_intf->p_sys->b_playing )
391         if( p_input->stream.b_seekable )
392         {
393 #define p_area p_input->stream.p_selected_area
394
395             // Set value of sliders
396             long Value = SLIDER_RANGE *
397                 p_input->stream.p_selected_area->i_tell /
398                 p_input->stream.p_selected_area->i_size;
399
400             // Update sliders
401             OSAPI_PostMessage( NULL, CTRL_SET_SLIDER, (unsigned int)
402                 p_intf->p_sys->p_theme->EvtBank->Get( "time" ), (long)Value );
403
404             // Text char * for updating text controls
405             char *text = new char[OFFSETTOTIME_MAX_SIZE];
406
407             // Create end time text
408             input_OffsetToTime( p_intf->p_sys->p_input, &text[1],
409                                 p_area->i_size - p_area->i_tell );
410             text[0] = '-';
411             p_intf->p_sys->p_theme->EvtBank->Get( "left_time" )
412                 ->PostTextMessage( text );
413
414             // Create time text and update
415             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_tell );
416             p_intf->p_sys->p_theme->EvtBank->Get( "time" )
417                 ->PostTextMessage( text );
418
419             // Create total time text
420             input_OffsetToTime( p_intf->p_sys->p_input, text, p_area->i_size );
421             p_intf->p_sys->p_theme->EvtBank->Get( "total_time" )
422                 ->PostTextMessage( text );
423
424             // Free memory
425             delete[] text;
426
427 #undef p_area
428         }
429         vlc_mutex_unlock( &p_input->stream.stream_lock );
430     }
431     //-------------------------------------------------------------------------
432     vlc_mutex_unlock( &p_intf->change_lock );
433
434     return( VLC_TRUE );
435 }