summaryrefslogtreecommitdiff
path: root/printev.c
blob: fc20356f7f73aac158ad4b9baa45228a5a574ea3 (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
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
/************************************************************
 Copyright (c) 1995 by Silicon Graphics Computer Systems, Inc.

 Permission to use, copy, modify, and distribute this
 software and its documentation for any purpose and without
 fee is hereby granted, provided that the above copyright
 notice appear in all copies and that both that copyright
 notice and this permission notice appear in supporting
 documentation, and that the name of Silicon Graphics not be
 used in advertising or publicity pertaining to distribution
 of the software without specific prior written permission.
 Silicon Graphics makes no representation about the suitability
 of this software for any purpose. It is provided "as is"
 without any express or implied warranty.

 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
 THE USE OR PERFORMANCE OF THIS SOFTWARE.

 ********************************************************/

#include "xkbevd.h"
#include <X11/Xutil.h>

#define	Yes	"yes"
#define	No	"no"
#define	Unknown	"unknown"
#define	ynText(v)	((v)?Yes:No)

static const char *
eventTypeToString(int evType)
{
    switch (evType) {
    case KeyPress:
        return "KeyPress";
        break;
    case KeyRelease:
        return "KeyRelease";
        break;
    default:
        return "unknown";
        break;
    }
}

static void
xkb_prologue(FILE *file, XkbEvent *ev, const char *name)
{
    XkbAnyEvent *e = &ev->any;

    fprintf(file,
            "\n%s event, serial %ld, synthetic %s, device %d, time %ld,\n",
            name, e->serial, ynText(e->send_event), e->device, e->time);
    return;
}

static void
do_XkbStateNotify(FILE *file, XkbEvent *xkbev)
{
    XkbStateNotifyEvent *state = &xkbev->state;

    if (state->keycode != 0)
        fprintf(file, "    keycode %d, eventType %s,\n",
                state->keycode, eventTypeToString(state->event_type));
    else
        fprintf(file, "    request %d/%d,\n", state->req_minor,
                state->req_minor);
    fprintf(file, "    group= %d%s, base= %d%s, latched= %d%s, locked= %d%s,\n",
            state->group, (state->changed & XkbGroupStateMask ? "*" : ""),
            state->base_group, (state->changed & XkbGroupBaseMask ? "*" : ""),
            state->latched_group,
            (state->changed & XkbGroupLatchMask ? "*" : ""),
            state->locked_group,
            (state->changed & XkbGroupLockMask ? "*" : ""));
    fprintf(file,
    "    mods= 0x%02x%s, base= 0x%02x%s, latched= 0x%02x%s, locked= 0x%02x%s\n",
            state->mods, (state->changed & XkbModifierStateMask ? "*" : ""),
            state->base_mods, (state->changed & XkbModifierBaseMask ? "*" : ""),
            state->latched_mods,
            (state->changed & XkbModifierLatchMask ? "*" : ""),
            state->locked_mods,
            (state->changed & XkbModifierLockMask ? "*" : ""));
    fprintf(file, "    grab mods= 0x%02x%s, compat grab mods= 0x%02x%s\n",
            state->grab_mods, (state->changed & XkbGrabModsMask ? "*" : ""),
            state->compat_grab_mods,
            (state->changed & XkbCompatGrabModsMask ? "*" : ""));
    fprintf(file, "    lookup mods= 0x%02x%s, compat lookup mods= 0x%02x%s\n",
            state->lookup_mods, (state->changed & XkbLookupModsMask ? "*" : ""),
            state->compat_lookup_mods,
            (state->changed & XkbCompatLookupModsMask ? "*" : ""));
    fprintf(file, "    compatState = 0x%02x%s, ", state->compat_state,
            (state->changed & XkbCompatStateMask ? "*" : ""));
    fprintf(file, "ptr_buttons= 0x%04x%s\n", state->ptr_buttons,
            (state->changed & XkbPointerButtonMask ? "*" : ""));
    return;
}

static void
do_map_message(const char *what, int first, int num, int eol)
{
    if (num > 1)
        printf("%ss %d..%d changed%s", what, first, first + num - 1,
               (eol ? "\n" : ""));
    else
        printf("%s %d changed%s", what, first, (eol ? "\n" : ""));
}

static void
do_XkbMapNotify(FILE *file, XkbEvent *xkbev)
{
    XkbMapNotifyEvent *map = &xkbev->map;

    if (map->changed & XkbKeyTypesMask) {
        do_map_message("key type", map->first_type, map->num_types, 0);
    }
    if (map->changed & XkbKeySymsMask) {
        do_map_message("symbols for key", map->first_key_sym, map->num_key_syms,
                       1);
    }
    if (map->changed & XkbKeyActionsMask) {
        do_map_message("acts for key", map->first_key_act,
                       map->num_key_acts, 1);
    }
    if (map->changed & XkbKeyBehaviorsMask) {
        do_map_message("behavior for key", map->first_key_behavior,
                       map->num_key_behaviors, 1);
    }
    if (map->changed & XkbVirtualModsMask) {
        fprintf(file, "    virtual modifiers changed (0x%04x)\n", map->vmods);
    }
    if (map->changed & XkbExplicitComponentsMask) {
        do_map_message("explicit components for key", map->first_key_explicit,
                       map->num_key_explicit, 1);
    }
    if (map->changed & XkbModifierMapMask) {
        do_map_message("modifier map for key", map->first_modmap_key,
                       map->num_modmap_keys, 1);
    }
    return;
}

static void
do_XkbControlsNotify(FILE *file, XkbEvent *xkbev)
{
    XkbControlsNotifyEvent *ctrls = &xkbev->ctrls;

    fprintf(file, "    changed= 0x%x, enabled= 0x%x, enabledChanges= 0x%x\n",
            ctrls->changed_ctrls, ctrls->enabled_ctrls,
            ctrls->enabled_ctrl_changes);
    fprintf(file, "    num_groups= %d\n", ctrls->num_groups);
    if (ctrls->keycode != 0)
        fprintf(file, "    keycode %d, eventType %s,",
                ctrls->keycode, eventTypeToString(ctrls->event_type));
    else
        fprintf(file, "    request %d/%d%s\n", ctrls->req_major,
                ctrls->req_minor,
                (ctrls->req_major != xkbOpcode ? " (NON-XKB)" : ""));
    return;
}

static void
do_XkbIndicatorNotify(FILE *file, XkbEvent *xkbev)
{
    XkbIndicatorNotifyEvent *leds = &xkbev->indicators;

    if (leds->xkb_type == XkbIndicatorStateNotify)
        fprintf(file, "    state changes= 0x%08x, new state= 0x%08x\n",
                leds->changed, leds->state);
    else
        fprintf(file, "    map changes= 0x%08x, state= 0x%08x\n",
                leds->changed, leds->state);
    return;
}

static void
do_XkbBellNotify(FILE *file, XkbEvent *xkbev)
{
    XkbBellNotifyEvent *bell = &xkbev->bell;

    fprintf(file, "    bell class= %d, id= %d\n", bell->bell_class,
            bell->bell_id);
    fprintf(file, "    percent= %d, pitch= %d, duration= %d", bell->percent,
            bell->pitch, bell->duration);
    if (bell->name != None) {
        char *name = XGetAtomName(dpy, bell->name);

        fprintf(file, "\n    name= \"%s\"\n", (name ? name : ""));
        if (name)
            XFree(name);
    }
    else
        fprintf(file, ", no name\n");
    fprintf(file, "    window= 0x%x, %sevent_only\n",
            (unsigned int) bell->window, (bell->event_only ? "" : "!"));
    return;
}

static void
do_XkbAccessXNotify(FILE *file, XkbEvent *xkbev)
{
    XkbAccessXNotifyEvent *sk = &xkbev->accessx;
    const char *detail;

    switch (sk->detail) {
    case XkbAXN_SKPress:
        detail = "skpress";
        break;
    case XkbAXN_SKAccept:
        detail = "skaccept";
        break;
    case XkbAXN_SKReject:
        detail = "skreject";
        break;
    case XkbAXN_SKRelease:
        detail = "skrelease";
        break;
    case XkbAXN_BKAccept:
        detail = "bkaccept";
        break;
    case XkbAXN_BKReject:
        detail = "bkreject";
        break;
    case XkbAXN_AXKWarning:
        detail = "warning";
        break;
    default:{
        static char buf[20];

        snprintf(buf, sizeof(buf), "unknown(%d)", sk->detail);
        detail = buf;
        break;
    }
    }
    fprintf(file,
            "    keycode= %d,detail= %s,slow keys delay= %d,debounce delay= %d\n",
            sk->keycode, detail, sk->sk_delay, sk->debounce_delay);
    return;
}

static void
do_XkbNamesNotify(FILE *file, XkbEvent *xkbev)
{
    XkbNamesNotifyEvent *names = &xkbev->names;

    if (names->changed &
        (XkbKeycodesNameMask | XkbGeometryNameMask | XkbSymbolsNameMask)) {
        int needComma = 0;

        fprintf(file, "    ");
        if (names->changed & XkbKeycodesNameMask) {
            fprintf(file, "keycodes");
            needComma++;
        }
        if (names->changed & XkbGeometryNameMask) {
            fprintf(file, "%sgeometry", (needComma ? ", " : ""));
            needComma++;
        }
        if (names->changed & XkbSymbolsNameMask) {
            fprintf(file, "%ssymbols", (needComma ? ", " : ""));
            needComma++;
        }
        if (names->changed & XkbPhysSymbolsNameMask) {
            fprintf(file, "%sphysical symbols", (needComma ? ", " : ""));
            needComma++;
        }
        fprintf(file, " name%s changed\n", (needComma > 1 ? "s" : ""));
    }
    if (names->changed & XkbKeyTypeNamesMask) {
        do_map_message("key type name", names->first_type, names->num_types, 1);
    }
    if (names->changed & XkbKTLevelNamesMask) {
        do_map_message("level names for key type",
                       names->first_lvl, names->num_lvls, 1);
    }
    if (names->changed & XkbIndicatorNamesMask) {
        fprintf(file, "    names of indicators in 0x%08x changed\n",
                names->changed_indicators);
    }
    if (names->changed & XkbVirtualModNamesMask) {
        fprintf(file, "    names of virtual modifiers in 0x%04x changed\n",
                names->changed_vmods);
    }
    if (names->changed & XkbGroupNamesMask) {
        fprintf(file, "    names of groups in 0x%x changed\n",
                names->changed_groups);
    }
    if (names->changed & XkbKeyNamesMask) {
        do_map_message("names for key", names->first_key, names->num_keys, 1);
    }
    if (names->changed & XkbKeyAliasesMask) {
        fprintf(file, "key aliases changed (%d aliases total)\n",
                names->num_aliases);
    }
    if (names->changed & XkbRGNamesMask) {
        fprintf(file, "radio group names changed (%d radio groups total)\n",
                names->num_radio_groups);
    }
    return;
}

static void
do_XkbCompatMapNotify(FILE *file, XkbEvent *xkbev)
{
    XkbCompatMapNotifyEvent *map = &xkbev->compat;

    if (map->changed_groups)
        fprintf(file, "    compat maps for groups in 0x%02x changed\n",
                map->changed_groups);
    if (map->num_si > 0) {
        fprintf(file, "    symbol interpretations %d..%d (of %d) changed\n",
                map->first_si, map->first_si + map->num_si - 1,
                map->num_total_si);
    }
    else
        fprintf(file, "   keyboard has %d symbol interpretations\n",
                map->num_total_si);
    return;
}

static void
do_XkbActionMessage(FILE *file, XkbEvent *xkbev)
{
    XkbActionMessageEvent *msg = &xkbev->message;

    fprintf(file, "    message: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
            msg->message[0], msg->message[1],
            msg->message[2], msg->message[3], msg->message[4], msg->message[5]);
    fprintf(file, "    key %d, event: %s,  follows: %s\n", msg->keycode,
            (msg->press ? "press" : "release"),
            (msg->key_event_follows ? "yes" : "no"));
    return;
}

static void
do_XkbNewKeyboardNotify(FILE *file, XkbEvent *xkbev)
{
    XkbNewKeyboardNotifyEvent *nk = &xkbev->new_kbd;

    fprintf(file, "    new device: %d min_keycode: %d, max_keycode %d\n",
            nk->device, nk->min_key_code, nk->max_key_code);
    fprintf(file, "    old device: %d min_keycode: %d, max_keycode %d\n",
            nk->old_device, nk->old_min_key_code, nk->old_max_key_code);
    return;
}

static void
do_XkbExtensionDeviceNotify(FILE *file, XkbEvent *xkbev)
{
    XkbExtensionDeviceNotifyEvent *edn = &xkbev->device;

    fprintf(file, "    device= %d, class= %d, id= %d\n", edn->device,
            edn->led_class, edn->led_id);
    fprintf(file, "    reason= 0x%0x\n", edn->reason);
    fprintf(file, "    supported= 0x%0x, unsupported= 0x%0x\n", edn->supported,
            edn->unsupported);
    fprintf(file, "    first button= %d, num buttons= %d\n", edn->first_btn,
            edn->num_btns);
    fprintf(file, "    leds defined= 0x%08x, led state= 0x%08x\n",
            edn->leds_defined, edn->led_state);
    return;
}

void
PrintXkbEvent(FILE *file, XkbEvent *ev)
{
    if (ev->type == xkbEventCode) {
        switch (ev->any.xkb_type) {
        case XkbStateNotify:
            xkb_prologue(file, ev, "XkbStateNotify");
            do_XkbStateNotify(file, ev);
            break;
        case XkbMapNotify:
            xkb_prologue(file, ev, "XkbMapNotify");
            do_XkbMapNotify(file, ev);
            break;
        case XkbControlsNotify:
            xkb_prologue(file, ev, "XkbControlsNotify");
            do_XkbControlsNotify(file, ev);
            break;
        case XkbIndicatorMapNotify:
            xkb_prologue(file, ev, "XkbIndicatorMapNotify");
            do_XkbIndicatorNotify(file, ev);
            break;
        case XkbIndicatorStateNotify:
            xkb_prologue(file, ev, "XkbIndicatorStateNotify");
            do_XkbIndicatorNotify(file, ev);
            break;
        case XkbBellNotify:
            xkb_prologue(file, ev, "XkbBellNotify");
            do_XkbBellNotify(file, ev);
            break;
        case XkbAccessXNotify:
            xkb_prologue(file, ev, "XkbAccessXNotify");
            do_XkbAccessXNotify(file, ev);
            break;
        case XkbNamesNotify:
            xkb_prologue(file, ev, "XkbNamesNotify");
            do_XkbNamesNotify(file, ev);
            break;
        case XkbCompatMapNotify:
            xkb_prologue(file, ev, "XkbCompatMapNotify");
            do_XkbCompatMapNotify(file, ev);
            break;
        case XkbActionMessage:
            xkb_prologue(file, ev, "XkbActionMessage");
            do_XkbActionMessage(file, ev);
            break;
        case XkbNewKeyboardNotify:
            xkb_prologue(file, ev, "XkbNewKeyboard");
            do_XkbNewKeyboardNotify(file, ev);
            break;
        case XkbExtensionDeviceNotify:
            xkb_prologue(file, ev, "XkbExtensionDeviceNotify");
            do_XkbExtensionDeviceNotify(file, ev);
            break;
        default:
            xkb_prologue(file, ev, "XKB_UNKNOWN!!!");
            break;
        }
    }
    return;
}