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
|
/*
(c) Copyright 2012-2013 DirectFB integrated media GmbH
(c) Copyright 2001-2013 The world wide DirectFB Open Source Community (directfb.org)
(c) Copyright 2000-2004 Convergence (integrated media) GmbH
All rights reserved.
Written by Denis Oliver Kropp <dok@directfb.org>,
Andreas Shimokawa <andi@directfb.org>,
Marek Pikarski <mass@directfb.org>,
Sven Neumann <neo@directfb.org>,
Ville Syrjälä <syrjala@sci.fi> and
Claudio Ciccani <klan@users.sf.net>.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <directfb.h>
#include <directfb_keynames.h>
#include <core/coredefs.h>
#include <core/coretypes.h>
#include <core/input.h>
#include <direct/debug.h>
#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/thread.h>
#include <direct/util.h>
#include <core/input_driver.h>
DFB_INPUT_DRIVER( lirc )
static DirectFBKeySymbolNames(keynames);
static bool keynames_sorted = false;
typedef struct {
CoreInputDevice *device;
DirectThread *thread;
int fd;
} LircData;
static int keynames_compare (const void *key,
const void *base)
{
return strcmp ((const char *) key,
((const struct DFBKeySymbolName *) base)->name);
}
static int keynames_sort_compare (const void *d1,
const void *d2)
{
return strcmp (((const struct DFBKeySymbolName *) d1)->name,
((const struct DFBKeySymbolName *) d2)->name);
}
static DFBInputDeviceKeySymbol lirc_parse_line(const char *line)
{
struct DFBKeySymbolName *symbol_name;
char *s, *name;
if (!keynames_sorted) {
qsort ( keynames,
D_ARRAY_SIZE( keynames ),
sizeof(keynames[0]),
(__compar_fn_t) keynames_sort_compare );
keynames_sorted = true;
}
s = strchr( line, ' ' );
if (!s || !s[1])
return DIKS_NULL;
s = strchr( ++s, ' ' );
if (!s|| !s[1])
return DIKS_NULL;
name = ++s;
s = strchr( name, ' ' );
if (s)
*s = '\0';
switch (strlen( name )) {
case 0:
return DIKS_NULL;
case 1:
return (DFBInputDeviceKeySymbol) name[0];
default:
symbol_name = bsearch( name, keynames,
D_ARRAY_SIZE( keynames ),
sizeof(keynames[0]),
(__compar_fn_t) keynames_compare );
if (symbol_name)
return symbol_name->symbol;
break;
}
return DIKS_NULL;
}
static void*
lircEventThread( DirectThread *thread, void *driver_data )
{
int repeats = 0;
DFBInputDeviceKeySymbol last = DIKS_NULL;
LircData *data = (LircData*) driver_data;
while (true) {
struct timeval tv;
fd_set set;
int result;
int readlen;
char buf[128];
DFBInputEvent evt;
DFBInputDeviceKeySymbol symbol;
FD_ZERO(&set);
FD_SET(data->fd,&set);
tv.tv_sec = 0;
tv.tv_usec = 200000;
result = select (data->fd+1, &set, NULL, NULL, &tv);
switch (result) {
case -1:
/* error */
if (errno == EINTR)
continue;
D_PERROR("DirectFB/LIRC: select() failed\n");
return NULL;
case 0:
/* timeout, release last key */
if (last != DIKS_NULL) {
evt.flags = DIEF_KEYSYMBOL;
evt.type = DIET_KEYRELEASE;
evt.key_symbol = last;
dfb_input_dispatch( data->device, &evt );
last = DIKS_NULL;
}
continue;
default:
/* data arrived */
break;
}
direct_thread_testcancel( thread );
/* read data */
readlen = read( data->fd, buf, 128 );
if (readlen < 1)
continue;
/* get new key */
symbol = lirc_parse_line( buf );
if (symbol == DIKS_NULL)
continue;
/* repeated key? */
if (symbol == last) {
/* swallow the first three repeats */
if (++repeats < 4)
continue;
}
else {
/* reset repeat counter */
repeats = 0;
/* release previous key if not released yet */
if (last != DIKS_NULL) {
evt.flags = DIEF_KEYSYMBOL;
evt.type = DIET_KEYRELEASE;
evt.key_symbol = last;
dfb_input_dispatch( data->device, &evt );
}
}
/* send the press event */
evt.flags = DIEF_KEYSYMBOL;
evt.type = DIET_KEYPRESS;
evt.key_symbol = symbol;
dfb_input_dispatch( data->device, &evt );
/* remember last key */
last = symbol;
}
return NULL;
}
/* exported symbols */
static int
driver_get_available( void )
{
int fd;
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
direct_snputs( addr.sun_path, "/dev/lircd", sizeof(addr.sun_path) );
fd = socket( PF_UNIX, SOCK_STREAM, 0 );
if (fd < 0)
return 0;
if (connect( fd, (struct sockaddr*)&addr, sizeof(addr) ) < 0) {
/*
* for LIRC version >= 0.8.6 the Unix socket is no more created
* under /dev/lircd but under /var/run/lirc/lircd
*/
direct_snputs( addr.sun_path, "/var/run/lirc/lircd", sizeof(addr.sun_path) );
if (connect( fd, (struct sockaddr*)&addr, sizeof(addr) ) < 0) {
close( fd );
return 0;
}
}
close( fd );
return 1;
}
static void
driver_get_info( InputDriverInfo *info )
{
/* fill driver info structure */
snprintf( info->name,
DFB_INPUT_DRIVER_INFO_NAME_LENGTH, "LIRC Driver" );
snprintf( info->vendor,
DFB_INPUT_DRIVER_INFO_VENDOR_LENGTH, "directfb.org" );
info->version.major = 0;
info->version.minor = 2;
}
static DFBResult
driver_open_device( CoreInputDevice *device,
unsigned int number,
InputDeviceInfo *info,
void **driver_data )
{
int fd;
LircData *data;
struct sockaddr_un sa;
/* create socket */
sa.sun_family = AF_UNIX;
direct_snputs( sa.sun_path, "/dev/lircd", sizeof(sa.sun_path) );
fd = socket( PF_UNIX, SOCK_STREAM, 0 );
if (fd < 0) {
D_PERROR( "DirectFB/LIRC: socket" );
return DFB_INIT;
}
/* initiate connection */
if (connect( fd, (struct sockaddr*)&sa, sizeof(sa) ) < 0) {
/*
* try "/var/run/lirc/lircd"
*/
direct_snputs( sa.sun_path, "/var/run/lirc/lircd", sizeof(sa.sun_path) );
if (connect( fd, (struct sockaddr*)&sa, sizeof(sa) ) < 0) {
D_PERROR( "DirectFB/LIRC: connect" );
close( fd );
return DFB_INIT;
}
}
/* fill driver info structure */
snprintf( info->desc.name,
DFB_INPUT_DEVICE_DESC_NAME_LENGTH, "LIRC Device" );
snprintf( info->desc.vendor,
DFB_INPUT_DEVICE_DESC_VENDOR_LENGTH, "Unknown" );
info->prefered_id = DIDID_REMOTE;
info->desc.type = DIDTF_REMOTE;
#ifndef DIRECTFB_DISABLE_DEPRECATED
info->desc.caps = DICAPS_KEYS;
#else
info->desc.caps = DIDCAPS_KEYS;
#endif
/* allocate and fill private data */
data = D_CALLOC( 1, sizeof(LircData) );
if (!data) {
close( fd );
return D_OOM();
}
data->fd = fd;
data->device = device;
/* start input thread */
data->thread = direct_thread_create( DTT_INPUT, lircEventThread, data, "LiRC Input" );
/* set private data pointer */
*driver_data = data;
return DFB_OK;
}
/*
* Fetch one entry from the device's keymap if supported.
*/
static DFBResult
driver_get_keymap_entry( CoreInputDevice *device,
void *driver_data,
DFBInputDeviceKeymapEntry *entry )
{
return DFB_UNSUPPORTED;
}
static void
driver_close_device( void *driver_data )
{
LircData *data = (LircData*) driver_data;
/* stop input thread */
direct_thread_cancel( data->thread );
direct_thread_join( data->thread );
direct_thread_destroy( data->thread );
/* close socket */
close( data->fd );
/* free private data */
D_FREE( data );
}
|