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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470

#![allow(non_camel_case_types)]

use libc::FILE;
use std::os::raw::{c_char, c_int, c_uint, c_void};


pub enum xkb_context {}

pub enum xkb_keymap {}

pub enum xkb_state {}


pub type xkb_keycode_t = u32;

pub type xkb_keysym_t = u32;

pub type xkb_layout_index_t = u32;

pub type xkb_layout_mask_t = u32;


pub type xkb_level_index_t = u32;

pub type xkb_mod_index_t = u32;

pub type xkb_mod_mask_t = u32;

pub type xkb_led_index_t = u32;

pub type xkb_led_mask_t = u32;

pub const XKB_KEYCODE_INVALID:u32 = 0xffffffff;
pub const XKB_LAYOUT_INVALID :u32 = 0xffffffff;
pub const XKB_LEVEL_INVALID  :u32 = 0xffffffff;
pub const XKB_MOD_INVALID    :u32 = 0xffffffff;
pub const XKB_LED_INVALID    :u32 = 0xffffffff;

pub const XKB_KEYCODE_MAX    :u32 = 0xfffffffe;

pub fn xkb_keycode_is_legal_ext(key: u32) -> bool {
    key <= XKB_KEYCODE_MAX
}

pub fn xkb_keycode_is_legal_x11(key: u32) -> bool {
    key >= 8 && key <= 255
}


#[repr(C)]
pub struct xkb_rule_names {
    pub rules: *const c_char,
    pub model: *const c_char,
    pub layout: *const c_char,
    pub variant: *const c_char,
    pub options: *const c_char,
}


pub type xkb_keysym_flags = u32;
pub const XKB_KEYSYM_NO_FLAGS: u32 = 0;
pub const XKB_KEYSYM_CASE_INSENSITIVE: u32 = (1 << 0);

pub type xkb_context_flags = u32;
pub const XKB_CONTEXT_NO_FLAGS: u32 = 0;
pub const XKB_CONTEXT_NO_DEFAULT_INCLUDES: u32 = (1 << 0);
pub const XKB_CONTEXT_NO_ENVIRONMENT_NAMES: u32 = (1 << 1);


#[repr(C)]
pub enum xkb_log_level {
    CRITICAL = 10,
    ERROR = 20,
    WARNING = 30,
    INFO = 40,
    DEBUG = 50,
}


pub type xkb_keymap_compile_flags = u32;
pub const XKB_KEYMAP_COMPILE_NO_FLAGS: u32 = 0;

pub type xkb_keymap_format = u32;
pub const XKB_KEYMAP_FORMAT_TEXT_V1: u32 = 1;
pub const XKB_KEYMAP_FORMAT_USE_ORIGINAL: u32 = 0xffffffff;


#[repr(C)]
pub enum xkb_key_direction {
    UP,
    DOWN
}


pub type xkb_state_component = u32;
pub const XKB_STATE_MODS_DEPRESSED: u32 = (1 << 0);
pub const XKB_STATE_MODS_LATCHED: u32 = (1 << 1);
pub const XKB_STATE_MODS_LOCKED: u32 = (1 << 2);
pub const XKB_STATE_MODS_EFFECTIVE: u32 = (1 << 3);
pub const XKB_STATE_LAYOUT_DEPRESSED: u32 = (1 << 4);
pub const XKB_STATE_LAYOUT_LATCHED: u32 = (1 << 5);
pub const XKB_STATE_LAYOUT_LOCKED: u32 = (1 << 6);
pub const XKB_STATE_LAYOUT_EFFECTIVE: u32 = (1 << 7);
pub const XKB_STATE_LEDS: u32 = (1 << 8);


pub type xkb_state_match = u32;
pub const XKB_STATE_MATCH_ANY: u32 = (1 << 0);
pub const XKB_STATE_MATCH_ALL: u32 = (1 << 1);
pub const XKB_STATE_MATCH_NON_EXCLUSIVE: u32 = (1 << 16);



pub type xkb_log_fn_t = unsafe extern fn(context: *mut xkb_context,
                                         level: xkb_log_level,
                                         format: *const c_char, ...);


pub type xkb_keymap_key_iter_t = unsafe extern fn (keymap: *mut xkb_keymap,
                                                    key: xkb_keycode_t,
                                                    data: *mut c_void);

#[link(name = "xkbcommon")]
extern "C" {

    pub fn xkb_keysym_get_name(keysym: xkb_keysym_t,
                               buffer: *mut c_char,
                               size: usize) -> c_int;

    pub fn xkb_keysym_from_name(name: *const c_char, flags: xkb_keysym_flags)
            -> xkb_keysym_t;

    pub fn xkb_keysym_to_utf8(keysym: xkb_keysym_t,
            buffer: *mut c_char, size: usize) -> c_int;

    pub fn xkb_keysym_to_utf32(keysym: xkb_keysym_t) -> u32;

    pub fn xkb_context_new(flags: xkb_context_flags) -> *mut xkb_context;

    pub fn xkb_context_ref(context: *mut xkb_context) -> *mut xkb_context;

    pub fn xkb_context_unref(context: *mut xkb_context);


    pub fn xkb_context_set_user_data(context: *mut xkb_context,
                                     user_data: *mut c_void);

    pub fn xkb_context_get_user_data(context: *mut xkb_context)
            -> *mut c_void;

    pub fn xkb_context_include_path_append(context: *mut xkb_context,
                                           path: *const c_char) -> c_int;

    pub fn xkb_context_include_path_append_default(context: *mut xkb_context)
            -> c_int;

    pub fn xkb_context_include_path_reset_defaults(context: *mut xkb_context)
            -> c_int;

    pub fn xkb_context_include_path_clear(context: *mut xkb_context);

    pub fn xkb_context_num_include_paths(context: *mut xkb_context) -> c_uint;

    pub fn xkb_context_include_path_get(context: *mut xkb_context, index: c_uint)
            -> *const c_char;


    pub fn xkb_context_set_log_level(context: *mut xkb_context,
                                     level: xkb_log_level);

    pub fn xkb_context_get_log_level(context: *mut xkb_context)
            -> xkb_log_level;

    pub fn xkb_context_set_log_verbosity(context: *mut xkb_context,
                                         verbosity: c_int);

    pub fn xkb_context_get_log_verbosity(context: *mut xkb_context)
            -> c_int;

    pub fn xkb_context_set_log_fn(context: *mut xkb_context,
                                  log_fn: xkb_log_fn_t);


    pub fn xkb_keymap_new_from_names(context: *mut xkb_context,
                                     names: *const xkb_rule_names,
                                     flags: xkb_keymap_compile_flags)
            -> *mut xkb_keymap;


    pub fn xkb_keymap_new_from_file(context: *mut xkb_context,
                                    file: *mut FILE,
                                    format: xkb_keymap_format,
                                    flags: xkb_keymap_compile_flags)
            -> *mut xkb_keymap;

    pub fn xkb_keymap_new_from_string(context: *mut xkb_context,
                                      s: *const c_char,
                                      format: xkb_keymap_format,
                                      flags: xkb_keymap_compile_flags)
            -> *mut xkb_keymap;


    pub fn xkb_keymap_new_from_buffer(context: *mut xkb_context,
                                      buffer: *const c_char,
                                      length: usize, format: xkb_keymap_format,
                                      flags: xkb_keymap_compile_flags)
            -> *mut xkb_keymap;

    pub fn xkb_keymap_ref(keymap: *mut xkb_keymap) -> *mut xkb_keymap;

    pub fn xkb_keymap_unref(keymap: *mut xkb_keymap);


    pub fn xkb_keymap_get_as_string(keymap: *mut xkb_keymap,
                                    format: xkb_keymap_format)
            -> *mut c_char;

    pub fn xkb_keymap_min_keycode(keymap: *mut xkb_keymap)
            -> xkb_keycode_t;

    pub fn xkb_keymap_max_keycode(keymap: *mut xkb_keymap)
            -> xkb_keycode_t;


    pub fn xkb_keymap_key_for_each(keymap: *mut xkb_keymap,
                                   iter: xkb_keymap_key_iter_t,
                                   data: *mut c_void);

    pub fn xkb_keymap_num_mods(keymap: *mut xkb_keymap) -> xkb_mod_index_t;

    pub fn xkb_keymap_mod_get_name(keymap: *mut xkb_keymap,
                                   idx: xkb_mod_index_t)
            -> *const c_char;

    pub fn xkb_keymap_mod_get_index(keymap: *mut xkb_keymap,
                                   name: *const c_char)
            -> xkb_mod_index_t;


    pub fn xkb_keymap_num_layouts(keymap: *mut xkb_keymap)
            -> xkb_layout_index_t;

    pub fn xkb_keymap_layout_get_name(keymap: *mut xkb_keymap,
                                      idx: xkb_layout_index_t)
            -> *const c_char;

    pub fn xkb_keymap_layout_get_index(keymap: *mut xkb_keymap,
                                       name: *const c_char)
            -> xkb_layout_index_t;


    pub fn xkb_keymap_num_leds(keymap: *mut xkb_keymap)
            -> xkb_led_index_t;

    pub fn xkb_keymap_led_get_name(keymap: *mut xkb_keymap,
                                   idx: xkb_led_index_t)
            -> *const c_char;

    pub fn xkb_keymap_led_get_index(keymap: *mut xkb_keymap,
                                    name: *const c_char)
            -> xkb_led_index_t;


    pub fn xkb_keymap_num_layouts_for_key(keymap: *mut xkb_keymap,
                                          key: xkb_keycode_t)
            -> xkb_layout_index_t;

    pub fn xkb_keymap_num_levels_for_key(keymap: *mut xkb_keymap,
                                         key: xkb_keycode_t,
                                         layout: xkb_layout_index_t)
            -> xkb_level_index_t;

    pub fn xkb_keymap_key_get_syms_by_level(keymap: *mut xkb_keymap,
                                            key: xkb_keycode_t,
                                            layout: xkb_layout_index_t,
                                            level: xkb_level_index_t,
                                            syms_out: *mut *const xkb_keysym_t)
            -> c_int;

    pub fn xkb_keymap_key_repeats(keymap: *mut xkb_keymap,
                                  key: xkb_keycode_t)
            -> c_int;



    pub fn xkb_state_ref(state: *mut xkb_state) -> *mut xkb_state;

    pub fn xkb_state_unref(state: *mut xkb_state);


    pub fn xkb_state_new(keymap: *mut xkb_keymap) -> *mut xkb_state;

    pub fn xkb_state_get_keymap(state: *mut xkb_state) -> *mut xkb_keymap;

    pub fn xkb_state_update_key(state: *mut xkb_state, key: xkb_keycode_t,
                         direction: xkb_key_direction)
            -> xkb_state_component;

    pub fn xkb_state_update_mask(state: *mut xkb_state,
                                 depressed_mods: xkb_mod_mask_t,
                                 latched_mods: xkb_mod_mask_t,
                                 locked_mods: xkb_mod_mask_t,
                                 depressed_layout: xkb_layout_index_t,
                                 latched_layout: xkb_layout_index_t,
                                 locked_layout: xkb_layout_index_t)
            -> xkb_state_component;


    pub fn xkb_state_key_get_syms(state: *mut xkb_state, key: xkb_keycode_t,
                                  syms_out: *mut *const xkb_keysym_t) -> c_int;

    pub fn xkb_state_key_get_utf8(state: *mut xkb_state, key: xkb_keycode_t,
                                  buffer: *mut c_char, size: usize) -> c_int;

    pub fn xkb_state_key_get_utf32(state: *mut xkb_state, key: xkb_keycode_t)
            -> u32;

    pub fn xkb_state_key_get_one_sym(state: *mut xkb_state, key: xkb_keycode_t)
            -> xkb_keysym_t;

    pub fn xkb_state_key_get_layout(state: *mut xkb_state, key: xkb_keycode_t)
            -> xkb_layout_index_t;

    pub fn xkb_state_key_get_level(state: *mut xkb_state, key: xkb_keycode_t,
                                   layout: xkb_layout_index_t)
            -> xkb_level_index_t;



    pub fn xkb_state_serialize_mods(state: *mut xkb_state,
                                    components: xkb_state_component)
            -> xkb_mod_mask_t;

    pub fn xkb_state_serialize_layout(state: *mut xkb_state,
                                    components: xkb_state_component)
            -> xkb_layout_index_t;

    pub fn xkb_state_mod_name_is_active(state: *mut xkb_state,
                                        name: *const c_char,
                                        type_: xkb_state_component)
            -> c_int;


    pub fn xkb_state_mod_names_are_active(state: *mut xkb_state,
                                          type_: xkb_state_component,
                                          match_: xkb_state_match, ...)
            -> c_int;

    pub fn xkb_state_mod_index_is_active(state: *mut xkb_state,
                                         idx: xkb_mod_index_t,
                                         type_: xkb_state_component)
            -> c_int;

    pub fn xkb_state_mod_index_are_active(state: *mut xkb_state,
                                          type_: xkb_state_component,
                                          match_: xkb_state_match, ...)
            -> c_int;

    pub fn xkb_state_mod_index_is_consumed(state: *mut xkb_state,
                                           key: xkb_keycode_t,
                                           idx: xkb_mod_index_t)
            -> c_int;

    pub fn xkb_state_mod_mask_remove_consumed(state: *mut xkb_state,
                                              key: xkb_keycode_t,
                                              mask: xkb_mod_mask_t)
            -> xkb_mod_mask_t;

    pub fn xkb_state_key_get_consumed_mods(state: *mut xkb_state,
                                           key: xkb_keycode_t)
            -> xkb_mod_mask_t;

    pub fn xkb_state_layout_name_is_active(state: *mut xkb_state,
                                           name: *const c_char,
                                           type_: xkb_state_component)
            -> c_int;

    pub fn xkb_state_layout_index_is_active(state: *mut xkb_state,
                                           idx: xkb_layout_index_t,
                                           type_: xkb_state_component)
            -> c_int;

    pub fn xkb_state_led_name_is_active(state: *mut xkb_state,
                                        name: *const c_char)
            -> c_int;

    pub fn xkb_state_led_index_is_active(state: *mut xkb_state,
                                         idx: xkb_led_index_t)
            -> c_int;

}

pub mod compose {
    use libc::{FILE, c_char, c_int, size_t};
    use super::{xkb_context, xkb_keysym_t};

    pub enum xkb_compose_table {}

    pub enum xkb_compose_state {}

    pub type xkb_compose_compile_flags = u32;

    pub type xkb_compose_format = u32;

    pub type xkb_compose_state_flags = u32;

    pub type xkb_compose_status = u32;

    pub type xkb_compose_feed_result = u32;

    #[link(name = "xkbcommon")]
    extern "C" {

        pub fn xkb_compose_table_new_from_locale(context: *mut xkb_context,
                                                 locale: *const c_char,
                                                 flags: xkb_compose_compile_flags)
            -> *mut xkb_compose_table;

        pub fn xkb_compose_table_new_from_file(context: *mut xkb_context,
                                               file: *mut FILE,
                                               locale: *const c_char,
                                               format: xkb_compose_format,
                                               flags: xkb_compose_compile_flags)
            -> *mut xkb_compose_table;

        pub fn xkb_compose_table_new_from_buffer(context: *mut xkb_context,
                                                 buffer: *const c_char,
                                                 length: size_t,
                                                 locale: *const c_char,
                                                 format: xkb_compose_format,
                                                 flags: xkb_compose_compile_flags)
            -> *mut xkb_compose_table;

        pub fn xkb_compose_table_ref(table: *mut xkb_compose_table)
            -> *mut xkb_compose_table;

        pub fn xkb_compose_table_unref(table: *mut xkb_compose_table);

        pub fn xkb_compose_state_new(table: *mut xkb_compose_table,
                                     flags: xkb_compose_state_flags)
            -> *mut xkb_compose_state;

        pub fn xkb_compose_state_ref(state: *mut xkb_compose_state)
            -> *mut xkb_compose_state;

        pub fn xkb_compose_state_unref(state: *mut xkb_compose_state);

        pub fn xkb_compose_state_get_compose_table(state: *mut xkb_compose_state)
            -> *mut xkb_compose_table;

        pub fn xkb_compose_state_feed(state: *mut xkb_compose_state,
                                      keysym: xkb_keysym_t)
            -> xkb_compose_feed_result;

        pub fn xkb_compose_state_reset(state: *mut xkb_compose_state);

        pub fn xkb_compose_state_get_status(state: *mut xkb_compose_state)
            -> xkb_compose_status;

        pub fn xkb_compose_state_get_utf8(state: *mut xkb_compose_state,
                                          buffer: *mut c_char,
                                          size: size_t)
            -> c_int;

        pub fn xkb_compose_state_get_one_sym(state: *mut xkb_compose_state)
            -> xkb_keysym_t;

    }
}