summaryrefslogtreecommitdiff
path: root/src/backends/native/meta-keymap-native.c
blob: 8db24f883ca3e91fc2675dcda04cb79efb98aaf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2018 Red Hat
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Author: Carlos Garnacho <carlosg@gnome.org>
 */

#include "config.h"

#include "backends/meta-keymap-utils.h"
#include "backends/native/meta-input-thread.h"
#include "backends/native/meta-seat-native.h"

static const char *option_xkb_layout = "us";
static const char *option_xkb_variant = "";
static const char *option_xkb_options = "";

typedef struct _MetaKeymapNative MetaKeymapNative;

struct _MetaKeymapNative
{
  ClutterKeymap parent_instance;

  struct xkb_keymap *keymap;
  gboolean num_lock;
  gboolean caps_lock;
};

G_DEFINE_TYPE (MetaKeymapNative, meta_keymap_native,
               CLUTTER_TYPE_KEYMAP)

static void
meta_keymap_native_finalize (GObject *object)
{
  MetaKeymapNative *keymap = META_KEYMAP_NATIVE (object);

  xkb_keymap_unref (keymap->keymap);

  G_OBJECT_CLASS (meta_keymap_native_parent_class)->finalize (object);
}

static gboolean
meta_keymap_native_get_num_lock_state (ClutterKeymap *keymap)
{
  MetaKeymapNative *keymap_native = META_KEYMAP_NATIVE (keymap);

  return keymap_native->num_lock;
}

static gboolean
meta_keymap_native_get_caps_lock_state (ClutterKeymap *keymap)
{
  MetaKeymapNative *keymap_native = META_KEYMAP_NATIVE (keymap);

  return keymap_native->caps_lock;
}

static PangoDirection
meta_keymap_native_get_direction (ClutterKeymap *keymap)
{
  return PANGO_DIRECTION_NEUTRAL;
}

static void
meta_keymap_native_class_init (MetaKeymapNativeClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  ClutterKeymapClass *keymap_class = CLUTTER_KEYMAP_CLASS (klass);

  object_class->finalize = meta_keymap_native_finalize;

  keymap_class->get_num_lock_state = meta_keymap_native_get_num_lock_state;
  keymap_class->get_caps_lock_state = meta_keymap_native_get_caps_lock_state;
  keymap_class->get_direction = meta_keymap_native_get_direction;
}

static void
meta_keymap_native_init (MetaKeymapNative *keymap)
{
  struct xkb_context *ctx;
  struct xkb_rule_names names;

  names.rules = "evdev";
  names.model = "pc105";
  names.layout = option_xkb_layout;
  names.variant = option_xkb_variant;
  names.options = option_xkb_options;

  ctx = meta_create_xkb_context ();
  g_assert (ctx);
  keymap->keymap = xkb_keymap_new_from_names (ctx, &names, 0);
  xkb_context_unref (ctx);
}

void
meta_keymap_native_set_keyboard_map_in_impl (MetaKeymapNative  *keymap,
                                             struct xkb_keymap *xkb_keymap)
{
  g_return_if_fail (xkb_keymap != NULL);

  if (keymap->keymap)
    xkb_keymap_unref (keymap->keymap);
  keymap->keymap = xkb_keymap_ref (xkb_keymap);
}

struct xkb_keymap *
meta_keymap_native_get_keyboard_map_in_impl (MetaKeymapNative *keymap)
{
  return keymap->keymap;
}

void
meta_keymap_native_update_in_impl (MetaKeymapNative *keymap,
                                   struct xkb_state *xkb_state)
{
  keymap->num_lock =
    xkb_state_mod_name_is_active (xkb_state,
                                  XKB_MOD_NAME_NUM,
                                  XKB_STATE_MODS_LATCHED |
                                  XKB_STATE_MODS_LOCKED);
  keymap->caps_lock =
    xkb_state_mod_name_is_active (xkb_state,
                                  XKB_MOD_NAME_CAPS,
                                  XKB_STATE_MODS_LATCHED |
                                  XKB_STATE_MODS_LOCKED);
}