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