summaryrefslogtreecommitdiffstats
path: root/input_hal/src/input_udev_monitor.cpp
blob: 5d8cc6b73fafb514896d905f4c7e758ca0dd8f54 (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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
 * @copyright Copyright (c) 2017-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <libudev.h>
#include <native_service/ns_message_center_if.h>

#include "input_hal.h"
#include "input_hal_debug.h"
#include "input_hal_internal.h"
#include "input_drm.h"
#include "input_udev_monitor.h"

#define HAL_INPUT_UDEVMONITOR_THREADNAME "input_hal_udevm"
#define max_value(x, y) ((x) > (y) ? (x) : (y))

#define INPUT_VIRTUAL_INPUTDEVICE_NAME "/devices/virtual" /* Virtual device name created by SwitchManager */

// Enough name length to contain node name
#define INPUT_NODE_NAME_LENGTH 256

#define TRACKING_ID_NONE    -1

static struct InputUdevMonitorInfo g_input_udevmonitor_info;
static HANDLE g_sender_handle;
static bool g_input_touch_init_notify = false;
extern char* g_app_name;  /* app name */
bool g_break_from_watch = false;  /* watch thread loop break flag */
pthread_t g_udev_monitor_thread = -1;  /* udev monitor thread */

static void InputUdevMonitorTouchInitFinEventSend(int result) {
  struct TouchInitFinishInput msg;

  if (g_input_touch_init_notify) {
    return;
  }

  if (NULL == g_sender_handle) {
    g_sender_handle = McOpenSender(g_app_name);
  }
  msg.result = result;

  InputUtilMCSend(
      g_sender_handle, HAL_INPUT_SOURCE_NAME,
      HAL_INPUT_NOTIFY_TOUCH_INIT_FINISH, sizeof(msg), &msg);
  g_input_touch_init_notify = true;
}

static void InputUdevMonitorInputEventHandle(int fd, int device_type, int notify_id) {
  ssize_t   read_size;
  struct EventsPackageInput events = {0};

  read_size = read(fd, events.event, sizeof(events.event));
  if (read_size > 0) {
    events.count = read_size / sizeof(struct input_event);
    events.device_type = device_type;

    if (NULL == g_sender_handle) {
      g_sender_handle = McOpenSender(g_app_name);
    }

    InputUtilMCSend(
        g_sender_handle, HAL_INPUT_SOURCE_NAME,
        notify_id, sizeof(events), &events);
  }
}

static void InputUdevMonitorTouchEventHandle(int fd, int notify_id) {
  ssize_t   read_size;
  struct input_event event[HAL_INPUT_EVENT_COUNT] = {0};
  int touch_status;

  read_size = read(fd, event, sizeof(event));

  if (read_size > 0) {
    if (NULL == g_sender_handle) {
      g_sender_handle = McOpenSender(g_app_name);
    }

    int event_num = read_size / sizeof(struct input_event);

    for (int index = 0; index < event_num; ++index) {
      if (EV_ABS == event[index].type) {
        if (event[index].code == ABS_MT_TRACKING_ID) {
          if (TRACKING_ID_NONE == event[index].value) {
            touch_status = HAL_INPUT_TOUCH_RELEASE;
          } else {
            touch_status = HAL_INPUT_TOUCH_PRESS;
          }
          InputUtilMCSend(
              g_sender_handle, HAL_INPUT_SOURCE_NAME,
              notify_id, sizeof(touch_status), &touch_status);
        }
      }
    }
  }
}

static int InputUdevMonitorDeviceAssort(struct udev_device *dev) {
  const char *property;

  char touch_key_device_name[INPUT_NODE_NAME_LENGTH] = { 0 };
  GetKeyNameTouch(touch_key_device_name, sizeof(touch_key_device_name));
  const char *sysattr_name = udev_device_get_sysattr_value(dev, "name");
  if ((sysattr_name) &&
      (0 == strcmp(touch_key_device_name, sysattr_name))) {
    INPUT_LOG_TRACE("DeviceAssort : ESC_SW\n");
    return HAL_INPUT_DEVICE_TOUCH_ESCKEY;
  }

  char touch_device_name[INPUT_NODE_NAME_LENGTH] = { 0 };
  GetPanelNameTouch(touch_device_name, sizeof(touch_device_name));
  sysattr_name = udev_device_get_sysattr_value(dev, "name");
  if ((sysattr_name) &&
      (0 == strcmp(touch_device_name, sysattr_name))) {
    INPUT_LOG_TRACE("DeviceAssort : Touch\n");
    return HAL_INPUT_DEVICE_TOUCH;
  }

  property = udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD");
  if ((property) && (strcmp("1", property) == 0)) {
    INPUT_LOG_TRACE("DeviceAssort : KeyBoard\n");
    return HAL_INPUT_DEVICE_KEYBOARD;
  }

  property = udev_device_get_property_value(dev, "ID_INPUT_TABLET");
  if ((property) && (strcmp("1", property) == 0)) {
    INPUT_LOG_TRACE("DeviceAssort : Tablet\n");
    return HAL_INPUT_DEVICE_INVALID;
  }

  property = udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD");
  if ((property) && (strcmp("1", property) == 0)) {
    INPUT_LOG_TRACE("DeviceAssort : Touch pad\n");
    return HAL_INPUT_DEVICE_TABLET_FINGER;
  }

  return HAL_INPUT_DEVICE_INVALID;
}

static void InputUdevMonitorDeviceListOutput(void) {
  struct InputUtilList *p;

  INPUT_LOG_TRACE("OutputList >> start ======================\n");
  input_list_for_each(p, &g_input_udevmonitor_info.dev_list.list) {
    struct InputInputDeviceList *entry = input_list_entry(p, struct InputInputDeviceList, list);
    if (NULL != entry) {
      INPUT_LOG_TRACE("FD: %d Device=%s Assort=%d\n",
                     entry->fd, entry->device_node, entry->device_assort);
    }
  }
  INPUT_LOG_TRACE("OutputList << end ======================\n");
}

static void InputUdevMonitorDeviceListDelete(const char *node) {
  struct InputUtilList *p;

  input_list_for_each(p, &g_input_udevmonitor_info.dev_list.list) {
    struct InputInputDeviceList *entry = input_list_entry(p, struct InputInputDeviceList, list);
    if (strcmp(node, entry->device_node) == 0) {
      close(entry->fd);
      InputUtilListDelete(p);
      free(entry);
      break;
    }
  }
  InputUdevMonitorDeviceListOutput();
}

static int InputUdevMonitorDeviceListAdd(int fd, int device_assort, const char *node) {
  int   ret = HAL_INPUT_RET_NORMAL;
  struct InputInputDeviceList *p;

  p = (struct InputInputDeviceList *) malloc(sizeof(struct InputInputDeviceList));
  if (p) {
    p->fd = fd;
    p->device_assort = device_assort;

    if (strlen(node) < INPUT_DEVICE_NODE_LENGTH_MAX) {
      strncpy(p->device_node, node, sizeof(p->device_node) - 1);
      InputUdevMonitorDeviceListDelete(node);
      InputUtilListAdd(&p->list, &g_input_udevmonitor_info.dev_list.list);
    } else {
      ret = HAL_INPUT_RET_ERROR;
    }
  } else {
    ret = HAL_INPUT_RET_ERROR;
  }

  InputUdevMonitorDeviceListOutput();

  return ret;
}

static int InputUdevMonitorInputEventGrab(const char *node, struct udev_device *dev) {
  int fd, rtn;

  InputUdevMonitorDeviceListDelete(node);

  fd = open(node, O_RDONLY);

  if (fd < 0) {
    INPUT_ERROR_LOG("ERR: open %s errno=%d \n", node, errno);
    goto err_rtn;
  }

  rtn = ioctl(fd, EVIOCGRAB, 1);
  if (rtn) {
    INPUT_ERROR_LOG("ERR: ioctl grab %s \n", node);
    goto err_rtn_close;
  }
  INPUT_LOG_TRACE("%s Grab \n", node);

  /* Backup FD */
  rtn = InputUdevMonitorDeviceListAdd(fd, InputUdevMonitorDeviceAssort(dev), node);
  if (rtn) {
    goto err_rtn_close;
  }

  return HAL_INPUT_RET_NORMAL;

err_rtn_close:
  close(fd);
err_rtn:
  INPUT_LOG_TRACE("%s Grab impossible \n", node);
  return HAL_INPUT_RET_ERROR;
}

static void InputUdevMonitorFDSet(int *nfds, fd_set *fds) {
  struct InputUtilList *p;

  input_list_for_each(p, &g_input_udevmonitor_info.dev_list.list) {
    struct InputInputDeviceList *entry = input_list_entry(p, struct InputInputDeviceList, list);

    switch (entry->device_assort) {
      case HAL_INPUT_DEVICE_TOUCH_ESCKEY:
      case HAL_INPUT_DEVICE_TOUCH:
      case HAL_INPUT_DEVICE_KEYBOARD:
      case HAL_INPUT_DEVICE_TABLET_FINGER:
      FD_SET(entry->fd, fds);
      *nfds = max_value(*nfds, entry->fd);
      break;
      default:
      break;
    }
  }
}

static void InputUdevMonitorFDHandle(fd_set *fds) {
  struct InputUtilList *p;

  input_list_for_each(p, &g_input_udevmonitor_info.dev_list.list) {
    struct InputInputDeviceList *entry = input_list_entry(p, struct InputInputDeviceList, list);
    if (FD_ISSET(entry->fd, fds)) {
      int notify_id = 0;
      switch (entry->device_assort) {
      case HAL_INPUT_DEVICE_TOUCH_ESCKEY:
        notify_id = HAL_INPUT_NOTIFY_ESC_KEY;
        break;
      case HAL_INPUT_DEVICE_TOUCH:
        notify_id = HAL_INPUT_NOTIFY_TOUCH;
        break;
      case HAL_INPUT_DEVICE_KEYBOARD:
        notify_id = HAL_INPUT_NOTIFY_KEY_BOARD;
        break;
      case HAL_INPUT_DEVICE_TABLET_FINGER:
        notify_id = HAL_INPUT_NOTIFY_TABLET_FINGER;
        break;
      default:
        return;
      }
      if (notify_id == HAL_INPUT_NOTIFY_TOUCH) {
        InputUdevMonitorTouchEventHandle(entry->fd, notify_id);
      } else {
        InputUdevMonitorInputEventHandle(entry->fd, entry->device_assort, notify_id);
      }
    }
  }
}

static int InputUdevMonitorInit(void) {
  g_input_udevmonitor_info.udev = udev_new();
  if (NULL == g_input_udevmonitor_info.udev) {
    INPUT_ERROR_LOG("ERR: udev_new ret=%d \n", errno);
    return HAL_INPUT_RET_ERROR;
  }

  // create the udev monitor
  g_input_udevmonitor_info.monitor = udev_monitor_new_from_netlink(g_input_udevmonitor_info.udev, "udev");
  udev_monitor_filter_add_match_subsystem_devtype(g_input_udevmonitor_info.monitor, "input", NULL);

  // start receiving hotplug events
  udev_monitor_enable_receiving(g_input_udevmonitor_info.monitor);

  INPUT_INIT_LIST_HEAD(&g_input_udevmonitor_info.dev_list.list)

  return HAL_INPUT_RET_NORMAL;
}

static void InputUdevMonitorDeinit(void) {
    // destroy the udev monitor
    udev_monitor_unref(g_input_udevmonitor_info.monitor);
    // destroy the udev object
    udev_unref(g_input_udevmonitor_info.udev);

    if (g_sender_handle != NULL) {
      McClose(g_sender_handle);
      g_sender_handle = NULL;
    }

    g_input_touch_init_notify = false;
}

static int InputUdevMonitorDevicesGet(void) {
  int   ret;
  struct udev_enumerate *enumerate;
  struct udev_list_entry *devices, *dev_list_entry;
  char touch_device_name[INPUT_NODE_NAME_LENGTH] = { 0};
  const char* sysattr_name;
  bool input_touch_device_found = false;

  // Create a list of the devices in the 'usb_device' subsystem.
  enumerate = udev_enumerate_new(g_input_udevmonitor_info.udev);
  udev_enumerate_add_match_subsystem(enumerate, "input");
  udev_enumerate_scan_devices(enumerate);
  devices = udev_enumerate_get_list_entry(enumerate);
  GetPanelNameTouch(touch_device_name, sizeof(touch_device_name));

  // Enumerate device list
  udev_list_entry_foreach(dev_list_entry, devices) {
    const char *path, *node;

    // Get the filename of the /sys entry for the device and create a udev_device object (dev) representing it
    path = udev_list_entry_get_name(dev_list_entry);
    struct udev_device* dev = udev_device_new_from_syspath(g_input_udevmonitor_info.udev, path);
    if (NULL == dev) continue;

    // usb_device_get_devnode() returns the path to the device node itself in /dev.
    node = udev_device_get_devnode(dev);
    if (!node) {
      udev_device_unref(dev);
      continue;
    }

    // Filter device name is eventX
    if (strncmp("event", udev_device_get_sysname(dev), sizeof("event") -1) != 0) {
      udev_device_unref(dev);
      continue;
    }

    // virtual device
    if (strncmp(INPUT_VIRTUAL_INPUTDEVICE_NAME, udev_device_get_devpath(dev),
                sizeof(INPUT_VIRTUAL_INPUTDEVICE_NAME) - 1) == 0) {
      INPUT_LOG_TRACE("Found Virtual Device : %s \n", node);
      udev_device_unref(dev);
      continue;
    }

    // check parent is input
    struct udev_device* input_dev = udev_device_get_parent_with_subsystem_devtype(dev, "input", NULL);
    if (NULL == input_dev) {
      udev_device_unref(dev);
      continue;
    }

    sysattr_name = udev_device_get_sysattr_value(input_dev, "name");
    if (NULL == sysattr_name) {
      INPUT_ERROR_LOG("ERR: Unable to find sysattr \n");
      udev_device_unref(dev);
      continue;
    }

    // touchpanel device
    if (0 == strcmp(touch_device_name, sysattr_name)) {
      INPUT_LOG_TRACE("Found %s : %s \n", touch_device_name, path);
      int spec_reso_h;
      int spec_reso_v;
      GetPanelSpecResolutionInput(&spec_reso_h, &spec_reso_v);
      ret = ConfigTouch(udev_device_get_syspath(input_dev),
                        spec_reso_h, spec_reso_v);

      if (HAL_INPUT_RET_NORMAL == ret) {
        int status;
        ret = GetConfigStatusTouch(&status);
        if (HAL_INPUT_RET_NORMAL == ret) {
          if (HAL_INPUT_TOUCH_CONFIG_OFF == status) {
            InputUdevMonitorTouchInitFinEventSend(HAL_INPUT_RET_NORMAL);
          }
        } else {
          INPUT_ERROR_LOG("GetConfigStatusTouch fail\n");
          InputUdevMonitorTouchInitFinEventSend(HAL_INPUT_RET_ERROR);
        }
      } else {
        INPUT_ERROR_LOG("ConfigTouch fail\n");
        InputUdevMonitorTouchInitFinEventSend(HAL_INPUT_RET_ERROR);
      }

      input_touch_device_found = true;
    }

    INPUT_LOG_TRACE("Found Device : %s \n", node);

    /* Modified not to notify input events to other processes */
    InputUdevMonitorInputEventGrab(node, input_dev);

    udev_device_unref(dev);
  }  // end foreach

  if (!input_touch_device_found) {
    INPUT_ERROR_LOG("ERR: Dummy Device Create");
    InputUdevMonitorTouchInitFinEventSend(HAL_INPUT_RET_NORMAL);
  }

  // Free the enumerator object
  udev_enumerate_unref(enumerate);

  return HAL_INPUT_RET_NORMAL;
}

static void InputUdevMonitorDeviceStatusChange(int fd, fd_set *fds) {
  if (FD_ISSET(fd, fds)) {
    // receive the relevant device
    struct udev_device* dev = udev_monitor_receive_device(g_input_udevmonitor_info.monitor);
    if (NULL == dev) return;

    //   input_udev_DevicesGet(udm);
    const char* action = udev_device_get_action(dev);
    const char* node = udev_device_get_devnode(dev);
    if ((!action) || (!node)) {
      udev_device_unref(dev);
      return;
    }

    if (strncmp("event", udev_device_get_sysname(dev), sizeof("event") -1) != 0) {
      INPUT_LOG_TRACE("not event device %s \n", udev_device_get_sysname(dev));
      udev_device_unref(dev);
      return;
    }

    if (strncmp(INPUT_VIRTUAL_INPUTDEVICE_NAME, udev_device_get_devpath(dev),
                sizeof(INPUT_VIRTUAL_INPUTDEVICE_NAME) - 1) == 0) {
      udev_device_unref(dev);
      return;
    }

    if (strcmp(action, "remove") == 0) {
      InputUdevMonitorDeviceListDelete(node);
    }

    struct udev_device* input_dev = udev_device_get_parent_with_subsystem_devtype(dev, "input", NULL);
    if (NULL == input_dev) {
      udev_device_unref(dev);
      return;
    }
    char touch_device_name[INPUT_NODE_NAME_LENGTH] = { 0};
    GetPanelNameTouch(touch_device_name, sizeof(touch_device_name));
    const char *sysattr_name = udev_device_get_sysattr_value(input_dev, "name");
    if (NULL == sysattr_name) {
      INPUT_ERROR_LOG("ERR: Unable to find sysattr \n");
      udev_device_unref(dev);
      return;
    }

    if (0 == strcmp(touch_device_name, sysattr_name)) {
      if (strcmp(action, "remove") == 0) {
      } else {
        INPUT_LOG_TRACE("Found %s \n", touch_device_name);
        InputUdevMonitorTouchInitFinEventSend(HAL_INPUT_RET_NORMAL);
      }
    }

    INPUT_LOG_TRACE("%s : %s \n", node, action);
    if (strcmp(action, "remove") == 0) {
    } else {
      /* Modified not to notify input events to other processes */
      InputUdevMonitorInputEventGrab(node, input_dev);
    }
    udev_device_unref(dev);
  }
}

static void InputUdevMonitorWatch(void) {
  fd_set fds;
  int fd = udev_monitor_get_fd(g_input_udevmonitor_info.monitor);

  if (fd != -1) {
    while (!g_break_from_watch) {
      FD_ZERO(&fds);
      FD_SET(fd, &fds);
      int nfds = max_value(0, fd);

      InputUdevMonitorFDSet(&nfds, &fds);

      struct timeval timeout;
      timeout.tv_sec = 0;
      timeout.tv_usec = 100 * 1000;
      int ret = select(nfds + 1, &fds, NULL, NULL, &timeout);
      if (ret > 0) {
        InputUdevMonitorDeviceStatusChange(fd, &fds);

        InputUdevMonitorFDHandle(&fds);
      }
    }
  } else {
    INPUT_ERROR_LOG("ERR: udev_monitor_get_fd");
  }
}

static void *InputUdevMonitorMain(void * arg) {
  int   rtn;
  prctl(PR_SET_NAME, HAL_INPUT_UDEVMONITOR_THREADNAME);

  rtn = InputUdevMonitorInit();
    if (rtn != HAL_INPUT_RET_NORMAL) {
    goto err_rtn;
  }

  /* Get input device */
  InputUdevMonitorDevicesGet();

  /* Input device monitoring (basically never get out from here) */
  InputUdevMonitorWatch();

  InputUdevMonitorDeinit();

  return NULL;

err_rtn:
  INPUT_LOG_TRACE("pthread_detach\n");
  return NULL;
}

int32_t InputUdevMonitorThreadCreate(void) {
  int ret;

  ret = pthread_create(&g_udev_monitor_thread, NULL, InputUdevMonitorMain, NULL);
  if (ret != 0) {
    INPUT_ERROR_LOG("ERR: pthread_create =%d\n", errno);
    return HAL_INPUT_RET_ERROR;
  }

  return HAL_INPUT_RET_NORMAL;
}