2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
7 #include <aros/debug.h>
9 #include <exec/memory.h>
11 #include <dos/exall.h>
12 #include <dos/datetime.h>
13 #include <proto/dos.h>
14 #include <proto/utility.h>
15 #include <utility/tagitem.h>
16 #include <utility/utility.h>
17 #include <devices/serial.h>
19 #include <proto/alib.h>
20 #include <proto/exec.h>
21 #include <proto/dos.h>
22 #include <proto/commodities.h>
24 #include <devices/serial.h>
30 #define ARG_TEMPLATE "KILL/S,UNIT/N,PROBE/S"
40 static UBYTE mousebuffer
[3];
51 const char * signature
;
52 ULONG signature_length
;
54 void (*handler
)(char *, ULONG
, struct mouse_action
*);
60 * The following flags are defined:
62 #define MOUSE_LEFT_BUTTON 0x01
63 #define MOUSE_RIGHT_BUTTON 0x02
64 #define MOUSE_MIDDLE_BUTTON 0x04
66 #define MOUSE_DATA_VALID 0x8000
68 const char ms_mouse
[] =
95 static void ms_mouse_protocol(char *, ULONG
, struct mouse_action
* );
99 * All known protocols and their handlers
101 const struct protocol protocols
[] = {
102 {ms_mouse
, sizeof(ms_mouse
), 3, ms_mouse_protocol
, "ms-mouse"},
103 {NULL
, 0 , 0, NULL
, NULL
}
107 static struct NewBroker nb
=
113 NBU_NOTIFY
| NBU_UNIQUE
,
120 static CxObj
* cxbroker
;
125 static void ms_mouse_protocol(char * buffer
,
127 struct mouse_action
* ma
)
134 printf("0x%02x,",buffer
[j
++]);
143 if (0 == (buffer
[i
] & 0x40)) {
144 mousebuffer
[bufptr
++] = buffer
[i
];
149 mousebuffer
[bufptr
] = buffer
[i
];
153 if ((mousebuffer
[2] & 0x40)) {
154 ma
->flags
= MOUSE_DATA_VALID
;
155 if ((mousebuffer
[2] & 0x20))
156 ma
->flags
|= MOUSE_LEFT_BUTTON
;
157 if ((mousebuffer
[2] & 0x10))
158 ma
->flags
|= MOUSE_RIGHT_BUTTON
;
159 ma
->dy
= (mousebuffer
[1] & 0x20)
160 ? (mousebuffer
[1]-0x40)
162 ma
->dx
= (mousebuffer
[0] & 0x20)
163 ? (mousebuffer
[0]-0x40)
177 static void check_mouse_action(struct mouse_action
* old_action
,
178 struct mouse_action
* cur_action
)
180 if (cur_action
->flags
& MOUSE_DATA_VALID
&&
181 old_action
->flags
& MOUSE_DATA_VALID
) {
185 if (old_action
->flags
& MOUSE_LEFT_BUTTON
) {
186 if (0 == (cur_action
->flags
& MOUSE_LEFT_BUTTON
)) {
187 printf("Left mouse button released!\n");
190 if (0 == (old_action
->flags
& MOUSE_LEFT_BUTTON
)) {
191 if (cur_action
->flags
& MOUSE_LEFT_BUTTON
) {
192 printf("Left mouse button pressed!\n");
196 if (old_action
->flags
& MOUSE_RIGHT_BUTTON
) {
197 if (0 == (cur_action
->flags
& MOUSE_RIGHT_BUTTON
)) {
198 printf("Right mouse button released!\n");
201 if (0 == (old_action
->flags
& MOUSE_RIGHT_BUTTON
)) {
202 if (cur_action
->flags
& MOUSE_RIGHT_BUTTON
) {
203 printf("Right mouse button pressed!\n");
207 if (old_action
->flags
& MOUSE_MIDDLE_BUTTON
) {
208 if (0 == (cur_action
->flags
& MOUSE_MIDDLE_BUTTON
)) {
209 printf("Middle mouse button released!\n");
212 if (0 == (old_action
->flags
& MOUSE_MIDDLE_BUTTON
)) {
213 if (cur_action
->flags
& MOUSE_MIDDLE_BUTTON
) {
214 printf("Middle mouse button pressed!\n");
218 if (cur_action
->dx
) {
219 printf("Mouse movement left/right: %d\n",cur_action
->dx
);
221 if (cur_action
->dy
) {
222 printf("Mouse movement up/down: %d\n",cur_action
->dy
);
227 static void read_input(struct IOExtSer
* IORequest
,
228 struct MsgPort
* notifport
,
229 struct MsgPort
* cxport
,
230 void (*handler
)(char *, ULONG
, struct mouse_action
*))
232 struct mouse_action old_action
, cur_action
;
234 old_action
.flags
= 0;
235 cur_action
.flags
= 0;
237 while (FALSE
== end
) {
239 struct Message
* msg
;
242 ULONG cxsig
= (NULL
!= cxport
) ? (1 << cxport
->mp_SigBit
)
245 memset(buf
, 0x00, 10);
246 IORequest
->IOSer
.io_Command
= CMD_READ
;
247 IORequest
->IOSer
.io_Flags
= IOF_QUICK
;
248 IORequest
->IOSer
.io_Length
= n
;
249 IORequest
->IOSer
.io_Data
= buf
;
250 SendIO((struct IORequest
*)IORequest
);
251 sigs
= Wait((1 << ((struct IORequest
*)IORequest
)->io_Message
.mn_ReplyPort
->mp_SigBit
) |
252 (1 << notifport
->mp_SigBit
) |
255 if (NULL
!= CheckIO((struct IORequest
*)IORequest
)) {
257 if (NULL
== handler
) {
258 printf("No handler given. Calling def. handler!\n");
259 ms_mouse_protocol(buf
, n
, &cur_action
);
261 handler(buf
, n
, &cur_action
);
264 check_mouse_action(&old_action
, &cur_action
);
266 if (cur_action
.flags
& MOUSE_DATA_VALID
)
267 old_action
= cur_action
;
274 printf("Got a signal for me as commodity.\n");
275 while (NULL
!= (cxmsg
= (CxMsg
*)GetMsg(cxport
))) {
276 switch (CxMsgType(cxmsg
)) {
278 switch (CxMsgID(cxmsg
)) {
280 ActivateCxObj(cxbroker
, FALSE
);
284 ActivateCxObj(cxbroker
, TRUE
);
293 ReplyMsg((struct Message
*)cxmsg
);
297 if (NULL
!= (msg
= GetMsg(notifport
))) {
298 printf("Serial mouse driver ends.\n");
300 AbortIO((struct IORequest
*)IORequest
);
301 FreeMem(msg
, sizeof(struct Message
));
305 if (sigs
& SIGBREAKF_CTRL_C
) {
308 } /* while (FALSE == end) */
312 static const struct protocol
* probe_protocol(struct IOExtSer
* IORequest
, struct MsgPort
* notifport
)
314 const struct protocol
* p
= NULL
;
317 printf("Supposed to probe for protocol!\n");
318 IORequest
->IOSer
.io_Command
= SDCMD_QUERY
;
319 DoIO((struct IORequest
*)IORequest
);
320 printf("Number of bytes in buffer: %d\n",(int)IORequest
->IOSer
.io_Actual
);
321 if (0 != (n
= IORequest
->IOSer
.io_Actual
)) {
322 UBYTE
* buffer
= AllocMem(n
, MEMF_CLEAR
);
323 if (NULL
!= buffer
) {
325 IORequest
->IOSer
.io_Command
= CMD_READ
;
326 IORequest
->IOSer
.io_Flags
= IOF_QUICK
;
327 IORequest
->IOSer
.io_Length
= n
;
328 IORequest
->IOSer
.io_Data
= buffer
;
329 DoIO((struct IORequest
*)IORequest
);
331 while (protocols
[i
].signature
) {
332 printf("Possible: %s, sign_length=%ld\n",
334 (long)protocols
[i
].signature_length
);
336 if (n
>= protocols
[i
].signature_length
) {
337 ULONG d
= n
- protocols
[i
].signature_length
;
340 if (0 == memcmp(&buffer
[k
],
341 protocols
[i
].signature
,
342 protocols
[i
].signature_length
)) {
343 printf("Found signature for %s.\n",protocols
[i
].name
);
359 static void mouse_driver(ULONG unit
, BOOL probe_proto
,struct MsgPort
* notifport
,struct MsgPort
*cxport
)
361 struct MsgPort
* SerPort
;
362 ULONG unitnum
= unit
;
364 SerPort
= CreatePort(NULL
,0);
365 if (NULL
!= SerPort
) {
366 struct IOExtSer
* IORequest
;
367 IORequest
= (struct IOExtSer
*)CreateExtIO(SerPort
, sizeof(struct IOExtSer
));
368 if (NULL
!= IORequest
) {
369 BYTE err
= OpenDevice("serial.device", unitnum
, (struct IORequest
*)IORequest
, 0);
372 * Set parameters to read from mouse.
374 IORequest
->IOSer
.io_Command
= SDCMD_SETPARAMS
;
375 IORequest
->io_Baud
= 1200;
376 IORequest
->io_ReadLen
= 7;
377 IORequest
->io_WriteLen
= 7;
378 IORequest
->io_StopBits
= 1;
379 IORequest
->io_RBufLen
= 512;
380 IORequest
->io_ExtFlags
= 0;
381 IORequest
->IOSer
.io_Flags
= 0;
382 DoIO((struct IORequest
*)IORequest
);
385 if (0 == ((struct IORequest
*)IORequest
)->io_Error
) {
386 void (*handler
) (char*,ULONG
,struct mouse_action
*) = NULL
;
387 if (TRUE
== probe_proto
) {
388 const struct protocol
* p
;
389 p
= probe_protocol(IORequest
, notifport
);
391 handler
= p
->handler
;
393 printf("Could not detect mouse protocol!\n");
397 read_input(IORequest
, notifport
, cxport
, handler
);
399 printf("Could not set parameters for serial port.\n");
400 printf("Error code: %d\n",((struct IORequest
*)IORequest
)->io_Error
);
403 CloseDevice((struct IORequest
*)IORequest
);
405 DeleteExtIO((struct IORequest
*)IORequest
);
413 static BOOL
InitCommodity(void)
417 if (NULL
!= CxBase
) {
419 nb
.nb_Name
= strdup("Mouse Driver");
420 nb
.nb_Title
= strdup("Mouse Driver");
421 nb
.nb_Descr
= strdup("Mouse Driver for serial mice.");
423 if (NULL
!= (nb
.nb_Port
= CreateMsgPort())) {
424 if (NULL
!= (cxbroker
= CxBroker(&nb
, 0))) {
425 ActivateCxObj(cxbroker
, TRUE
);
428 DeleteMsgPort(nb
.nb_Port
);
437 static void CleanupCommodity(void)
439 if (NULL
!= CxBase
) {
440 if (NULL
!= cxbroker
)
441 DeleteCxObjAll(cxbroker
);
442 if (NULL
!= nb
.nb_Port
) {
443 struct Message
* msg
;
444 while (NULL
!= (msg
= GetMsg(nb
.nb_Port
))) {
447 DeleteMsgPort(nb
.nb_Port
);
453 #define MSGPORT_NAME "serial_mouse_driver"
455 int main(int argc
, char **argv
)
457 IPTR args
[NOOFARGS
] = {FALSE
, // ARG_KILL
462 rda
= ReadArgs(ARG_TEMPLATE
, args
, NULL
);
464 if (TRUE
== args
[ARG_KILL
]) {
465 struct MsgPort
* mport
= FindPort(MSGPORT_NAME
);
467 printf("Program seems not to be running. Cannot kill it.\n");
470 struct Message
* msg
= AllocMem(sizeof(struct Message
), MEMF_CLEAR
);
473 * Just send a message to the port.
474 * The content does not matter so far.
480 struct MsgPort
* mport
= FindPort(MSGPORT_NAME
);
482 printf("Program already running!\n");
484 BOOL have_cx
= InitCommodity();
485 struct MsgPort
* notifport
= CreatePort(MSGPORT_NAME
, 0);
486 if (NULL
!= notifport
) {
487 mouse_driver(args
[ARG_UNIT
],
491 DeletePort(notifport
);
492 if (TRUE
== have_cx
) {
496 printf("Could not create notification port!\n");