Clean code
[irreco.git] / irreco / src / core / irreco_hardkey_map.c
blob3430d3e3165e9f08a1bf748a5755c7e92e68217d
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
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.
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 * @addtogroup IrrecoHardkeyMap
24 * @ingroup Irreco
26 * @todo PURPOCE OF CLASS.
28 * @{
31 /**
32 * @file
33 * Source file of @ref IrrecoHardkeyMap.
36 /**
37 * IrrecoHardkey
40 gchar *irreco_hardkey_to_str(guint keyval)
42 gchar *name = NULL;
43 GString *string;
44 IRRECO_ENTER
46 switch (keyval) {
47 case IRRECO_HARDKEY_UP: name = "UP"; break;
48 case IRRECO_HARDKEY_DOWN: name = "DOWN"; break;
49 case IRRECO_HARDKEY_LEFT: name = "LEFT"; break;
50 case IRRECO_HARDKEY_RIGHT: name = "RIGHT"; break;
51 case IRRECO_HARDKEY_SELECT: name = "SELECT"; break;
52 case IRRECO_HARDKEY_BACK: name = "BACK"; break;
53 case IRRECO_HARDKEY_MENU: name = "MENU"; break;
54 case IRRECO_HARDKEY_HOME: name = "HOME"; break;
55 case IRRECO_HARDKEY_FULLSCREEN: name = "FULLSCREEN"; break;
56 case IRRECO_HARDKEY_PLUS: name = "PLUS"; break;
57 case IRRECO_HARDKEY_MINUS: name = "MINUS"; break;
60 string = g_string_new(NULL);
61 if (name) {
62 g_string_printf(string, "%u %s", keyval, name);
63 } else {
64 g_string_printf(string, "%u", keyval);
66 IRRECO_RETURN_STR(g_string_free(string, FALSE));
70 /**
71 * @typedef IrrecoHardkeyMap
73 * Store data assosiated with device keys. That is only IrrecoCmdChains at the
74 * moment.
77 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
78 /* Prototypes */
79 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
80 static gboolean irreco_hardkey_map_free_foreach(gpointer key,
81 gpointer value,
82 gpointer user_data);
83 static gboolean irreco_hardkey_map_find_cmd_chain_id_foreach(gpointer key,
84 gpointer value,
85 gpointer data);
86 static void irreco_hardkey_map_chain_foreach_frapper(gpointer key,
87 gpointer value,
88 gpointer user_data);
92 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
93 /* Datatypes */
94 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
96 typedef struct _IrrecoHardkeyMapForeachData IrrecoHardkeyMapForeachData;
97 struct _IrrecoHardkeyMapForeachData {
98 IrrecoHardkeyMap *self;
99 IrrecoHardkeyMapFunc func;
100 gpointer user_data;
103 typedef struct _IrrecoHardkeyMapFindChainData IrrecoHardkeyMapFindChainData;
104 struct _IrrecoHardkeyMapFindChainData {
105 guint *keyval;
106 IrrecoCmdChainId id;
111 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
112 /* Construction & Destruction */
113 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
116 * @name Construction & Destruction
117 * @{
120 IrrecoHardkeyMap *irreco_hardkey_map_new(IrrecoCmdChainManager *manager)
122 IrrecoHardkeyMap *self;
123 IRRECO_ENTER
125 self = g_slice_new0(IrrecoHardkeyMap);
126 self->manager = manager;
127 self->table = g_hash_table_new(NULL, NULL);
128 IRRECO_RETURN_PTR(self);
131 void irreco_hardkey_map_free(IrrecoHardkeyMap *self)
133 IRRECO_ENTER
134 if (self == NULL) IRRECO_RETURN;
135 g_hash_table_foreach_remove(self->table,
136 irreco_hardkey_map_free_foreach, self);
137 g_hash_table_destroy(self->table);
138 g_slice_free(IrrecoHardkeyMap, self);
139 IRRECO_RETURN
141 static gboolean irreco_hardkey_map_free_foreach(gpointer key,
142 gpointer value,
143 gpointer user_data)
145 irreco_cmd_chain_manager_free_chain(
146 ((IrrecoHardkeyMap *) user_data )->manager,
147 (IrrecoCmdChainId) value );
148 return TRUE;
151 /** @} */
155 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
156 /* Functions */
157 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
160 * @name Private Functions
161 * @{
165 * Make sure some IrrecoCmdChain is assosiated with a keyval.
167 void irreco_hardkey_map_assosiate_chain(IrrecoHardkeyMap *self,
168 guint keyval)
170 IrrecoCmdChainId id;
171 IRRECO_ENTER
173 g_assert(self != NULL);
175 id = (IrrecoCmdChainId) g_hash_table_lookup(self->table,
176 (gpointer) keyval);
177 if (id == 0) {
178 gchar *hardkey_str;
179 hardkey_str = irreco_hardkey_to_str(keyval);
180 IRRECO_DEBUG("Creating new chain for key \"%s\".\n",
181 hardkey_str);
182 g_free(hardkey_str);
183 id = irreco_cmd_chain_manager_new_chain(self->manager);
184 g_hash_table_insert(self->table,
185 (gpointer) keyval,
186 (gpointer) id);
188 IRRECO_RETURN
192 * Assosiate IrrecoCmdChain with a particular keyval.
194 gboolean irreco_hardkey_map_assosiate_chain_with_id(IrrecoHardkeyMap *self,
195 guint keyval,
196 IrrecoCmdChainId id)
198 guint old_keyval;
199 gchar *hardkey_str;
200 IRRECO_ENTER
202 g_assert(self != NULL);
203 g_assert(id > 0);
205 if (irreco_hardkey_map_find_cmd_chain_id(self, id, &old_keyval)) {
207 gchar *keyval_str = irreco_hardkey_to_str(keyval);
208 gchar *old_keyval_str = irreco_hardkey_to_str(old_keyval);
210 if (keyval == old_keyval) {
211 IRRECO_DEBUG("Keyval \"%s\" is already assosiated "
212 "with command chain \"%i\".\n",
213 keyval_str, id);
214 } else {
216 IRRECO_DEBUG("Id \"%i\" cannot be assosiated with "
217 "keyval \"%s\" because is is already "
218 "assosited with keyval \"%s\".\n",
219 id, keyval_str, old_keyval_str);
222 g_free(keyval_str);
223 g_free(old_keyval_str);
224 IRRECO_RETURN_BOOL(FALSE);
227 hardkey_str = irreco_hardkey_to_str(keyval);
228 IRRECO_DEBUG("Creating new chain for key \"%s\" with id \"%i\".\n",
229 hardkey_str, id);
230 g_free(hardkey_str);
232 irreco_cmd_chain_manager_new_chain_with_id(self->manager, id);
233 g_hash_table_insert(self->table,
234 (gpointer) keyval,
235 (gpointer) id);
236 IRRECO_RETURN_BOOL(TRUE);
240 * Get a IrrecoCmdChain assosiated with a keyval.
242 IrrecoCmdChain *irreco_hardkey_map_get_cmd_chain(IrrecoHardkeyMap *self,
243 guint keyval)
245 gchar *hardkey_str;
246 IrrecoCmdChain *chain;
247 IrrecoCmdChainId id;
248 IRRECO_ENTER
250 g_assert(self != NULL);
252 id = (IrrecoCmdChainId) g_hash_table_lookup(self->table,
253 (gpointer) keyval);
254 chain = irreco_cmd_chain_manager_get_chain(self->manager, id);
256 hardkey_str = irreco_hardkey_to_str(keyval);
257 if (id == 0) {
258 IRRECO_DEBUG("Hardkey \"%s\" is not assositead with a command "
259 "chain.\n", hardkey_str);
260 } else {
261 IRRECO_DEBUG("Hardkey \"%s\" is assositead with command "
262 "chain \"%i\".\n", hardkey_str, id);
264 g_free(hardkey_str);
266 IRRECO_RETURN_PTR(chain);
270 * Find out which keyval is assosiated with some IrrecoCmdChainId.
272 * The assosiated keyval will se stored to keyval pointer.
274 * @return TRUE if found. FALSE if not assosiated.
276 gboolean irreco_hardkey_map_find_cmd_chain_id(IrrecoHardkeyMap *self,
277 IrrecoCmdChainId id,
278 guint *keyval)
280 IrrecoHardkeyMapFindChainData find_data;
281 IRRECO_ENTER
283 find_data.id = id;
284 find_data.keyval = keyval;
285 IRRECO_RETURN_BOOL(g_hash_table_find(self->table,
286 irreco_hardkey_map_find_cmd_chain_id_foreach,
287 &find_data) != NULL);
289 static gboolean irreco_hardkey_map_find_cmd_chain_id_foreach(gpointer key,
290 gpointer value,
291 gpointer data)
293 IrrecoHardkeyMapFindChainData *find_data = data;
294 if ((IrrecoCmdChainId) value == find_data->id) {
295 if (find_data->keyval != NULL) *find_data->keyval = (guint) key;
296 return TRUE;
298 return FALSE;
302 * Check if a IrrecoCmdChain has been assosiated with a keyval.
304 gboolean irreco_hardkey_map_cmd_chain_exists(IrrecoHardkeyMap *self, guint keyval)
306 IRRECO_ENTER
307 IRRECO_RETURN_BOOL(g_hash_table_lookup(self->table, (gpointer) keyval)
308 != NULL);
312 * For iterating trough the map.
314 void irreco_hardkey_map_chain_foreach(IrrecoHardkeyMap *self,
315 IrrecoHardkeyMapFunc func,
316 gpointer user_data)
318 IrrecoHardkeyMapForeachData data;
319 IRRECO_ENTER
321 data.self = self;
322 data.func = func;
323 data.user_data = user_data;
324 g_hash_table_foreach(self->table,
325 irreco_hardkey_map_chain_foreach_frapper, &data);
326 IRRECO_RETURN
328 static void irreco_hardkey_map_chain_foreach_frapper(gpointer key,
329 gpointer value,
330 gpointer user_data)
332 IrrecoCmdChain *chain;
333 IrrecoHardkeyMapForeachData *data = user_data;
334 IRRECO_ENTER
336 chain = irreco_cmd_chain_manager_get_chain(data->self->manager,
337 (IrrecoCmdChainId) value);
338 if (chain == NULL) IRRECO_RETURN;
339 data->func(data->self,
340 (guint) key,
341 (IrrecoCmdChainId) value,
342 chain,
343 data->user_data);
344 IRRECO_RETURN
347 /** @} */
348 /** @} */