summaryrefslogtreecommitdiff
path: root/MouseMate.c
blob: 3d972d3a2b3781d948a22489fbe8fdc506ae2d8e (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
 * mousemate plugin for VDR
 *
 * MouseMate.c - MouseMate control class
 *
 *
 * This code is distributed under the terms and conditions of the
 * GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
 */

#include <linux/input.h>
#include <string.h>
#include <errno.h>
#include <iostream>

#include "MouseMate.h"
#include "ButtonObserver.h"

// buffer size for reading events
const int BUFFER_SIZE=32;

#ifdef DEBUG
const char * strContextsDebug[kContexts] =
{
    "Normal",
    "Menu",
    "Replay",
    "Replay DVD",
    "Replay MP3",
    "Replay CDDA",
    "Replay IMAGE",
    "Normal alternative",
    "Menu alternative",
    "Replay alternative",
    "Replay DVD alternative",
    "Replay MP3 alternative",
    "Replay CDDA alternative",
    "Replay IMAGE alternative"
};

const char * strEventsDebug[kEvents] =
{
    "TurnWheelLeft",
    "TurnWheelRight",
    "TurnXLeft",
    "TurnXRight",
    "TurnYLeft",
    "TurnYRight",
    "LeftClick",
    "LeftDoubleClick",
    "LeftPushedTurnLeft",
    "LeftPushedTurnRight",
    "MiddleClick",
    "MiddleDoubleClick",
    "MiddlePushedTurnLeft",
    "MiddlePushedTurnRight",
    "RightClick",
    "RightDoubleClick",
    "RightPushedTurnLeft",
    "RightPushedTurnRight",
    "SidePushedTurnLeft",
    "SidePushedTurnRight",
    "SideClick",
    "SideDoubleClick",
    "ExtraClick",
    "ExtraDoubleClick",
    "ExtraPushedTurnLeft",
    "ExtraPushedTurnRight",
    "ForwardClick",
    "ForwardDoubleClick",
    "ForwardPushedTurnLeft",
    "ForwardPushedTurnRight",
    "BackClick",
    "BackDoubleClick",
    "BackPushedTurnLeft",
    "BackPushedTurnRight",
    "TaskClick",
    "TaskDoubleClick"
    "TaskPushedTurnLeft",
    "TaskPushedTurnRight"
};
#endif

cMouseMate * MouseMate;

cMouseMate::cMouseMate()
:pushed(false),
lastPush(0),
offset(0),
offsetX(0),
offsetY(0),
click(false),
turn(false),
toggle(false),
toggletime(0),
btnCount(0),
active(false)
{
    fd = open(MouseMateSetup.device, O_RDONLY);
    if (fd < 0)
    {
        esyslog("mousemate: unable to open %s: %s\n", MouseMateSetup.device, strerror(errno));
        fd = -1;
        return;
    }

    // Check if same EV_VERSION is used, because of hardcoded values below
    if (EV_VERSION!=0x010000)
    {
        esyslog("mousemate: Warning this Plugin is written for another EV_VERSION!"
            "mousemate: This may cause incompatibility!");
    }

    uint8_t evtype_bitmask[KEY_MAX/8+1];
    int yalv;
    memset(evtype_bitmask, 0, sizeof(evtype_bitmask));

    if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(evtype_bitmask)), evtype_bitmask) < 0)
    {
        esyslog("mousemate: %s: EVIOCGBIT failed: %s\n", MouseMateSetup.device, strerror(errno));
        return;
    }

    for (yalv = BTN_MOUSE; yalv < BTN_MOUSE+9; yalv++)
    {
        if (test_bit(yalv, evtype_bitmask))
        {
            /* this means that the bit is set in the event types list */
            switch (yalv)
            {
                case BTN_LEFT :
                #ifdef DEBUG
                    dsyslog("mousemate: button found BTN_LEFT\n");
                #endif
                    buttonObservers[0].Init(BTN_LEFT, kEventLeftClick, kEventLeftDoubleClick, kEventLeftPushedTurnLeft, kEventLeftPushedTurnRight);
                    btnCount++;
                    break;
                case BTN_RIGHT :
                #ifdef DEBUG
                    dsyslog("mousemate: button found BTN_RIGHT\n");
                #endif
                    buttonObservers[1].Init(BTN_RIGHT, kEventRightClick, kEventRightDoubleClick,kEventRightPushedTurnLeft, kEventRightPushedTurnRight);
                    btnCount++;
                    break;
                case BTN_MIDDLE :
                #ifdef DEBUG
                    dsyslog("mousemate: button found BTN_MIDDLE\n");
                #endif
                    buttonObservers[2].Init(BTN_MIDDLE, kEventMiddleClick, kEventMiddleDoubleClick, kEventMiddlePushedTurnLeft, kEventMiddlePushedTurnRight);
                    btnCount++;
                    break;
                case BTN_SIDE :
                #ifdef DEBUG
                    dsyslog("mousemate: button found TN_SIDE\n");
                #endif
                    buttonObservers[3].Init(BTN_SIDE, kEventSideClick, kEventSideDoubleClick, kEventSidePushedTurnLeft, kEventSidePushedTurnRight);
                    btnCount++;
                    break;
                case BTN_EXTRA :
                #ifdef DEBUG
                    dsyslog("mousemate: button found BTN_EXTRA\n");
                #endif
                    buttonObservers[4].Init(BTN_EXTRA, kEventExtraClick, kEventExtraDoubleClick, kEventExtraPushedTurnLeft, kEventExtraPushedTurnRight);
                    btnCount++;
                    break;
                case BTN_FORWARD :
                #ifdef DEBUG
                    dsyslog("mousemate: button found BTN_FORWARD\n");
                #endif
                    buttonObservers[5].Init(BTN_FORWARD, kEventForwardClick, kEventForwardDoubleClick, kEventForwardPushedTurnLeft, kEventForwardPushedTurnRight);
                    btnCount++;
                    break;
                case BTN_BACK :
                #ifdef DEBUG
                    dsyslog("mousemate: button found BTN_BACK\n");
                #endif
                    buttonObservers[6].Init(BTN_BACK, kEventBackClick, kEventBackDoubleClick,kEventBackPushedTurnLeft, kEventBackPushedTurnRight);
                    btnCount++;
                    break;
                case BTN_TASK :
                #ifdef DEBUG
                    dsyslog("mousemate: button found BTN_TASK\n");
                #endif
                    buttonObservers[7].Init(BTN_TASK, kEventTaskClick, kEventTaskDoubleClick, kEventTaskPushedTurnLeft, kEventTaskPushedTurnRight);
                    btnCount++;
                    break;
            }
        }
    }

    #ifdef DEBUG
        dsyslog("mousemate: number of found buttons btnCount = %i\n",btnCount);
    #endif

    if (btnCount<3)
    {
        esyslog("mousemate: number of buttons < 3 \n"
            "mousemate: ******** plugin not started ******** \n" );
        return;
    }

    Monitor=new cMouseMateMonitor;
    Start();
}


cMouseMate::~cMouseMate()
{
    delete Monitor;
    active = false;
    close(fd);
    Cancel(3);
}


bool cMouseMate::IsButtonUsed(unsigned int buCo)
{
    return !buttonObservers[buCo-BTN_MOUSE].buttonCode ? false : true;
}


short int cMouseMate::Get_btnCount()
{
    return btnCount;
}


void cMouseMate::Action()
{
    active = true;
    dsyslog("MouseMate thread started (pid=%d)", getpid());

    struct input_event events[BUFFER_SIZE];
    int r;
    int num;
    int i;

    while (fd >= 0 && active)
    {
        LOCK_THREAD;
        if (cFile::FileReady(fd, 100))
        {
            r = safe_read(fd, events, sizeof(struct input_event) * BUFFER_SIZE);
            if (r > 0)
            {
                num = r / sizeof(struct input_event);
                for (i = 0; i < num; i++)
                {
                    ProcessInputEvent(&events[i]);
                }
            }
            else
            {
                LOG_ERROR;
            }
        }

        // controll more than one button with buttonObserver & for loop
        for (i=0; i < btnCount; i++)
        {
            if (buttonObservers[i].click && !buttonObservers[i].pushed)
            {
                uint64_t current = TimeMs();
                if (current - buttonObservers[i].lastPush > (uint64_t) MouseMateSetup.doubleClickTime)
                {
                    #ifdef DEBUG
                    dsyslog("mousemate: click (main loop)\n");
                    #endif
                    ProcessEvent(buttonObservers[i].clickEvent);
                    buttonObservers[i].click = false;
                }
            }

            // toggle?
            if (buttonObservers[i].pushed && !turn && MouseMateSetup.alternative)
            {
                uint64_t current = TimeMs();
                if (current - buttonObservers[i].lastPush > (uint64_t) MouseMateSetup.toggleClickTime)
                {
                    Toggle();
                    // clear events
                    struct input_event dummyevents[BUFFER_SIZE];
                    r = safe_read(fd, dummyevents, sizeof(struct input_event) * BUFFER_SIZE);
                }
            }
        }

        // handle automatic toggle off after "toggleOffTime" since last event
        if(toggle && MouseMateSetup.autotoggleoff && MouseMateSetup.alternative)
        {
            uint64_t current = TimeMs();
            if (current - toggletime > (uint64_t) MouseMateSetup.toggleOffTime  )
            {
                Toggle();
                #ifdef DEBUG
                dsyslog("mousemate: toggle inactive automatically after %i ms \n",  MouseMateSetup.toggleOffTime);
                #endif
                // clear events
                struct input_event dummyevents[BUFFER_SIZE];
                r = safe_read(fd, dummyevents, sizeof(struct input_event) * BUFFER_SIZE);
            }
        }
    }
    dsyslog("MouseMate thread ended (pid=%d)", getpid());
}


void cMouseMate::Toggle()
{
    if (toggle)
    {
        toggle= false;
        #ifdef DEBUG
           dsyslog("mousemate: toggle inactive \n");
        #endif
    }
    else
    {
        toggle=true;
        toggletime=TimeMs();
        #ifdef DEBUG
           dsyslog("mousemate: toggle active after %i ms pushed button\n", MouseMateSetup.toggleClickTime);
        #endif
    }

    // controll more than one button with buttonObservers & for loop
    int i;
    for (i=0; i<btnCount; i++)
    {
        buttonObservers[i].pushed=false;
        buttonObservers[i].click=false;
    }

    // set scroll lock led if led = true
    if (MouseMateSetup.led)
    {
        int fc;
        fc = open(MouseMateSetup.cdevice, O_RDWR);
        // can console device be opened?
        if (fc)
        {
            struct input_event ev;
            ev.type = EV_LED;
            ev.code = LED_SCROLLL;
            ev.value = toggle;
            if (write(fc, &ev, sizeof(struct input_event)) < 0)
            {
               esyslog("mousemate: unable to write to console device %s: %s\n", MouseMateSetup.cdevice, strerror(errno));
               return;
            }
            close(fc);
        }
        else
        {
            esyslog("mousemate: unable to open console device %s: %s\n", MouseMateSetup.cdevice, strerror(errno));
            fc = -1;
            return;
        }
    }
}


void cMouseMate::ProcessInputEvent(struct input_event * ev)
{
    bool pushed=false;
    int button=2;

    switch (ev->type)
    {
        case EV_REL:
            if (ev->code == Wheel)
            {
                turn = true;
                offset += (int) ev->value;
                for (int i=0; i < btnCount; i++)
                {
                    if (buttonObservers[i].pushed)
                    {
                        pushed=true;
                        button=i;
                    }
                }
                if (pushed)
                {
                    if ((int) ev->value < 0)
                    {
                        while (offset <= -MouseMateSetup.sensitivity)
                        {
                            ProcessEvent(buttonObservers[button].pushedTurnLeftEvent);
                            offset += MouseMateSetup.sensitivity;
                        }
                    }
                    if ((int) ev->value > 0)
                    {
                        while (offset >= MouseMateSetup.sensitivity)
                        {
                            ProcessEvent(buttonObservers[button].pushedTurnRightEvent);
                            offset -= MouseMateSetup.sensitivity;
                        }
                    }
                }
                else
                {
                    for (int i=0; i < btnCount; i++)
                    {
                        if (buttonObservers[i].click)
                        {
                            #ifdef DEBUG
                                dsyslog("mousemate: click (ProcessInputEvent)\n");
                            #endif
                            ProcessEvent(buttonObservers[i].clickEvent);
                            buttonObservers[i].click = false;
                        }
                    }
                    if ((int) ev->value < 0)
                    {
                        while (offset <= -MouseMateSetup.sensitivity)
                        {
                            ProcessEvent(kEventTurnWheelLeft);
                            offset += MouseMateSetup.sensitivity;
                        }
                    }
                    if ((int) ev->value > 0)
                    {
                        while (offset >= MouseMateSetup.sensitivity)
                        {
                            ProcessEvent(kEventTurnWheelRight);
                            offset -= MouseMateSetup.sensitivity;
                        }
                    }
                }
            }

            if(MouseMateSetup.extraturns)
            {
                if (ev->code == REL_X)
                {
                    offsetX += (int) ev->value;
                    if ((int) ev->value < 0)
                    {
                        while (offsetX <= -MouseMateSetup.sensitivityX)
                        {
                            ProcessEvent(kEventTurnXLeft);
                            offsetX += MouseMateSetup.sensitivityX;
                        }
                    }
                    if ((int) ev->value > 0)
                    {
                        while (offsetX >= MouseMateSetup.sensitivityX)
                        {
                            ProcessEvent(kEventTurnXRight);
                            offsetX -= MouseMateSetup.sensitivityX;
                        }
                    }
                }
                if (ev->code == REL_Y)
                {
                    offsetY += (int) ev->value;
                    if ((int) ev->value < 0)
                    {
                        while (offsetY <= -MouseMateSetup.sensitivityY)
                        {
                            ProcessEvent(kEventTurnYLeft);
                            offsetY += MouseMateSetup.sensitivityY;
                        }
                    }
                    if ((int) ev->value > 0)
                    {
                        while (offsetY >= MouseMateSetup.sensitivityY)
                        {
                            ProcessEvent(kEventTurnYRight);
                            offsetY -= MouseMateSetup.sensitivityY;
                        }
                    }
                }
            }
            break;
        case EV_KEY:

            for (int i=0; i<btnCount; i++)
            {
                if (ev->code == buttonObservers[i].buttonCode)
                {
                    if (ev->value)
                    {
                        buttonObservers[i].pushed = true;
                        turn = false;
                        offset = 0;
                        buttonObservers[i].lastPush = TimeMs();
                    }
                    else
                    {
                        if (buttonObservers[i].click)
                        {
                            #ifdef DEBUG
                                dsyslog("mousemate: double click\n");
                            #endif
                            ProcessEvent(buttonObservers[i].doubleClickEvent);
                            buttonObservers[i].click = false;
                        }
                        else
                        {
                            if (!turn)
                                buttonObservers[i].click = true;
                        }
                        buttonObservers[i].pushed = false;
                        turn = false;
                        offset = 0;
                    }
                }
            }
            break;
    }
    #ifdef EVENTCODE_SNIFFER
    printf("mousemate: ev->type  = 0x%04x\n", ev->type);
    printf("mousemate: ev->code  = 0x%04x\n", ev->code);
    printf("mousemate: ev->value = %i\n", int(ev->value));
    printf("-----------------------------\n");
    #endif
}


void cMouseMate::ProcessEvent(int event)
{
    int context= Monitor->GetContext(toggle);

    if (!toggle)
    {
        SendKey(MouseMateSetup.keys[context][event]);
    }
    else
    {
        toggletime=TimeMs();                      // updating toggletime for key is pressed in toggle mode
        SendKey(MouseMateSetup.keys[context][event]);
    }

    #ifdef DEBUG
        dsyslog("mousemate: toggle=%i, event=%s, context=%s\n",toggle,
                 strEventsDebug[event],strContextsDebug[context]);
        eKeys keyNr=MouseMateSetup.keys[context][event];
        dsyslog("mousemate: sent key= nr %i, %s\n",keyNr, cKey::ToString(keyNr));
    #endif
}


void cMouseMate::SendKey(eKeys key)
{
    if (key != kNone)
        cRemote::Put(key);
}