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"
25 gchar
*irreco_hardkey_to_str(guint 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
);
47 g_string_printf(string
, "%u %s", keyval
, name
);
49 g_string_printf(string
, "%u", keyval
);
51 IRRECO_RETURN_STR(g_string_free(string
, FALSE
));
56 * @typedef IrrecoHardkeyMap
58 * Store data assosiated with device keys. That is only IrrecoCmdChains at the
62 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
64 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
65 static gboolean
irreco_hardkey_map_free_foreach(gpointer key
,
68 static gboolean
irreco_hardkey_map_find_cmd_chain_id_foreach(gpointer key
,
71 static void irreco_hardkey_map_chain_foreach_frapper(gpointer key
,
77 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
79 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
81 typedef struct _IrrecoHardkeyMapForeachData IrrecoHardkeyMapForeachData
;
82 struct _IrrecoHardkeyMapForeachData
{
83 IrrecoHardkeyMap
*self
;
84 IrrecoHardkeyMapFunc func
;
88 typedef struct _IrrecoHardkeyMapFindChainData IrrecoHardkeyMapFindChainData
;
89 struct _IrrecoHardkeyMapFindChainData
{
96 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
97 /* Construction & Destruction */
98 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
100 IrrecoHardkeyMap
*irreco_hardkey_map_new(IrrecoCmdChainManager
*manager
)
102 IrrecoHardkeyMap
*self
;
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
)
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
);
121 static gboolean
irreco_hardkey_map_free_foreach(gpointer key
,
125 irreco_cmd_chain_manager_free_chain(
126 ((IrrecoHardkeyMap
*) user_data
)->manager
,
127 (IrrecoCmdChainId
) value
);
133 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
135 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
138 * Make sure some IrrecoCmdChain is assosiated with a keyval.
140 void irreco_hardkey_map_assosiate_chain(IrrecoHardkeyMap
*self
,
146 g_assert(self
!= NULL
);
148 id
= (IrrecoCmdChainId
) g_hash_table_lookup(self
->table
,
152 hardkey_str
= irreco_hardkey_to_str(keyval
);
153 IRRECO_DEBUG("Creating new chain for key \"%s\".\n",
156 id
= irreco_cmd_chain_manager_new_chain(self
->manager
);
157 g_hash_table_insert(self
->table
,
165 * Assosiate IrrecoCmdChain with a particular keyval.
167 gboolean
irreco_hardkey_map_assosiate_chain_with_id(IrrecoHardkeyMap
*self
,
175 g_assert(self
!= NULL
);
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",
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
);
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",
205 irreco_cmd_chain_manager_new_chain_with_id(self
->manager
, id
);
206 g_hash_table_insert(self
->table
,
209 IRRECO_RETURN_BOOL(TRUE
);
213 * Get a IrrecoCmdChain assosiated with a keyval.
215 IrrecoCmdChain
*irreco_hardkey_map_get_cmd_chain(IrrecoHardkeyMap
*self
,
219 IrrecoCmdChain
*chain
;
223 g_assert(self
!= NULL
);
225 id
= (IrrecoCmdChainId
) g_hash_table_lookup(self
->table
,
227 chain
= irreco_cmd_chain_manager_get_chain(self
->manager
, id
);
229 hardkey_str
= irreco_hardkey_to_str(keyval
);
231 IRRECO_DEBUG("Hardkey \"%s\" is not assositead with a command "
232 "chain.\n", hardkey_str
);
234 IRRECO_DEBUG("Hardkey \"%s\" is assositead with command "
235 "chain \"%i\".\n", hardkey_str
, id
);
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
,
253 IrrecoHardkeyMapFindChainData find_data
;
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
,
266 IrrecoHardkeyMapFindChainData
*find_data
= data
;
267 if ((IrrecoCmdChainId
) value
== find_data
->id
) {
268 if (find_data
->keyval
!= NULL
) *find_data
->keyval
= (guint
) key
;
275 * Check if a IrrecoCmdChain has been assosiated with a keyval.
277 gboolean
irreco_hardkey_map_cmd_chain_exists(IrrecoHardkeyMap
*self
, guint keyval
)
280 IRRECO_RETURN_BOOL(g_hash_table_lookup(self
->table
, (gpointer
) keyval
)
285 * For iterating trough the map.
287 void irreco_hardkey_map_chain_foreach(IrrecoHardkeyMap
*self
,
288 IrrecoHardkeyMapFunc func
,
291 IrrecoHardkeyMapForeachData data
;
296 data
.user_data
= user_data
;
297 g_hash_table_foreach(self
->table
,
298 irreco_hardkey_map_chain_foreach_frapper
, &data
);
301 static void irreco_hardkey_map_chain_foreach_frapper(gpointer key
,
305 IrrecoCmdChain
*chain
;
306 IrrecoHardkeyMapForeachData
*data
= user_data
;
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
,
314 (IrrecoCmdChainId
) value
,