Panda3D
vrpnButton.cxx
1 // Filename: vrpnButton.cxx
2 // Created by: drose (26Jan01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "vrpnButton.h"
16 #include "vrpnButtonDevice.h"
17 #include "vrpnClient.h"
18 #include "config_vrpn.h"
19 
20 #include "indent.h"
21 
22 #include <algorithm>
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: VrpnButton::Constructor
26 // Access: Public
27 // Description:
28 ////////////////////////////////////////////////////////////////////
29 VrpnButton::
30 VrpnButton(const string &button_name, vrpn_Connection *connection) :
31  _button_name(button_name)
32 {
33  _button = new vrpn_Button_Remote(_button_name.c_str(), connection);
34 
35  _button->register_change_handler((void*)this, &vrpn_button_callback);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: VrpnButton::Destructor
40 // Access: Public
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 VrpnButton::
44 ~VrpnButton() {
45  delete _button;
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: VrpnButton::mark
50 // Access: Public
51 // Description: Adds the indicated VrpnButtonDevice to the list of
52 // devices that are sharing this VrpnButton.
53 ////////////////////////////////////////////////////////////////////
54 void VrpnButton::
56  if (vrpn_cat.is_debug()) {
57  vrpn_cat.debug() << *this << " marking " << *device << "\n";
58  }
59  _devices.push_back(device);
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: VrpnButton::unmark
64 // Access: Public
65 // Description: Removes the indicated VrpnButtonDevice from the list
66 // of devices that are sharing this VrpnButton.
67 ////////////////////////////////////////////////////////////////////
68 void VrpnButton::
70  if (vrpn_cat.is_debug()) {
71  vrpn_cat.debug() << *this << " unmarking " << *device << "\n";
72  }
73 
74  Devices::iterator di =
75  find(_devices.begin(), _devices.end(), device);
76 
77  if (di != _devices.end()) {
78  _devices.erase(di);
79  }
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: VrpnButton::output
84 // Access: Public
85 // Description:
86 ////////////////////////////////////////////////////////////////////
87 void VrpnButton::
88 output(ostream &out) const {
89  out << _button_name;
90 }
91 
92 ////////////////////////////////////////////////////////////////////
93 // Function: VrpnButton::write
94 // Access: Public
95 // Description:
96 ////////////////////////////////////////////////////////////////////
97 void VrpnButton::
98 write(ostream &out, int indent_level) const {
99  indent(out, indent_level)
100  << get_button_name() << " ("
101  << _devices.size() << " devices)\n";
102 }
103 
104 ////////////////////////////////////////////////////////////////////
105 // Function: VrpnButton::vrpn_button_callback
106 // Access: Private, Static
107 // Description: Receives the button event data from the VRPN
108 // code and sends it to any interested
109 // VrpnButtonDevices.
110 ////////////////////////////////////////////////////////////////////
111 void VRPN_CALLBACK VrpnButton::
112 vrpn_button_callback(void *userdata, const vrpn_BUTTONCB info) {
113  VrpnButton *self = (VrpnButton *)userdata;
114  if (vrpn_cat.is_debug()) {
115  vrpn_cat.debug()
116  << *self << " got button " << info.button << " = " << info.state << "\n";
117  }
118 
119  Devices::iterator di;
120  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
121  VrpnButtonDevice *device = (*di);
122  device->acquire();
123  device->set_button_state(info.button, info.state != 0);
124  device->unlock();
125  }
126 }
void unmark(VrpnButtonDevice *device)
Removes the indicated VrpnButtonDevice from the list of devices that are sharing this VrpnButton...
Definition: vrpnButton.cxx:69
const string & get_button_name() const
Returns the name of the button device that was used to create this VrpnButton.
Definition: vrpnButton.I:22
void mark(VrpnButtonDevice *device)
Adds the indicated VrpnButtonDevice to the list of devices that are sharing this VrpnButton.
Definition: vrpnButton.cxx:55
void unlock()
Releases the mutex associated with this particular device.
Definition: clientDevice.I:90
The Panda interface to a VRPN button.
This is the actual interface to a particular VRPN button device, and all of its numbered buttons...
Definition: vrpnButton.h:42
void acquire()
Grabs the mutex associated with this particular device.
Definition: clientDevice.I:76
void set_button_state(int index, bool down)
Sets the state of the indicated button index, where true indicates down, and false indicates up...