Mir
xkb_mapper.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2013-2016 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by:
17  * Robert Carr <robert.carr@canonical.com>
18  * Andreas Pokorny <andreas.pokorny@canonical.com>
19  */
20 
21 #ifndef MIR_INPUT_RECEIVER_XKB_MAPPER_H_
22 #define MIR_INPUT_RECEIVER_XKB_MAPPER_H_
23 
24 #include "mir/input/key_mapper.h"
25 #include "mir/optional_value.h"
26 
27 #include <mutex>
28 #include <unordered_map>
29 
30 namespace mir
31 {
32 namespace input
33 {
34 
35 using XKBContextPtr = std::unique_ptr<xkb_context, void(*)(xkb_context*)>;
37 
38 using XKBKeymapPtr = std::unique_ptr<xkb_keymap, void(*)(xkb_keymap*)>;
39 XKBKeymapPtr make_unique_keymap(xkb_context* context, Keymap const& keymap);
40 XKBKeymapPtr make_unique_keymap(xkb_context* context, char const* buffer, size_t size);
41 
42 using XKBStatePtr = std::unique_ptr<xkb_state, void(*)(xkb_state*)>;
43 XKBStatePtr make_unique_state(xkb_keymap* keymap);
44 
45 namespace receiver
46 {
47 
48 class XKBMapper : public KeyMapper
49 {
50 public:
51  XKBMapper();
52 
53  void set_key_state(MirInputDeviceId id, std::vector<uint32_t> const& key_state) override;
54  void set_keymap_for_device(MirInputDeviceId id, Keymap const& map) override;
55  void set_keymap_for_device(MirInputDeviceId id, char const* buffer, size_t len) override;
56  void set_keymap_for_all_devices(Keymap const& map) override;
57  void set_keymap_for_all_devices(char const* buffer, size_t len) override;
58  void clear_keymap_for_device(MirInputDeviceId id) override;
59  void clear_all_keymaps() override;
60  void map_event(MirEvent& event) override;
61  MirInputEventModifiers modifiers() const override;
62 
63 protected:
64  XKBMapper(XKBMapper const&) = delete;
65  XKBMapper& operator=(XKBMapper const&) = delete;
66 
67 private:
68  void set_keymap(MirInputDeviceId id, XKBKeymapPtr map);
69  void set_keymap(XKBKeymapPtr map);
70  void update_modifier();
71 
72  std::mutex mutable guard;
73 
74  struct XkbMappingState
75  {
76  explicit XkbMappingState(std::shared_ptr<xkb_keymap> const& keymap);
77  void set_key_state(std::vector<uint32_t> const& key_state);
78  bool update_and_map(MirEvent& event);
79  xkb_keysym_t update_state(uint32_t scan_code, MirKeyboardAction direction);
80  std::shared_ptr<xkb_keymap> const keymap;
81  XKBStatePtr state;
82  MirInputEventModifiers modifier_state{0};
83  // TODO optional compose key state..
84  };
85 
86  XkbMappingState* get_keymapping_state(MirInputDeviceId id);
87 
88  XKBContextPtr context;
89  std::shared_ptr<xkb_keymap> default_keymap;
90 
92  std::unordered_map<MirInputDeviceId, XkbMappingState> device_mapping;
93 };
94 }
95 }
96 }
97 
98 #endif // MIR_INPUT_RECEIVER_XKB_MAPPER_H_
All things Mir.
Definition: atomic_callback.h:25
XKBContextPtr make_unique_context()
Definition: xkb_mapper.cpp:81
std::unique_ptr< xkb_keymap, void(*)(xkb_keymap *)> XKBKeymapPtr
Definition: xkb_mapper.h:38
MirInputEventModifiers modifiers() const override
XKBKeymapPtr make_unique_keymap(xkb_context *context, Keymap const &keymap)
Definition: xkb_mapper.cpp:86
void clear_keymap_for_device(MirInputDeviceId id) override
Remove the specific keymap defined for device identified via the id.
void map_event(MirEvent &event) override
Map the given event based on the key maps configured.
std::unique_ptr< xkb_context, void(*)(xkb_context *)> XKBContextPtr
Definition: xkb_mapper.h:35
MirKeyboardAction
Possible actions for changing key state.
Definition: keyboard_event.h:46
unsigned int MirInputEventModifiers
Definition: input_event.h:66
std::unique_ptr< xkb_state, void(*)(xkb_state *)> XKBStatePtr
Definition: xkb_mapper.h:42
void set_keymap_for_device(MirInputDeviceId id, Keymap const &map) override
Set a keymap for the device id.
Definition: xkb_mapper.h:48
The key mapping interface KeyMapper allows configuring a key map for each device individually or a si...
Definition: key_mapper.h:45
Definition: keymap.h:30
int64_t MirInputDeviceId
Definition: input_event.h:35
XKBStatePtr make_unique_state(xkb_keymap *keymap)
Definition: xkb_mapper.cpp:106
void set_keymap_for_all_devices(Keymap const &map) override
Set a default keymap for all devices.
XKBMapper & operator=(XKBMapper const &)=delete
int const size
Definition: make_socket_rpc_channel.cpp:51
Definition: event.h:30
void set_key_state(MirInputDeviceId id, std::vector< uint32_t > const &key_state) override
Update the key state of device id, with the given sequence of pressed scan codes. ...

Copyright © 2012-2015 Canonical Ltd.
Generated on Thu Sep 8 14:50:19 UTC 2016