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