Panda3D

vrpnButton.cxx

00001 // Filename: vrpnButton.cxx
00002 // Created by:  drose (26Jan01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "vrpnButton.h"
00016 #include "vrpnButtonDevice.h"
00017 #include "vrpnClient.h"
00018 #include "config_vrpn.h"
00019 
00020 #include "indent.h"
00021 
00022 #include <algorithm>
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: VrpnButton::Constructor
00026 //       Access: Public
00027 //  Description:
00028 ////////////////////////////////////////////////////////////////////
00029 VrpnButton::
00030 VrpnButton(const string &button_name, vrpn_Connection *connection) :
00031   _button_name(button_name)
00032 {
00033   _button = new vrpn_Button_Remote(_button_name.c_str(), connection);
00034 
00035   _button->register_change_handler((void*)this, &vrpn_button_callback);
00036 }
00037 
00038 ////////////////////////////////////////////////////////////////////
00039 //     Function: VrpnButton::Destructor
00040 //       Access: Public
00041 //  Description:
00042 ////////////////////////////////////////////////////////////////////
00043 VrpnButton::
00044 ~VrpnButton() {
00045   delete _button;
00046 }
00047 
00048 ////////////////////////////////////////////////////////////////////
00049 //     Function: VrpnButton::mark
00050 //       Access: Public
00051 //  Description: Adds the indicated VrpnButtonDevice to the list of
00052 //               devices that are sharing this VrpnButton.
00053 ////////////////////////////////////////////////////////////////////
00054 void VrpnButton::
00055 mark(VrpnButtonDevice *device) {
00056   if (vrpn_cat.is_debug()) {
00057     vrpn_cat.debug() << *this << " marking " << *device << "\n";
00058   }
00059   _devices.push_back(device);
00060 }
00061 
00062 ////////////////////////////////////////////////////////////////////
00063 //     Function: VrpnButton::unmark
00064 //       Access: Public
00065 //  Description: Removes the indicated VrpnButtonDevice from the list
00066 //               of devices that are sharing this VrpnButton.
00067 ////////////////////////////////////////////////////////////////////
00068 void VrpnButton::
00069 unmark(VrpnButtonDevice *device) {
00070   if (vrpn_cat.is_debug()) {
00071     vrpn_cat.debug() << *this << " unmarking " << *device << "\n";
00072   }
00073 
00074   Devices::iterator di =
00075     find(_devices.begin(), _devices.end(), device);
00076 
00077   if (di != _devices.end()) {
00078     _devices.erase(di);
00079   }
00080 }
00081 
00082 ////////////////////////////////////////////////////////////////////
00083 //     Function: VrpnButton::output
00084 //       Access: Public
00085 //  Description:
00086 ////////////////////////////////////////////////////////////////////
00087 void VrpnButton::
00088 output(ostream &out) const {
00089   out << _button_name;
00090 }
00091 
00092 ////////////////////////////////////////////////////////////////////
00093 //     Function: VrpnButton::write
00094 //       Access: Public
00095 //  Description:
00096 ////////////////////////////////////////////////////////////////////
00097 void VrpnButton::
00098 write(ostream &out, int indent_level) const {
00099   indent(out, indent_level)
00100     << get_button_name() << " ("
00101     << _devices.size() << " devices)\n";
00102 }
00103 
00104 ////////////////////////////////////////////////////////////////////
00105 //     Function: VrpnButton::vrpn_button_callback
00106 //       Access: Private, Static
00107 //  Description: Receives the button event data from the VRPN
00108 //               code and sends it to any interested
00109 //               VrpnButtonDevices.
00110 ////////////////////////////////////////////////////////////////////
00111 void VRPN_CALLBACK VrpnButton::
00112 vrpn_button_callback(void *userdata, const vrpn_BUTTONCB info) {
00113   VrpnButton *self = (VrpnButton *)userdata;
00114   if (vrpn_cat.is_debug()) {
00115     vrpn_cat.debug()
00116       << *self << " got button " << info.button << " = " << info.state << "\n";
00117   }
00118 
00119   Devices::iterator di;
00120   for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
00121     VrpnButtonDevice *device = (*di);
00122     device->acquire();
00123     device->set_button_state(info.button, info.state != 0);
00124     device->unlock();
00125   }
00126 }
 All Classes Functions Variables Enumerations