Bump to kbd 2.2.0
[platform/upstream/kbd.git] / src / libkeymap / summary.c
1 /* summary.c
2  *
3  * This file is part of kbd project.
4  * Copyright (C) 2012-2013  Alexey Gladkov <gladkov.alexey@gmail.com>
5  *
6  * This file is covered by the GNU General Public License,
7  * which should be included with kbd as the file COPYING.
8  */
9 #include "config.h"
10 #include <string.h>
11 #include <errno.h>
12 #include <sys/ioctl.h>
13
14 #include "keymap.h"
15
16 #include "libcommon.h"
17 #include "contextP.h"
18 #include "ksyms.h"
19 #include "modifiers.h"
20
21 static char
22 valid_type(int fd, int t)
23 {
24         struct kbentry ke;
25
26         ke.kb_index = 0;
27         ke.kb_table = 0;
28         ke.kb_value = (unsigned short) K(t, 0);
29
30         return (ioctl(fd, KDSKBENT, (unsigned long)&ke) == 0);
31 }
32
33 static unsigned char
34 maximum_val(int fd, int t)
35 {
36         struct kbentry ke, ke0;
37         int i;
38
39         ke.kb_index = 0;
40         ke.kb_table = 0;
41         ke.kb_value = K_HOLE;
42         ke0         = ke;
43
44         ioctl(fd, KDGKBENT, (unsigned long)&ke0);
45
46         for (i = 0; i < 256; i++) {
47                 ke.kb_value = (unsigned short) K(t, i);
48                 if (ioctl(fd, KDSKBENT, (unsigned long)&ke))
49                         break;
50         }
51         ke.kb_value = K_HOLE;
52         ioctl(fd, KDSKBENT, (unsigned long)&ke0);
53
54         return (unsigned char) (i - 1);
55 }
56
57 int lk_get_kmapinfo(struct lk_ctx *ctx, struct kmapinfo *res)
58 {
59         int i;
60
61         if (!ctx)
62                 return -1;
63
64         res->flags     = ctx->flags;
65         res->keywords  = ctx->keywords;
66         res->keymaps   = ctx->keymap->count;
67         res->functions = ctx->func_table->count;
68         res->composes  = ctx->accent_table->count;
69
70         res->keymaps_total   = ctx->keymap->total;
71         res->functions_total = ctx->func_table->total;
72         res->composes_total  = ctx->accent_table->total;
73
74         res->keymaps_alloced = 0;
75
76         for (i = 0; i < MAX_NR_KEYMAPS; i++) {
77                 if (lk_map_exists(ctx, i) && lk_get_key(ctx, i, 0) == K_ALLOCATED) {
78                         res->keymaps_alloced++;
79                 }
80         }
81
82         return 0;
83 }
84
85 #define NR_TYPES 15
86
87 void lk_dump_summary(struct lk_ctx *ctx, FILE *fd, int console)
88 {
89         int i;
90         struct kmapinfo info;
91
92         if (lk_get_kmapinfo(ctx, &info) < 0)
93                 return;
94
95         fprintf(fd, _("keycode range supported by kernel:           1 - %d\n"),
96                 NR_KEYS - 1);
97         fprintf(fd, _("max number of actions bindable to a key:         %d\n"),
98                 MAX_NR_KEYMAPS);
99         fprintf(fd, _("number of keymaps in actual use:                 %u\n"),
100                 (unsigned int)info.keymaps);
101
102         fprintf(fd, _("of which %u dynamically allocated\n"),
103                 (unsigned int)info.keymaps_alloced);
104
105         fprintf(fd, _("ranges of action codes supported by kernel:\n"));
106
107         for (i = 0; i < NR_TYPES && valid_type(console, i); i++)
108                 fprintf(fd, "   0x%04x - 0x%04x\n",
109                         K(i, 0), K(i, maximum_val(console, i)));
110
111         fprintf(fd, _("number of function keys supported by kernel: %d\n"),
112                 MAX_NR_FUNC);
113         fprintf(fd, _("max nr of compose definitions: %d\n"),
114                 MAX_DIACR);
115         fprintf(fd, _("nr of compose definitions in actual use: %u\n"),
116                 (unsigned int)info.composes);
117 }
118
119 void lk_dump_symbols(struct lk_ctx *ctx, FILE *fd)
120 {
121         int t, v;
122         modifier_t *mod;
123         const char *p;
124
125         for (t = 0; t < syms_size; t++) {
126                 if (get_sym_size(ctx, t)) {
127                         for (v = 0; v < get_sym_size(ctx, t); v++) {
128                                 if ((p = get_sym(ctx, t, v))[0])
129                                         fprintf(fd, "0x%04x\t%s\n", K(t, v), p);
130                         }
131                 } else if (t == KT_META) {
132                         for (v = 0; v < get_sym_size(ctx, KT_LATIN) && v < 128; v++) {
133                                 if ((p = get_sym(ctx, KT_LATIN, v))[0])
134                                         fprintf(fd, "0x%04x\tMeta_%s\n", K(t, v), p);
135                         }
136                 }
137         }
138
139         fprintf(fd, _("\nThe following synonyms are recognized:\n\n"));
140
141         for (t = 0; t < syn_size; t++) {
142                 fprintf(fd, _("%-15s for %s\n"),
143                         synonyms[t].synonym, synonyms[t].official_name);
144         }
145
146         fprintf(fd, _("\nRecognized modifier names and their column numbers:\n"));
147
148         mod = (modifier_t *)modifiers;
149         while (mod->name) {
150                 fprintf(fd, "%s\t\t%3d\n", mod->name, 1 << mod->bit);
151                 mod++;
152         }
153 }