move around - flatter.
[profile/ivi/evas.git] / src / lib / canvas / evas_key.c
1 #include "evas_common.h"
2 #include "evas_private.h"
3
4 /* private calls */
5
6 static int
7 evas_key_modifier_number(const Evas_Modifier *m, const char *keyname)
8 {
9    int i;
10
11    for (i = 0; i < m->mod.count; i++)
12      {
13         if (!strcmp(m->mod.list[i], keyname)) return i;
14      }
15    return -1;
16 }
17
18 static int
19 evas_key_lock_number(const Evas_Lock *l, const char *keyname)
20 {
21    int i;
22
23    for (i = 0; i < l->lock.count; i++)
24      {
25         if (!strcmp(l->lock.list[i], keyname)) return i;
26      }
27    return -1;
28 }
29
30 /* local calls */
31
32 /* public calls */
33
34 /**
35  * Returns a handle to the modifiers available in the system.  This is required to check
36  * for modifiers with the evas_key_modifier_is_set_get function.
37  *
38  *
39  * @see evas_key_modifier_add
40  * @see evas_key_modifier_del
41  * @see evas_key_modifier_on
42  * @see evas_key_modifier_off
43  * @see evas_key_modifier_is_set_get
44  *
45  * @param e The pointer to the Evas Canvas
46  *
47  * @return An Evas_Modifier handle to query the modifier subsystem with
48  *      evas_key_modifier_is_set_get, or NULL on error.
49  */
50 EAPI const Evas_Modifier *
51 evas_key_modifier_get(const Evas *e)
52 {
53    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
54    return NULL;
55    MAGIC_CHECK_END();
56    return &(e->modifiers);
57 }
58
59 /**
60  * Returns a handle to the locks available in the system.  This is required to check for
61  * locks with the evas_key_lock_is_set_get function.
62  *
63  * @see evas_key_lock_add
64  * @see evas_key_lock_del
65  * @see evas_key_lock_on
66  * @see evas_key_lock_off
67  *
68  * @see evas_key_lock_is_set_get
69  * @param e The pointer to the Evas Canvas
70  *
71  * @return An Evas_Lock handle to query the lock subsystem with
72  *      evas_key_lock_is_set_get, or NULL on error.
73  */
74 EAPI const Evas_Lock *
75 evas_key_lock_get(const Evas *e)
76 {
77    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
78    return NULL;
79    MAGIC_CHECK_END();
80    return &(e->locks);
81 }
82
83 /**
84  * Checks the state of a given modifier.  If the modifier is set, such as shift being pressed
85  * this function returns true.
86  * 
87  * @see evas_key_modifier_add
88  * @see evas_key_modifier_del
89  * @see evas_key_modifier_get
90  * @see evas_key_modifier_on
91  * @see evas_key_modifier_off
92  * 
93  * @param m The current modifier set as returned by evas_key_modifier_get.
94  * @param keyname The name of the key to check its status.
95  *
96  * @return 1 if the @p keyname is on, 0 otherwise.
97  */
98 EAPI Evas_Bool
99 evas_key_modifier_is_set(const Evas_Modifier *m, const char *keyname)
100 {
101    Evas_Modifier_Mask num;
102    int n;
103
104    if (!m) return 0;
105    if (!keyname) return 0;
106    n = evas_key_modifier_number(m, keyname);
107    if (n < 0) return 0;
108    num = (Evas_Modifier_Mask)n;
109    num = 1 << num;
110    if (m->mask & num) return 1;
111    return 0;
112 }
113
114 /**
115  * Checks the state of a given lock.  If the lock is set, such as caps lock, this function
116  * returns true.
117  *
118  * @see evas_key_lock_get
119  * @see evas_key_lock_add
120  * @see evas_key_lock_del
121  * @see evas_key_lock_on
122  * @see evas_key_lock_off
123  *
124  * @param l The current lock set as returned by evas_key_lock_get.
125  * @param keyname The name of the lock to add the the list.
126  * 
127  * @param 1 if the @p keyname kock is set, 0 otherwise.
128  */
129 EAPI Evas_Bool
130 evas_key_lock_is_set(const Evas_Lock *l, const char *keyname)
131 {
132    Evas_Modifier_Mask num;
133    int n;
134
135    if (!l) return 0;
136    if (!keyname) return 0;
137    n = evas_key_lock_number(l, keyname);
138    if (n < 0) return 0;
139    num = (Evas_Modifier_Mask)n;
140    num = 1 << num;
141    if (l->mask & num) return 1;
142    return 0;
143 }
144
145 /**
146  * Adds the @p keyname to the current list of modifiers.
147  * 
148  * Modifiers can be keys like shift, alt and ctrl, as well as user defined.  This allows
149  * custom modifiers to be added to the evas system as run time.  It is then possible to set
150  * and unset the modifier for other parts of the program to check and act on.
151  *
152  * @see evas_key_modifier_del
153  * @see evas_key_modifier_get
154  * @see evas_key_modifier_on
155  * @see evas_key_modifier_off
156  * @see evas_key_modifier_is_set_get
157  * 
158  * @param e The pointer to the Evas Canvas
159  * @param keyname The name of the modifier to add to the list.
160  */
161 EAPI void
162 evas_key_modifier_add(Evas *e, const char *keyname)
163 {
164    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
165    return;
166    MAGIC_CHECK_END();
167    if (!keyname) return;
168    if (e->modifiers.mod.count >= 64) return;
169    evas_key_modifier_del(e, keyname);
170    e->modifiers.mod.count++;
171    e->modifiers.mod.list = realloc(e->modifiers.mod.list, e->modifiers.mod.count * sizeof(char *));
172    e->modifiers.mod.list[e->modifiers.mod.count - 1] = strdup(keyname);
173    e->modifiers.mask = 0;
174 }
175
176 /**
177  * Removes the @p keyname from the current list of modifiers.
178  *
179  * @see evas_key_modifier_add
180  * @see evas_key_modifier_get
181  * @see evas_key_modifier_on
182  * @see evas_key_modifier_off
183  * @see evas_key_modifier_is_set_get
184  * 
185  * @param e The pointer to the Evas Canvas
186  * @param keyname The name of the key to remove from the modifiers list.
187  */
188 EAPI void
189 evas_key_modifier_del(Evas *e, const char *keyname)
190 {
191    int i;
192
193    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
194    return;
195    MAGIC_CHECK_END();
196    if (!keyname) return;
197    for (i = 0; i < e->modifiers.mod.count; i++)
198      {
199         if (!strcmp(e->modifiers.mod.list[i], keyname))
200           {
201              int j;
202
203              free(e->modifiers.mod.list[i]);
204              e->modifiers.mod.count--;
205              for (j = i; j < e->modifiers.mod.count; j++)
206                e->modifiers.mod.list[j] = e->modifiers.mod.list[j + 1];
207              e->modifiers.mask = 0;
208              return;
209           }
210      }
211 }
212
213 /**
214  * Adds the @p keyname to the current list of locks.
215  *
216  * Locks can be keys like caps lock, num lock or scroll lock, as well as user defined.  This
217  * allows custom locks to be added to the evas system at run time.  It is then possible to
218  * set and unset the lock for other parts of the program to check and act on.
219  * 
220  * @see evas_key_lock_get
221  * @see evas_key_lock_del
222  * @see evas_key_lock_on
223  * @see evas_key_lock_off
224  * 
225  * @param e The pointer to the Evas Canvas
226  * @param keyname The name of the key to remove from the modifier list.
227  */
228 EAPI void
229 evas_key_lock_add(Evas *e, const char *keyname)
230 {
231    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
232    return;
233    MAGIC_CHECK_END();
234    if (!keyname) return;
235    if (e->locks.lock.count >= 64) return;
236    evas_key_lock_del(e, keyname);
237    e->locks.lock.count++;
238    e->locks.lock.list = realloc(e->locks.lock.list, e->locks.lock.count * sizeof(char *));
239    e->locks.lock.list[e->locks.lock.count - 1] = strdup(keyname);
240    e->locks.mask = 0;
241 }
242
243 /**
244  * Removes The @p keyname from the current list of locks.
245  *
246  * @see evas_key_lock_get
247  * @see evas_key_lock_add
248  * @see evas_key_lock_on
249  * @see evas_key_lock_off
250  * 
251  * @param e The pointer to the Evas Canvas
252  * @param keyname The name of the key to remove from the lock list.
253  */
254 EAPI void
255 evas_key_lock_del(Evas *e, const char *keyname)
256 {
257    int i;
258
259    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
260    return;
261    MAGIC_CHECK_END();
262    if (!keyname) return;
263    e->locks.mask = 0;
264    for (i = 0; i < e->locks.lock.count; i++)
265      {
266         if (!strcmp(e->locks.lock.list[i], keyname))
267           {
268              int j;
269
270              free(e->locks.lock.list[i]);
271              e->locks.lock.count--;
272              for (j = i; j < e->locks.lock.count; j++)
273                e->locks.lock.list[j] = e->locks.lock.list[j + 1];
274              e->locks.mask = 0;
275              return;
276           }
277      }
278 }
279
280 /**
281  * Enables or turns on the modifier with name @p keyname.
282  * 
283  * @see evas_key_modifier_add
284  * @see evas_key_modifier_get
285  * @see evas_key_modifier_off
286  * @see evas_key_modifier_is_set_get
287  * 
288  * @param e The pointer to the Evas Canvas
289  * @param keyname The name of the modifier to set.
290  */
291 EAPI void
292 evas_key_modifier_on(Evas *e, const char *keyname)
293 {
294    Evas_Modifier_Mask num;
295    int n;
296
297    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
298    return;
299    MAGIC_CHECK_END();
300    n = (Evas_Modifier_Mask)evas_key_modifier_number(&(e->modifiers), keyname);
301    if (n < 0) return;
302    num = (Evas_Modifier_Mask)n;
303    num = 1 << num;
304    e->modifiers.mask |= num;
305 }
306
307 /**
308  * Disables or turns off the modifier with name @p keyname.
309  * 
310  * @see evas_key_modifier_add
311  * @see evas_key_modifier_get
312  * @see evas_key_modifier_on
313  * @see evas_key_modifier_is_set_get
314  * 
315  * @param e The pointer to the Evas Canvas
316  * @param keyname The name of the modifier to un-set.
317  */
318 EAPI void
319 evas_key_modifier_off(Evas *e, const char *keyname)
320 {
321    Evas_Modifier_Mask num;
322    int n;
323
324    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
325    return;
326    MAGIC_CHECK_END();
327    n = evas_key_modifier_number(&(e->modifiers), keyname);
328    if (n < 0) return;
329    num = (Evas_Modifier_Mask)n;
330    num = 1 << num;
331    e->modifiers.mask &= ~num;
332 }
333
334 /**
335  * Enables or turns on the lock with name @p keyname.
336  *
337  * @see evas_key_lock_get
338  * @see evas_key_lock_add
339  * @see evas_key_lock_del
340  * @see evas_key_lock_off
341  * 
342  * @param e The pointer to the Evas Canvas
343  * @param keyname The name of the lock to set.
344  */
345 EAPI void
346 evas_key_lock_on(Evas *e, const char *keyname)
347 {
348    Evas_Modifier_Mask num;
349    int n;
350
351    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
352    return;
353    MAGIC_CHECK_END();
354    n = evas_key_lock_number(&(e->locks), keyname);
355    if (n < 0) return;
356    num = (Evas_Modifier_Mask)n;
357    num = 1 << num;
358    e->locks.mask |= num;
359 }
360
361 /**
362  * Disables or turns off the lock with name @p keyname.
363  *
364  * @see evas_key_lock_get
365  * @see evas_key_lock_add
366  * @see evas_key_lock_del
367  * @see evas_key_lock_on
368  * 
369  * @param e The pointer to the Evas Canvas
370  * @param keyname The name of the lock to un-set.
371  */
372 EAPI void
373 evas_key_lock_off(Evas *e, const char *keyname)
374 {
375    Evas_Modifier_Mask num;
376    int n;
377
378    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
379    return;
380    MAGIC_CHECK_END();
381    n = evas_key_lock_number(&(e->locks), keyname);
382    if (n < 0) return;
383    num = (Evas_Modifier_Mask)n;
384    num = 1 << num;
385    e->locks.mask &= ~num;
386 }
387
388 /* errr need to add key grabbing/ungrabbing calls - missing modifier stuff. */
389
390 /**
391  * Creates a bit mask from the @p keyname for use with events to check for the presence
392  * of the @p keyname modifier.
393  *
394  * @see evas_key_modifier_add
395  * @see evas_key_modifier_get
396  * @see evas_key_modifier_on
397  * @see evas_key_modifier_off
398  * @see evas_key_modifier_is_set_get
399  * 
400  * @param keyname The name of the modifier to create the mask for.
401  *
402  * @returns the bit mask or 0 if the @p keyname wasn't registered as a modifier.
403  */
404 EAPI Evas_Modifier_Mask
405 evas_key_modifier_mask_get(const Evas *e, const char *keyname)
406 {
407    Evas_Modifier_Mask num;
408    int n;
409
410    MAGIC_CHECK(e, Evas, MAGIC_EVAS);
411    return 0;
412    MAGIC_CHECK_END();
413    if (!keyname) return 0;
414    n = evas_key_modifier_number(&(e->modifiers), keyname);
415    if (n < 0) return 0;
416    num = (Evas_Modifier_Mask)n;
417    return 1 << num;
418 }