1 #![allow(dead_code, non_camel_case_types)]
2 #![cfg_attr(rustfmt, rustfmt_skip)]
3 
4 use std::os::raw::{c_char, c_int, c_void, c_uint};
5 
6 pub const XKB_MOD_NAME_SHIFT   : &[u8]  = b"Shift\0";
7 pub const XKB_MOD_NAME_CAPS    : &[u8]  = b"Lock\0";
8 pub const XKB_MOD_NAME_CTRL    : &[u8]  = b"Control\0";
9 pub const XKB_MOD_NAME_ALT     : &[u8]  = b"Mod1\0";
10 pub const XKB_MOD_NAME_NUM     : &[u8]  = b"Mod2\0";
11 pub const XKB_MOD_NAME_LOGO    : &[u8]  = b"Mod4\0";
12 
13 pub const XKB_LED_NAME_CAPS    : &[u8]  = b"Caps Lock\0";
14 pub const XKB_LED_NAME_NUM     : &[u8]  = b"Num Lock\0";
15 pub const XKB_LED_NAME_SCROLL  : &[u8]  = b"Scroll Lock\0";
16 
17 pub struct xkb_context;
18 pub struct xkb_keymap;
19 pub struct xkb_state;
20 pub struct xkb_compose_table;
21 pub struct xkb_compose_state;
22 
23 pub type xkb_keycode_t = u32;
24 pub type xkb_keysym_t = u32;
25 pub type xkb_layout_index_t = u32;
26 pub type xkb_layout_mask_t = u32;
27 pub type xkb_level_index_t = u32;
28 pub type xkb_mod_index_t = u32;
29 pub type xkb_mod_mask_t = u32;
30 pub type xkb_led_index_t = u32;
31 pub type xkb_led_mask_t = u32;
32 
33 pub const XKB_KEYCODE_INVALID :u32 = 0xffffffff;
34 pub const XKB_LAYOUT_INVALID  :u32 = 0xffffffff;
35 pub const XKB_LEVEL_INVALID   :u32 = 0xffffffff;
36 pub const XKB_MOD_INVALID     :u32 = 0xffffffff;
37 pub const XKB_LED_INVALID     :u32 = 0xffffffff;
38 pub const XKB_KEYCODE_MAX     :u32 = 0xffffffff - 1;
39 
40 #[repr(C)]
41 #[derive(Copy, Clone)]
42 pub struct xkb_rule_names {
43     pub rules:   *const c_char,
44     pub model:   *const c_char ,
45     pub layout:  *const c_char,
46     pub variant: *const c_char,
47     pub options: *const c_char,
48 }
49 
50 #[repr(C)]
51 #[derive(Copy, Clone, Debug, PartialEq)]
52 pub enum xkb_keysym_flags {
53     /** Do not apply any flags. */
54     XKB_KEYSYM_NO_FLAGS = 0,
55     /** Find keysym by case-insensitive search. */
56     XKB_KEYSYM_CASE_INSENSITIVE = (1 << 0)
57 }
58 
59 #[repr(C)]
60 #[derive(Copy, Clone, Debug, PartialEq)]
61 pub enum xkb_context_flags {
62     /** Do not apply any context flags. */
63     XKB_CONTEXT_NO_FLAGS = 0,
64     /** Create this context with an empty include path. */
65     XKB_CONTEXT_NO_DEFAULT_INCLUDES = (1 << 0),
66     /**
67      * Don't take RMLVO names from the environment.
68      * @since 0.3.0
69      */
70     XKB_CONTEXT_NO_ENVIRONMENT_NAMES = (1 << 1)
71 }
72 
73 #[repr(C)]
74 #[derive(Copy, Clone, Debug, PartialEq)]
75 pub enum xkb_log_level {
76     /** Log critical internal errors only. */
77     XKB_LOG_LEVEL_CRITICAL = 10,
78     /** Log all errors. */
79     XKB_LOG_LEVEL_ERROR = 20,
80     /** Log warnings and errors. */
81     XKB_LOG_LEVEL_WARNING = 30,
82     /** Log information, warnings, and errors. */
83     XKB_LOG_LEVEL_INFO = 40,
84     /** Log everything. */
85     XKB_LOG_LEVEL_DEBUG = 50
86 }
87 
88 #[repr(C)]
89 #[derive(Copy, Clone, Debug, PartialEq)]
90 pub enum xkb_keymap_compile_flags {
91     /** Do not apply any flags. */
92     XKB_KEYMAP_COMPILE_NO_FLAGS = 0,
93 }
94 
95 #[repr(C)]
96 #[derive(Copy, Clone, Debug, PartialEq)]
97 pub enum xkb_keymap_format {
98     /** Cannot be used for creation */
99     XKB_KEYMAP_USE_ORIGINAL_FORMAT = 0,
100     /** The current/classic XKB text format, as generated by xkbcomp -xkb. */
101     XKB_KEYMAP_FORMAT_TEXT_V1 = 1,
102 }
103 
104 #[repr(C)]
105 #[derive(Copy, Clone, Debug, PartialEq)]
106 pub enum xkb_key_direction {
107     /** The key was released. */
108     XKB_KEY_UP,
109     /** The key was pressed. */
110     XKB_KEY_DOWN
111 }
112 
113 #[repr(C)]
114 #[derive(Copy, Clone, Debug, PartialEq)]
115 pub enum xkb_compose_compile_flags {
116     XKB_COMPOSE_COMPILE_NO_FLAGS = 0
117 }
118 
119 #[repr(C)]
120 #[derive(Copy, Clone, Debug, PartialEq)]
121 pub enum xkb_compose_format {
122     XKB_COMPOSE_FORMAT_TEXT_V1 = 1
123 }
124 
125 #[repr(C)]
126 #[derive(Copy, Clone, Debug, PartialEq)]
127 pub enum xkb_compose_state_flags {
128     XKB_COMPOSE_STATE_NO_FLAGS = 0
129 }
130 
131 #[repr(C)]
132 #[derive(Copy, Clone, Debug, PartialEq)]
133 pub enum xkb_compose_status {
134     XKB_COMPOSE_NOTHING,
135     XKB_COMPOSE_COMPOSING,
136     XKB_COMPOSE_COMPOSED,
137     XKB_COMPOSE_CANCELLED
138 }
139 
140 #[repr(C)]
141 #[derive(Copy, Clone, Debug, PartialEq)]
142 pub enum xkb_compose_feed_result {
143     XKB_COMPOSE_FEED_IGNORED,
144     XKB_COMPOSE_FEED_ACCEPTED
145 }
146 
147 bitflags!(
148     pub struct xkb_state_component: u32 {
149         /** Depressed modifiers, i.e. a key is physically holding them. */
150         const XKB_STATE_MODS_DEPRESSED = (1 << 0);
151         /** Latched modifiers, i.e. will be unset after the next non-modifier
152          *  key press. */
153         const XKB_STATE_MODS_LATCHED = (1 << 1);
154         /** Locked modifiers, i.e. will be unset after the key provoking the
155          *  lock has been pressed again. */
156         const XKB_STATE_MODS_LOCKED = (1 << 2);
157         /** Effective modifiers, i.e. currently active and affect key
158          *  processing (derived from the other state components).
159          *  Use this unless you explictly care how the state came about. */
160         const XKB_STATE_MODS_EFFECTIVE = (1 << 3);
161         /** Depressed layout, i.e. a key is physically holding it. */
162         const XKB_STATE_LAYOUT_DEPRESSED = (1 << 4);
163         /** Latched layout, i.e. will be unset after the next non-modifier
164          *  key press. */
165         const XKB_STATE_LAYOUT_LATCHED = (1 << 5);
166         /** Locked layout, i.e. will be unset after the key provoking the lock
167          *  has been pressed again. */
168         const XKB_STATE_LAYOUT_LOCKED = (1 << 6);
169         /** Effective layout, i.e. currently active and affects key processing
170          *  (derived from the other state components).
171          *  Use this unless you explictly care how the state came about. */
172         const XKB_STATE_LAYOUT_EFFECTIVE = (1 << 7);
173         /** LEDs (derived from the other state components). */
174         const XKB_STATE_LEDS = (1 << 8);
175     }
176 );
177 
178 dlopen_external_library!(XkbCommon,
179 functions:
180     fn xkb_keysym_get_name(xkb_keysym_t, *mut c_char, usize) -> c_int,
181     fn xkb_keysym_from_name(*const c_char, xkb_keysym_flags) -> xkb_keysym_t,
182     fn xkb_keysym_to_utf8(xkb_keysym_t, *mut c_char, usize) -> c_int,
183     fn xkb_keysym_to_utf32(xkb_keysym_t) -> u32,
184     fn xkb_context_new(xkb_context_flags) -> *mut xkb_context,
185     fn xkb_context_ref(*mut xkb_context) -> *mut xkb_context,
186     fn xkb_context_unref(*mut xkb_context) -> (),
187     fn xkb_context_set_user_data(*mut xkb_context, *mut c_void) -> (),
188     fn xkb_context_get_user_data(*mut xkb_context) -> *mut c_void,
189     fn xkb_context_include_path_append(*mut xkb_context, *const c_char) -> c_int,
190     fn xkb_context_include_path_append_default(*mut xkb_context) -> c_int,
191     fn xkb_context_include_path_reset_defaults(*mut xkb_context) -> c_int,
192     fn xkb_context_include_path_clear(*mut xkb_context) -> (),
193     fn xkb_context_num_include_paths(*mut xkb_context) -> c_uint,
194     fn xkb_context_include_path_get(*mut xkb_context, c_uint) -> *const c_char,
195     fn xkb_context_set_log_level(*mut xkb_context, xkb_log_level) -> (),
196     fn xkb_context_get_log_level(*mut xkb_context) -> xkb_log_level,
197     fn xkb_context_set_log_verbosity(*mut xkb_context, c_int) -> (),
198     fn xkb_context_get_log_verbosity(*mut xkb_context) -> c_int,
199     fn xkb_keymap_new_from_names(*mut xkb_context,
200                                  *const xkb_rule_names,
201                                  xkb_keymap_compile_flags
202                                 ) -> *mut xkb_keymap,
203     fn xkb_keymap_new_from_string(*mut xkb_context,
204                                   *const c_char,
205                                   xkb_keymap_format,
206                                   xkb_keymap_compile_flags
207                                  ) -> *mut xkb_keymap,
208     fn xkb_keymap_new_from_buffer(*mut xkb_context,
209                                   *const c_char,
210                                   usize,
211                                   xkb_keymap_format,
212                                   xkb_keymap_compile_flags
213                                  ) -> *mut xkb_keymap,
214     fn xkb_keymap_ref(*mut xkb_keymap) -> *mut xkb_keymap,
215     fn xkb_keymap_unref(*mut xkb_keymap) -> (),
216     fn xkb_keymap_get_as_string(*mut xkb_keymap, xkb_keymap_format) -> *const c_char,
217     fn xkb_keymap_key_repeats(*mut xkb_keymap, xkb_keycode_t) -> c_int,
218 
219     fn xkb_state_new(*mut xkb_keymap) -> *mut xkb_state,
220     fn xkb_state_ref(*mut xkb_state) -> *mut xkb_state,
221     fn xkb_state_unref(*mut xkb_state) -> (),
222     fn xkb_state_update_mask(*mut xkb_state,
223                              xkb_mod_mask_t,
224                              xkb_mod_mask_t,
225                              xkb_mod_mask_t,
226                              xkb_layout_index_t,
227                              xkb_layout_index_t,
228                              xkb_layout_index_t
229                             ) -> xkb_state_component,
230     fn xkb_state_update_key(*mut xkb_state,
231                             xkb_keycode_t,
232                             xkb_key_direction
233                            ) -> xkb_state_component,
234     fn xkb_state_key_get_syms(*mut xkb_state,
235                               xkb_keycode_t,
236                               *const *mut xkb_keysym_t
237                              ) -> c_int,
238     fn xkb_state_key_get_utf8(*mut xkb_state,
239                               xkb_keycode_t,
240                               *mut c_char,
241                               usize
242                              ) -> c_int,
243     fn xkb_state_key_get_utf32(*mut xkb_state, xkb_keycode_t) -> u32,
244     fn xkb_state_key_get_one_sym(*mut xkb_state, xkb_keycode_t) -> xkb_keysym_t,
245     fn xkb_state_mod_name_is_active(*mut xkb_state, *const c_char, xkb_state_component) -> c_int,
246     fn xkb_compose_table_new_from_locale(*mut xkb_context, *const c_char, xkb_compose_compile_flags) -> *mut xkb_compose_table,
247     fn xkb_compose_table_unref(*mut xkb_compose_table) -> (),
248     fn xkb_compose_state_new(*mut xkb_compose_table, xkb_compose_state_flags) -> *mut xkb_compose_state,
249     fn xkb_compose_state_unref(*mut xkb_compose_state) -> (),
250     fn xkb_compose_state_feed(*mut xkb_compose_state, xkb_keysym_t) -> xkb_compose_feed_result,
251     fn xkb_compose_state_reset(*mut xkb_compose_state) -> (),
252     fn xkb_compose_state_get_status(*mut xkb_compose_state) -> xkb_compose_status,
253     fn xkb_compose_state_get_utf8(*mut xkb_compose_state, *mut c_char, usize) -> c_int,
254     fn xkb_compose_state_get_one_sym(*mut xkb_compose_state) -> xkb_keysym_t,
255 );
256 
257 lazy_static!(
258     pub static ref XKBCOMMON_OPTION: Option<XkbCommon> = {
259         XkbCommon::open("libxkbcommon.so.0")
260             .or_else(|_| XkbCommon::open("libxkbcommon.so"))
261             .ok()
262     };
263     pub static ref XKBCOMMON_HANDLE: &'static XkbCommon = {
264         XKBCOMMON_OPTION.as_ref().expect("Library libxkbcommon.so could not be loaded.")
265     };
266 );
267