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