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