]> git.sesse.net Git - vlc/blob - modules/gui/skins/src/event.cpp
* modules/gui/skins/*:
[vlc] / modules / gui / skins / src / event.cpp
1 /*****************************************************************************
2  * event.cpp: Event class
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: event.cpp,v 1.24 2004/02/15 18:58:38 ipkiss 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
27 //--- VLC -------------------------------------------------------------------
28 #include <vlc/intf.h>
29
30 //--- SKIN ------------------------------------------------------------------
31 #include "../os_api.h"
32 #include "skin_common.h"
33 #include "banks.h"
34 #include "../controls/generic.h"
35 #include "window.h"
36 #include "theme.h"
37 #include "event.h"
38 #include "../os_event.h"
39
40
41
42 //---------------------------------------------------------------------------
43 //   VLC Event
44 //---------------------------------------------------------------------------
45 Event::Event( intf_thread_t *_p_intf, string Desc, string shortcut )
46 {
47     p_intf = _p_intf;
48     EventDesc = Desc;
49     Message   = VLC_NOTHING;
50     Param1    = 0;
51     Param2    = 0;
52     Shortcut  = shortcut;
53 }
54 //---------------------------------------------------------------------------
55 Event::Event( intf_thread_t *_p_intf, unsigned int msg, unsigned int par1,
56               long par2 )
57 {
58     p_intf = _p_intf;
59     Message  = msg;
60     Param1   = par1;
61     Param2   = par2;
62     Shortcut = "none";
63 }
64 //---------------------------------------------------------------------------
65 Event::~Event()
66 {
67 }
68 //---------------------------------------------------------------------------
69 void Event::DestructParameters( bool force )
70 {
71     switch( Message )
72     {
73         case CTRL_SYNCHRO:
74             if( Param2 == (int)true || force )
75                 delete (Event *)Param1;
76             break;
77
78         case CTRL_SET_TEXT:
79             delete[] (char *)Param2;
80             break;
81     }
82 }
83 //---------------------------------------------------------------------------
84 bool Event::IsEqual( Event *evt )
85 {
86     return( evt->GetMessage() == Message && evt->GetParam1() == Param1 &&
87             evt->GetParam2()  == Param2 );
88 }
89 //---------------------------------------------------------------------------
90 void Event::PostSynchroMessage( bool autodelete )
91 {
92     OSAPI_PostMessage( NULL, CTRL_SYNCHRO, (unsigned int)this,
93                       (long)autodelete );
94 }
95 //---------------------------------------------------------------------------
96 void Event::PostTextMessage( string text )
97 {
98     char *txt = new char[text.size()+1];
99     strcpy( txt, text.c_str() );
100     OSAPI_PostMessage( NULL, CTRL_SET_TEXT, (unsigned int)this, (long)txt );
101 }
102 //---------------------------------------------------------------------------
103 unsigned int Event::GetMessageType( string Desc )
104 {
105     if( Desc == "VLC_NOTHING" )
106         return VLC_NOTHING;
107
108     // VLC messages
109     else if( Desc == "VLC_QUIT" )
110         return VLC_QUIT;
111     else if( Desc == "VLC_HIDE" )
112         return VLC_HIDE;
113     else if( Desc == "VLC_OPEN" )
114         return VLC_OPEN;
115     else if( Desc == "VLC_NET" )
116         return VLC_NET;
117     else if( Desc == "VLC_LOAD_SKIN" )
118         return VLC_LOAD_SKIN;
119     else if( Desc == "VLC_ON_TOP" )
120         return VLC_ON_TOP;
121     else if( Desc == "VLC_CHANGE_TRAY" )
122         return VLC_CHANGE_TRAY;
123     else if( Desc == "VLC_CHANGE_TASKBAR" )
124         return VLC_CHANGE_TASKBAR;
125
126     // Stream control
127     else if( Desc == "VLC_PLAY" )
128         return VLC_PLAY;
129     else if( Desc == "VLC_STOP" )
130         return VLC_STOP;
131     else if( Desc == "VLC_PAUSE" )
132         return VLC_PAUSE;
133     else if( Desc == "VLC_NEXT" )
134         return VLC_NEXT;
135     else if( Desc == "VLC_PREV" )
136         return VLC_PREV;
137     else if( Desc == "VLC_STREAMPOS" )
138         return VLC_STREAMPOS;
139     else if( Desc == "VLC_ENDSTREAMPOS" )
140         return VLC_ENDSTREAMPOS;
141     else if( Desc == "VLC_TOTALSTREAMPOS" )
142         return VLC_TOTALSTREAMPOS;
143     else if( Desc == "VLC_STREAM_NAME" )
144         return VLC_STREAM_NAME;
145     else if( Desc == "VLC_STREAM_TITLE" )
146         return VLC_STREAM_TITLE;
147     else if( Desc == "VLC_HELP_TEXT" )
148         return VLC_HELP_TEXT;
149     else if( Desc == "VLC_SLOWER" )
150         return VLC_SLOWER;
151     else if( Desc == "VLC_FASTER" )
152         return VLC_FASTER;
153
154     // Volume control
155     else if( Desc == "VLC_VOLUME_CHANGE" )
156         return VLC_VOLUME_CHANGE;
157     else if( Desc == "VLC_VOLUME_MUTE" )
158         return VLC_VOLUME_MUTE;
159     else if( Desc == "VLC_VOLUME_UP" )
160         return VLC_VOLUME_UP;
161     else if( Desc == "VLC_VOLUME_DOWN" )
162         return VLC_VOLUME_DOWN;
163     else if( Desc == "VLC_VOLUME_SET" )
164         return VLC_VOLUME_SET;
165
166     // Dialogs
167     else if( Desc == "VLC_LOG_SHOW" )
168         return VLC_LOG_SHOW;
169     else if( Desc == "VLC_PREFS_SHOW" )
170         return VLC_PREFS_SHOW;
171     else if( Desc == "VLC_INFO_SHOW" )
172         return VLC_INFO_SHOW;
173
174     // Playlist events
175     else if( Desc == "VLC_PLAYLIST_ADD_FILE" )
176         return VLC_PLAYLIST_ADD_FILE;
177
178     // Video output events
179     else if( Desc == "VLC_FULLSCREEN" )
180         return VLC_FULLSCREEN;
181
182     // Window events
183     else if( Desc == "WINDOW_MOVE" )
184         return WINDOW_MOVE;
185     else if( Desc == "WINDOW_OPEN" )
186         return WINDOW_OPEN;
187     else if( Desc == "WINDOW_CLOSE" )
188         return WINDOW_CLOSE;
189     else if( Desc == "WINDOW_SHOW" )
190         return WINDOW_SHOW;
191     else if( Desc == "WINDOW_HIDE" )
192         return WINDOW_HIDE;
193     else if( Desc == "WINDOW_FADE" )
194         return WINDOW_FADE;
195
196     // Control events
197     else if( Desc == "CTRL_ENABLED" )
198         return CTRL_ENABLED;
199     else if( Desc == "CTRL_VISIBLE" )
200         return CTRL_VISIBLE;
201     else if( Desc == "CTRL_SYNCHRO" )
202         return CTRL_SYNCHRO;
203     else if( Desc == "CTRL_SET_TEXT" )
204         return CTRL_SET_TEXT;
205     else if( Desc == "CTRL_SET_SLIDER" )
206         return CTRL_SET_SLIDER;
207
208
209     // Control events by ID
210     else if( Desc == "CTRL_ID_VISIBLE" )
211         return CTRL_ID_VISIBLE;
212     else if( Desc == "CTRL_ID_ENABLED" )
213         return CTRL_ID_ENABLED;
214     else if( Desc == "CTRL_ID_MOVE" )
215         return CTRL_ID_MOVE;
216
217     // Control definition
218     else if( Desc == "CTRL_SLIDER" )
219         return CTRL_SLIDER;
220     else if( Desc == "CTRL_TIME" )
221         return CTRL_TIME;
222     else if( Desc == "CTRL_PLAYLIST" )
223         return CTRL_PLAYLIST;
224
225     // Playlist
226     else if( Desc == "PLAYLIST_ID_DEL" )
227         return PLAYLIST_ID_DEL;
228
229     // Not found
230     else
231     {
232         msg_Warn( p_intf, "Theme: Unknown event (%s)", EventDesc.c_str() );
233         return VLC_NOTHING;
234     }
235 }
236 //---------------------------------------------------------------------------
237 void Event::CreateEvent()
238 {
239     // Initialization
240     int x, y;
241     char *msg   = new char[MAX_EVENT_SIZE];
242     char *para1 = new char[MAX_PARAM_SIZE];
243     char *para2 = new char[MAX_PARAM_SIZE];
244     char *para3 = new char[MAX_PARAM_SIZE];
245
246     // Scan the event
247     int scan = sscanf( EventDesc.c_str(),
248         "%[^(](%[^,)],%[^,)],%[^,)])", msg, para1, para2, para3 );
249
250     // Check parameters
251     if( scan < 1 )
252         strcpy( msg, "VLC_NOTHING" );
253     if( scan < 2 )
254         strcpy( para1, "" );
255     if( scan < 3 )
256         strcpy( para2, "" );
257     if( scan < 4 )
258         strcpy( para3, "" );
259
260     // Find Message type
261     Message = GetMessageType( msg );
262
263     // Find Parameters
264     switch( Message )
265     {
266         case VLC_HIDE:
267             Param1 = GetMessageType( para1 );
268             break;
269
270         case VLC_VOLUME_CHANGE:
271             if( strcmp( para1, "MUTE" ) == 0 )
272                 Param1 = VLC_VOLUME_MUTE;
273             else if( strcmp( para1, "UP" ) == 0 )
274                 Param1 = VLC_VOLUME_UP;
275             else if( strcmp( para1, "DOWN" ) == 0 )
276                 Param1 = VLC_VOLUME_DOWN;
277             else if( strcmp( para1, "SET" ) == 0 )
278             {
279                 Param1 = VLC_VOLUME_SET;
280                 Param2 = atoi( para2 ) * (AOUT_VOLUME_MAX * 2) / 100;
281             }
282             break;
283
284         case VLC_LOG_SHOW:
285             Param2 = GetBool( para1 );
286             break;
287
288         case CTRL_ID_VISIBLE:
289             Param1 = (unsigned int)FindControl( para1 );
290             Param2 = GetBool( para2 );
291             break;
292
293         case CTRL_ID_ENABLED:
294             Param1 = (unsigned int)FindControl( para1 );
295             Param2 = GetBool( para2 );
296             break;
297
298         case CTRL_ID_MOVE:
299             Param1 = (unsigned int)FindControl( para1 );
300             x = atoi( para2 );
301             y = atoi( para3 );
302             if( x < 0 )
303                 x = -x + 0x8000;
304             if( y < 0 )
305                 y = -y + 0x8000;
306             Param2 = ( y << 16 ) | x;
307             break;
308
309         case WINDOW_OPEN:
310             Param1 = GetBool( para2 );
311             break;
312
313         case WINDOW_CLOSE:
314             Param1 = GetBool( para2 );
315             break;
316
317         case PLAYLIST_ID_DEL:
318             Param1 = (unsigned int)FindControl( para1 );
319             break;
320
321         default:
322             break;
323     }
324
325     // Get OS specific parameters
326     CreateOSEvent( para1, para2, para3 );
327
328     // Free memory
329     delete[] msg;
330     delete[] para1;
331     delete[] para2;
332     delete[] para3;
333
334     // Create shortcut
335     CreateShortcut();
336 }
337 //---------------------------------------------------------------------------
338 GenericControl * Event::FindControl( string id )
339 {
340     list<SkinWindow *>::const_iterator win;
341     unsigned int i;
342
343     for( win = p_intf->p_sys->p_theme->WindowList.begin();
344          win != p_intf->p_sys->p_theme->WindowList.end(); win++ )
345     {
346         for( i = 0; i < (*win)->ControlList.size(); i++ )
347         {
348             if( (*win)->ControlList[i]->GetId() == id )
349                 return (*win)->ControlList[i];
350         }
351     }
352     return NULL;
353 }
354 //---------------------------------------------------------------------------
355 int Event::GetBool( string expr )
356 {
357     if( expr == "FALSE" )
358     {
359         return 0;
360     }
361     else if( expr == "TRUE" )
362     {
363         return 1;
364     }
365     else if( expr == "CHANGE" )
366     {
367         return 2;
368     }
369     return 1;
370 }
371 //---------------------------------------------------------------------------
372 void Event::CreateShortcut()
373 {
374     if( Shortcut == "none" )
375         return;
376
377     // Initiatization
378     char *mod = new char[5];
379     char *key = new char[4];
380
381     // Scan the event
382     int scan = sscanf( Shortcut.c_str(), "%[^+]+%s", mod, key );
383
384     // Check parameters
385     if( scan == 2 )
386     {
387         Key = (int)key[0];
388         if( (string)mod == "ALT" )
389             KeyModifier = 1;
390         else if( (string)mod == "CTRL" )
391             KeyModifier = 2;
392         else
393             KeyModifier = 0;
394     }
395     else if( scan == 1 )
396     {
397         Key = (int)mod[0];
398         KeyModifier = 0;
399     }
400
401     delete[] mod;
402     delete[] key;
403 }
404 //---------------------------------------------------------------------------
405 bool Event::MatchShortcut( int key, int mod )
406 {
407     // Modifier
408     // None    = 0
409     // ALT     = 1
410     // CONTROL = 2
411     if( Shortcut != "none" && key == Key && mod == KeyModifier )
412         return true;
413     else
414         return false;
415 }
416 //---------------------------------------------------------------------------
417
418
419
420
421 //---------------------------------------------------------------------------
422 // Action
423 //---------------------------------------------------------------------------
424 Action::Action( intf_thread_t *_p_intf, string code )
425 {
426     p_intf = _p_intf;
427
428     // Initiatization
429     int scan;
430     char *evt  = new char[MAX_EVENT_SIZE];
431     char *next = new char[MAX_PARAM_SIZE];
432
433     // Create events separated with a semicolon
434     while( code != "none" )
435     {
436         scan  = sscanf( code.c_str(), "%[^;];%s", evt, next );
437         EventList.push_back( p_intf->p_sys->p_theme->EvtBank->Get( evt ) );
438
439         // Check if script is finished
440         if( scan < 2 )
441             code = "none";
442         else
443             code = next;
444     }
445
446     // Free memory
447     delete[] evt;
448     delete[] next;
449 }
450 //---------------------------------------------------------------------------
451 Action::~Action()
452 {
453 }
454 //---------------------------------------------------------------------------
455 bool Action::SendEvent()
456 {
457     bool res = false;
458     for( list<Event *>::const_iterator evt = EventList.begin();
459          evt != EventList.end(); evt++ )
460     {
461         res |= (*evt)->SendEvent();
462     }
463     return res;
464 }
465 //---------------------------------------------------------------------------
466 bool Action::MatchEvent( Event *evt, int flag )
467 {
468     list<Event *>::const_iterator event;
469
470     switch( flag )
471     {
472         case ACTION_MATCH_ALL:
473             for( event = EventList.begin(); event != EventList.end(); event++ )
474                 if( !(*event)->IsEqual( evt ) )
475                     return false;
476             break;
477
478         case ACTION_MATCH_ONE:
479             for( event = EventList.begin(); event != EventList.end(); event++ )
480                 if( (*event)->IsEqual( evt ) )
481                     return true;
482             return false;
483             break;
484
485         case ACTION_MATCH_FIRST:
486             if( !(*EventList.begin())->IsEqual( evt ) )
487                 return false;
488             break;
489     }
490     return true;
491 }
492 //---------------------------------------------------------------------------
493