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 1024:c67f2d61ab97
prev1015:ad448bedc48a
next1050:7d88277590d9
author nkeynes
date Sat Jun 13 00:50:48 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Build drivers with library dependencies as shared objects (ie plugins)
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/giochannel.h>
    32 #include <glib.h>
    34 #include <lirc/lirc_client.h>
    36 #include "lxdream.h"
    37 #include "plugin.h"
    38 #include "display.h"
    39 #include "maple/maple.h"
    41 typedef struct input_lirc {
    42     struct input_driver driver;
    43     char name[5];
    44     int fd;
    45     GIOChannel *channel;
    46 } *input_lirc_t;
    48 #define MAX_KEYSYMS 256
    49 static char *keysyms[MAX_KEYSYMS];
    51 static uint16_t input_lirc_resolve_keysym( input_driver_t dev, const gchar *str );
    52 static gchar *input_lirc_keysym_for_keycode( input_driver_t dev, uint16_t keycode );
    53 static gboolean input_lirc_callback( GIOChannel *source, GIOCondition condition, gpointer data );
    55 input_driver_t system_lirc_driver;
    57 static gboolean input_lirc_init()
    58 {
    59     input_lirc_t system_lirc_driver = g_malloc0(sizeof(struct input_lirc));
    60     strcpy(system_lirc_driver->name, "LIRC");
    61     system_lirc_driver->driver.id = system_lirc_driver->name;
    62     system_lirc_driver->driver.resolve_keysym = input_lirc_resolve_keysym;
    63     system_lirc_driver->driver.get_keysym_for_keycode = input_lirc_keysym_for_keycode;
    64     system_lirc_driver->driver.destroy = NULL;
    66     system_lirc_driver->fd = lirc_init("lxdream", 1);
    67     if (system_lirc_driver->fd == -1) {
    68         WARN("Could not initialize LIRC.  LIRC hotkeys will be disabled.");
    69         return FALSE;
    70     }
    72     system_lirc_driver->channel = g_io_channel_unix_new(system_lirc_driver->fd);
    73     g_io_channel_set_flags(system_lirc_driver->channel, G_IO_FLAG_IS_READABLE | G_IO_FLAG_NONBLOCK, NULL);
    74     g_io_add_watch(system_lirc_driver->channel, G_IO_IN|G_IO_ERR|G_IO_HUP, input_lirc_callback, system_lirc_driver);
    75     memset(keysyms, 0, MAX_KEYSYMS);
    76     input_register_device((input_driver_t)system_lirc_driver, MAX_KEYSYMS - 1);
    77     return TRUE;
    78 }
    80 void input_lirc_shutdown(void)
    81 {
    82     input_lirc_t lirc = (input_lirc_t)system_lirc_driver;
    83     g_io_channel_shutdown(lirc->channel, FALSE, NULL );
    84     g_io_channel_unref(lirc->channel);
    85     lirc_deinit();
    86     g_free(system_lirc_driver);
    87 }
    89 static uint16_t input_lirc_resolve_keysym( input_driver_t dev, const gchar *str )
    90 {
    91     //LIRC uses keysyms but no keycodes.  To generate a keycode, we'll just make them up as we go.
    92     //As long as we store keysyms instead of keycodes in any config files, this should be fine.
    93     uint16_t i;
    94     for (i = 1; i < MAX_KEYSYMS && keysyms[i] != NULL; i++) {
    95         if (strcasecmp(str, keysyms[i]) == 0) {
    96             //keycode already exists
    97             return i;
    98         }
    99     }
   101     if (i < MAX_KEYSYMS) {
   102         //this key is not in the list yet, so make a new keycode for it
   103         keysyms[i] = g_strdup(str);
   104         return i;
   105     } else {
   106         //if your remote has more than 256 buttons, you may need to increase MAX_KEYSYMS
   107         ERROR("LIRC has too many keysyms!");
   108         return 0;
   109     }
   110 }
   112 static gchar *input_lirc_keysym_for_keycode( input_driver_t dev, uint16_t keycode )
   113 {
   114     //This won't work if you send in keycodes that haven't been fired by the callback
   115     //or looked up using resolve_keysym yet.  This shouldn't be a problem, since these
   116     //two functions are the only way to get a keycode in the first place.
   118     if (keycode < MAX_KEYSYMS)
   119         return g_strdup(keysyms[keycode]);
   120     else
   121         return NULL;
   122 }
   124 static gboolean input_lirc_callback( GIOChannel *source, GIOCondition condition, gpointer data )
   125 {
   126     int ret;
   127     char *code, *c;
   128     char keysym[256];
   130     input_lirc_t lirc = (input_lirc_t)data;
   132     if (condition & G_IO_IN)
   133     {
   134         //loop through all queued commands
   135         while ((ret = lirc_nextcode(&code)) == 0 && code != NULL)
   136         {
   137             INFO("LIRC code (%s)", code);
   138             //code contains id, repeat count, and keysym separated by spaces
   139             gchar **code_split = g_strsplit(code, " ", 4);
   141             //eliminate repeats by only accepting the first instance of a keysym
   142             if (atoi(code_split[1]) == 0)
   143             {
   144                 input_event_keydown((input_driver_t)lirc, input_lirc_resolve_keysym((input_driver_t)lirc, code_split[2]), MAX_PRESSURE);
   145             }
   147             g_strfreev(code_split);
   148             free(code);
   149         }
   150     }
   151     return TRUE;
   152 }
   154 DEFINE_PLUGIN( PLUGIN_INPUT_DRIVER, "lirc", input_lirc_init );
.