]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/gfx/mw/v2_0/src/drivers/kbd_pipe.c
Initial revision
[karo-tx-redboot.git] / packages / services / gfx / mw / v2_0 / src / drivers / kbd_pipe.c
1 /*
2  * Copyright (C) 2000 by VTech Informations LTD.
3  *
4  * This source code is released under MPL
5  * 
6  * Vladimir Cotfas <vladimircotfas@vtech.ca> Aug 31, 2000
7  *
8  * Named pipe Keyboard Driver for Microwindows
9  * See src/demos/nxkbd/srvconn.c for example client-side driver
10  */
11 #include <stdio.h>
12 #include <stddef.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include "device.h"
21
22 static char kbdName[] = "/tmp/.nano-X-softkbd";
23 #define KBD_NAMED_PIPE  kbdName 
24
25 #define _SOFT_DEBUG     0
26
27 static int kbd_fd = -1; /* keyboar driver FIFO */
28
29 static int  soft_Open(KBDDEVICE* pkd);
30 static void soft_Close();
31 static void soft_GetModifierInfo(MWKEYMOD* modifiers, MWKEYMOD* curmodifiers);
32 static int  soft_Read(MWKEY* buf, MWKEYMOD* modifiers, MWSCANCODE* scancode);
33
34 KBDDEVICE kbddev = {
35     soft_Open,
36     soft_Close,
37     soft_GetModifierInfo,
38     soft_Read,
39     NULL
40 };
41
42 /*
43  * Open the keyboard.
44  */
45 static int
46 soft_Open(KBDDEVICE* pkd)
47 {
48         struct stat s;
49
50         /* Check if the file already exists: */
51         if (!stat(KBD_NAMED_PIPE, &s)) {
52                 if (unlink(KBD_NAMED_PIPE) < 0)
53                         return -1;
54         }
55         if (mkfifo(KBD_NAMED_PIPE, 0600) < 0) {
56                 EPRINTF("mkfifo() error %d ('%s')\n", \
57                         errno, sys_errlist[errno]);
58                 return -1;
59         }
60
61         /* Open the named pipe */
62         if ((kbd_fd = open(KBD_NAMED_PIPE, O_RDONLY | O_NONBLOCK)) < 0) {
63                 EPRINTF("open() error %d ('%s')\n", \
64                         errno, sys_errlist[errno]);
65                 return -1;
66         }
67         
68         return kbd_fd;
69 }
70
71 /*
72  * Close the keyboard.
73  */
74 static void
75 soft_Close()
76 {
77 #if _SOFT_DEBUG
78     EPRINTF("kbd_soft.c: soft_Close(): closing named pipe %d\n", kbd_fd);
79 #endif
80     if (kbd_fd >= 0)
81             close(kbd_fd);
82     kbd_fd = -1;
83
84     unlink(KBD_NAMED_PIPE);
85 }
86
87 /*
88  * Return the possible modifiers for the keyboard.
89  */
90 static  void
91 soft_GetModifierInfo(MWKEYMOD* modifiers, MWKEYMOD* curmodifiers)
92 {
93 #if _SOFT_DEBUG
94     EPRINTF("kbd_soft.c: soft_GetModifierInfo(): being asked about modifiers\n");
95 #endif
96     if (modifiers)
97         *modifiers = 0;                 /* no modifiers available */
98     if (curmodifiers)
99         *curmodifiers = 0;              /* no modifiers available */
100 }
101
102 /*
103  * This reads one keystroke from the keyboard, and the current state of
104  * the mode keys (ALT, SHIFT, CTRL).  Returns -1 on error, 0 if no data
105  * is ready, and 1 if data was read.  This is a non-blocking call.
106  */
107
108 static int
109 soft_Read(MWKEY* buf, MWKEYMOD* modifiers, MWSCANCODE* scancode)
110 {
111     int cc;
112     *modifiers = 0;         /* no modifiers yet */
113     *scancode = 0;          /* no scancode yet */
114
115     cc = read(kbd_fd, buf, 1); /* this is NON BLOCKING read */
116
117     if (cc > 0) {
118         if(*buf == 0x1b)
119               return -2;      /* special case ESC*/
120 #if _SOFT_DEBUG
121         EPRINTF("kbd_soft.c: soft_Read(): read '%c', cc = %d\n", 
122                 buf[0], cc); 
123         fflush(NULL);
124 #endif
125         return 1;
126     }
127
128     return 0;
129 }