[systemd-devel] [PATCH] udev: Add keyboard leds helper app
Carlos Garnacho
carlosg at gnome.org
Fri Jan 17 03:01:00 PST 2014
This helper program checks the leds available on keyboard devices
and exports these as list of keywords in the ID_INPUT_KEYBOARD_LEDS
property. The new .rule ensures this helper program runs at the time
any keyboard is detected/plugged.
---
Makefile.am | 14 ++
rules/61-keyboard-leds.rules | 3 +
src/udev/keyboard_leds/keyboard_leds.c | 262 +++++++++++++++++++++++++++++++++
3 files changed, 279 insertions(+)
create mode 100644 rules/61-keyboard-leds.rules
create mode 100644 src/udev/keyboard_leds/keyboard_leds.c
diff --git a/Makefile.am b/Makefile.am
index 0d0ff49..97c6ffc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2707,6 +2707,20 @@ dist_udevrules_DATA += \
rules/60-persistent-v4l.rules
# ------------------------------------------------------------------------------
+keyboard_leds_SOURCES = \
+ src/udev/keyboard_leds/keyboard_leds.c
+
+keyboard_leds_LDADD = \
+ libudev-internal.la -lm \
+ libsystemd-shared.la
+
+udevlibexec_PROGRAMS += \
+ keyboard_leds
+
+dist_udevrules_DATA += \
+ rules/61-keyboard-leds.rules
+
+# ------------------------------------------------------------------------------
accelerometer_SOURCES = \
src/udev/accelerometer/accelerometer.c
diff --git a/rules/61-keyboard-leds.rules b/rules/61-keyboard-leds.rules
new file mode 100644
index 0000000..db09dbb
--- /dev/null
+++ b/rules/61-keyboard-leds.rules
@@ -0,0 +1,3 @@
+# do not edit this file, it will be overwritten on update
+
+SUBSYSTEM=="input", ACTION=="add", ENV{ID_INPUT_KEYBOARD}=="1", IMPORT{program}="keyboard_leds %p"
diff --git a/src/udev/keyboard_leds/keyboard_leds.c b/src/udev/keyboard_leds/keyboard_leds.c
new file mode 100644
index 0000000..c23557a
--- /dev/null
+++ b/src/udev/keyboard_leds/keyboard_leds.c
@@ -0,0 +1,262 @@
+/*
+ * keyboard_leds - exports available keyboard leds
+ *
+ * Read leds on the device and export those as ID_INPUT_KEYBOARD_LEDS.
+ *
+ * Copyright (C) 2013 Carlos Garnacho <carlosg at gnome.org>
+ * Author:
+ * Carlos Garnacho <carlosg at gnome.org>
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <limits.h>
+#include <linux/limits.h>
+#include <linux/input.h>
+
+#include "libudev.h"
+#include "libudev-private.h"
+
+/* we must use this kernel-compatible implementation */
+#define BITS_PER_LONG (sizeof(unsigned long) * 8)
+#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
+#define OFF(x) ((x)%BITS_PER_LONG)
+#define BIT(x) (1UL<<OFF(x))
+#define LONG(x) ((x)/BITS_PER_LONG)
+#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
+
+static int debug = 0;
+
+/* Array matching LED_* defines in input.h */
+static const char *led_names[] = {
+ "num-lock",
+ "caps-lock",
+ "scroll-lock",
+ "compose",
+ "kana",
+ "sleep",
+ "suspend",
+ "mute",
+ "misc",
+ "mail",
+ "charging"
+};
+
+static const char * bit_to_string (unsigned int bit)
+{
+ if (bit > (sizeof (led_names) / sizeof (led_names[0])))
+ return NULL;
+
+ return led_names[bit];
+}
+
+static void log_fn(struct udev *udev, int priority,
+ const char *file, int line, const char *fn,
+ const char *format, va_list args)
+{
+ if (debug) {
+ fprintf(stderr, "%s: ", fn);
+ vfprintf(stderr, format, args);
+ } else {
+ vsyslog(priority, format, args);
+ }
+}
+
+static void append_string(char *str, int *pos,
+ const char *substr)
+{
+ int substr_len;
+
+ substr_len = strlen (substr);
+ strncpy(&str[*pos], substr, substr_len);
+ *pos = *pos + substr_len;
+}
+
+static char * array_to_string(const char * const *array, int len)
+{
+ int i, pos = 0, str_len = 0;
+ char *str;
+
+ if (len == 0)
+ return NULL;
+
+ for (i = 0; i < len; i++) {
+ if (!array[i])
+ continue;
+ str_len += strlen (array[i]) + 1;
+ }
+
+ str = malloc (sizeof (char) * str_len + 1);
+
+ for (i = 0; i < len; i++) {
+ if (!array[i])
+ continue;
+ append_string (str, &pos, array[i]);
+ append_string (str, &pos, " ");
+ }
+
+ str[str_len] = '\0';
+
+ return str;
+}
+
+static void check_leds(struct udev *udev,
+ struct udev_device *dev,
+ const char *devpath)
+{
+ unsigned long ledbits[NBITS(LED_MAX)];
+ _cleanup_close_ int fd = -1;
+ const char *leds[LED_MAX];
+ char text[1024], *led_str;
+ int i, j = 0;
+
+ if ((fd = open(devpath, O_RDONLY)) < 0)
+ return;
+
+ memset(ledbits, 0, sizeof(ledbits));
+ ioctl(fd, EVIOCGBIT(EV_LED, LED_MAX), ledbits);
+
+ for (i = 0; i < LED_MAX; i++)
+ if (test_bit (i, ledbits))
+ leds[j++] = bit_to_string (i);
+
+ led_str = array_to_string (leds, j);
+
+ if (led_str) {
+ snprintf(text, sizeof(text), "ID_INPUT_KEYBOARD_LEDS=%s", led_str);
+ puts(text);
+ free(led_str);
+ }
+}
+
+static void help(void)
+{
+ printf("Usage: accelerometer [options] <device path>\n"
+ " --debug debug to stderr\n"
+ " --help print this help text\n\n");
+}
+
+int main(int argc, char** argv)
+{
+ struct udev *udev;
+ struct udev_device *dev;
+
+ static const struct option options[] = {
+ { "debug", no_argument, NULL, 'd' },
+ { "help", no_argument, NULL, 'h' },
+ {}
+ };
+
+ char devpath[PATH_MAX];
+ char *devnode;
+ struct udev_enumerate *enumerate;
+ struct udev_list_entry *list_entry;
+
+ udev = udev_new();
+ if (udev == NULL)
+ return 1;
+
+ log_open();
+ udev_set_log_fn(udev, log_fn);
+
+ /* CLI argument parsing */
+ while (1) {
+ int option;
+
+ option = getopt_long(argc, argv, "dxh", options, NULL);
+ if (option == -1)
+ break;
+
+ switch (option) {
+ case 'd':
+ debug = 1;
+ if (udev_get_log_priority(udev) < LOG_INFO)
+ udev_set_log_priority(udev, LOG_INFO);
+ break;
+ case 'h':
+ help();
+ exit(0);
+ default:
+ exit(1);
+ }
+ }
+
+ if (argv[optind] == NULL) {
+ help();
+ exit(1);
+ }
+
+ /* get the device */
+ snprintf(devpath, sizeof(devpath), "/sys/%s", argv[optind]);
+ dev = udev_device_new_from_syspath(udev, devpath);
+ if (dev == NULL) {
+ fprintf(stderr, "unable to access '%s'\n", devpath);
+ return 1;
+ }
+
+ /* Get the children devices and find the devnode */
+ devnode = NULL;
+ enumerate = udev_enumerate_new(udev);
+ udev_enumerate_add_match_parent(enumerate, dev);
+ udev_enumerate_scan_devices(enumerate);
+ udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
+ struct udev_device *device;
+ const char *node;
+
+ device = udev_device_new_from_syspath(udev_enumerate_get_udev(enumerate),
+ udev_list_entry_get_name(list_entry));
+ if (device == NULL)
+ continue;
+ /* Already found it */
+ if (devnode != NULL) {
+ udev_device_unref(device);
+ continue;
+ }
+
+ node = udev_device_get_devnode(device);
+ if (node == NULL) {
+ udev_device_unref(device);
+ continue;
+ }
+ /* Use the event sub-device */
+ if (strstr(node, "/event") == NULL) {
+ udev_device_unref(device);
+ continue;
+ }
+
+ devnode = strdup(node);
+ udev_device_unref(device);
+ }
+
+ if (devnode == NULL) {
+ fprintf(stderr, "unable to get device node for '%s'\n", devpath);
+ return 0;
+ }
+
+ log_debug("Opening input device %s\n", devnode);
+ check_leds(udev, dev, devnode);
+ free(devnode);
+
+ return 0;
+}
--
1.8.5.2
More information about the systemd-devel
mailing list