Change the API for working with the diacritical table
[platform/upstream/kbd.git] / src / libkeymap / kernel.c
1 /* kernel.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
18 int
19 lk_kernel_keys(struct lk_ctx *ctx, int fd)
20 {
21         int i, t;
22         struct kbentry ke;
23
24         for (t = 0; t < MAX_NR_KEYMAPS; t++) {
25                 for (i = 0; i < NR_KEYS; i++) {
26                         ke.kb_table = t;
27                         ke.kb_index = i;
28                         ke.kb_value = 0;
29
30                         if (ioctl(fd, KDGKBENT, (unsigned long) &ke)) {
31                                 ERR(ctx, _("KDGKBENT: %s: error at index %d in table %d"),
32                                         strerror(errno), i, t);
33                                 return -1;
34                         }
35
36                         if (lk_add_key(ctx, t, i, ke.kb_value) < 0)
37                                 return -1;
38                 }
39         }
40
41         if (lk_add_constants(ctx) < 0)
42                 return -1;
43
44         return 0;
45 }
46
47 int
48 lk_kernel_funcs(struct lk_ctx *ctx, int fd)
49 {
50         int i;
51         struct kbsentry kbs;
52
53         for (i = 0; i < MAX_NR_FUNC; i++) {
54                 kbs.kb_func = i;
55
56                 if (ioctl(fd, KDGKBSENT, (unsigned long) &kbs)) {
57                         ERR(ctx, _("KDGKBSENT: %s: Unable to get function key string"),
58                                 strerror(errno));
59                         return -1;
60                 }
61
62                 if (!strlen((char *) kbs.kb_string))
63                         continue;
64
65                 if (lk_add_func(ctx, kbs) < 0)
66                         return -1;
67         }
68
69         return 0;
70 }
71
72 int
73 lk_kernel_diacrs(struct lk_ctx *ctx, int fd)
74 {
75 #ifdef KDGKBDIACRUC
76         int request = KDGKBDIACRUC;
77         struct kbdiacrsuc kd;
78         struct kbdiacruc *ar = kd.kbdiacruc;
79 #else
80         int request = KDGKBDIACR;
81         struct kbdiacrs kd;
82         struct kbdiacr *ar = kd.kbdiacr;
83 #endif
84         unsigned int i;
85         struct lk_kbdiacr dcr;
86
87         if (ioctl(fd, request, (unsigned long) &kd)) {
88                 ERR(ctx, _("KDGKBDIACR(UC): %s: Unable to get accent table"),
89                         strerror(errno));
90                 return -1;
91         }
92
93         for (i = 0; i < kd.kb_cnt; i++) {
94                 dcr.diacr  = (ar+i)->diacr;
95                 dcr.base   = (ar+i)->base;
96                 dcr.result = (ar+i)->result;
97
98                 if (lk_add_diacr(ctx, i, &dcr) < 0)
99                         return -1;
100         }
101
102         return 0;
103 }
104
105 int
106 lk_kernel_keymap(struct lk_ctx *ctx, int fd)
107 {
108         if (lk_kernel_keys(ctx, fd)   < 0 ||
109             lk_kernel_funcs(ctx, fd)  < 0 ||
110             lk_kernel_diacrs(ctx, fd) < 0)
111                 return -1;
112         return 0;
113 }