]> git.sesse.net Git - vlc/blob - src/misc/action.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / misc / action.c
1 /*****************************************************************************
2  * action.c: key to action mapping
3  *****************************************************************************
4  * Copyright © 2008 Rémi Denis-Courmont
5  *           © 2009 Antoine Cellerier
6  *
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.
11  *
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.
16  *
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  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include "../libvlc.h"
28 #include <vlc_keys.h>
29 #include <stdlib.h>
30 #include <limits.h>
31
32 static int keycmp (const void *a, const void *b)
33 {
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
40 }
41
42 /**
43  * Get the action associated with a VLC key code, if any.
44  */
45 static
46 vlc_key_t vlc_TranslateKey (const vlc_object_t *obj, uint_fast32_t keycode)
47 {
48     struct hotkey k = { .psz_action = NULL, .i_key = keycode, .i_action = 0 };
49     const struct hotkey *key;
50
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;
54 }
55
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)
58 {
59     (void)varname;
60     (void)prevkey;
61     (void)d;
62
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);
67 }
68
69
70 int vlc_InitActions (libvlc_int_t *libvlc)
71 {
72     struct hotkey *keys;
73
74     var_Create (libvlc, "key-pressed", VLC_VAR_INTEGER);
75     var_Create (libvlc, "key-action", VLC_VAR_INTEGER);
76
77     keys = malloc ((libvlc_actions_count + 1) * sizeof (*keys));
78     if (keys == NULL)
79     {
80         libvlc->p_hotkeys = NULL;
81         return VLC_ENOMEM;
82     }
83
84     /* Initialize from configuration */
85     for (size_t i = 0; i < libvlc_actions_count; i++)
86     {
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)
93         {
94             msg_Err (libvlc, "%s and %s are not ordered properly",
95                      libvlc_actions[i-1].name, libvlc_actions[i].name);
96             abort ();
97         }
98 #endif
99     }
100     qsort (keys, libvlc_actions_count, sizeof (*keys), keycmp);
101
102     keys[libvlc_actions_count].psz_action = NULL;
103     keys[libvlc_actions_count].i_key = 0;
104     keys[libvlc_actions_count].i_action = 0;
105
106     libvlc->p_hotkeys = keys;
107     var_AddCallback (libvlc, "key-pressed", vlc_key_to_action, NULL);
108     return VLC_SUCCESS;
109 }
110
111 void vlc_DeinitActions (libvlc_int_t *libvlc)
112 {
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);
117 }
118
119
120 static int actcmp(const void *key, const void *ent)
121 {
122     const struct action *act = ent;
123     return strcmp(key, act->name);
124 }
125
126 vlc_key_t vlc_GetActionId(const char *name)
127 {
128     const struct action *act;
129
130     act = bsearch(name, libvlc_actions, libvlc_actions_count, sizeof(*act),
131                   actcmp);
132     return (act != NULL) ? act->value : ACTIONID_NONE;
133 }
134