/
strokeit_input_device.cpp
178 lines (142 loc) · 3.19 KB
/
strokeit_input_device.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
This code is based upon the input server add on from the:
BeOS VNC Server component by Mikael Eiman (mikael@eiman.tv)
Copyright 2001
The following rules apply to the use of this code:
* The code may be freely distributed, as long as this comment isn't removed or edited
* The code may not be used for commercial purposes without the explicit permission of the author
* If you fix anything, send a copy of the corrected code to the author!
*/
#include <InputServerDevice.h>
#include <stdio.h>
#include <InterfaceDefs.h>
#include <Messenger.h>
#include <MessageQueue.h>
int32 port_thread_func( void * _data );
int32 message_shuffler( void * _data );
typedef struct data_t
{
BMessageQueue queue;
BInputServerDevice * device;
};
class StrokeITinput : public BInputServerDevice
{
private:
thread_id port_thread, messenger_thread;
data_t data;
public:
StrokeITinput()
: port_thread(0),
messenger_thread(0)
{
data.device = this;
};
virtual status_t InitCheck()
{
input_device_ref mouse = {
"StrokeIT Mouse", B_POINTING_DEVICE, (void*)this
};
input_device_ref keyboard = {
"StrokeIT Keyboard", B_KEYBOARD_DEVICE, (void*)this
};
input_device_ref * devices[3] = {
&mouse,
&keyboard,
NULL
};
RegisterDevices( devices );
return B_OK;
};
virtual status_t Start( const char *, void*)
{
if ( port_thread )
return B_OK;
messenger_thread = spawn_thread(
message_shuffler,
"StrokeIT Message thread",
B_NORMAL_PRIORITY,
&data
);
resume_thread( messenger_thread );
port_thread = spawn_thread(
port_thread_func,
"StrokeIT port reader thread",
B_NORMAL_PRIORITY,
&data
);
resume_thread( port_thread );
return B_OK;
};
virtual status_t Stop( const char *, void*)
{
return B_OK;
};
virtual ~StrokeITinput()
{
};
};
int32 message_shuffler( void * _data )
{
data_t * data = (data_t*)_data;
while ( true )
{
BMessage * msg = NULL;
data->queue.Lock();
if ( !data->queue.IsEmpty() )
{
msg = data->queue.NextMessage();
}
data->queue.Unlock();
if ( msg )
data->device->EnqueueMessage( msg );
else
snooze(10000);
}
return 0;
};
int32 port_thread_func( void * _data )
{
printf("StrokeIT: Port reader running\n");
data_t * data = (data_t*)_data;
port_id port = create_port(100, "StrokeIT Input port");
while ( true )
{
char msgbuffer[512];
int32 msgcode;
ssize_t numread = read_port(
port,
&msgcode,
msgbuffer,
sizeof(msgbuffer)
);
if ( numread < 0 )
{ // Error reading port
continue;
}
BMessage * msg = new BMessage;
status_t res = msg->Unflatten( msgbuffer );
if ( res == B_OK )
{
msg->AddInt64("when", system_time() );
msg->AddInt32("modifiers", modifiers() );
data->queue.Lock();
data->queue.AddMessage( msg );
data->queue.Unlock();
//data->device->EnqueueMessage(msg);
} else
{ // Some error, shouldn't happen
delete msg;
}
};
return 0;
}
#ifdef __POWERPC__
#pragma export on
#endif
extern "C" _EXPORT
BInputServerDevice *
instantiate_input_device()
{
printf("StrokeIT: instantiate_input_device\n");
return new StrokeITinput;
}