2 #include <aros/debug.h>
4 #include <exec/memory.h>
7 #include <dos/datetime.h>
9 #include <proto/utility.h>
10 #include <utility/tagitem.h>
11 #include <utility/utility.h>
12 #include <devices/serial.h>
14 #include <proto/alib.h>
15 #include <proto/exec.h>
16 #include <proto/dos.h>
17 #include <proto/commodities.h>
19 #include <devices/serial.h>
25 #define ARG_TEMPLATE "KILL/S,UNIT/N,PROBE/S"
35 static UBYTE mousebuffer
[3];
46 const char * signature
;
47 ULONG signature_length
;
49 void (*handler
)(char *, ULONG
, struct mouse_action
*);
55 * The following flags are defined:
57 #define MOUSE_LEFT_BUTTON 0x01
58 #define MOUSE_RIGHT_BUTTON 0x02
59 #define MOUSE_MIDDLE_BUTTON 0x04
61 #define MOUSE_DATA_VALID 0x8000
63 const char ms_mouse
[] =
90 static void ms_mouse_protocol(char *, ULONG
, struct mouse_action
* );
94 * All known protocols and their handlers
96 const struct protocol protocols
[] = {
97 {ms_mouse
, sizeof(ms_mouse
), 3, ms_mouse_protocol
, "ms-mouse"},
98 {NULL
, 0 , 0, NULL
, NULL
}
102 static struct NewBroker nb
=
108 NBU_NOTIFY
| NBU_UNIQUE
,
115 static CxObj
* cxbroker
;
120 static void ms_mouse_protocol(char * buffer
,
122 struct mouse_action
* ma
)
129 printf("0x%02x,",buffer
[j
++]);
138 if (0 == (buffer
[i
] & 0x40)) {
139 mousebuffer
[bufptr
++] = buffer
[i
];
144 mousebuffer
[bufptr
] = buffer
[i
];
148 if ((mousebuffer
[2] & 0x40)) {
149 ma
->flags
= MOUSE_DATA_VALID
;
150 if ((mousebuffer
[2] & 0x20))
151 ma
->flags
|= MOUSE_LEFT_BUTTON
;
152 if ((mousebuffer
[2] & 0x10))
153 ma
->flags
|= MOUSE_RIGHT_BUTTON
;
154 ma
->dy
= (mousebuffer
[1] & 0x20)
155 ? (mousebuffer
[1]-0x40)
157 ma
->dx
= (mousebuffer
[0] & 0x20)
158 ? (mousebuffer
[0]-0x40)
172 static void check_mouse_action(struct mouse_action
* old_action
,
173 struct mouse_action
* cur_action
)
175 if (cur_action
->flags
& MOUSE_DATA_VALID
&&
176 old_action
->flags
& MOUSE_DATA_VALID
) {
180 if (old_action
->flags
& MOUSE_LEFT_BUTTON
) {
181 if (0 == (cur_action
->flags
& MOUSE_LEFT_BUTTON
)) {
182 printf("Left mouse button released!\n");
185 if (0 == (old_action
->flags
& MOUSE_LEFT_BUTTON
)) {
186 if (cur_action
->flags
& MOUSE_LEFT_BUTTON
) {
187 printf("Left mouse button pressed!\n");
191 if (old_action
->flags
& MOUSE_RIGHT_BUTTON
) {
192 if (0 == (cur_action
->flags
& MOUSE_RIGHT_BUTTON
)) {
193 printf("Right mouse button released!\n");
196 if (0 == (old_action
->flags
& MOUSE_RIGHT_BUTTON
)) {
197 if (cur_action
->flags
& MOUSE_RIGHT_BUTTON
) {
198 printf("Right mouse button pressed!\n");
202 if (old_action
->flags
& MOUSE_MIDDLE_BUTTON
) {
203 if (0 == (cur_action
->flags
& MOUSE_MIDDLE_BUTTON
)) {
204 printf("Middle mouse button released!\n");
207 if (0 == (old_action
->flags
& MOUSE_MIDDLE_BUTTON
)) {
208 if (cur_action
->flags
& MOUSE_MIDDLE_BUTTON
) {
209 printf("Middle mouse button pressed!\n");
213 if (cur_action
->dx
) {
214 printf("Mouse movement left/right: %d\n",cur_action
->dx
);
216 if (cur_action
->dy
) {
217 printf("Mouse movement up/down: %d\n",cur_action
->dy
);
222 static void read_input(struct IOExtSer
* IORequest
,
223 struct MsgPort
* notifport
,
224 struct MsgPort
* cxport
,
225 void (*handler
)(char *, ULONG
, struct mouse_action
*))
227 struct mouse_action old_action
, cur_action
;
229 old_action
.flags
= 0;
230 cur_action
.flags
= 0;
232 while (FALSE
== end
) {
234 struct Message
* msg
;
237 ULONG cxsig
= (NULL
!= cxport
) ? (1 << cxport
->mp_SigBit
)
240 memset(buf
, 0x00, 10);
241 IORequest
->IOSer
.io_Command
= CMD_READ
;
242 IORequest
->IOSer
.io_Flags
= IOF_QUICK
;
243 IORequest
->IOSer
.io_Length
= n
;
244 IORequest
->IOSer
.io_Data
= buf
;
245 SendIO((struct IORequest
*)IORequest
);
246 sigs
= Wait((1 << ((struct IORequest
*)IORequest
)->io_Message
.mn_ReplyPort
->mp_SigBit
) |
247 (1 << notifport
->mp_SigBit
) |
250 if (NULL
!= CheckIO((struct IORequest
*)IORequest
)) {
252 if (NULL
== handler
) {
253 printf("No handler given. Calling def. handler!\n");
254 ms_mouse_protocol(buf
, n
, &cur_action
);
256 handler(buf
, n
, &cur_action
);
259 check_mouse_action(&old_action
, &cur_action
);
261 if (cur_action
.flags
& MOUSE_DATA_VALID
)
262 old_action
= cur_action
;
269 printf("Got a signal for me as commodity.\n");
270 while (NULL
!= (cxmsg
= (CxMsg
*)GetMsg(cxport
))) {
271 switch (CxMsgType(cxmsg
)) {
273 switch (CxMsgID(cxmsg
)) {
275 ActivateCxObj(cxbroker
, FALSE
);
279 ActivateCxObj(cxbroker
, TRUE
);
288 ReplyMsg((struct Message
*)cxmsg
);
292 if (NULL
!= (msg
= GetMsg(notifport
))) {
293 printf("Serial mouse driver ends.\n");
295 AbortIO((struct IORequest
*)IORequest
);
296 FreeMem(msg
, sizeof(struct Message
));
300 if (sigs
& SIGBREAKF_CTRL_C
) {
303 } /* while (FALSE == end) */
307 static const struct protocol
* probe_protocol(struct IOExtSer
* IORequest
, struct MsgPort
* notifport
)
309 const struct protocol
* p
= NULL
;
312 printf("Supposed to probe for protocol!\n");
313 IORequest
->IOSer
.io_Command
= SDCMD_QUERY
;
314 DoIO((struct IORequest
*)IORequest
);
315 printf("Number of bytes in buffer: %d\n",(int)IORequest
->IOSer
.io_Actual
);
316 if (0 != (n
= IORequest
->IOSer
.io_Actual
)) {
317 UBYTE
* buffer
= AllocMem(n
, MEMF_CLEAR
);
318 if (NULL
!= buffer
) {
320 IORequest
->IOSer
.io_Command
= CMD_READ
;
321 IORequest
->IOSer
.io_Flags
= IOF_QUICK
;
322 IORequest
->IOSer
.io_Length
= n
;
323 IORequest
->IOSer
.io_Data
= buffer
;
324 DoIO((struct IORequest
*)IORequest
);
326 while (protocols
[i
].signature
) {
327 printf("Possible: %s, sign_length=%ld\n",
329 (long)protocols
[i
].signature_length
);
331 if (n
>= protocols
[i
].signature_length
) {
332 ULONG d
= n
- protocols
[i
].signature_length
;
335 if (0 == memcmp(&buffer
[k
],
336 protocols
[i
].signature
,
337 protocols
[i
].signature_length
)) {
338 printf("Found signature for %s.\n",protocols
[i
].name
);
354 static void mouse_driver(ULONG unit
, BOOL probe_proto
,struct MsgPort
* notifport
,struct MsgPort
*cxport
)
356 struct MsgPort
* SerPort
;
357 ULONG unitnum
= unit
;
359 SerPort
= CreatePort(NULL
,0);
360 if (NULL
!= SerPort
) {
361 struct IOExtSer
* IORequest
;
362 IORequest
= (struct IOExtSer
*)CreateExtIO(SerPort
, sizeof(struct IOExtSer
));
363 if (NULL
!= IORequest
) {
364 BYTE err
= OpenDevice("serial.device", unitnum
, (struct IORequest
*)IORequest
, 0);
367 * Set parameters to read from mouse.
369 IORequest
->IOSer
.io_Command
= SDCMD_SETPARAMS
;
370 IORequest
->io_Baud
= 1200;
371 IORequest
->io_ReadLen
= 7;
372 IORequest
->io_WriteLen
= 7;
373 IORequest
->io_StopBits
= 1;
374 IORequest
->io_RBufLen
= 512;
375 IORequest
->io_ExtFlags
= 0;
376 IORequest
->IOSer
.io_Flags
= 0;
377 DoIO((struct IORequest
*)IORequest
);
380 if (0 == ((struct IORequest
*)IORequest
)->io_Error
) {
381 void (*handler
) (char*,ULONG
,struct mouse_action
*) = NULL
;
382 if (TRUE
== probe_proto
) {
383 const struct protocol
* p
;
384 p
= probe_protocol(IORequest
, notifport
);
386 handler
= p
->handler
;
388 printf("Could not detect mouse protocol!\n");
392 read_input(IORequest
, notifport
, cxport
, handler
);
394 printf("Could not set parameters for serial port.\n");
395 printf("Error code: %d\n",((struct IORequest
*)IORequest
)->io_Error
);
398 CloseDevice((struct IORequest
*)IORequest
);
400 DeleteExtIO((struct IORequest
*)IORequest
);
408 static BOOL
InitCommodity(void)
412 if (NULL
!= CxBase
) {
414 nb
.nb_Name
= strdup("Mouse Driver");
415 nb
.nb_Title
= strdup("Mouse Driver");
416 nb
.nb_Descr
= strdup("Mouse Driver for serial mice.");
418 if (NULL
!= (nb
.nb_Port
= CreateMsgPort())) {
419 if (NULL
!= (cxbroker
= CxBroker(&nb
, 0))) {
420 ActivateCxObj(cxbroker
, TRUE
);
423 DeleteMsgPort(nb
.nb_Port
);
432 static void CleanupCommodity(void)
434 if (NULL
!= CxBase
) {
435 if (NULL
!= cxbroker
)
436 DeleteCxObjAll(cxbroker
);
437 if (NULL
!= nb
.nb_Port
) {
438 struct Message
* msg
;
439 while (NULL
!= (msg
= GetMsg(nb
.nb_Port
))) {
442 DeleteMsgPort(nb
.nb_Port
);
448 #define MSGPORT_NAME "serial_mouse_driver"
450 int main(int argc
, char **argv
)
452 IPTR args
[NOOFARGS
] = {FALSE
, // ARG_KILL
457 rda
= ReadArgs(ARG_TEMPLATE
, args
, NULL
);
459 if (TRUE
== args
[ARG_KILL
]) {
460 struct MsgPort
* mport
= FindPort(MSGPORT_NAME
);
462 printf("Program seems not to be running. Cannot kill it.\n");
465 struct Message
* msg
= AllocMem(sizeof(struct Message
), MEMF_CLEAR
);
468 * Just send a message to the port.
469 * The content does not matter so far.
475 struct MsgPort
* mport
= FindPort(MSGPORT_NAME
);
477 printf("Program already running!\n");
479 BOOL have_cx
= InitCommodity();
480 struct MsgPort
* notifport
= CreatePort(MSGPORT_NAME
, 0);
481 if (NULL
!= notifport
) {
482 mouse_driver(args
[ARG_UNIT
],
486 DeletePort(notifport
);
487 if (TRUE
== have_cx
) {
491 printf("Could not create notification port!\n");