File: g15_plugin_uinput.c

package info (click to toggle)
g15daemon 1.9.5.3-8.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,916 kB
  • sloc: sh: 9,205; ansic: 3,197; perl: 115; makefile: 74
file content (338 lines) | stat: -rw-r--r-- 11,221 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
    This file is part of g15daemon.

    g15daemon is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    g15daemon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with g15daemon; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    (c) 2006-2008 Mike Lampard, Philip Lawatsch, and others
    
    $Revision: 388 $ -  $Date: 2008-01-02 12:57:03 +1030 (Wed, 02 Jan 2008) $ $Author: mlampard $
    
    UINPUT key processing plugin.  receives events and sends keycodes to the linux kernel.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <config.h>
#include <g15daemon.h>
#include <pwd.h>

#ifdef HAVE_CONFIG_H
#ifdef HAVE_LINUX_UINPUT_H
#include <linux/input.h>
#include <linux/uinput.h>

#include <libg15.h>

static int uinp_fd = -1;
static config_section_t *uinput_cfg=NULL;
static int map_Lkeys = 0;

#define GKEY_OFFSET 167
#define MKEY_OFFSET 185
#define LKEY_OFFSET 189

#define G15KEY_DOWN 1
#define G15KEY_UP 0

static int g15_init_uinput(void *plugin_args) {
    
    int i=0;
    char *custom_filename;
    g15daemon_t *masterlist = (g15daemon_t*) plugin_args;
    struct uinput_user_dev uinp;
    static const char *uinput_device_fn[] = { "/dev/uinput", "/dev/input/uinput","/dev/misc/uinput",0};
    
    uinput_cfg = g15daemon_cfg_load_section(masterlist,"Keyboard OS Mapping (uinput)");
    custom_filename = g15daemon_cfg_read_string(uinput_cfg, "device",(char*)uinput_device_fn[1]);
    map_Lkeys=g15daemon_cfg_read_int(uinput_cfg, "Lkeys.mapped",0);
    
    seteuid(0);
    setegid(0);
    while (uinput_device_fn[i] && (uinp_fd = open(uinput_device_fn[i],O_RDWR))<0){
        ++i;
    }
    if(uinp_fd<0) {	/* try reading the users preference in the config */
        uinp_fd = open(custom_filename,O_RDWR);
    }
    if(uinp_fd<0){
        g15daemon_log(LOG_ERR,"Unable to open UINPUT device.  Please ensure the uinput driver is loaded into the kernel and that you have permission to open the device.");
        return -1;
    }
    /* all other processes/threads should be seteuid nobody */
     seteuid(masterlist->nobody->pw_uid);
     setegid(masterlist->nobody->pw_gid);
    
    
    memset(&uinp,0,sizeof(uinp));
    strncpy(uinp.name, "G15 Extra Keys", UINPUT_MAX_NAME_SIZE);

#ifdef HAVE_UINPUT_USER_DEV_ID
    uinp.id.version = 4;
    uinp.id.bustype = BUS_USB;
#else
    uinp.idversion = 4;
    uinp.idbus = BUS_USB;
#endif 

    ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);

    for (i=0; i<256; ++i)
        ioctl(uinp_fd, UI_SET_KEYBIT, i);

    write(uinp_fd, &uinp, sizeof(uinp));
    
    if (ioctl(uinp_fd, UI_DEV_CREATE))
    {
        g15daemon_log(LOG_ERR,"Unable to create UINPUT device.");
        return -1;
    }
    return 0;
}

void g15_exit_uinput(void *plugin_args){
    ioctl(uinp_fd, UI_DEV_DESTROY);
    close(uinp_fd);
}


static void g15_uinput_keydown(unsigned char code)
{
    struct input_event event;
    memset(&event, 0, sizeof(event));

    event.type = EV_KEY;
    event.code = code;
    event.value = G15KEY_DOWN;
    
    write (uinp_fd, &event, sizeof(event));

    memset(&event, 0, sizeof(event));

    event.type = EV_SYN;
    event.code = SYN_REPORT;

    write (uinp_fd, &event, sizeof(event));
}

static void g15_uinput_keyup(unsigned char code)
{
    struct input_event event;
    memset(&event, 0, sizeof(event));

    event.type = EV_KEY;
    event.code = code;
    event.value = G15KEY_UP;
    
    write (uinp_fd, &event, sizeof(event));

    memset(&event, 0, sizeof(event));

    event.type = EV_SYN;
    event.code = SYN_REPORT;

    write (uinp_fd, &event, sizeof(event));
}

    void (*keyup)(unsigned char code) = &g15_uinput_keyup;
    void (*keydown)(unsigned char code) = &g15_uinput_keydown;
#else
    void keyup(unsigned char code) { printf("Extra Keys not supported due to missing Uinput.h\n"); }
    void keydown(unsigned char code) { printf("Extra Keys not supported due to missing Uinput.h\n"); }
#endif
#endif
    
static void g15_process_keys(g15daemon_t *masterlist, unsigned int currentkeys, unsigned int lastkeys)
{
    /* 'G' keys */
    if((currentkeys & G15_KEY_G1) && !(lastkeys & G15_KEY_G1))
        keydown(GKEY_OFFSET);
    else if(!(currentkeys & G15_KEY_G1) && (lastkeys & G15_KEY_G1))
        keyup(GKEY_OFFSET);

    if((currentkeys & G15_KEY_G2) && !(lastkeys & G15_KEY_G2))
        keydown(GKEY_OFFSET+1);
    else if(!(currentkeys & G15_KEY_G2) && (lastkeys & G15_KEY_G2))
        keyup(GKEY_OFFSET+1);

    if((currentkeys & G15_KEY_G3) && !(lastkeys & G15_KEY_G3))
        keydown(GKEY_OFFSET+2);
    else if(!(currentkeys & G15_KEY_G3) && (lastkeys & G15_KEY_G3))
        keyup(GKEY_OFFSET+2);

    if((currentkeys & G15_KEY_G4) && !(lastkeys & G15_KEY_G4))
        keydown(GKEY_OFFSET+3);
    else if(!(currentkeys & G15_KEY_G4) && (lastkeys & G15_KEY_G4))
        keyup(GKEY_OFFSET+3);

    if((currentkeys & G15_KEY_G5) && !(lastkeys & G15_KEY_G5))
        keydown(GKEY_OFFSET+4);
    else if(!(currentkeys & G15_KEY_G5) && (lastkeys & G15_KEY_G5))
        keyup(GKEY_OFFSET+4);

    if((currentkeys & G15_KEY_G6) && !(lastkeys & G15_KEY_G6))
        keydown(GKEY_OFFSET+5);
    else if(!(currentkeys & G15_KEY_G6) && (lastkeys & G15_KEY_G6))
        keyup(GKEY_OFFSET+5);

    if((currentkeys & G15_KEY_G7) && !(lastkeys & G15_KEY_G7))
        keydown(GKEY_OFFSET+6);
    else if(!(currentkeys & G15_KEY_G7) && (lastkeys & G15_KEY_G7))
        keyup(GKEY_OFFSET+6);

    if((currentkeys & G15_KEY_G8) && !(lastkeys & G15_KEY_G8))
        keydown(GKEY_OFFSET+7);
    else if(!(currentkeys & G15_KEY_G8) && (lastkeys & G15_KEY_G8))
        keyup(GKEY_OFFSET+7);

    if((currentkeys & G15_KEY_G9) && !(lastkeys & G15_KEY_G9))
        keydown(GKEY_OFFSET+8);
    else if(!(currentkeys & G15_KEY_G9) && (lastkeys & G15_KEY_G9))
        keyup(GKEY_OFFSET+8);

    if((currentkeys & G15_KEY_G10) && !(lastkeys & G15_KEY_G10))
        keydown(GKEY_OFFSET+9);
    else if(!(currentkeys & G15_KEY_G10) && (lastkeys & G15_KEY_G10))
        keyup(GKEY_OFFSET+9);

    if((currentkeys & G15_KEY_G11) && !(lastkeys & G15_KEY_G11))
        keydown(GKEY_OFFSET+10);
    else if(!(currentkeys & G15_KEY_G11) && (lastkeys & G15_KEY_G11))
        keyup(GKEY_OFFSET+10);

    if((currentkeys & G15_KEY_G12) && !(lastkeys & G15_KEY_G12))
        keydown(GKEY_OFFSET+11);
    else if(!(currentkeys & G15_KEY_G12) && (lastkeys & G15_KEY_G12))
        keyup(GKEY_OFFSET+11);

    if((currentkeys & G15_KEY_G13) && !(lastkeys & G15_KEY_G13))
        keydown(GKEY_OFFSET+12);
    else if(!(currentkeys & G15_KEY_G13) && (lastkeys & G15_KEY_G13))
        keyup(GKEY_OFFSET+12);

    if((currentkeys & G15_KEY_G14) && !(lastkeys & G15_KEY_G14))
        keydown(GKEY_OFFSET+13);
    else if(!(currentkeys & G15_KEY_G14) && (lastkeys & G15_KEY_G14))
        keyup(GKEY_OFFSET+13);

    if((currentkeys & G15_KEY_G15) && !(lastkeys & G15_KEY_G15))
        keydown(GKEY_OFFSET+14);
    else if(!(currentkeys & G15_KEY_G15) && (lastkeys & G15_KEY_G15))
        keyup(GKEY_OFFSET+14);

    if((currentkeys & G15_KEY_G16) && !(lastkeys & G15_KEY_G16))
        keydown(GKEY_OFFSET+15);
    else if(!(currentkeys & G15_KEY_G16) && (lastkeys & G15_KEY_G16))
        keyup(GKEY_OFFSET+15);

    if((currentkeys & G15_KEY_G17) && !(lastkeys & G15_KEY_G17))
        keydown(GKEY_OFFSET+16);
    else if(!(currentkeys & G15_KEY_G17) && (lastkeys & G15_KEY_G17))
        keyup(GKEY_OFFSET+16);

    if((currentkeys & G15_KEY_G18) && !(lastkeys & G15_KEY_G18))
        keydown(GKEY_OFFSET+17);
    else if(!(currentkeys & G15_KEY_G18) && (lastkeys & G15_KEY_G18))
        keyup(GKEY_OFFSET+17);

    /* 'M' keys */

    if((currentkeys & G15_KEY_M1) && !(lastkeys & G15_KEY_M1))
        keydown(MKEY_OFFSET);
    else if(!(currentkeys & G15_KEY_M1) && (lastkeys & G15_KEY_M1))
        keyup(MKEY_OFFSET);

    if((currentkeys & G15_KEY_M2) && !(lastkeys & G15_KEY_M2))
        keydown(MKEY_OFFSET+1);
    else if(!(currentkeys & G15_KEY_M2) && (lastkeys & G15_KEY_M2))
        keyup(MKEY_OFFSET+1);

    if((currentkeys & G15_KEY_M3) && !(lastkeys & G15_KEY_M3))
        keydown(MKEY_OFFSET+2);
    else if(!(currentkeys & G15_KEY_M3) && (lastkeys & G15_KEY_M3))
        keyup(MKEY_OFFSET+2);

    if((currentkeys & G15_KEY_MR) && !(lastkeys & G15_KEY_MR))
        keydown(MKEY_OFFSET+3);
    else if(!(currentkeys & G15_KEY_MR) && (lastkeys & G15_KEY_MR))
        keyup(MKEY_OFFSET+3);
    
    if(map_Lkeys){
        /* 'L' keys...  */
        if((currentkeys & G15_KEY_L1) && !(lastkeys & G15_KEY_L1))
            keydown(LKEY_OFFSET);
        else if(!(currentkeys & G15_KEY_L1) && (lastkeys & G15_KEY_L1))
            keyup(LKEY_OFFSET);

        if((currentkeys & G15_KEY_L2) && !(lastkeys & G15_KEY_L2))
            keydown(LKEY_OFFSET+1);
        else if(!(currentkeys & G15_KEY_L2) && (lastkeys & G15_KEY_L2))
            keyup(LKEY_OFFSET+1);

        if((currentkeys & G15_KEY_L3) && !(lastkeys & G15_KEY_L3))
            keydown(LKEY_OFFSET+2);
        else if(!(currentkeys & G15_KEY_L3) && (lastkeys & G15_KEY_L3))
            keyup(LKEY_OFFSET+2);

        if((currentkeys & G15_KEY_L4) && !(lastkeys & G15_KEY_L4))
            keydown(LKEY_OFFSET+3);
        else if(!(currentkeys & G15_KEY_L4) && (lastkeys & G15_KEY_L4))
            keyup(LKEY_OFFSET+3);

        if((currentkeys & G15_KEY_L5) && !(lastkeys & G15_KEY_L5))
            keydown(LKEY_OFFSET+4);
        else if(!(currentkeys & G15_KEY_L5) && (lastkeys & G15_KEY_L5))
            keyup(LKEY_OFFSET+4);
    }
}


static int keyevents(plugin_event_t *myevent) {
    lcd_t *lcd = (lcd_t*) myevent->lcd;
    static int lastkeys;
    switch (myevent->event)
    {
        case G15_EVENT_KEYPRESS:{
            g15_process_keys(lcd->masterlist, myevent->value,lastkeys);
            lastkeys = myevent->value;
            break;
        }
        case G15_EVENT_VISIBILITY_CHANGED:
        case G15_EVENT_USER_FOREGROUND:
	case G15_EVENT_MLED:
        case G15_EVENT_BACKLIGHT:
        case G15_EVENT_CONTRAST:
        case G15_EVENT_REQ_PRIORITY:
        case G15_EVENT_CYCLE_PRIORITY:
        default:
            break;
    }
    return G15_PLUGIN_OK;
}


    /* if no exitfunc or eventhandler, member should be NULL */
plugin_info_t g15plugin_info[] = {
        /* TYPE, name, initfunc, updatefreq, exitfunc, eventhandler, initfunc */
   {G15_PLUGIN_CORE_OS_KB, "Linux UINPUT Keyboard Output"	, NULL, 500, (void*)g15_exit_uinput, (void*)keyevents, (void*)g15_init_uinput},
   {G15_PLUGIN_NONE,               ""          			, NULL,   0,   			NULL,            NULL,           NULL}
};