loadkeys.c

Go to the documentation of this file.
00001 /*      loadkeys - load national keyboard map           Author: Marcus Hampel
00002  */
00003 #include <sys/types.h>
00004 #include <sys/ioctl.h>
00005 #include <minix/keymap.h>
00006 #include <fcntl.h>
00007 #include <unistd.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include <errno.h>
00011 
00012 #if __minix_vmd
00013 #define KBD_DEVICE      "/dev/kbd"
00014 #else
00015 #define KBD_DEVICE      "/dev/console"
00016 #endif
00017 
00018 u16_t keymap[NR_SCAN_CODES * MAP_COLS];
00019 u8_t comprmap[4 + NR_SCAN_CODES * MAP_COLS * 9/8 * 2 + 1];
00020 
00021 
00022 void tell(char *s)
00023 {
00024   write(2, s, strlen(s));
00025 }
00026 
00027 
00028 void fatal(char *say)
00029 {
00030   int err = errno;
00031   tell("loadkeys: ");
00032   if (say != NULL) {
00033         tell(say);
00034         tell(": ");
00035   }
00036   tell(strerror(err));
00037   tell("\n");
00038   exit(1);
00039 }
00040 
00041 
00042 void usage(void)
00043 {
00044   tell("Usage: loadkeys mapfile\n");
00045   exit(1);
00046 }
00047 
00048 
00049 int main(int argc, char *argv[])
00050 {
00051   u8_t *cm;
00052   u16_t *km;
00053   int fd, n, fb;
00054 
00055   if (argc != 2)
00056         usage();
00057 
00058   if ((fd = open(argv[1], O_RDONLY)) < 0) fatal(argv[1]);
00059 
00060   if (read(fd, comprmap, sizeof(comprmap)) < 0) fatal(argv[1]);
00061 
00062   if (memcmp(comprmap, KEY_MAGIC, 4) != 0) {
00063         tell("loadkeys: ");
00064         tell(argv[1]);
00065         tell(": not a keymap file\n");
00066         exit(1);
00067   }
00068   close(fd);
00069 
00070   /* Decompress the keymap data. */
00071   cm = comprmap + 4;
00072   n = 8;
00073   for (km = keymap; km < keymap + NR_SCAN_CODES * MAP_COLS; km++) {
00074         if (n == 8) {
00075                 /* Need a new flag byte. */
00076                 fb = *cm++;
00077                 n = 0;
00078         }
00079         *km = *cm++;                    /* Low byte. */
00080         if (fb & (1 << n)) {
00081                 *km |= (*cm++ << 8);    /* One of the few special keys. */
00082         }
00083         n++;
00084   }
00085 
00086   if ((fd = open(KBD_DEVICE, O_WRONLY)) < 0) fatal(KBD_DEVICE);
00087 
00088   if (ioctl(fd, KIOCSMAP, keymap) < 0) fatal(KBD_DEVICE);
00089 
00090   return 0;
00091 }

Generated on Fri Apr 14 22:56:57 2006 for minix by  doxygen 1.4.6