Search
lxdream.org :: lxdream/src/drivers/input_lirc.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/input_lirc.c
changeset 1296:30ecee61f811
prev1075:1a21750d300c
author nkeynes
date Fri May 29 18:47:05 2015 +1000 (7 years ago)
permissions -rw-r--r--
last change Fix test case
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * LIRC input device support
     5  *
     6  * Copyright (c) 2009 wahrhaft
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <sys/types.h>
    20 #include <sys/ioctl.h>
    21 #include <errno.h>
    22 #include <stdio.h>
    23 #include <signal.h>
    24 #include <string.h>
    25 #include <stdlib.h>
    26 #include <unistd.h>
    27 #include <fcntl.h>
    28 #include <dirent.h>
    29 #include <ctype.h>
    31 #include <glib.h>
    33 #include <lirc/lirc_client.h>
    35 #include "lxdream.h"
    36 #include "plugin.h"
    37 #include "display.h"
    38 #include "maple/maple.h"
    40 typedef struct input_lirc {
    41     struct input_driver driver;
    42     char name[5];
    43     int fd;
    44     GIOChannel *channel;
    45 } *input_lirc_t;
    47 #define MAX_KEYSYMS 65536
    48 static GData *keysyms;
    49 static uint16_t last_keycode;
    50 static GQuark keysym_by_keycode_result;
    53 static uint16_t input_lirc_resolve_keysym( input_driver_t dev, const gchar *str );
    54 static gchar *input_lirc_keysym_for_keycode( input_driver_t dev, uint16_t keycode );
    55 static void get_keysym_by_keycode(GQuark key_id, gpointer data, gpointer user_data);
    56 static gboolean input_lirc_callback( GIOChannel *source, GIOCondition condition, gpointer data );
    58 input_driver_t system_lirc_driver;
    60 static gboolean input_lirc_init()
    61 {
    62     input_lirc_t system_lirc_driver = g_malloc0(sizeof(struct input_lirc));
    63     strcpy(system_lirc_driver->name, "LIRC");
    64     system_lirc_driver->driver.id = system_lirc_driver->name;
    65     system_lirc_driver->driver.resolve_keysym = input_lirc_resolve_keysym;
    66     system_lirc_driver->driver.get_keysym_for_keycode = input_lirc_keysym_for_keycode;
    67     system_lirc_driver->driver.destroy = NULL;
    69     system_lirc_driver->fd = lirc_init("lxdream", 1);
    70     if (system_lirc_driver->fd == -1) {
    71         WARN("Could not initialize LIRC.  LIRC hotkeys will be disabled.");
    72         return FALSE;
    73     }
    75     system_lirc_driver->channel = g_io_channel_unix_new(system_lirc_driver->fd);
    76     g_io_channel_set_flags(system_lirc_driver->channel, G_IO_FLAG_IS_READABLE | G_IO_FLAG_NONBLOCK, NULL);
    77     g_io_add_watch(system_lirc_driver->channel, G_IO_IN|G_IO_ERR|G_IO_HUP, input_lirc_callback, system_lirc_driver);
    78     g_datalist_init(&keysyms);
    79     input_register_device((input_driver_t)system_lirc_driver, MAX_KEYSYMS - 1);
    80     return TRUE;
    81 }
    83 void input_lirc_shutdown(void)
    84 {
    85     input_lirc_t lirc = (input_lirc_t)system_lirc_driver;
    86     g_io_channel_shutdown(lirc->channel, FALSE, NULL );
    87     g_io_channel_unref(lirc->channel);
    88     lirc_deinit();
    89     g_free(system_lirc_driver);
    90 }
    92 static uint16_t input_lirc_resolve_keysym( input_driver_t dev, const gchar *str )
    93 {
    94     //LIRC uses keysyms but no keycodes.  To generate a keycode, we'll just make them up as we go.
    95     //As long as we store keysyms instead of keycodes in any config files, this should be fine.
    97     uint16_t keycode;
    98     keycode = (uint16_t)GPOINTER_TO_INT(g_datalist_get_data(&keysyms, str));
   100     if (keycode == 0) {
   101         //this key is not in the list yet, so make a new keycode for it
   102         g_datalist_set_data(&keysyms, str, GINT_TO_POINTER(++last_keycode));
   103         return last_keycode;
   104     } else {
   105         return keycode;
   106     }
   108 }
   110 static gchar *input_lirc_keysym_for_keycode( input_driver_t dev, uint16_t keycode )
   111 {
   112     //This won't work if you send in keycodes that haven't been fired by the callback
   113     //or looked up using resolve_keysym yet.  This shouldn't be a problem, since these
   114     //two functions are the only way to get a keycode in the first place.
   116     if (keycode <= last_keycode) {
   117         //reverse lookup
   118         keysym_by_keycode_result = 0;
   119         g_datalist_foreach(&keysyms, get_keysym_by_keycode, GINT_TO_POINTER(keycode));
   120         return g_strdup(g_quark_to_string(keysym_by_keycode_result));
   121     } else {
   122         return NULL;
   123     }
   124 }
   126 static void get_keysym_by_keycode(GQuark key_id, gpointer data, gpointer user_data)
   127 {
   128     if (data == user_data)
   129         keysym_by_keycode_result = key_id;
   130 }
   132 static gboolean input_lirc_callback( GIOChannel *source, GIOCondition condition, gpointer data )
   133 {
   134     int ret;
   135     char *code, *c;
   137     input_lirc_t lirc = (input_lirc_t)data;
   139     if (condition & G_IO_IN)
   140     {
   141         //loop through all queued commands
   142         while ((ret = lirc_nextcode(&code)) == 0 && code != NULL)
   143         {
   144             //code contains id, repeat count, and keysym separated by spaces
   145             gchar **code_split = g_strsplit(code, " ", 4);
   147             //eliminate repeats by only accepting the first instance of a keysym
   148             if (atoi(code_split[1]) == 0)
   149             {
   150                 input_event_keydown((input_driver_t)lirc, input_lirc_resolve_keysym((input_driver_t)lirc, code_split[2]), MAX_PRESSURE);
   151             }
   153             g_strfreev(code_split);
   154             free(code);
   155         }
   156     }
   157     return TRUE;
   158 }
   160 DEFINE_PLUGIN( PLUGIN_INPUT_DRIVER, "lirc", input_lirc_init );
.