src/modules/input/kbd/kbd.c
author Benjamin Zores <ben@geexbox.org>
Thu, 31 Dec 2009 14:37:29 +0100
branchenna
changeset 2596 35d6ab8d3531
parent 2574 8468bb001d5a
child 2627 e419cd06ef5b
permissions -rw-r--r--
update fr po file
     1 /*
     2  * GeeXboX Enna Media Center.
     3  * Copyright (C) 2005-2009 The Enna Project
     4  *
     5  * This file is part of Enna.
     6  *
     7  * Enna is free software; you can redistribute it and/or
     8  * modify it under the terms of the GNU Lesser General Public
     9  * License as published by the Free Software Foundation; either
    10  * version 2.1 of the License, or (at your option) any later version.
    11  *
    12  * Enna 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 GNU
    15  * Lesser General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU Lesser General Public
    18  * License along with Enna; if not, write to the Free Software
    19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    20  */
    21 
    22 #include <Ecore.h>
    23 
    24 #include "enna.h"
    25 #include "enna_config.h"
    26 #include "logs.h"
    27 #include "input.h"
    28 #include "module.h"
    29 
    30 #define ENNA_MODULE_NAME "input_kbd"
    31 
    32 
    33 static const struct
    34 {
    35     const char *keyname;
    36     Ecore_Event_Modifier modifier;
    37     enna_input input;
    38 } enna_keymap[] = {
    39     { "Left",         ECORE_NONE,              ENNA_INPUT_LEFT          },
    40     { "Right",        ECORE_NONE,              ENNA_INPUT_RIGHT         },
    41     { "Up",           ECORE_NONE,              ENNA_INPUT_UP            },
    42     { "KP_Up",        ECORE_NONE,              ENNA_INPUT_UP            },
    43     { "Down",         ECORE_NONE,              ENNA_INPUT_DOWN          },
    44     { "KP_Down",      ECORE_NONE,              ENNA_INPUT_DOWN          },
    45     { "Home",         ECORE_NONE,              ENNA_INPUT_HOME          },
    46     { "KP_Home",      ECORE_NONE,              ENNA_INPUT_HOME          },
    47     { "End",          ECORE_NONE,              ENNA_INPUT_END           },
    48     { "KP_End",       ECORE_NONE,              ENNA_INPUT_END           },
    49     { "Prior",        ECORE_NONE,              ENNA_INPUT_PREV          },
    50     { "Next",         ECORE_NONE,              ENNA_INPUT_NEXT          },
    51     { "Return",       ECORE_NONE,              ENNA_INPUT_OK            },
    52     { "KP_Enter",     ECORE_NONE,              ENNA_INPUT_OK            },
    53     { "space",        ECORE_NONE,              ENNA_INPUT_SPACE         },
    54     //~ { "Stop",         ECORE_NONE,              ENNA_KEY_STOP         }, stop on the keyboard? multimedia keyb?
    55     { "BackSpace",    ECORE_NONE,              ENNA_INPUT_EXIT          },
    56     
    57     { "Escape",       ECORE_NONE,              ENNA_INPUT_QUIT          },
    58     { "Super_L",      ECORE_NONE,              ENNA_INPUT_MENU          },
    59     { "Meta_L",       ECORE_NONE,              ENNA_INPUT_MENU          },
    60     { "Hyper_L",      ECORE_NONE,              ENNA_INPUT_MENU          },
    61     { "plus",         ECORE_NONE,              ENNA_INPUT_PLUS          },
    62     { "plus",         ECORE_SHIFT,             ENNA_INPUT_PLUS          },
    63     { "KP_Add",       ECORE_NONE,              ENNA_INPUT_PLUS          },
    64     { "minus",        ECORE_NONE,              ENNA_INPUT_MINUS         },
    65     { "KP_Subtract",  ECORE_NONE,              ENNA_INPUT_MINUS         },
    66     { "m",            ECORE_CTRL,              ENNA_INPUT_MUTE          },
    67     { "M",            ECORE_CTRL,              ENNA_INPUT_MUTE          },
    68     { "f",            ECORE_CTRL,              ENNA_INPUT_FULLSCREEN    },
    69     { "F",            ECORE_CTRL,              ENNA_INPUT_FULLSCREEN    },
    70     { "0",            ECORE_NONE,              ENNA_INPUT_KEY_0         },
    71     { "KP_0",         ECORE_NONE,              ENNA_INPUT_KEY_0         },
    72     { "1",            ECORE_NONE,              ENNA_INPUT_KEY_1         },
    73     { "KP_1",         ECORE_NONE,              ENNA_INPUT_KEY_1         },
    74     { "2",            ECORE_NONE,              ENNA_INPUT_KEY_2         },
    75     { "KP_2",         ECORE_NONE,              ENNA_INPUT_KEY_2         },
    76     { "3",            ECORE_NONE,              ENNA_INPUT_KEY_3         },
    77     { "KP_3",         ECORE_NONE,              ENNA_INPUT_KEY_3         },
    78     { "4",            ECORE_NONE,              ENNA_INPUT_KEY_4         },
    79     { "KP_4",         ECORE_NONE,              ENNA_INPUT_KEY_4         },
    80     { "5",            ECORE_NONE,              ENNA_INPUT_KEY_5         },
    81     { "KP_5",         ECORE_NONE,              ENNA_INPUT_KEY_5         },
    82     { "6",            ECORE_NONE,              ENNA_INPUT_KEY_6         },
    83     { "KP_6",         ECORE_NONE,              ENNA_INPUT_KEY_6         },
    84     { "7",            ECORE_NONE,              ENNA_INPUT_KEY_7         },
    85     { "KP_7",         ECORE_NONE,              ENNA_INPUT_KEY_7         },
    86     { "8",            ECORE_NONE,              ENNA_INPUT_KEY_8         },
    87     { "KP_8",         ECORE_NONE,              ENNA_INPUT_KEY_8         },
    88     { "9",            ECORE_NONE,              ENNA_INPUT_KEY_9         },
    89     { "KP_9",         ECORE_NONE,              ENNA_INPUT_KEY_9         },
    90     { "a",            ECORE_NONE,              ENNA_INPUT_KEY_A         },
    91     { "b",            ECORE_NONE,              ENNA_INPUT_KEY_B         },
    92     { "c",            ECORE_NONE,              ENNA_INPUT_KEY_C         },
    93     { "d",            ECORE_NONE,              ENNA_INPUT_KEY_D         },
    94     { "e",            ECORE_NONE,              ENNA_INPUT_KEY_E         },
    95     { "f",            ECORE_NONE,              ENNA_INPUT_KEY_F         },
    96     { "g",            ECORE_NONE,              ENNA_INPUT_KEY_G         },
    97     { "h",            ECORE_NONE,              ENNA_INPUT_KEY_H         },
    98     { "i",            ECORE_NONE,              ENNA_INPUT_KEY_I         },
    99     { "j",            ECORE_NONE,              ENNA_INPUT_KEY_J         },
   100     { "k",            ECORE_NONE,              ENNA_INPUT_KEY_K         },
   101     { "l",            ECORE_NONE,              ENNA_INPUT_KEY_L         },
   102     { "m",            ECORE_NONE,              ENNA_INPUT_KEY_M         },
   103     { "n",            ECORE_NONE,              ENNA_INPUT_KEY_N         },
   104     { "o",            ECORE_NONE,              ENNA_INPUT_KEY_O         },
   105     { "p",            ECORE_NONE,              ENNA_INPUT_KEY_P         },
   106     { "q",            ECORE_NONE,              ENNA_INPUT_KEY_Q         },
   107     { "r",            ECORE_NONE,              ENNA_INPUT_KEY_R         },
   108     { "s",            ECORE_NONE,              ENNA_INPUT_KEY_S         },
   109     { "t",            ECORE_NONE,              ENNA_INPUT_KEY_T         },
   110     { "u",            ECORE_NONE,              ENNA_INPUT_KEY_U         },
   111     { "v",            ECORE_NONE,              ENNA_INPUT_KEY_V         },
   112     { "w",            ECORE_NONE,              ENNA_INPUT_KEY_W         },
   113     { "x",            ECORE_NONE,              ENNA_INPUT_KEY_X         },
   114     { "y",            ECORE_NONE,              ENNA_INPUT_KEY_Y         },
   115     { "z",            ECORE_NONE,              ENNA_INPUT_KEY_Z         },
   116     { NULL,           ECORE_NONE,              ENNA_INPUT_UNKNOWN       }
   117 };
   118 
   119 
   120 
   121 static Ecore_Event_Handler *key_down_event_handler;
   122 
   123 static enna_input
   124 _input_event_modifier (Ecore_Event_Key *ev)
   125 {
   126     int i;
   127 
   128     for (i = 0; enna_keymap[i].keyname; i++)
   129     {
   130         if (ev->modifiers == enna_keymap[i].modifier &&
   131             !strcmp(enna_keymap[i].keyname, ev->key))
   132         {
   133             enna_log(ENNA_MSG_EVENT, NULL, "Key pressed : [%d] + %s",
   134                      enna_keymap[i].modifier, enna_keymap[i] );
   135             return enna_keymap[i].input;
   136         }
   137     }
   138 
   139     return ENNA_INPUT_UNKNOWN;
   140 }
   141 
   142 static enna_input
   143 _input_event (Ecore_Event_Key *ev)
   144 {
   145     int i;
   146 
   147     for (i = 0; enna_keymap[i].keyname; i++)
   148     {
   149         if ((enna_keymap[i].modifier == ECORE_NONE) &&
   150             !strcmp(enna_keymap[i].keyname, ev->key))
   151         {
   152             enna_log(ENNA_MSG_EVENT, NULL, "Key pressed : %s",
   153                      enna_keymap[i].keyname );
   154             return enna_keymap[i].input;
   155         }
   156     }
   157 
   158     return ENNA_INPUT_UNKNOWN;
   159 }
   160 
   161 static enna_input
   162 _get_input_from_event(Ecore_Event_Key *ev)
   163 {
   164     if (!ev)
   165         return ENNA_INPUT_UNKNOWN;
   166 
   167     /* discard some modifiers */
   168     if (ev->modifiers >= ECORE_EVENT_LOCK_CAPS)
   169         ev->modifiers -= ECORE_EVENT_LOCK_CAPS;
   170     if (ev->modifiers >= ECORE_EVENT_LOCK_NUM)
   171         ev->modifiers -= ECORE_EVENT_LOCK_NUM;
   172     if (ev->modifiers >= ECORE_EVENT_LOCK_SCROLL)
   173         ev->modifiers -= ECORE_EVENT_LOCK_SCROLL;
   174 
   175     return (ev->modifiers && ev->modifiers < ECORE_LAST) ?
   176       _input_event_modifier(ev) : _input_event(ev);
   177 }
   178 
   179 static int
   180 _ecore_event_key_down_cb(void *data, int type, void *event)
   181 {
   182     Ecore_Event_Key *e = event;
   183     enna_input in;
   184 
   185     enna_idle_timer_renew();
   186 
   187     in = _get_input_from_event(e);
   188     if (in != ENNA_INPUT_UNKNOWN)
   189         enna_input_event_emit(in);
   190 
   191     return ECORE_CALLBACK_CANCEL;
   192 }
   193 
   194 
   195 /* Module interface */
   196 
   197 #ifdef USE_STATIC_MODULES
   198 #undef MOD_PREFIX
   199 #define MOD_PREFIX enna_mod_input_kbd
   200 #endif /* USE_STATIC_MODULES */
   201 
   202 Enna_Module_Api ENNA_MODULE_API =
   203 {
   204     ENNA_MODULE_VERSION,
   205     ENNA_MODULE_NAME,
   206     N_("Keyboard controls"),
   207     NULL,
   208     N_("Module to control enna from the keyboard"),
   209     "bla bla bla<br><b>bla bla bla</b><br><br>bla."
   210 };
   211 
   212 void
   213 ENNA_MODULE_INIT(Enna_Module *em)
   214 {
   215     key_down_event_handler =
   216         ecore_event_handler_add (ECORE_EVENT_KEY_DOWN, _ecore_event_key_down_cb, NULL);
   217 }
   218 
   219 void
   220 ENNA_MODULE_SHUTDOWN(Enna_Module *em)
   221 {
   222     ENNA_EVENT_HANDLER_DEL(key_down_event_handler);
   223 }