accf92a0302016d3c18bd32f42261652e31cd581
[people/teissler/ipfire-2.x.git] / src / install+setup / 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 #include "setup.h"
14
15 extern FILE *flog;
16 extern char *mylog;
17
18 extern char **ctr;
19
20 extern int automode;
21
22 #define MAX_FILENAMES 5000
23 #define KEYMAPROOT "/lib/kbd/keymaps/i386/"
24
25 static int filenamecount;
26 static char *filenames[MAX_FILENAMES];
27 static char *displaynames[MAX_FILENAMES];
28
29 static int process(char *prefix, char *path);
30 static int cmp(const void *s1, const void *s2);
31
32 int handlekeymap(void)
33 {
34 int c;
35 int choice;
36 char *temp;
37 struct keyvalue *kv = initkeyvalues();
38 int rc;
39 int result;
40 char keymap[STRING_SIZE];
41 char commandstring[STRING_SIZE];
42
43 filenamecount = 0;
44
45 process(KEYMAPROOT "azerty", "");
46 process(KEYMAPROOT "dvorak", "");
47 process(KEYMAPROOT "fgGIod", "");
48 process(KEYMAPROOT "qwerty", "");
49 process(KEYMAPROOT "qwertz", "");
50 filenames[filenamecount] = NULL;
51 qsort(filenames, filenamecount, sizeof(char *), cmp);
52
53 for (c = 0; filenames[c]; c++)
54 {
55 displaynames[c] = malloc(STRING_SIZE);
56 if ((temp = strrchr(filenames[c], '/')))
57 strcpy(displaynames[c], temp + 1);
58 else
59 strcpy(displaynames[c], filenames[c]);
60 if ((temp = strstr(displaynames[c], ".map.gz")))
61 *temp = '\0';
62 }
63 displaynames[c] = NULL;
64
65 if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings")))
66 {
67 freekeyvalues(kv);
68 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
69 return 0;
70 }
71
72 strcpy(keymap, "/lib/kbd/keymaps/i386/qwertz/de-latin1-nodeadkeys.map.gz");
73 findkey(kv, "KEYMAP", keymap);
74
75 choice = 0;
76 for (c = 0; filenames[c]; c++)
77 {
78 if (strcmp(keymap, filenames[c]) == 0)
79 choice = c;
80 }
81
82 rc = newtWinMenu(ctr[TR_KEYBOARD_MAPPING], ctr[TR_KEYBOARD_MAPPING_LONG], 50, 5, 5, 6, displaynames, &choice,
83 ctr[TR_OK], ctr[TR_CANCEL], NULL);
84
85 strcpy(keymap, filenames[choice]);
86
87 if (rc != 2)
88 {
89 replacekeyvalue(kv, "KEYMAP", keymap);
90 writekeyvalues(kv, CONFIG_ROOT "/main/settings");
91 sprintf(commandstring, "/bin/loadkeys %s", keymap);
92 mysystem(commandstring);
93 result = 1;
94 }
95 else
96 result = 0;
97
98 for (c = 0; filenames[c]; c++)
99 {
100 free(filenames[c]);
101 free(displaynames[c]);
102 }
103 freekeyvalues(kv);
104
105 return result;
106 }
107
108 static int process(char *prefix, char *path)
109 {
110 DIR *dir;
111 struct dirent *de;
112 char newpath[PATH_MAX];
113
114 snprintf(newpath, PATH_MAX, "%s%s", prefix, path);
115
116 if (!(dir = opendir(newpath)))
117 {
118 if (filenamecount > MAX_FILENAMES)
119 return 1;
120
121 filenames[filenamecount] = (char *) strdup(newpath);
122 filenamecount++;
123 return 0;
124 }
125
126 while ((de = readdir(dir)))
127 {
128 if (de->d_name[0] == '.') continue;
129 snprintf(newpath, PATH_MAX, "%s/%s", path, de->d_name);
130 process(prefix, newpath);
131 }
132 closedir(dir);
133
134 return 1;
135 }
136
137 /* Small wrapper for use with qsort() to sort filename part. */
138 static int cmp(const void *s1, const void *s2)
139 {
140 /* c1 and c2 are copies. */
141 char *c1 = strdup(* (char **) s1);
142 char *c2 = strdup(* (char **) s2);
143 /* point to somewhere in cN. */
144 char *f1, *f2;
145 char *temp;
146 int res;
147
148 if ((temp = strrchr(c1, '/')))
149 f1 = temp + 1;
150 else
151 f1 = c1;
152 if ((temp = strrchr(c2, '/')))
153 f2 = temp + 1;
154 else
155 f2 = c2;
156 /* bang off the . */
157 if ((temp = strchr(f1, '.')))
158 *temp = '\0';
159 if ((temp = strchr(f2, '.')))
160 *temp = '\0';
161
162 res = strcmp(f1, f2);
163
164 free(c1); free(c2);
165
166 return res;
167 }