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