x11 factory: use vlc_readdir
[vlc/solaris.git] / modules / gui / skins2 / utils / fsm.cpp
bloba4db29d0df8e820341bf3ff19a6c0a4d39c04aca
1 /*****************************************************************************
2 * fsm.cpp
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
5 * $Id$
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "fsm.hpp"
26 #include "../commands/cmd_generic.hpp"
29 void FSM::addState( const string &state )
31 m_states.insert( state );
35 void FSM::addTransition( const string &state1, const string &event,
36 const string &state2, CmdGeneric *pCmd )
38 // Check that we already know the states
39 if( m_states.find( state1 ) == m_states.end() ||
40 m_states.find( state2 ) == m_states.end() )
42 msg_Warn( getIntf(),
43 "FSM: ignoring transition between invalid states" );
44 return;
47 Key_t key( state1, event );
48 Data_t data( state2, pCmd );
50 // Check that the transition doesn't already exist
51 if( m_transitions.find( key ) != m_transitions.end() )
53 msg_Warn( getIntf(), "FSM: transition already exists" );
54 return;
57 m_transitions[key] = data;
61 void FSM::setState( const string &state )
63 if( m_states.find( state ) == m_states.end() )
65 msg_Warn( getIntf(), "FSM: trying to set an invalid state" );
66 return;
68 m_currentState = state;
72 void FSM::handleTransition( const string &event )
74 string tmpEvent = event;
75 Key_t key( m_currentState, event );
76 map<Key_t, Data_t>::const_iterator it;
78 // Find a transition
79 it = m_transitions.find( key );
81 // While the matching fails, try to match a more generic transition
82 // For example, if "key:up:F" isn't a transition, "key:up" or "key" may be
83 while( it == m_transitions.end() &&
84 tmpEvent.rfind( ":", tmpEvent.size() ) != string::npos )
86 // Cut the last part
87 tmpEvent = tmpEvent.substr( 0, tmpEvent.rfind( ":", tmpEvent.size() ) );
89 key.second = tmpEvent;
90 it = m_transitions.find( key );
93 // No transition was found
94 if( it == m_transitions.end() )
96 return;
99 // Change state
100 m_currentState = (*it).second.first;
102 // Call the callback, if any
103 CmdGeneric *pCmd = (*it).second.second;
104 if( pCmd != NULL )
106 pCmd->execute();