00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00026
00027
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
00040
00041
00042
00043 VrpnButton::
00044 ~VrpnButton() {
00045 delete _button;
00046 }
00047
00048
00049
00050
00051
00052
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
00064
00065
00066
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
00084
00085
00086
00087 void VrpnButton::
00088 output(ostream &out) const {
00089 out << _button_name;
00090 }
00091
00092
00093
00094
00095
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
00106
00107
00108
00109
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 }