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