]> git.ipfire.org Git - ipfire-2.x.git/blob - src/setup/keymap.c
core130: Ship SSH Agent Forwarding changes
[ipfire-2.x.git] / src / setup / keymap.c
1 /* SmoothWall setup program.
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 * (c) Lawrence Manning, 2001
7 * Stuff for setting the keymap.
8 *
9 * $Id: keymap.c,v 1.9.2.1 2004/04/14 22:05:41 gespinasse Exp $
10 *
11 */
12
13 // Translation
14 #include <libintl.h>
15 #define _(x) dgettext("setup", x)
16
17 #include "setup.h"
18
19 extern FILE *flog;
20 extern char *mylog;
21
22 extern int automode;
23
24 #define MAX_FILENAMES 5000
25 #define KEYMAPROOT "/lib/kbd/keymaps/i386/"
26
27 static int filenamecount;
28 static char *filenames[MAX_FILENAMES];
29 static char *displaynames[MAX_FILENAMES];
30
31 static int process(char *prefix, char *path);
32 static int cmp(const void *s1, const void *s2);
33
34 int handlekeymap(void)
35 {
36 int c;
37 int choice;
38 char *temp;
39 struct keyvalue *kv = initkeyvalues();
40 int rc;
41 int result;
42 char keymap[STRING_SIZE];
43 char commandstring[STRING_SIZE];
44
45 filenamecount = 0;
46
47 process(KEYMAPROOT "azerty", "");
48 process(KEYMAPROOT "dvorak", "");
49 process(KEYMAPROOT "fgGIod", "");
50 process(KEYMAPROOT "qwerty", "");
51 process(KEYMAPROOT "qwertz", "");
52 filenames[filenamecount] = NULL;
53 qsort(filenames, filenamecount, sizeof(char *), cmp);
54
55 for (c = 0; filenames[c]; c++)
56 {
57 displaynames[c] = malloc(STRING_SIZE);
58 if ((temp = strrchr(filenames[c], '/')))
59 strcpy(displaynames[c], temp + 1);
60 else
61 strcpy(displaynames[c], filenames[c]);
62 if ((temp = strstr(displaynames[c], ".map.gz")))
63 *temp = '\0';
64 }
65 displaynames[c] = NULL;
66
67 if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings")))
68 {
69 freekeyvalues(kv);
70 errorbox(_("Unable to open settings file"));
71 return 0;
72 }
73
74 strcpy(keymap, "/lib/kbd/keymaps/i386/qwerty/us.map.gz");
75 findkey(kv, "KEYMAP", keymap);
76
77 choice = 0;
78 for (c = 0; filenames[c]; c++)
79 {
80 if (strcmp(keymap, filenames[c]) == 0)
81 choice = c;
82 }
83
84 rc = newtWinMenu(_("Keyboard mapping"),
85 _("Choose the type of keyboard you are using from the list below."),
86 50, 5, 5, 6, displaynames, &choice, _("OK"), _("Cancel"), NULL);
87
88 strcpy(keymap, filenames[choice]);
89
90 if (rc != 2)
91 {
92 replacekeyvalue(kv, "KEYMAP", keymap);
93 writekeyvalues(kv, CONFIG_ROOT "/main/settings");
94 sprintf(commandstring, "/bin/loadkeys %s", keymap);
95 mysystem(NULL, commandstring);
96 result = 1;
97 }
98 else
99 result = 0;
100
101 for (c = 0; filenames[c]; c++)
102 {
103 free(filenames[c]);
104 free(displaynames[c]);
105 }
106 freekeyvalues(kv);
107
108 return result;
109 }
110
111 static int process(char *prefix, char *path)
112 {
113 DIR *dir;
114 struct dirent *de;
115 char newpath[PATH_MAX];
116
117 snprintf(newpath, PATH_MAX, "%s%s", prefix, path);
118
119 if (!(dir = opendir(newpath)))
120 {
121 if (filenamecount > MAX_FILENAMES)
122 return 1;
123
124 filenames[filenamecount] = (char *) strdup(newpath);
125 filenamecount++;
126 return 0;
127 }
128
129 while ((de = readdir(dir)))
130 {
131 if (de->d_name[0] == '.') continue;
132 snprintf(newpath, PATH_MAX, "%s/%s", path, de->d_name);
133 process(prefix, newpath);
134 }
135 closedir(dir);
136
137 return 1;
138 }
139
140 /* Small wrapper for use with qsort() to sort filename part. */
141 static int cmp(const void *s1, const void *s2)
142 {
143 /* c1 and c2 are copies. */
144 char *c1 = strdup(* (char **) s1);
145 char *c2 = strdup(* (char **) s2);
146 /* point to somewhere in cN. */
147 char *f1, *f2;
148 char *temp;
149 int res;
150
151 if ((temp = strrchr(c1, '/')))
152 f1 = temp + 1;
153 else
154 f1 = c1;
155 if ((temp = strrchr(c2, '/')))
156 f2 = temp + 1;
157 else
158 f2 = c2;
159 /* bang off the . */
160 if ((temp = strchr(f1, '.')))
161 *temp = '\0';
162 if ((temp = strchr(f2, '.')))
163 *temp = '\0';
164
165 res = strcmp(f1, f2);
166
167 free(c1); free(c2);
168
169 return res;
170 }