]> Pileus Git - ~andy/gtk/blob - gdk/wayland/gdkkeys-wayland.c
Drop the Motif DND protocol
[~andy/gtk] / gdk / wayland / gdkkeys-wayland.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2000 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 #include "config.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include <sys/mman.h>
34
35 #include "gdk.h"
36 #include "gdkwayland.h"
37
38 #include "gdkprivate-wayland.h"
39 #include "gdkinternals.h"
40 #include "gdkkeysprivate.h"
41
42 #include <xkbcommon/xkbcommon.h>
43
44 typedef struct _GdkWaylandKeymap          GdkWaylandKeymap;
45 typedef struct _GdkWaylandKeymapClass     GdkWaylandKeymapClass;
46
47 struct _GdkWaylandKeymap
48 {
49   GdkKeymap parent_instance;
50
51   struct xkb_keymap *xkb_keymap;
52   struct xkb_state *xkb_state;
53 };
54
55 struct _GdkWaylandKeymapClass
56 {
57   GdkKeymapClass parent_class;
58 };
59
60 #define GDK_TYPE_WAYLAND_KEYMAP          (_gdk_wayland_keymap_get_type ())
61 #define GDK_WAYLAND_KEYMAP(object)       (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_WAYLAND_KEYMAP, GdkWaylandKeymap))
62 #define GDK_IS_WAYLAND_KEYMAP(object)    (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_WAYLAND_KEYMAP))
63
64 G_DEFINE_TYPE (GdkWaylandKeymap, _gdk_wayland_keymap, GDK_TYPE_KEYMAP)
65
66 static void
67 gdk_wayland_keymap_finalize (GObject *object)
68 {
69   G_OBJECT_CLASS (_gdk_wayland_keymap_parent_class)->finalize (object);
70 }
71
72 static PangoDirection
73 gdk_wayland_keymap_get_direction (GdkKeymap *keymap)
74 {
75     return PANGO_DIRECTION_NEUTRAL;
76 }
77
78 static gboolean
79 gdk_wayland_keymap_have_bidi_layouts (GdkKeymap *keymap)
80 {
81     return FALSE;
82 }
83
84 static gboolean
85 gdk_wayland_keymap_get_caps_lock_state (GdkKeymap *keymap)
86 {
87   return xkb_state_led_name_is_active (GDK_WAYLAND_KEYMAP (keymap)->xkb_state,
88                                        XKB_LED_NAME_CAPS);
89 }
90
91 static gboolean
92 gdk_wayland_keymap_get_num_lock_state (GdkKeymap *keymap)
93 {
94   return xkb_state_led_name_is_active (GDK_WAYLAND_KEYMAP (keymap)->xkb_state,
95                                        XKB_LED_NAME_NUM);
96 }
97
98 static gboolean
99 gdk_wayland_keymap_get_entries_for_keyval (GdkKeymap     *keymap,
100                                            guint          keyval,
101                                            GdkKeymapKey **keys,
102                                            gint          *n_keys)
103 {
104   if (n_keys)
105     *n_keys = 1;
106   if (keys)
107     {
108       *keys = g_new0 (GdkKeymapKey, 1);
109       (*keys)->keycode = keyval;
110     }
111
112   return TRUE;
113 }
114
115 static gboolean
116 gdk_wayland_keymap_get_entries_for_keycode (GdkKeymap     *keymap,
117                                             guint          hardware_keycode,
118                                             GdkKeymapKey **keys,
119                                             guint        **keyvals,
120                                             gint          *n_entries)
121 {
122  if (n_entries)
123     *n_entries = 1;
124   if (keys)
125     {
126       *keys = g_new0 (GdkKeymapKey, 1);
127       (*keys)->keycode = hardware_keycode;
128     }
129   if (keyvals)
130     {
131       *keyvals = g_new0 (guint, 1);
132       (*keyvals)[0] = hardware_keycode;
133     }
134   return TRUE;
135 }
136
137 static guint
138 gdk_wayland_keymap_lookup_key (GdkKeymap          *keymap,
139                                const GdkKeymapKey *key)
140 {
141   return key->keycode;
142 }
143
144 static gboolean
145 gdk_wayland_keymap_translate_keyboard_state (GdkKeymap       *keymap,
146                                              guint            hardware_keycode,
147                                              GdkModifierType  state,
148                                              gint             group,
149                                              guint           *keyval,
150                                              gint            *effective_group,
151                                              gint            *level,
152                                              GdkModifierType *consumed_modifiers)
153 {
154   g_return_val_if_fail (keymap == NULL || GDK_IS_KEYMAP (keymap), FALSE);
155   g_return_val_if_fail (group < 4, FALSE);
156
157   if (keyval)
158     *keyval = hardware_keycode;
159   if (effective_group)
160     *effective_group = 0;
161   if (level)
162     *level = 0;
163   if (consumed_modifiers)
164     *consumed_modifiers = 0;
165
166   return TRUE;
167 }
168
169 static void
170 gdk_wayland_keymap_add_virtual_modifiers (GdkKeymap       *keymap,
171                                           GdkModifierType *state)
172 {
173   return;
174 }
175
176 static gboolean
177 gdk_wayland_keymap_map_virtual_modifiers (GdkKeymap       *keymap,
178                                           GdkModifierType *state)
179 {
180   return TRUE;
181 }
182
183 static void
184 _gdk_wayland_keymap_class_init (GdkWaylandKeymapClass *klass)
185 {
186   GObjectClass *object_class = G_OBJECT_CLASS (klass);
187   GdkKeymapClass *keymap_class = GDK_KEYMAP_CLASS (klass);
188
189   object_class->finalize = gdk_wayland_keymap_finalize;
190
191   keymap_class->get_direction = gdk_wayland_keymap_get_direction;
192   keymap_class->have_bidi_layouts = gdk_wayland_keymap_have_bidi_layouts;
193   keymap_class->get_caps_lock_state = gdk_wayland_keymap_get_caps_lock_state;
194   keymap_class->get_num_lock_state = gdk_wayland_keymap_get_num_lock_state;
195   keymap_class->get_entries_for_keyval = gdk_wayland_keymap_get_entries_for_keyval;
196   keymap_class->get_entries_for_keycode = gdk_wayland_keymap_get_entries_for_keycode;
197   keymap_class->lookup_key = gdk_wayland_keymap_lookup_key;
198   keymap_class->translate_keyboard_state = gdk_wayland_keymap_translate_keyboard_state;
199   keymap_class->add_virtual_modifiers = gdk_wayland_keymap_add_virtual_modifiers;
200   keymap_class->map_virtual_modifiers = gdk_wayland_keymap_map_virtual_modifiers;
201 }
202
203 static void
204 _gdk_wayland_keymap_init (GdkWaylandKeymap *keymap)
205 {
206 }
207
208 GdkKeymap *
209 _gdk_wayland_keymap_new ()
210 {
211   GdkWaylandKeymap *keymap;
212   struct xkb_context *context;
213   struct xkb_rule_names names;
214
215   keymap = g_object_new (_gdk_wayland_keymap_get_type(), NULL);
216
217   context = xkb_context_new (0);
218
219   names.rules = "evdev";
220   names.model = "pc105";
221   names.layout = "us";
222   names.variant = "";
223   names.options = "";
224   keymap->xkb_keymap = xkb_keymap_new_from_names (context, &names, 0);
225   keymap->xkb_state = xkb_state_new (keymap->xkb_keymap);
226   xkb_context_unref (context);
227
228   return GDK_KEYMAP (keymap);
229 }
230
231 GdkKeymap *
232 _gdk_wayland_keymap_new_from_fd (uint32_t format,
233                                  uint32_t fd, uint32_t size)
234 {
235   GdkWaylandKeymap *keymap;
236   struct xkb_context *context;
237   char *map_str;
238
239   keymap = g_object_new (_gdk_wayland_keymap_get_type(), NULL);
240
241   context = xkb_context_new (0);
242
243   map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
244   if (map_str == MAP_FAILED) {
245     close(fd);
246     return NULL;
247   }
248
249   keymap->xkb_keymap = xkb_keymap_new_from_string (context, map_str, format, 0);
250   munmap (map_str, size);
251   close (fd);
252   keymap->xkb_state = xkb_state_new (keymap->xkb_keymap);
253   xkb_context_unref (context);
254
255   return GDK_KEYMAP (keymap);
256 }
257
258 struct xkb_keymap *_gdk_wayland_keymap_get_xkb_keymap (GdkKeymap *keymap)
259 {
260   return GDK_WAYLAND_KEYMAP (keymap)->xkb_keymap;
261 }
262
263 struct xkb_state *_gdk_wayland_keymap_get_xkb_state (GdkKeymap *keymap)
264 {
265   return GDK_WAYLAND_KEYMAP (keymap)->xkb_state;
266 }