Tizen 2.0 Release
[framework/connectivity/bluez.git] / input / fakehid.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34
35 #include <bluetooth/bluetooth.h>
36
37 #include <glib.h>
38
39 #include "../src/adapter.h"
40 #include "../src/device.h"
41
42 #include "log.h"
43 #include "device.h"
44 #include "fakehid.h"
45 #include "uinput.h"
46
47 enum ps3remote_special_keys {
48         PS3R_BIT_PS = 0,
49         PS3R_BIT_ENTER = 3,
50         PS3R_BIT_L2 = 8,
51         PS3R_BIT_R2 = 9,
52         PS3R_BIT_L1 = 10,
53         PS3R_BIT_R1 = 11,
54         PS3R_BIT_TRIANGLE = 12,
55         PS3R_BIT_CIRCLE = 13,
56         PS3R_BIT_CROSS = 14,
57         PS3R_BIT_SQUARE = 15,
58         PS3R_BIT_SELECT = 16,
59         PS3R_BIT_L3 = 17,
60         PS3R_BIT_R3 = 18,
61         PS3R_BIT_START = 19,
62         PS3R_BIT_UP = 20,
63         PS3R_BIT_RIGHT = 21,
64         PS3R_BIT_DOWN = 22,
65         PS3R_BIT_LEFT = 23,
66 };
67
68 static unsigned int ps3remote_bits[] = {
69         [PS3R_BIT_ENTER] = 0x0b,
70         [PS3R_BIT_PS] = 0x43,
71         [PS3R_BIT_SQUARE] = 0x5f,
72         [PS3R_BIT_CROSS] = 0x5e,
73         [PS3R_BIT_CIRCLE] = 0x5d,
74         [PS3R_BIT_TRIANGLE] = 0x5c,
75         [PS3R_BIT_R1] = 0x5b,
76         [PS3R_BIT_L1] = 0x5a,
77         [PS3R_BIT_R2] = 0x59,
78         [PS3R_BIT_L2] = 0x58,
79         [PS3R_BIT_LEFT] = 0x57,
80         [PS3R_BIT_DOWN] = 0x56,
81         [PS3R_BIT_RIGHT] = 0x55,
82         [PS3R_BIT_UP] = 0x54,
83         [PS3R_BIT_START] = 0x53,
84         [PS3R_BIT_R3] = 0x52,
85         [PS3R_BIT_L3] = 0x51,
86         [PS3R_BIT_SELECT] = 0x50,
87 };
88
89 static unsigned int ps3remote_keymap[] = {
90         [0x16] = KEY_EJECTCD,
91         [0x64] = KEY_AUDIO,
92         [0x65] = KEY_ANGLE,
93         [0x63] = KEY_SUBTITLE,
94         [0x0f] = KEY_CLEAR,
95         [0x28] = KEY_TIME,
96         [0x00] = KEY_1,
97         [0x01] = KEY_2,
98         [0x02] = KEY_3,
99         [0x03] = KEY_4,
100         [0x04] = KEY_5,
101         [0x05] = KEY_6,
102         [0x06] = KEY_7,
103         [0x07] = KEY_8,
104         [0x08] = KEY_9,
105         [0x09] = KEY_0,
106         [0x81] = KEY_RED,
107         [0x82] = KEY_GREEN,
108         [0x80] = KEY_BLUE,
109         [0x83] = KEY_YELLOW,
110         [0x70] = KEY_INFO,              /* display */
111         [0x1a] = KEY_MENU,              /* top menu */
112         [0x40] = KEY_CONTEXT_MENU,      /* pop up/menu */
113         [0x0e] = KEY_ESC,               /* return */
114         [0x5c] = KEY_OPTION,            /* options/triangle */
115         [0x5d] = KEY_BACK,              /* back/circle */
116         [0x5f] = KEY_SCREEN,            /* view/square */
117         [0x5e] = BTN_0,                 /* cross */
118         [0x54] = KEY_UP,
119         [0x56] = KEY_DOWN,
120         [0x57] = KEY_LEFT,
121         [0x55] = KEY_RIGHT,
122         [0x0b] = KEY_ENTER,
123         [0x5a] = BTN_TL,                /* L1 */
124         [0x58] = BTN_TL2,               /* L2 */
125         [0x51] = BTN_THUMBL,            /* L3 */
126         [0x5b] = BTN_TR,                /* R1 */
127         [0x59] = BTN_TR2,               /* R2 */
128         [0x52] = BTN_THUMBR,            /* R3 */
129         [0x43] = KEY_HOMEPAGE,          /* PS button */
130         [0x50] = KEY_SELECT,
131         [0x53] = BTN_START,
132         [0x33] = KEY_REWIND,            /* scan back */
133         [0x32] = KEY_PLAY,
134         [0x34] = KEY_FORWARD,           /* scan forward */
135         [0x30] = KEY_PREVIOUS,
136         [0x38] = KEY_STOP,
137         [0x31] = KEY_NEXT,
138         [0x60] = KEY_FRAMEBACK,         /* slow/step back */
139         [0x39] = KEY_PAUSE,
140         [0x61] = KEY_FRAMEFORWARD,      /* slow/step forward */
141         [0xff] = KEY_MAX,
142 };
143
144 static int ps3remote_decode(char *buff, int size, unsigned int *value)
145 {
146         static unsigned int lastkey = 0;
147         static unsigned int lastmask = 0;
148         unsigned int i, mask;
149         int retval;
150         guint8 key;
151
152         if (size < 12) {
153                 error("Got a shorter packet! (size %i)\n", size);
154                 return KEY_RESERVED;
155         }
156
157         mask = (buff[2] << 16) + (buff[3] << 8) + buff[4];
158         key = buff[5];
159
160         /* first, check flags */
161         for (i = 0; i < 24; i++) {
162                 if ((lastmask & (1 << i)) == (mask & (1 << i)))
163                         continue;
164                 if (ps3remote_bits[i] == 0)
165                         goto error;
166                 retval = ps3remote_keymap[ps3remote_bits[i]];
167                 if (mask & (1 << i))
168                         /* key pressed */
169                         *value = 1;
170                 else
171                         /* key released */
172                         *value = 0;
173
174                 goto out;
175         }
176
177         *value = buff[11];
178         if (buff[11] == 1) {
179                 retval = ps3remote_keymap[key];
180         } else
181                 retval = lastkey;
182
183         if (retval == KEY_RESERVED)
184                 goto error;
185         if (retval == KEY_MAX)
186                 return retval;
187
188         lastkey = retval;
189
190 out:
191         fflush(stdout);
192
193         lastmask = mask;
194
195         return retval;
196
197 error:
198         error("ps3remote: unrecognized sequence [%#x][%#x][%#x][%#x] [%#x],"
199                         "last: [%#x][%#x][%#x][%#x]",
200                         buff[2], buff[3], buff[4], buff[5], buff[11],
201                                 lastmask >> 16, lastmask >> 8 & 0xff,
202                                                 lastmask & 0xff, lastkey);
203         return -1;
204 }
205
206 static gboolean ps3remote_event(GIOChannel *chan, GIOCondition cond,
207                                 gpointer data)
208 {
209         struct fake_input *fake = data;
210         struct uinput_event event;
211         unsigned int key, value = 0;
212         ssize_t size;
213         char buff[50];
214         int fd;
215
216         if (cond & G_IO_NVAL)
217                 return FALSE;
218
219         if (cond & (G_IO_HUP | G_IO_ERR)) {
220                 error("Hangup or error on rfcomm server socket");
221                 goto failed;
222         }
223
224         fd = g_io_channel_unix_get_fd(chan);
225
226         memset(buff, 0, sizeof(buff));
227         size = read(fd, buff, sizeof(buff));
228         if (size < 0) {
229                 error("IO Channel read error");
230                 goto failed;
231         }
232
233         key = ps3remote_decode(buff, size, &value);
234         if (key == KEY_RESERVED) {
235                 error("Got invalid key from decode");
236                 goto failed;
237         } else if (key == KEY_MAX)
238                 return TRUE;
239
240         memset(&event, 0, sizeof(event));
241         gettimeofday(&event.time, NULL);
242         event.type = EV_KEY;
243         event.code = key;
244         event.value = value;
245         if (write(fake->uinput, &event, sizeof(event)) != sizeof(event)) {
246                 error("Error writing to uinput device");
247                 goto failed;
248         }
249
250         memset(&event, 0, sizeof(event));
251         gettimeofday(&event.time, NULL);
252         event.type = EV_SYN;
253         event.code = SYN_REPORT;
254         if (write(fake->uinput, &event, sizeof(event)) != sizeof(event)) {
255                 error("Error writing to uinput device");
256                 goto failed;
257         }
258
259         return TRUE;
260
261 failed:
262         ioctl(fake->uinput, UI_DEV_DESTROY);
263         close(fake->uinput);
264         fake->uinput = -1;
265         g_io_channel_unref(fake->io);
266
267         return FALSE;
268 }
269
270 static int ps3remote_setup_uinput(struct fake_input *fake,
271                                                 struct fake_hid *fake_hid)
272 {
273         struct uinput_dev dev;
274         int i;
275
276         fake->uinput = open("/dev/input/uinput", O_RDWR);
277         if (fake->uinput < 0) {
278                 fake->uinput = open("/dev/uinput", O_RDWR);
279                 if (fake->uinput < 0) {
280                         fake->uinput = open("/dev/misc/uinput", O_RDWR);
281                         if (fake->uinput < 0) {
282                                 error("Error opening uinput device file");
283                                 return 1;
284                         }
285                 }
286         }
287
288         memset(&dev, 0, sizeof(dev));
289         snprintf(dev.name, sizeof(dev.name), "%s", "PS3 Remote Controller");
290         dev.id.bustype = BUS_BLUETOOTH;
291         dev.id.vendor = fake_hid->vendor;
292         dev.id.product = fake_hid->product;
293
294         if (write(fake->uinput, &dev, sizeof(dev)) != sizeof(dev)) {
295                 error("Error creating uinput device");
296                 goto err;
297         }
298
299         /* enabling key events */
300         if (ioctl(fake->uinput, UI_SET_EVBIT, EV_KEY) < 0) {
301                 error("Error enabling uinput device key events");
302                 goto err;
303         }
304
305         /* enabling keys */
306         for (i = 0; i < 256; i++)
307                 if (ps3remote_keymap[i] != KEY_RESERVED)
308                         if (ioctl(fake->uinput, UI_SET_KEYBIT,
309                                                 ps3remote_keymap[i]) < 0) {
310                                 error("Error enabling uinput key %i",
311                                                         ps3remote_keymap[i]);
312                                 goto err;
313                         }
314
315         /* creating the device */
316         if (ioctl(fake->uinput, UI_DEV_CREATE) < 0) {
317                 error("Error creating uinput device");
318                 goto err;
319         }
320
321         return 0;
322
323 err:
324         close(fake->uinput);
325         return 1;
326 }
327
328 static gboolean fake_hid_common_connect(struct fake_input *fake, GError **err)
329 {
330         return TRUE;
331 }
332
333 static int fake_hid_common_disconnect(struct fake_input *fake)
334 {
335         return 0;
336 }
337
338 static struct fake_hid fake_hid_table[] = {
339         /* Sony PS3 remote device */
340         {
341                 .vendor         = 0x054c,
342                 .product        = 0x0306,
343                 .connect        = fake_hid_common_connect,
344                 .disconnect     = fake_hid_common_disconnect,
345                 .event          = ps3remote_event,
346                 .setup_uinput   = ps3remote_setup_uinput,
347                 .devices        = NULL,
348         },
349
350         { },
351 };
352
353 static inline int fake_hid_match_device(uint16_t vendor, uint16_t product,
354                                                         struct fake_hid *fhid)
355 {
356         return vendor == fhid->vendor && product == fhid->product;
357 }
358
359 struct fake_hid *get_fake_hid(uint16_t vendor, uint16_t product)
360 {
361         int i;
362
363         for (i = 0; fake_hid_table[i].vendor != 0; i++)
364                 if (fake_hid_match_device(vendor, product, &fake_hid_table[i]))
365                         return &fake_hid_table[i];
366
367         return NULL;
368 }
369
370 struct fake_input *fake_hid_connadd(struct fake_input *fake,
371                                                 GIOChannel *intr_io,
372                                                 struct fake_hid *fake_hid)
373 {
374         GList *l;
375         struct fake_input *old = NULL;
376
377         /* Look for an already setup device */
378         for (l = fake_hid->devices; l != NULL; l = l->next) {
379                 old = l->data;
380                 if (old->idev == fake->idev) {
381                         g_free(fake);
382                         fake = old;
383                         fake_hid->connect(fake, NULL);
384                         break;
385                 }
386                 old = NULL;
387         }
388
389         /* New device? Add it to the list of known devices,
390          * and create the uinput necessary */
391         if (old == NULL || old->uinput < 0) {
392                 if (fake_hid->setup_uinput(fake, fake_hid)) {
393                         error("Error setting up uinput");
394                         g_free(fake);
395                         return NULL;
396                 }
397         }
398
399         if (old == NULL)
400                 fake_hid->devices = g_list_append(fake_hid->devices, fake);
401
402         fake->io = g_io_channel_ref(intr_io);
403         g_io_channel_set_close_on_unref(fake->io, TRUE);
404         g_io_add_watch(fake->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
405                                         (GIOFunc) fake_hid->event, fake);
406
407         return fake;
408 }