chiark / gitweb /
detect-virt: detect User-Mode Linux
[elogind.git] / src / udev / keymap / keymap.c
index 5cbce86db1b0f9f3cf8ce5cadbcbbf0f73448cfc..ae0a19d3a3e2d913976c65852cc94bee5194d725 100644 (file)
@@ -7,19 +7,19 @@
  * Copyright (C) 2006, Lennart Poettering
  * Copyright (C) 2009, Canonical Ltd.
  *
- * keymap is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
- * keymap is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with keymap; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <stdio.h>
 #include <linux/limits.h>
 #include <linux/input.h>
 
-const struct key* lookup_key (const char *str, unsigned int len);
+static const struct key* lookup_key (const char *str, unsigned int len);
 
 #include "keys-from-name.h"
 #include "keys-to-name.h"
+#include "macro.h"
 #include "util.h"
 
 #define MAX_SCANCODES 1024
@@ -61,28 +62,28 @@ static int evdev_open(const char *dev)
         return fd;
 }
 
-static int evdev_get_keycode(int fd, int scancode, int e)
+static int evdev_get_keycode(int fd, unsigned scancode, int e)
 {
-        int codes[2];
+        unsigned codes[2];
 
         codes[0] = scancode;
         if (ioctl(fd, EVIOCGKEYCODE, codes) < 0) {
                 if (e && errno == EINVAL) {
                         return -2;
                 } else {
-                        fprintf(stderr, "EVIOCGKEYCODE: %m\n");
+                        fprintf(stderr, "EVIOCGKEYCODE for scan code 0x%x: %m\n", scancode);
                         return -1;
                 }
         }
         return codes[1];
 }
 
-static int evdev_set_keycode(int fd, int scancode, int keycode)
+static int evdev_set_keycode(int fd, unsigned scancode, int keycode)
 {
-        int codes[2];
+        unsigned codes[2];
 
         codes[0] = scancode;
-        codes[1] = keycode;
+        codes[1] = (unsigned) keycode;
 
         if (ioctl(fd, EVIOCSKEYCODE, codes) < 0) {
                 fprintf(stderr, "EVIOCSKEYCODE: %m\n");
@@ -127,7 +128,8 @@ static const char* format_keyname(const char* key) {
 
 static int dump_table(int fd) {
         char version[256], name[256];
-        int scancode, r = -1;
+        unsigned scancode;
+        int r = -1;
 
         if (evdev_driver_version(fd, version, sizeof(version)) < 0)
                 goto fail;
@@ -191,7 +193,8 @@ static int merge_table(int fd, FILE *f) {
 
         while (!feof(f)) {
                 char s[256], *p;
-                int scancode, new_keycode, old_keycode;
+                unsigned scancode;
+                int new_keycode, old_keycode;
 
                 if (!fgets(s, sizeof(s), f))
                         break;
@@ -223,19 +226,19 @@ static int merge_table(int fd, FILE *f) {
 
                 if ((old_keycode = evdev_get_keycode(fd, scancode, 0)) < 0) {
                         r = -1;
-                        goto fail;
+                        continue;
                 }
 
                 if (evdev_set_keycode(fd, scancode, new_keycode) < 0) {
                         r = -1;
-                        goto fail;
+                        continue;
                 }
 
                 if (new_keycode != old_keycode)
                         fprintf(stderr, "Remapped scancode 0x%02x to 0x%02x (prior: 0x%02x)\n",
                                 scancode, new_keycode, old_keycode);
         }
-fail:
+
         fclose(f);
         return r;
 }
@@ -259,7 +262,7 @@ static int read_event(int fd, struct input_event* ev)
         return 1;
 }
 
-static void print_key(uint32_t scancode, uint16_t keycode, int has_scan, int has_key)
+static void print_key(unsigned scancode, uint16_t keycode, int has_scan, int has_key)
 {
         const char *keyname;
 
@@ -288,7 +291,7 @@ static void print_key(uint32_t scancode, uint16_t keycode, int has_scan, int has
 static void interactive(int fd)
 {
         struct input_event ev;
-        uint32_t last_scan = 0;
+        unsigned last_scan = 0;
         uint16_t last_key = 0;
         int has_scan; /* boolean */
         int has_key; /* 0: none, 1: release, 2: press */
@@ -346,7 +349,7 @@ static void interactive(int fd)
         ioctl(fd, EVIOCGRAB, 0);
 }
 
-static void help(int error)
+_noreturn_ static void help(int error)
 {
         const char* h = "Usage: keymap <event device> [<map file>]\n"
                         "       keymap <event device> scancode keyname [...]\n"
@@ -362,13 +365,8 @@ static void help(int error)
 
 int main(int argc, char **argv)
 {
-        enum {
-                ARG_VERSION = 0x100,
-        };
-
         static const struct option options[] = {
                 { "help", no_argument, NULL, 'h' },
-                { "version", no_argument, NULL, ARG_VERSION },
                 { "interactive", no_argument, NULL, 'i' },
                 {}
         };
@@ -387,10 +385,6 @@ int main(int argc, char **argv)
                 case 'h':
                         help(0);
 
-                case ARG_VERSION:
-                        puts(PACKAGE_STRING);
-                        exit(0);
-
                 case 'i':
                         opt_interactive = 1;
                         break;
@@ -429,12 +423,13 @@ int main(int argc, char **argv)
                         /* Open override file if present, otherwise default file */
                         char keymap_path[PATH_MAX];
                         FILE *f;
-                        snprintf(keymap_path, sizeof(keymap_path), "%s%s", SYSCONFDIR "/udev/keymaps/", filearg);
+
+                        snprintf(keymap_path, sizeof(keymap_path), "/etc/udev/keymaps/%s", filearg);
                         f = fopen(keymap_path, "re");
                         if (f) {
                                 merge_table(fd, f);
                         } else {
-                                snprintf(keymap_path, sizeof(keymap_path), "%s%s", UDEVLIBEXECDIR "/keymaps/", filearg);
+                                snprintf(keymap_path, sizeof(keymap_path), UDEVLIBEXECDIR "/keymaps/%s", filearg);
                                 f = fopen(keymap_path, "re");
                                 if (f)
                                         merge_table(fd, f);