~elementary-os/ubuntu-package-imports/mutter-bionic

« back to all changes in this revision

Viewing changes to clutter/clutter/evdev/clutter-xkb-utils.c

  • Committer: RabbitBot
  • Date: 2018-04-11 14:49:36 UTC
  • Revision ID: rabbitbot@elementary.io-20180411144936-hgymqa9d8d1xfpbh
Initial import, version 3.28.0-2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Clutter.
 
3
 *
 
4
 * An OpenGL based 'interactive canvas' library.
 
5
 *
 
6
 * Copyright (C) 2010  Intel Corporation.
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2 of the License, or (at your option) any later version.
 
12
 *
 
13
 * This library is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * Lesser General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
 * Authors:
 
22
 *  Kristian Høgsberg
 
23
 *  Damien Lespiau <damien.lespiau@intel.com>
 
24
 */
 
25
 
 
26
#include "clutter-build-config.h"
 
27
 
 
28
#include "clutter-keysyms.h"
 
29
#include "clutter-event-private.h"
 
30
#include "clutter-xkb-utils.h"
 
31
 
 
32
/*
 
33
 * _clutter_event_new_from_evdev: Create a new Clutter ClutterKeyEvent
 
34
 * @device: a ClutterInputDevice
 
35
 * @stage: the stage the event should be delivered to
 
36
 * @xkb: XKB rules to translate the event
 
37
 * @_time: timestamp of the event
 
38
 * @key: a key code coming from a Linux input device
 
39
 * @state: TRUE if a press event, FALSE if a release event
 
40
 * @modifer_state: in/out
 
41
 *
 
42
 * Translate @key to a #ClutterKeyEvent using rules from xbbcommon.
 
43
 *
 
44
 * Return value: the new #ClutterEvent
 
45
 */
 
46
ClutterEvent *
 
47
_clutter_key_event_new_from_evdev (ClutterInputDevice *device,
 
48
                                   ClutterInputDevice *core_device,
 
49
                                   ClutterStage       *stage,
 
50
                                   struct xkb_state   *xkb_state,
 
51
                                   uint32_t            button_state,
 
52
                                   uint32_t            _time,
 
53
                                   xkb_keycode_t       key,
 
54
                                   uint32_t            state)
 
55
{
 
56
  ClutterEvent *event;
 
57
  xkb_keysym_t sym;
 
58
  const xkb_keysym_t *syms;
 
59
  char buffer[8];
 
60
  int n;
 
61
 
 
62
  if (state)
 
63
    event = clutter_event_new (CLUTTER_KEY_PRESS);
 
64
  else
 
65
    event = clutter_event_new (CLUTTER_KEY_RELEASE);
 
66
 
 
67
  /* We use a fixed offset of 8 because evdev starts KEY_* numbering from
 
68
   * 0, whereas X11's minimum keycode, for really stupid reasons, is 8.
 
69
   * So the evdev XKB rules are based on the keycodes all being shifted
 
70
   * upwards by 8. */
 
71
  key += 8;
 
72
 
 
73
  n = xkb_key_get_syms (xkb_state, key, &syms);
 
74
  if (n == 1)
 
75
    sym = syms[0];
 
76
  else
 
77
    sym = XKB_KEY_NoSymbol;
 
78
 
 
79
  event->key.device = core_device;
 
80
  event->key.stage = stage;
 
81
  event->key.time = _time;
 
82
  _clutter_xkb_translate_state (event, xkb_state, button_state);
 
83
  event->key.hardware_keycode = key;
 
84
  event->key.keyval = sym;
 
85
  clutter_event_set_source_device (event, device);
 
86
 
 
87
  n = xkb_keysym_to_utf8 (sym, buffer, sizeof (buffer));
 
88
 
 
89
  if (n == 0)
 
90
    {
 
91
      /* not printable */
 
92
      event->key.unicode_value = (gunichar) '\0';
 
93
    }
 
94
  else
 
95
    {
 
96
      event->key.unicode_value = g_utf8_get_char_validated (buffer, n);
 
97
      if (event->key.unicode_value == -1 || event->key.unicode_value == -2)
 
98
        event->key.unicode_value = (gunichar) '\0';
 
99
    }
 
100
 
 
101
  return event;
 
102
}
 
103
 
 
104
void
 
105
_clutter_xkb_translate_state (ClutterEvent     *event,
 
106
                              struct xkb_state *state,
 
107
                              uint32_t          button_state)
 
108
{
 
109
  _clutter_event_set_state_full (event,
 
110
                                 button_state,
 
111
                                 xkb_state_serialize_mods (state, XKB_STATE_MODS_DEPRESSED),
 
112
                                 xkb_state_serialize_mods (state, XKB_STATE_MODS_LATCHED),
 
113
                                 xkb_state_serialize_mods (state, XKB_STATE_MODS_LOCKED),
 
114
                                 xkb_state_serialize_mods (state, XKB_STATE_MODS_EFFECTIVE) | button_state);
 
115
}