7 #if defined(linux) && !defined(VRPN_CLIENT_ONLY) 13 #ifndef VRPN_CLIENT_ONLY 23 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(_WIN32_WCE) 27 #if (defined(sgi) && (!defined(VRPN_CLIENT_ONLY))) 28 #include <gl/device.h> 35 #define BUTTON_READY (1) 36 #define BUTTON_FAIL (-1) 39 #ifndef VRPN_CLIENT_ONLY 40 static const unsigned char PORT_ERROR = (1 << 3);
41 static const unsigned char PORT_SLCT = (1 << 4);
42 static const unsigned char PORT_PE = (1 << 5);
43 static const unsigned char PORT_ACK = (1 << 6);
44 static const unsigned char PORT_BUSY = (1 << 7);
45 static const unsigned char BIT_MASK =
46 PORT_ERROR | PORT_SLCT | PORT_PE | PORT_ACK | PORT_BUSY;
58 #define PACK_ADMIN_MESSAGE(i, event) \ 61 vrpn_int32 len = encode_to(msgbuf, i, event); \ 62 if (d_connection->pack_message(len, timestamp, admin_message_id, \ 63 d_sender_id, msgbuf, \ 64 vrpn_CONNECTION_RELIABLE)) { \ 65 fprintf(stderr, "vrpn_Button: can't write message: tossing\n"); \ 68 #define PACK_ALERT_MESSAGE(i, event) \ 71 vrpn_int32 len = encode_to(msgbuf, i, event); \ 72 if (d_connection->pack_message(len, timestamp, alert_message_id, \ 73 d_sender_id, msgbuf, \ 74 vrpn_CONNECTION_RELIABLE)) { \ 75 fprintf(stderr, "vrpn_Button: can't write message: tossing\n"); \ 79 #define PACK_MESSAGE(i, event) \ 82 vrpn_int32 len = encode_to(msgbuf, i, event); \ 83 if (d_connection->pack_message(len, timestamp, change_message_id, \ 84 d_sender_id, msgbuf, \ 85 vrpn_CONNECTION_RELIABLE)) { \ 86 fprintf(stderr, "vrpn_Button: can't write message: tossing\n"); \ 132 fprintf(stderr,
"vrpn_Button: Can't register IDs\n");
170 if (i == 0 || i == 1)
173 fprintf(stderr,
"Invalid send_alert state\n");
181 sprintf(msg,
"vrpn_Button::set_momentary() buttons id %d is greater " 182 "than the number of buttons(%d)\n",
195 sprintf(msg,
"vrpn_Button::set_momentary() buttons id %d is greater " 196 "than the number of buttons(%d)\n",
205 vrpn_int32 current_state)
209 sprintf(msg,
"vrpn_Button::set_toggle() buttons id %d is greater then " 210 "the number of buttons(%d)\n",
230 sprintf(msg,
"vrpn_Button::set_toggle() buttons id %d is greater then " 231 "the number of buttons(%d)\n",
279 printf(
"CurrButtons: ");
281 printf(
"%c",
buttons[i] ?
'1' :
'0');
285 printf(
"LastButtons: ");
308 return 1000 - buflen;
359 const char *bufptr = p.
buffer;
410 fprintf(stderr,
"vrpn_Button::report_changes(): Button %d in \ 411 invalid state (%d)\n",
418 fprintf(stderr,
"vrpn_Button: No valid connection\n");
433 fprintf(stderr,
"vrpn_Button: No valid connection\n");
441 char *msgbuf = (
char *)ibuf;
450 fprintf(stderr,
"vrpn_Button: cannot write states message: tossing\n");
454 #ifndef VRPN_CLIENT_ONLY 482 buttons[button] = (
unsigned char)(new_value != 0);
507 struct timeval current_time;
523 timestamp.tv_usec = current_time.tv_usec;
541 int portno,
unsigned porthex)
545 const char *portname;
548 portname =
"/dev/lp0";
551 portname =
"/dev/lp1";
554 portname =
"/dev/lp2";
557 fprintf(stderr,
"vrpn_Button_Parallel: " 558 "Bad port number (%x) for Linux lp#\n",
561 portname =
"UNKNOWN";
566 if ((
port = open(portname, O_RDWR)) < 0) {
567 perror(
"vrpn_Button_Parallel::vrpn_Button_Parallel(): " 569 fprintf(stderr,
"vrpn_Button_Parallel::vrpn_Button_Parallel(): " 570 "Can't open port %s\n",
579 fprintf(stderr,
"vrpn_Button: need giveio driver for port access!\n");
580 fprintf(stderr,
"vrpn_Button: can't use vrpn_Button()\n");
600 fprintf(stderr,
"vrpn_Button_Parallel: Bad port number (%d)\n",
606 fprintf(stderr,
"vrpn_Button_Parallel: Using port %x\n",
port);
608 fprintf(stderr,
"vrpn_Button_Parallel: not supported on this platform\n?");
614 #if defined(linux) || defined(_WIN32) 623 const unsigned short DATA_REGISTER_OFFSET = 0;
624 _outp((
unsigned short)(
port + DATA_REGISTER_OFFSET), 3);
626 fprintf(stderr,
"vrpn_Button_Parallel: Not setting bit 0 on Linux, may not " 627 "work with all ports\n");
677 fprintf(stderr,
"vrpn_Button_Python failure!\n");
690 const int debounce_count = 30;
691 int status_register[debounce_count];
700 for (i = 0; i < debounce_count; i++)
701 if (ioctl(
port, LPGETSTATUS, &status_register[i]) == -1) {
702 perror(
"vrpn_Button_Python::read(): ioctl() failed");
708 const unsigned short STATUS_REGISTER_OFFSET = 1;
709 for (i = 0; i < debounce_count; i++) {
712 _inp((
unsigned short)(
port + STATUS_REGISTER_OFFSET));
714 status_register[i] = 0;
718 for (i = 0; i < debounce_count; i++) {
719 status_register[i] = 0;
723 for (i = 0; i < debounce_count; i++) {
724 status_register[i] = 0;
727 for (i = 0; i < debounce_count; i++) {
728 status_register[i] = status_register[i] & BIT_MASK;
733 for (i = 1; i < debounce_count; i++) {
734 if (status_register[0] != status_register[i]) {
740 buttons[0] = ((status_register[0] & PORT_SLCT) == 0);
741 buttons[1] = ((status_register[0] & PORT_BUSY) != 0);
742 buttons[2] = ((status_register[0] & PORT_PE) == 0);
743 buttons[3] = ((status_register[0] & PORT_ERROR) == 0);
744 buttons[4] = ((status_register[0] & PORT_ACK) == 0);
750 const char *port,
long baud)
755 fprintf(stderr,
"vrpn_Button_Serial: NULL port name\n");
767 fprintf(stderr,
"vrpn_Button_Serial: Cannot Open serial port\n");
781 const char *port,
long baud)
783 , reported_failure(false)
791 "vrpn_Button_PinchGlove: Too many buttons. The limit is ");
821 fprintf(stderr,
"vrpn_Button_PinchGlove failure!\n");
859 unsigned char mask = 0x10;
861 for (
int j = 0; j < 5; j++, mask = (unsigned char)(mask >> 1)) {
878 "vrpn_Button_PinchGlove message start byte: time stamped byte!",
899 struct timeval timeout = {
901 struct timeval timeout_to_pass;
908 timeout_to_pass.tv_sec = timeout.tv_sec;
909 timeout_to_pass.tv_usec = timeout.tv_usec;
918 #endif // VRPN_CLIENT_ONLY 931 "vrpn_Button_Remote: can't register change handler\n");
937 "vrpn_Button_Remote: can't register states handler\n");
942 fprintf(stderr,
"vrpn_Button_Remote: Can't get connection!\n");
970 const char *bufptr = p.
buffer;
975 fprintf(stderr,
"vrpn_Button: change message payload error\n");
976 fprintf(stderr,
" (got %d, expected %lud)\n", p.
payload_len,
977 static_cast<unsigned long>(2 *
sizeof(vrpn_int32)));
994 const char *bufptr = p.
buffer;
995 vrpn_int32 numbuttons;
1025 #ifndef VRPN_CLIENT_ONLY 1027 int vrpn_Button_Parallel::openGiveIO(
void)
1031 memset(&osvi, 0,
sizeof(OSVERSIONINFO));
1032 osvi.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
1033 GetVersionEx(&osvi);
1036 if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
1041 if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) {
1042 HANDLE h = CreateFile(
"\\\\.\\giveio", GENERIC_READ, 0, NULL,
1043 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1044 if (h == INVALID_HANDLE_VALUE) {
1055 "vrpn_Button_Parallel::openGiveIO: unknown windows version\n");
1059 #endif // VRPN_CLIENT_ONLY
virtual int mainloop(const struct timeval *timeout=NULL)=0
Call each time through program main loop to handle receiving any incoming messages and sending any pa...
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
int vrpn_write_characters(int comm, const unsigned char *buffer, size_t bytes)
Write the buffer to the serial port.
int vrpn_close_commport(int comm)
int vrpn_open_commport(const char *portname, long baud, int charsize, vrpn_SER_PARITY parity, bool rts_flow)
Open a serial port, given its name and baud rate.
VRPN_API int vrpn_unbuffer(const char **buffer, timeval *t)
Utility routine for taking a struct timeval from a buffer that was sent as a message.
const vrpn_uint32 vrpn_CONNECTION_RELIABLE
Classes of service for messages, specify multiple by ORing them together Priority of satisfying these...
int vrpn_flush_input_buffer(int comm)
Throw out any characters within the input buffer.
void client_mainloop(void)
Handles functions that all clients should provide in their mainloop() (warning of no server...
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
vrpn_int32 d_ping_message_id
Ask the server if they are there.
Generic connection class not specific to the transport mechanism.
int vrpn_drain_output_buffer(int comm)
Wait until all of the characters in the output buffer are sent, then return.
virtual int init(void)
Initialize things that the constructor can't. Returns 0 on success, -1 on failure.
int vrpn_read_available_characters(int comm, unsigned char *buffer, size_t bytes)
int register_autodeleted_handler(vrpn_int32 type, vrpn_MESSAGEHANDLER handler, void *userdata, vrpn_int32 sender=vrpn_ANY_SENDER)
Registers a handler with the connection, and remembers to delete at destruction.
vrpn_Connection * d_connection
Connection that this object talks to.
This structure is what is passed to a vrpn_Connection message callback.
virtual int pack_message(vrpn_uint32 len, struct timeval time, vrpn_int32 type, vrpn_int32 sender, const char *buffer, vrpn_uint32 class_of_service)
Pack a message that will be sent the next time mainloop() is called. Turn off the RELIABLE flag if yo...
const char * vrpn_got_connection
int send_text_message(const char *msg, struct timeval timestamp, vrpn_TEXT_SEVERITY type=vrpn_TEXT_NORMAL, vrpn_uint32 level=0)
Sends a NULL-terminated text message from the device d_sender_id.
#define vrpn_gettimeofday
VRPN_API int vrpn_buffer(char **insertPt, vrpn_int32 *buflen, const timeval t)
Utility routine for placing a timeval struct into a buffer that is to be sent as a message...
Class from which all user-level (and other) classes that communicate with vrpn_Connections should der...
vrpn_int32 d_sender_id
Sender ID registered with the connection.
unsigned long vrpn_TimevalDuration(struct timeval endT, struct timeval startT)
Return number of microseconds between startT and endT.
virtual vrpn_int32 register_message_type(const char *name)
void call_handlers(const CALLBACK_STRUCT &info)
This will pass the referenced parameter as a const to all the callbacks.
const int vrpn_ANY_SENDER
vrpn_ANY_SENDER can be used to register callbacks on a given message type from any sender...