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  */
48 mark(VrpnButtonDevice *device) {
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  */
60 unmark(VrpnButtonDevice *device) {
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  * Polls the connected device. Normally you should not call this directly;
75  * this will be called by the VrpnClient.
76  */
78 poll() {
79  _button->mainloop();
80 }
81 
82 /**
83  *
84  */
85 void VrpnButton::
86 output(std::ostream &out) const {
87  out << _button_name;
88 }
89 
90 /**
91  *
92  */
93 void VrpnButton::
94 write(std::ostream &out, int indent_level) const {
95  indent(out, indent_level)
96  << get_button_name() << " ("
97  << _devices.size() << " devices)\n";
98 }
99 
100 /**
101  * Receives the button event data from the VRPN code and sends it to any
102  * interested VrpnButtonDevices.
103  */
104 void VRPN_CALLBACK VrpnButton::
105 vrpn_button_callback(void *userdata, const vrpn_BUTTONCB info) {
106  VrpnButton *self = (VrpnButton *)userdata;
107  if (vrpn_cat.is_debug()) {
108  vrpn_cat.debug()
109  << *self << " got button " << info.button << " = " << info.state << "\n";
110  }
111 
112  Devices::iterator di;
113  for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
114  VrpnButtonDevice *device = (*di);
115  device->button_changed(info.button, info.state != 0);
116  }
117 }
indent
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
config_vrpn.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
VrpnButton
This is the actual interface to a particular VRPN button device, and all of its numbered buttons.
Definition: vrpnButton.h:37
vrpnButtonDevice.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
vrpnClient.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
VrpnButton::mark
void mark(VrpnButtonDevice *device)
Adds the indicated VrpnButtonDevice to the list of devices that are sharing this VrpnButton.
Definition: vrpnButton.cxx:48
vrpnButton.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
VrpnButton::poll
void poll()
Polls the connected device.
Definition: vrpnButton.cxx:78
indent.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
VrpnButton::unmark
void unmark(VrpnButtonDevice *device)
Removes the indicated VrpnButtonDevice from the list of devices that are sharing this VrpnButton.
Definition: vrpnButton.cxx:60
VrpnButton::get_button_name
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
VrpnButtonDevice
The Panda interface to a VRPN button.
Definition: vrpnButtonDevice.h:30