~stefanlsd/btnx/devel

« back to all changes in this revision

Viewing changes to src/uinput.c

  • Committer: Stefan Lesicnik
  • Date: 2009-02-10 21:37:42 UTC
  • Revision ID: stefan@lsd.co.za-20090210213742-k4h6x3pkkv7jbvc0
Perhaps more correct way to catch return value of -1 from write().

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#include <stdlib.h>
14
14
#include <string.h>
15
15
#include <unistd.h>
 
16
#include <errno.h>
16
17
#include <libdaemon/dlog.h>
17
18
 
18
19
#include "uinput.h"
34
35
int uinput_init(void) 
35
36
{
36
37
  struct uinput_user_dev dev_mouse, dev_kbd;
37
 
  int i, ignore;
 
38
  int i, rem, written;
 
39
  unsigned char *buf;
38
40
 
39
41
  uinput_mouse_fd = open_handler("uinput", O_WRONLY | O_NDELAY);
40
42
  if (uinput_mouse_fd < 0) 
56
58
  dev_mouse.id.product = BTNX_PRODUCT_MOUSE;
57
59
  dev_mouse.id.version = 0;
58
60
  strcpy(dev_mouse.name, UMOUSE_NAME);
59
 
  ignore = write(uinput_mouse_fd, &dev_mouse, sizeof(dev_mouse));
60
61
 
 
62
  rem = sizeof(dev_mouse);
 
63
  buf = (unsigned char *)&dev_mouse;
 
64
  do {
 
65
          written = write(uinput_mouse_fd, buf, rem);
 
66
          if (written == rem)
 
67
                  break;
 
68
          if (written > 0) {
 
69
                  buf += written;
 
70
                  rem -= written;
 
71
          }
 
72
  } while (written >= 0 || errno == EINTR);
 
73
  if (written < 0) {
 
74
    perror(OUT_PRE "Error writing to uinput device");
 
75
    exit(BTNX_ERROR_WRITE_UINPUT);
 
76
  }
61
77
  
62
78
  memset(&dev_kbd, 0, sizeof(dev_kbd));
63
79
  dev_kbd.id.bustype = 0;
65
81
  dev_kbd.id.product = BTNX_PRODUCT_KBD;
66
82
  dev_kbd.id.version = 0;
67
83
  strcpy(dev_kbd.name, UKBD_NAME);
68
 
  ignore = write(uinput_kbd_fd, &dev_kbd, sizeof(dev_kbd));
 
84
 
 
85
  rem = sizeof(dev_kbd);
 
86
  buf = (unsigned char *)&dev_kbd;
 
87
  do {
 
88
          written = write(uinput_kbd_fd, buf, rem);
 
89
          if (written == rem)
 
90
                  break;
 
91
          if (written > 0) {
 
92
                  buf += written;
 
93
                  rem -= written;
 
94
          }
 
95
  } while (written >= 0 || errno == EINTR);
 
96
  if (written < 0) {
 
97
    perror(OUT_PRE "Error writing to uinput device");
 
98
    exit(BTNX_ERROR_WRITE_UINPUT);
 
99
  }
69
100
 
70
101
  ioctl(uinput_mouse_fd, UI_SET_EVBIT, EV_REL);
71
102
  ioctl(uinput_mouse_fd, UI_SET_RELBIT, REL_X);
104
135
 
105
136
/* Send any necessary modifier keys */
106
137
static void uinput_send_mods(struct btnx_event *bev, struct input_event event) {
107
 
        int i, ignore;
 
138
        int i, rem, written;
 
139
        unsigned char *buf;
108
140
        int mod_pressed=0;
109
141
        
110
142
        for (i=0; i<MAX_MODS; i++)
115
147
                event.type = EV_KEY;
116
148
                event.code = bev->mod[i];
117
149
                event.value = bev->pressed;
118
 
                ignore = write(uinput_kbd_fd, &event, sizeof(event));
119
 
                
 
150
 
 
151
                rem = sizeof(event);
 
152
                buf = (unsigned char *)&event;
 
153
                do {
 
154
                        written = write(uinput_kbd_fd, buf, rem);
 
155
                        if (written == rem)
 
156
                                break;
 
157
                        if (written > 0) {
 
158
                                buf += written;
 
159
                                rem -= written;
 
160
                        }
 
161
                } while (written >= 0 || errno == EINTR);
 
162
                if (written < 0) {
 
163
                        perror(OUT_PRE "Error writing to uinput device");
 
164
                        exit(BTNX_ERROR_WRITE_UINPUT);
 
165
                }
 
166
 
120
167
                event.type = EV_SYN;
121
 
                event.code = SYN_REPORT;
122
 
                event.value = 0;
123
 
                ignore = write(uinput_kbd_fd, &event, sizeof(event));
124
 
                
125
 
                mod_pressed = 1;
 
168
                event.code = SYN_REPORT;
 
169
                event.value = 0;
 
170
 
 
171
                rem = sizeof(event);
 
172
                buf = (unsigned char *)&event;
 
173
                do {
 
174
                        written = write(uinput_kbd_fd, buf, rem);
 
175
                        if (written == rem)
 
176
                                break;
 
177
                        if (written > 0) {
 
178
                                buf += written;
 
179
                                rem -= written;
 
180
                        }
 
181
                } while (written >= 0 || errno == EINTR);
 
182
                if (written < 0) {
 
183
                        perror(OUT_PRE "Error writing to uinput device");
 
184
                        exit(BTNX_ERROR_WRITE_UINPUT);
 
185
                }
 
186
                mod_pressed = 1;
126
187
        }
127
188
        
128
189
        if (mod_pressed)
131
192
 
132
193
/* Send the main key or button press/release */
133
194
static void uinput_send_key(struct btnx_event *bev, struct input_event event, int fd) {
134
 
        int ignore;
 
195
        int rem, written;
 
196
        unsigned char *buf;;
135
197
        if (bev->keycode > BTNX_EXTRA_EVENTS)
136
198
        {
137
199
                event.type = EV_REL;
143
205
                        event.value = 1;
144
206
                else if (bev->keycode == REL_WHEELBACK)
145
207
                        event.value = -1;
146
 
                ignore = write(fd, &event, sizeof(event));
 
208
 
 
209
                rem = sizeof(event);
 
210
                buf = (unsigned char *)&event;
 
211
                do {
 
212
                        written = write(fd, buf, rem);
 
213
                        if (written == rem)
 
214
                                break;
 
215
                        if (written > 0) {
 
216
                                buf += written;
 
217
                                rem -= written;
 
218
                        }
 
219
                } while (written >= 0 || errno == EINTR);
 
220
                if (written < 0) {
 
221
                        perror(OUT_PRE "Error writing to uinput device");
 
222
                        exit(BTNX_ERROR_WRITE_UINPUT);
 
223
                }
 
224
 
147
225
        }
148
226
        else
149
227
        {
150
228
                event.type = EV_KEY;
151
229
                event.code = bev->keycode;
152
230
                event.value = bev->pressed;
153
 
                ignore = write(fd, &event, sizeof(event));
 
231
 
 
232
                rem = sizeof(event);
 
233
                buf = (unsigned char *)&event;
 
234
                do {
 
235
                        written = write(fd, buf, rem);
 
236
                        if (written == rem)
 
237
                                break;
 
238
                        if (written > 0) {
 
239
                                buf += written;
 
240
                                rem -= written;
 
241
                        }
 
242
                } while (written >= 0 || errno == EINTR);
 
243
                if (written < 0) {
 
244
                        perror(OUT_PRE "Error writing to uinput device");
 
245
                        exit(BTNX_ERROR_WRITE_UINPUT);
 
246
                }
 
247
 
154
248
        }
155
249
        
156
250
        event.type = EV_SYN;
157
251
        event.code = SYN_REPORT;
158
 
        event.value = 0;
159
 
        ignore = write(fd, &event, sizeof(event));
 
252
        event.value = 0;
 
253
 
 
254
 
 
255
        rem = sizeof(event);
 
256
        buf = (unsigned char *)&event;
 
257
        do {
 
258
                written = write(fd, buf, rem);
 
259
                if (written == rem)
 
260
                        break;
 
261
                if (written > 0) {
 
262
                        buf += written;
 
263
                        rem -= written;
 
264
                }
 
265
        } while (written >= 0 || errno == EINTR);
 
266
        if (written < 0) {
 
267
                perror(OUT_PRE "Error writing to uinput device");
 
268
                exit(BTNX_ERROR_WRITE_UINPUT);
 
269
        }
 
270
 
160
271
}
161
272
 
162
273
/* Send a key combo event, either press or release */