comparison src/evdev-plug/ed_bindings_store.c @ 422:5e46b57d1eda trunk

[svn] - added evdev-plug, written-from-scratch plugin that allows to control the player via event devices on linux systems
author giacomo
date Sun, 14 Jan 2007 17:55:24 -0800
parents
children 581a057768c2
comparison
equal deleted inserted replaced
421:1a82af4f13cf 422:5e46b57d1eda
1 /*
2 *
3 * Author: Giacomo Lozito <james@develia.org>, (C) 2005-2007
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "ed_bindings_store.h"
22 #include "ed_common.h"
23 #include <stdlib.h>
24
25
26 /* currently, the bindings store is implemented as a string-based Hash Table;
27 however, a change of implementation would only require to re-write the
28 public API defined in this source file;
29 the rest of the code in EvDev-Plug doesn't care about the
30 implementation at all, and access it in a transparent fashion */
31
32
33 gpointer
34 ed_bindings_store_new ( void )
35 {
36 GHashTable *hashtable = g_hash_table_new_full( g_str_hash , g_str_equal , g_free , NULL );
37 return hashtable;
38 }
39
40
41 /* associate a triplet ev_type:ev_code:ev_value
42 (identifying an input event) to an action code in our bindings store;
43 bindings store should keep its own copy of ed_inputevent_t objects! */
44 gint
45 ed_bindings_store_insert ( gpointer hashtable_gp ,
46 ed_inputevent_t * inputev ,
47 gint action_code )
48 {
49 gchar *input_str;
50 input_str = g_strdup_printf( "%i:%i:%i" , inputev->type , inputev->code , inputev->value );
51 g_hash_table_insert( (GHashTable*)hashtable_gp , input_str , GINT_TO_POINTER(action_code) );
52
53 DEBUGMSG( "storing code %i -> event: %i:%i:%i\n" ,
54 action_code , inputev->type, inputev->code, inputev->value );
55 return 0;
56 }
57
58
59 typedef struct
60 {
61 ed_bindings_store_foreach_func callback;
62 gpointer data1;
63 gpointer data2;
64 }
65 hashfunc_foreach_container_t;
66
67 static void
68 hashfunc_foreach( gpointer key , gpointer value , gpointer container_gp )
69 {
70 hashfunc_foreach_container_t *container = container_gp;
71 ed_inputevent_t inputev;
72 gchar **toks;
73
74 /* convert key (it's a string in the "type:code:value" form) back to ed_inputevent_t object */
75 toks = g_strsplit( (gchar*)key , ":" , 3 );
76 inputev.type = atoi( toks[0] );
77 inputev.code = atoi( toks[1] );
78 inputev.value = atoi( toks[2] );
79 g_strfreev( toks );
80
81 container->callback( &inputev , GPOINTER_TO_INT(value) , container->data1 , container->data2 );
82 return;
83 }
84
85 gint
86 ed_bindings_store_foreach ( gpointer hashtable_gp ,
87 ed_bindings_store_foreach_func callback ,
88 gpointer user_data1 ,
89 gpointer user_data2 )
90 {
91 hashfunc_foreach_container_t *container = g_malloc(sizeof(hashfunc_foreach_container_t));
92 container->callback = callback;
93 container->data1 = user_data1;
94 container->data2 = user_data2;
95 g_hash_table_foreach( (GHashTable*)hashtable_gp , hashfunc_foreach , container );
96 g_free( container );
97 }
98
99
100 guint
101 ed_bindings_store_size ( gpointer hashtable_gp )
102 {
103 return g_hash_table_size( (GHashTable*)hashtable_gp );
104 }
105
106
107 gboolean
108 ed_bindings_store_lookup( gpointer hashtable_gp ,
109 ed_inputevent_t * inputev ,
110 gint * action_code )
111 {
112 gpointer p;
113 gchar *input_str;
114
115 input_str = g_strdup_printf( "%i:%i:%i" , inputev->type , inputev->code , inputev->value );
116
117 if ( g_hash_table_lookup_extended( (GHashTable*)hashtable_gp , input_str , NULL , &p ) == TRUE )
118 {
119 *action_code = GPOINTER_TO_INT(p);
120 g_free( input_str );
121 return TRUE;
122 }
123 else
124 {
125 g_free( input_str );
126 return FALSE;
127 }
128 }
129
130
131 gint
132 ed_bindings_store_delete ( gpointer hashtable_gp )
133 {
134 if ( hashtable_gp != NULL )
135 g_hash_table_destroy( hashtable_gp );
136 return 0;
137 }