]>
Commit | Line | Data |
---|---|---|
4c29606d MT |
1 | /* |
2 | * Copyright (c) 2008-2009 Red Hat, Inc. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License, version 2, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
16 | * | |
17 | */ | |
18 | ||
19 | #include <fcntl.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <unistd.h> | |
23 | ||
24 | #include <sys/ioctl.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
27 | #include <sys/wait.h> | |
28 | ||
29 | #include <linux/kd.h> | |
30 | ||
31 | #include "shvar.h" | |
32 | ||
33 | static char *lang = NULL; | |
34 | static char *font = NULL; | |
35 | static char *acm = NULL; | |
36 | static char *unimap = NULL; | |
37 | static char *keymap = NULL; | |
38 | ||
39 | static int linux_console(int fd) { | |
40 | unsigned char twelve = 12; | |
41 | ||
42 | if (ioctl(fd, TIOCLINUX, &twelve) >= 0) | |
43 | return 1; | |
44 | return 0; | |
45 | } | |
46 | ||
47 | static int configured_as_utf8() { | |
48 | shvarFile *i18nfile = NULL; | |
49 | ||
50 | if ((i18nfile = svNewFile("/etc/sysconfig/i18n")) == NULL) | |
51 | return 1; /* assume UTF-8 */ | |
52 | ||
53 | lang = svGetValue(i18nfile, "LANG"); | |
54 | font = svGetValue(i18nfile, "SYSFONT"); | |
55 | acm = svGetValue(i18nfile, "SYSFONTACM"); | |
56 | unimap = svGetValue(i18nfile, "UNIMAP"); | |
57 | svCloseFile(i18nfile); | |
58 | if (!lang) | |
59 | return 1; | |
60 | if (g_str_has_suffix(lang,".utf8") || g_str_has_suffix(lang,".UTF-8")) | |
61 | return 1; | |
62 | return 0; | |
63 | } | |
64 | ||
65 | static int read_keymap() { | |
66 | shvarFile *keyboard = NULL; | |
67 | char *tmp; | |
68 | struct stat sb; | |
69 | ||
70 | if (!stat("/etc/sysconfig/console/default.kmap",&sb)) { | |
71 | keymap = "/etc/sysconfig/console/default.kmap"; | |
72 | return 0; | |
73 | } | |
74 | ||
75 | if ((keyboard = svNewFile("/etc/sysconfig/keyboard")) == NULL) | |
76 | return 0; | |
77 | ||
78 | tmp = svGetValue(keyboard, "KEYMAP"); | |
79 | if (tmp) | |
80 | keymap = tmp; | |
81 | tmp = svGetValue(keyboard, "KEYTABLE"); | |
82 | if (tmp) { | |
83 | if (keymap) free(keymap); | |
84 | asprintf(&keymap, "%s.map", tmp); | |
85 | } | |
86 | return 0; | |
87 | } | |
88 | ||
89 | static void set_font(char *device) { | |
90 | int pid; | |
91 | ||
92 | if ( (pid = fork()) == 0) { | |
93 | char *args[] = { "setfont", "latarcyrheb-sun16", "-C", NULL, | |
94 | NULL, NULL, NULL, NULL, NULL }; | |
95 | ||
96 | if (font) | |
97 | args[1] = font; | |
98 | args[3] = device; | |
99 | if (acm) { | |
100 | args[4] = "-m"; | |
101 | args[5] = acm; | |
102 | if (unimap) { | |
103 | args[6] = "-u"; | |
104 | args[7] = unimap; | |
105 | } | |
106 | } else if (unimap) { | |
107 | args[4] = "-u"; | |
108 | args[5] = unimap; | |
109 | } | |
110 | execv("/bin/setfont", args); | |
111 | exit(1); | |
112 | } | |
113 | } | |
114 | ||
115 | static void set_keyboard(int fd, int utf8) { | |
116 | if (ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE)) | |
117 | perror("could not set keyboard mode"); | |
118 | } | |
119 | ||
120 | static void set_terminal(int fd, int utf8) { | |
121 | if (utf8) | |
122 | write(fd, "\033%G", 3); | |
123 | else | |
124 | write(fd, "\033%@", 3); | |
125 | } | |
126 | ||
127 | static void set_keymap(int fd, int utf8) { | |
128 | int pid; | |
129 | ||
130 | if ((pid = fork()) == 0) { | |
131 | char *args[] = { "loadkeys", "-q", NULL, NULL, NULL }; | |
132 | dup2(fd, 0); | |
133 | dup2(fd, 1); | |
134 | ||
135 | if (utf8) { | |
136 | args[2] = "-u"; | |
137 | args[3] = keymap; | |
138 | } else { | |
139 | args[2] = keymap; | |
140 | } | |
141 | execv("/bin/loadkeys", args); | |
142 | exit(1); | |
143 | } | |
144 | } | |
145 | ||
146 | int main(int argc, char **argv) { | |
147 | char *device; | |
148 | int dev; | |
149 | ||
150 | if (argc < 2) { | |
151 | printf("usage: console_init <device>\n"); | |
152 | exit(1); | |
153 | } | |
154 | chdir("/dev"); | |
155 | device = argv[1]; | |
156 | dev = open(device, O_RDWR); | |
157 | if (linux_console(dev)) { | |
158 | int utf8 = configured_as_utf8(); | |
159 | ||
160 | set_keyboard(dev, utf8); | |
161 | set_terminal(dev, utf8); | |
162 | set_font(device); | |
163 | read_keymap(); | |
164 | if (keymap) | |
165 | set_keymap(dev,utf8); | |
166 | } | |
167 | return 0; | |
168 | } |