home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / install / readmap.c < prev    next >
C/C++ Source or Header  |  1997-09-17  |  2KB  |  89 lines

  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <sys/ioctl.h>
  4. #include <linux/keyboard.h>
  5. #include <linux/kd.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9.  
  10. #include "kbd.h"
  11.  
  12. void main(int argc, char ** argv) {
  13.     int console;
  14.     int kmap, key;
  15.     struct kbentry entry;
  16.     int keymaps[MAX_NR_KEYMAPS];
  17.     int count = 0;
  18.     int out;
  19.     short keymap[NR_KEYS];
  20.     int magic = KMAP_MAGIC;
  21.  
  22.     if (argc != 2) {
  23.     printf("bad usage\n");
  24.     exit(1);
  25.     }
  26.  
  27.     memset(keymaps, 0, sizeof(keymaps));
  28.  
  29.     console = open("/dev/console", O_RDWR);
  30.     if (console < 0) {
  31.     perror("open console");
  32.     exit(1);
  33.     }
  34.  
  35.     for (kmap = 0; kmap < MAX_NR_KEYMAPS; kmap++) {
  36.     for (key = 0; key < NR_KEYS; key++) {
  37.         entry.kb_index = key;
  38.         entry.kb_table = kmap;
  39.         if (ioctl(console, KDGKBENT, &entry)) {
  40.         perror("ioctl failed");
  41.         exit(1);
  42.         } else if (KTYP(entry.kb_value) != KT_SPEC) {
  43.         keymaps[kmap] = 1;
  44.         count++;
  45.         break;
  46.         }
  47.     }
  48.     }
  49.  
  50.     printf("found %d valid keymaps\n", count);
  51.  
  52.     printf("creating keymap file %s\n", argv[1]);
  53.     if ((out = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 1) {
  54.     perror("open keymap");
  55.     exit(1);
  56.     }
  57.  
  58.     if (write(out, &magic, sizeof(magic)) != sizeof(magic)) {
  59.     perror("write magic");
  60.     exit(1);
  61.     }
  62.  
  63.     if (write(out, keymaps, sizeof(keymaps)) != sizeof(keymaps)) {
  64.     perror("write header");
  65.     exit(1);
  66.     }
  67.         
  68.     for (kmap = 0; kmap < MAX_NR_KEYMAPS; kmap++) {
  69.     if (!keymaps[kmap]) continue;
  70.     for (key = 0; key < NR_KEYS; key++) {
  71.         entry.kb_index = key;
  72.         entry.kb_table = kmap;
  73.         if (ioctl(console, KDGKBENT, &entry)) {
  74.         perror("ioctl failed");
  75.         exit(1);
  76.         } else {
  77.         keymap[key] = entry.kb_value;
  78.         }
  79.     }
  80.  
  81.     if (write(out, keymap, sizeof(keymap)) != sizeof(keymap)) {
  82.         perror("write keymap");
  83.         exit(1);
  84.     }
  85.     }
  86.  
  87.     close(out);
  88. }
  89.