Move lk_ctx content to private part of library
[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 <string.h>
10 #include <errno.h>
11 #include <sys/ioctl.h>
12
13 #include "keymap.h"
14
15 #include "nls.h"
16 #include "contextP.h"
17 #include "ksyms.h"
18 #include "modifiers.h"
19
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 = K(t, 0);
29
30         return (ioctl(fd, KDSKBENT, (unsigned long) &ke) == 0);
31 }
32
33 static u_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 = 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 i - 1;
55 }
56
57 int
58 lk_get_kmapinfo(struct lk_ctx *ctx, struct kmapinfo *res)
59 {
60         int i;
61
62         if (!ctx)
63                 return -1;
64
65         res->flags     = ctx->flags;
66         res->keywords  = ctx->keywords;
67         res->keymaps   = ctx->keymap->count;
68         res->functions = ctx->func_table->count;
69         res->composes  = ctx->accent_table->count;
70
71         res->keymaps_alloced = 0;
72
73         for (i = 0; i < MAX_NR_KEYMAPS; i++) {
74                 if (lk_get_key(ctx, i, 0) == K_ALLOCATED) {
75                         res->keymaps_alloced++;
76                 }
77         }
78
79         return 0;
80 }
81
82 #define NR_TYPES 15
83
84 void
85 lk_dump_summary(struct lk_ctx *ctx, FILE *fd, int console)
86 {
87         int i;
88         struct kmapinfo info;
89
90         if (lk_get_kmapinfo(ctx, &info) < 0)
91                 return;
92
93         fprintf(fd, _("keycode range supported by kernel:           1 - %d\n"),
94                 NR_KEYS - 1);
95         fprintf(fd, _("max number of actions bindable to a key:         %d\n"),
96                 MAX_NR_KEYMAPS);
97         fprintf(fd, _("number of keymaps in actual use:                 %u\n"),
98                 (unsigned int) info.keymaps);
99
100         fprintf(fd, _("of which %u dynamically allocated\n"),
101                 (unsigned int) info.keymaps_alloced);
102
103         fprintf(fd, _("ranges of action codes supported by kernel:\n"));
104
105         for (i = 0; i < NR_TYPES && valid_type(console, i); i++)
106                 fprintf(fd, "   0x%04x - 0x%04x\n",
107                         K(i, 0), K(i, maximum_val(console, i)));
108
109         fprintf(fd, _("number of function keys supported by kernel: %d\n"),
110                 MAX_NR_FUNC);
111         fprintf(fd, _("max nr of compose definitions: %d\n"),
112                 MAX_DIACR);
113         fprintf(fd, _("nr of compose definitions in actual use: %u\n"),
114                 (unsigned int) info.composes);
115 }
116
117 void
118 lk_dump_symbols(FILE *fd)
119 {
120         unsigned int t;
121         modifier_t *mod;
122         int v;
123         const char *p;
124
125         for (t = 0; t < syms_size; t++) {
126             if (syms[t].size) {
127                 for (v = 0; v < syms[t].size; v++) {
128                         if ((p = syms[t].table[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 < syms[0].size && v < 128; v++) {
133                         if ((p = syms[0].table[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 }