/src/event_out.c

http://bdremote-ng.googlecode.com/ · C · 131 lines · 95 code · 15 blank · 21 comment · 21 complexity · b6163e74d623766458623dd9416a1513 MD5 · raw file

  1. /*
  2. * bdremoteng - helper daemon for Sony(R) BD Remote Control
  3. * Based on bdremoted, written by Anton Starikov <antst@mail.ru>.
  4. *
  5. * Copyright (C) 2009 Paul Bender <pebender@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include <fcntl.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include "globaldefs.h"
  27. #include "keydef.h"
  28. #include "event_out.h"
  29. #include <unistd.h>
  30. #ifdef __linux__
  31. #include <linux/input.h>
  32. #include <linux/uinput.h>
  33. #endif
  34. static int event_out_fd = -1;
  35. void event_out_send(int _event_code,
  36. int _event_value)
  37. {
  38. #ifdef __linux__
  39. struct input_event event;
  40. if (event_out_fd != -1)
  41. {
  42. memset(&event, 0, sizeof(event));
  43. event.type = EV_KEY;
  44. event.code = _event_code;
  45. event.value = _event_value;
  46. if (write(event_out_fd, &event, sizeof(event)) != sizeof(event))
  47. {
  48. ;
  49. }
  50. }
  51. #endif
  52. }
  53. void event_out_destroy(void)
  54. {
  55. #ifdef __linux__
  56. if (event_out_fd != -1)
  57. {
  58. ioctl(event_out_fd, UI_DEV_DESTROY);
  59. close(event_out_fd);
  60. event_out_fd = -1;
  61. }
  62. #endif
  63. }
  64. void event_out_init(void)
  65. {
  66. #ifdef __linux__
  67. const char* uinput_devname[] =
  68. {
  69. "/dev/uinput",
  70. "/dev/input/uinput",
  71. "/dev/misc/uinput",
  72. NULL
  73. };
  74. int i;
  75. struct uinput_user_dev dev;
  76. event_out_fd = -1;
  77. for (i = 0; (event_out_fd == -1) && (uinput_devname[i] != NULL); i++)
  78. {
  79. event_out_fd = open(uinput_devname[i], O_WRONLY | O_NDELAY);
  80. }
  81. if (event_out_fd == -1)
  82. {
  83. return;
  84. }
  85. memset(&dev, 0, sizeof(dev));
  86. strncpy(dev.name, "bdremoteng", sizeof(dev.name));
  87. dev.name[sizeof(dev.name) - 1] = '\0';
  88. if (write(event_out_fd, &dev, sizeof(dev)) != sizeof(dev))
  89. {
  90. close(event_out_fd);
  91. event_out_fd = -1;
  92. return;
  93. }
  94. if (ioctl(event_out_fd, UI_SET_EVBIT, EV_KEY) != 0)
  95. {
  96. close(event_out_fd);
  97. event_out_fd = -1;
  98. return;
  99. }
  100. for(i = 0 ; i < ps3remote_num_keys ; i++)
  101. {
  102. if (ioctl(event_out_fd, UI_SET_KEYBIT, ps3remote_keys[i].event_code) != 0)
  103. {
  104. close(event_out_fd);
  105. event_out_fd = -1;
  106. return;
  107. }
  108. }
  109. if (ioctl(event_out_fd, UI_DEV_CREATE) != 0)
  110. {
  111. close(event_out_fd);
  112. event_out_fd = -1;
  113. return;
  114. }
  115. #endif
  116. }