1 /***************************************************************************** 2 * cmd_callbacks.hpp 3 ***************************************************************************** 4 * Copyright (C) 2009 the VideoLAN team 5 * $Id: 945a4e777c3ed5002260e1b3897a87056de783f9 $ 6 * 7 * Author: Erwan Tulou <erwan10 aT videolan doT org > 8 * JP Dinger <jpd (at) videolan (dot) org> 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 along 21 * with this program; if not, write to the Free Software Foundation, Inc., 22 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. 23 *****************************************************************************/ 24 25 #ifndef CMD_CALLBACKS_HPP 26 #define CMD_CALLBACKS_HPP 27 28 #include "cmd_generic.hpp" 29 #include "../src/vlcproc.hpp" 30 31 32 class CmdCallback : public CmdGeneric 33 { 34 public: CmdCallback(intf_thread_t * pIntf,vlc_object_t * pObj,vlc_value_t newVal,void (VlcProc::* func)(vlc_object_t *,vlc_value_t),std::string label)35 CmdCallback( intf_thread_t *pIntf, vlc_object_t *pObj, vlc_value_t newVal, 36 void (VlcProc::*func)(vlc_object_t *,vlc_value_t), 37 std::string label ) 38 : CmdGeneric( pIntf ), m_pObj( pObj ), m_newVal( newVal ), 39 m_label( label ), m_pfExecute( func ) 40 { 41 if( m_pObj ) 42 vlc_object_hold( m_pObj ); 43 } ~CmdCallback()44 virtual ~CmdCallback() 45 { 46 if( m_pObj ) 47 vlc_object_release( m_pObj ); 48 } execute()49 virtual void execute() 50 { 51 if( !m_pObj || !m_pfExecute ) 52 return; 53 54 (VlcProc::instance( getIntf() )->*m_pfExecute)( m_pObj, m_newVal ); 55 56 vlc_object_release( m_pObj ); 57 m_pObj = NULL; 58 } getType() const59 virtual std::string getType() const { return m_label; } 60 61 private: 62 vlc_object_t* m_pObj; 63 vlc_value_t m_newVal; 64 std::string m_label; 65 void (VlcProc::*m_pfExecute)(vlc_object_t *,vlc_value_t); 66 }; 67 68 69 class CmdExecuteBlock : public CmdGeneric 70 { 71 public: CmdExecuteBlock(intf_thread_t * pIntf,vlc_object_t * obj,void (* func)(intf_thread_t *,vlc_object_t *))72 CmdExecuteBlock( intf_thread_t* pIntf, vlc_object_t* obj, 73 void (*func) (intf_thread_t*, vlc_object_t* ) ) 74 : CmdGeneric( pIntf), m_pObj( obj ), m_pfFunc( func ), 75 m_executing( false ) 76 { 77 vlc_mutex_init( &m_lock ); 78 vlc_cond_init( &m_wait ); 79 if( m_pObj ) 80 vlc_object_hold( m_pObj ); 81 } 82 ~CmdExecuteBlock()83 virtual ~CmdExecuteBlock() 84 { 85 if( m_pObj ) 86 vlc_object_release( m_pObj ); 87 vlc_cond_destroy( &m_wait ); 88 vlc_mutex_destroy( &m_lock ); 89 } 90 executeWait(const CmdGenericPtr & rcCommand)91 static void executeWait( const CmdGenericPtr& rcCommand ) 92 { 93 CmdExecuteBlock& rCmd = (CmdExecuteBlock&)*rcCommand.get(); 94 vlc_mutex_locker locker( &rCmd.m_lock ); 95 96 if( !rCmd.m_pObj || !rCmd.m_pfFunc || rCmd.m_executing ) 97 { 98 msg_Err( rCmd.getIntf(), "unexpected command call" ); 99 return; 100 } 101 102 AsyncQueue *pQueue = AsyncQueue::instance( rCmd.getIntf() ); 103 pQueue->push( rcCommand, false ); 104 105 rCmd.m_executing = true; 106 while( rCmd.m_executing ) 107 vlc_cond_wait( &rCmd.m_wait, &rCmd.m_lock ); 108 } 109 execute()110 virtual void execute() 111 { 112 vlc_mutex_locker locker( &m_lock ); 113 114 if( !m_pObj || !m_pfFunc || !m_executing ) 115 { 116 msg_Err( getIntf(), "unexpected command call" ); 117 return; 118 } 119 120 (*m_pfFunc)( getIntf(), m_pObj ); 121 m_executing = false; 122 vlc_cond_signal( &m_wait ); 123 } 124 getType() const125 virtual std::string getType() const { return "CmdExecuteBlock"; } 126 127 private: 128 vlc_object_t* m_pObj; 129 void (*m_pfFunc)(intf_thread_t*, vlc_object_t*); 130 bool m_executing; 131 132 vlc_mutex_t m_lock; 133 vlc_cond_t m_wait; 134 }; 135 136 137 #endif 138