PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 23#include <fcntl.h>
 24#include <stdint.h>
 25#include <string.h>
 26#include <sys/types.h>
 27
 28#include "globaldefs.h"
 29#include "keydef.h"
 30#include "event_out.h"
 31
 32#include <unistd.h>
 33
 34#ifdef __linux__
 35#include <linux/input.h>
 36#include <linux/uinput.h>
 37#endif
 38
 39static int event_out_fd = -1;
 40
 41void event_out_send(int _event_code, 
 42                    int _event_value)
 43{
 44#ifdef __linux__
 45   struct input_event event;
 46
 47   if (event_out_fd != -1)
 48      {
 49         memset(&event, 0, sizeof(event));
 50         event.type  = EV_KEY;
 51         event.code  = _event_code;
 52         event.value = _event_value;
 53         if (write(event_out_fd, &event, sizeof(event)) != sizeof(event))
 54            {
 55               ;
 56            }
 57      }
 58#endif
 59}
 60
 61void event_out_destroy(void)
 62{
 63#ifdef __linux__
 64   if (event_out_fd != -1)
 65      {
 66         ioctl(event_out_fd, UI_DEV_DESTROY);
 67         close(event_out_fd);
 68         event_out_fd = -1;
 69      }
 70#endif
 71}
 72
 73void event_out_init(void)
 74{
 75#ifdef __linux__
 76   const char* uinput_devname[] =
 77      {
 78         "/dev/uinput",
 79         "/dev/input/uinput",
 80         "/dev/misc/uinput",
 81         NULL
 82      };
 83   int i;
 84   struct uinput_user_dev dev;
 85
 86   event_out_fd = -1;
 87   for (i = 0; (event_out_fd == -1) && (uinput_devname[i] != NULL); i++)
 88      {
 89         event_out_fd = open(uinput_devname[i], O_WRONLY | O_NDELAY);
 90      }
 91   if (event_out_fd == -1)
 92      {
 93         return;
 94      }
 95
 96   memset(&dev, 0, sizeof(dev));
 97   strncpy(dev.name, "bdremoteng", sizeof(dev.name));
 98   dev.name[sizeof(dev.name) - 1] = '\0';
 99
100   if (write(event_out_fd, &dev, sizeof(dev)) != sizeof(dev))
101      {
102         close(event_out_fd);
103         event_out_fd = -1;
104         return;
105      }
106
107   if (ioctl(event_out_fd, UI_SET_EVBIT, EV_KEY) != 0)
108      {
109         close(event_out_fd);
110         event_out_fd = -1;
111         return;
112      }
113
114   for(i = 0 ; i < ps3remote_num_keys ; i++)
115      {
116         if (ioctl(event_out_fd, UI_SET_KEYBIT, ps3remote_keys[i].event_code) != 0)
117            {
118               close(event_out_fd);
119               event_out_fd = -1;
120               return;
121            }
122      }
123
124   if (ioctl(event_out_fd, UI_DEV_CREATE) != 0)
125      {
126         close(event_out_fd);
127         event_out_fd = -1;
128         return;
129      }
130#endif
131}