Some documenting thingies.
[irreco.git] / irreco / trunk / src / core / irreco_hardkey_map.c
blob6950312a60f50df93f8adb8d2a4852952211f493
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco_hardkey_map.h"
22 /**
23 * IrrecoHardkey
25 gchar *irreco_hardkey_to_str(guint keyval)
27 gchar *name = NULL;
28 GString *string;
29 IRRECO_ENTER
31 switch (keyval) {
32 case IRRECO_HARDKEY_UP: name = "UP"; break;
33 case IRRECO_HARDKEY_DOWN: name = "DOWN"; break;
34 case IRRECO_HARDKEY_LEFT: name = "LEFT"; break;
35 case IRRECO_HARDKEY_RIGHT: name = "RIGHT"; break;
36 case IRRECO_HARDKEY_SELECT: name = "SELECT"; break;
37 case IRRECO_HARDKEY_BACK: name = "BACK"; break;
38 case IRRECO_HARDKEY_MENU: name = "MENU"; break;
39 case IRRECO_HARDKEY_HOME: name = "HOME"; break;
40 case IRRECO_HARDKEY_FULLSCREEN: name = "FULLSCREEN"; break;
41 case IRRECO_HARDKEY_PLUS: name = "PLUS"; break;
42 case IRRECO_HARDKEY_MINUS: name = "MINUS"; break;
45 string = g_string_new(NULL);
46 if (name) {
47 g_string_printf(string, "%u %s", keyval, name);
48 } else {
49 g_string_printf(string, "%u", keyval);
51 IRRECO_RETURN_STR(g_string_free(string, FALSE));
55 /**
56 * @typedef IrrecoHardkeyMap
58 * Store data assosiated with device keys. That is only IrrecoCmdChains at the
59 * moment.
62 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
63 /* Prototypes */
64 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
65 static gboolean irreco_hardkey_map_free_foreach(gpointer key,
66 gpointer value,
67 gpointer user_data);
68 static gboolean irreco_hardkey_map_find_cmd_chain_id_foreach(gpointer key,
69 gpointer value,
70 gpointer data);
71 static void irreco_hardkey_map_chain_foreach_frapper(gpointer key,
72 gpointer value,
73 gpointer user_data);
77 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
78 /* Datatypes */
79 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
81 typedef struct _IrrecoHardkeyMapForeachData IrrecoHardkeyMapForeachData;
82 struct _IrrecoHardkeyMapForeachData {
83 IrrecoHardkeyMap *self;
84 IrrecoHardkeyMapFunc func;
85 gpointer user_data;
88 typedef struct _IrrecoHardkeyMapFindChainData IrrecoHardkeyMapFindChainData;
89 struct _IrrecoHardkeyMapFindChainData {
90 guint *keyval;
91 IrrecoCmdChainId id;
96 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
97 /* Construction & Destruction */
98 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
100 IrrecoHardkeyMap *irreco_hardkey_map_new(IrrecoCmdChainManager *manager)
102 IrrecoHardkeyMap *self;
103 IRRECO_ENTER
105 self = g_slice_new0(IrrecoHardkeyMap);
106 self->manager = manager;
107 self->table = g_hash_table_new(NULL, NULL);
108 IRRECO_RETURN_PTR(self);
111 void irreco_hardkey_map_free(IrrecoHardkeyMap *self)
113 IRRECO_ENTER
114 if (self == NULL) IRRECO_RETURN;
115 g_hash_table_foreach_remove(self->table,
116 irreco_hardkey_map_free_foreach, self);
117 g_hash_table_destroy(self->table);
118 g_slice_free(IrrecoHardkeyMap, self);
119 IRRECO_RETURN
121 static gboolean irreco_hardkey_map_free_foreach(gpointer key,
122 gpointer value,
123 gpointer user_data)
125 irreco_cmd_chain_manager_free_chain(
126 ((IrrecoHardkeyMap *) user_data )->manager,
127 (IrrecoCmdChainId) value );
128 return TRUE;
133 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
134 /* Functions */
135 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
138 * Make sure some IrrecoCmdChain is assosiated with a keyval.
140 void irreco_hardkey_map_assosiate_chain(IrrecoHardkeyMap *self,
141 guint keyval)
143 IrrecoCmdChainId id;
144 IRRECO_ENTER
146 g_assert(self != NULL);
148 id = (IrrecoCmdChainId) g_hash_table_lookup(self->table,
149 (gpointer) keyval);
150 if (id == 0) {
151 gchar *hardkey_str;
152 hardkey_str = irreco_hardkey_to_str(keyval);
153 IRRECO_DEBUG("Creating new chain for key \"%s\".\n",
154 hardkey_str);
155 g_free(hardkey_str);
156 id = irreco_cmd_chain_manager_new_chain(self->manager);
157 g_hash_table_insert(self->table,
158 (gpointer) keyval,
159 (gpointer) id);
161 IRRECO_RETURN
165 * Assosiate IrrecoCmdChain with a particular keyval.
167 gboolean irreco_hardkey_map_assosiate_chain_with_id(IrrecoHardkeyMap *self,
168 guint keyval,
169 IrrecoCmdChainId id)
171 guint old_keyval;
172 gchar *hardkey_str;
173 IRRECO_ENTER
175 g_assert(self != NULL);
176 g_assert(id > 0);
178 if (irreco_hardkey_map_find_cmd_chain_id(self, id, &old_keyval)) {
180 gchar *keyval_str = irreco_hardkey_to_str(keyval);
181 gchar *old_keyval_str = irreco_hardkey_to_str(old_keyval);
183 if (keyval == old_keyval) {
184 IRRECO_DEBUG("Keyval \"%s\" is already assosiated "
185 "with command chain \"%i\".\n",
186 keyval_str, id);
187 } else {
189 IRRECO_DEBUG("Id \"%i\" cannot be assosiated with "
190 "keyval \"%s\" because is is already "
191 "assosited with keyval \"%s\".\n",
192 id, keyval_str, old_keyval_str);
195 g_free(keyval_str);
196 g_free(old_keyval_str);
197 IRRECO_RETURN_BOOL(FALSE);
200 hardkey_str = irreco_hardkey_to_str(keyval);
201 IRRECO_DEBUG("Creating new chain for key \"%s\" with id \"%i\".\n",
202 hardkey_str, id);
203 g_free(hardkey_str);
205 irreco_cmd_chain_manager_new_chain_with_id(self->manager, id);
206 g_hash_table_insert(self->table,
207 (gpointer) keyval,
208 (gpointer) id);
209 IRRECO_RETURN_BOOL(TRUE);
213 * Get a IrrecoCmdChain assosiated with a keyval.
215 IrrecoCmdChain *irreco_hardkey_map_get_cmd_chain(IrrecoHardkeyMap *self,
216 guint keyval)
218 gchar *hardkey_str;
219 IrrecoCmdChain *chain;
220 IrrecoCmdChainId id;
221 IRRECO_ENTER
223 g_assert(self != NULL);
225 id = (IrrecoCmdChainId) g_hash_table_lookup(self->table,
226 (gpointer) keyval);
227 chain = irreco_cmd_chain_manager_get_chain(self->manager, id);
229 hardkey_str = irreco_hardkey_to_str(keyval);
230 if (id == 0) {
231 IRRECO_DEBUG("Hardkey \"%s\" is not assositead with a command "
232 "chain.\n", hardkey_str);
233 } else {
234 IRRECO_DEBUG("Hardkey \"%s\" is assositead with command "
235 "chain \"%i\".\n", hardkey_str, id);
237 g_free(hardkey_str);
239 IRRECO_RETURN_PTR(chain);
243 * Find out which keyval is assosiated with some IrrecoCmdChainId.
245 * The assosiated keyval will se stored to keyval pointer.
247 * @return TRUE if found. FALSE if not assosiated.
249 gboolean irreco_hardkey_map_find_cmd_chain_id(IrrecoHardkeyMap *self,
250 IrrecoCmdChainId id,
251 guint *keyval)
253 IrrecoHardkeyMapFindChainData find_data;
254 IRRECO_ENTER
256 find_data.id = id;
257 find_data.keyval = keyval;
258 IRRECO_RETURN_BOOL(g_hash_table_find(self->table,
259 irreco_hardkey_map_find_cmd_chain_id_foreach,
260 &find_data) != NULL);
262 static gboolean irreco_hardkey_map_find_cmd_chain_id_foreach(gpointer key,
263 gpointer value,
264 gpointer data)
266 IrrecoHardkeyMapFindChainData *find_data = data;
267 if ((IrrecoCmdChainId) value == find_data->id) {
268 if (find_data->keyval != NULL) *find_data->keyval = (guint) key;
269 return TRUE;
271 return FALSE;
275 * Check if a IrrecoCmdChain has been assosiated with a keyval.
277 gboolean irreco_hardkey_map_cmd_chain_exists(IrrecoHardkeyMap *self, guint keyval)
279 IRRECO_ENTER
280 IRRECO_RETURN_BOOL(g_hash_table_lookup(self->table, (gpointer) keyval)
281 != NULL);
285 * For iterating trough the map.
287 void irreco_hardkey_map_chain_foreach(IrrecoHardkeyMap *self,
288 IrrecoHardkeyMapFunc func,
289 gpointer user_data)
291 IrrecoHardkeyMapForeachData data;
292 IRRECO_ENTER
294 data.self = self;
295 data.func = func;
296 data.user_data = user_data;
297 g_hash_table_foreach(self->table,
298 irreco_hardkey_map_chain_foreach_frapper, &data);
299 IRRECO_RETURN
301 static void irreco_hardkey_map_chain_foreach_frapper(gpointer key,
302 gpointer value,
303 gpointer user_data)
305 IrrecoCmdChain *chain;
306 IrrecoHardkeyMapForeachData *data = user_data;
307 IRRECO_ENTER
309 chain = irreco_cmd_chain_manager_get_chain(data->self->manager,
310 (IrrecoCmdChainId) value);
311 if (chain == NULL) IRRECO_RETURN;
312 data->func(data->self,
313 (guint) key,
314 (IrrecoCmdChainId) value,
315 chain,
316 data->user_data);
317 IRRECO_RETURN