qt4: improve code readability.
[vlc/asuraparaju-public.git] / src / misc / action.c
blob1f2657e1563d921a00de8f628a822b720c0643c2
1 /*****************************************************************************
2 * action.c: key to action mapping
3 *****************************************************************************
4 * Copyright © 2008 Rémi Denis-Courmont
5 * © 2009 Antoine Cellerier
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program 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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <vlc_common.h>
27 #include "../libvlc.h"
28 #include <vlc_keys.h>
29 #include <stdlib.h>
30 #include <limits.h>
32 static int keycmp (const void *a, const void *b)
34 const struct hotkey *ka = a, *kb = b;
35 #if (INT_MAX >= 0x7fffffff)
36 return ka->i_key - kb->i_key;
37 #else
38 return (ka->i_key < kb->i_key) ? -1 : (ka->i_key > kb->i_key) ? +1 : 0;
39 #endif
42 /**
43 * Get the action associated with a VLC key code, if any.
45 static
46 vlc_key_t vlc_TranslateKey (const vlc_object_t *obj, uint_fast32_t keycode)
48 struct hotkey k = { .psz_action = NULL, .i_key = keycode, .i_action = 0 };
49 const struct hotkey *key;
51 key = bsearch (&k, obj->p_libvlc->p_hotkeys, libvlc_actions_count,
52 sizeof (*key), keycmp);
53 return (key != NULL) ? key->i_action : ACTIONID_NONE;
56 static int vlc_key_to_action (vlc_object_t *libvlc, const char *varname,
57 vlc_value_t prevkey, vlc_value_t curkey, void *d)
59 (void)varname;
60 (void)prevkey;
61 (void)d;
63 vlc_key_t action = vlc_TranslateKey (libvlc, curkey.i_int);
64 if (!action)
65 return VLC_SUCCESS;
66 return var_SetInteger (libvlc, "key-action", action);
70 int vlc_InitActions (libvlc_int_t *libvlc)
72 struct hotkey *keys;
74 var_Create (libvlc, "key-pressed", VLC_VAR_INTEGER);
75 var_Create (libvlc, "key-action", VLC_VAR_INTEGER);
77 keys = malloc ((libvlc_actions_count + 1) * sizeof (*keys));
78 if (keys == NULL)
80 libvlc->p_hotkeys = NULL;
81 return VLC_ENOMEM;
84 /* Initialize from configuration */
85 for (size_t i = 0; i < libvlc_actions_count; i++)
87 keys[i].psz_action = libvlc_actions[i].name;
88 keys[i].i_key = var_InheritInteger (libvlc, libvlc_actions[i].name );
89 keys[i].i_action = libvlc_actions[i].value;
90 #ifndef NDEBUG
91 if (i > 0
92 && strcmp (libvlc_actions[i-1].name, libvlc_actions[i].name) >= 0)
94 msg_Err (libvlc, "%s and %s are not ordered properly",
95 libvlc_actions[i-1].name, libvlc_actions[i].name);
96 abort ();
98 #endif
100 qsort (keys, libvlc_actions_count, sizeof (*keys), keycmp);
102 keys[libvlc_actions_count].psz_action = NULL;
103 keys[libvlc_actions_count].i_key = 0;
104 keys[libvlc_actions_count].i_action = 0;
106 libvlc->p_hotkeys = keys;
107 var_AddCallback (libvlc, "key-pressed", vlc_key_to_action, NULL);
108 return VLC_SUCCESS;
111 void vlc_DeinitActions (libvlc_int_t *libvlc)
113 if (unlikely(libvlc->p_hotkeys == NULL))
114 return;
115 var_DelCallback (libvlc, "key-pressed", vlc_key_to_action, NULL);
116 free ((void *)libvlc->p_hotkeys);
120 static int actcmp(const void *key, const void *ent)
122 const struct action *act = ent;
123 return strcmp(key, act->name);
126 vlc_key_t vlc_GetActionId(const char *name)
128 const struct action *act;
130 act = bsearch(name, libvlc_actions, libvlc_actions_count, sizeof(*act),
131 actcmp);
132 return (act != NULL) ? act->value : ACTIONID_NONE;