From 751079032006b208f2944c19a4f7c9039a947217 Mon Sep 17 00:00:00 2001 From: Andrew Fyfe Date: Thu, 16 Sep 2010 12:09:29 +0100 Subject: [PATCH] Clean up the code Signed-off-by: Andrew Fyfe (cherry picked from commit b10271c7db15777734fdf21ee1809c184ee98d00) --- hama_mce.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++--------- hama_mce.rules | 2 +- 2 files changed, 67 insertions(+), 12 deletions(-) diff --git a/hama_mce.cpp b/hama_mce.cpp index 16beb7c..f720812 100644 --- a/hama_mce.cpp +++ b/hama_mce.cpp @@ -35,12 +35,15 @@ #include #include #include +#include #include #include "xbmcclient.h" -#define msg(x...) do { fprintf(stdout, x); } while (0) -#define err(x...) do { fprintf(stderr, x); } while (0) +#define VERSION "1.3" + +#define msg(x...) fprintf(stdout, x); +#define err(x...) fprintf(stderr, x); #define PCHK(x...) \ do { \ @@ -258,7 +261,8 @@ static void transfer0x82_cb (struct libusb_transfer *transfer) if (--rclick_pending == 0) emit_right_click(); } - } else if (transfer->buffer[1] == 0x01) + } + else if (transfer->buffer[1] == 0x01) emit_left_click(); else if (transfer->buffer[1] == 0x02) rclick_pending = 2; @@ -299,24 +303,75 @@ static void handle_exit(int sig) quit = true; } +void print_usage(char* progname) +{ + msg("HAMA MCE remote event client for XBMC\n"); + msg("\tUsage: %s [options]\n", progname); + msg("\t\t -h --help\t\tdisplay usage summary\n"); + msg("\t\t -v --version\t\tdisplay version\n"); + msg("\t\t -d --daemon\t\trun in background\n"); +} + int main (int argc, char **argv) { - if (argc > 1 && strcmp(argv[1],"--fork") == 0) { + bool daemonize = false; + + while (true) { + int c; + static struct option long_options[] = + { + {"help", no_argument, NULL, 'h'}, + {"version", no_argument, NULL, 'v'}, + {"daemon", no_argument, NULL, 'd'}, + {"fork", no_argument, NULL, 'f'}, + {0, 0, 0, 0} + }; + + c = getopt_long(argc, argv, "hvdf", long_options, NULL); + if (c == -1) + break; + switch (c) { + case 'h': + print_usage(argv[0]); + exit(EXIT_SUCCESS); + case 'v': + msg("HAMA MCE remote event client v%s for XBMC\n", VERSION); + exit(EXIT_SUCCESS); + case 'd': + daemonize = true; + break; + case 'f': + err("%s: The --fork argument is deprectiated, change it to -d or --daemon\n", argv[0]); + daemonize = true; + break; + default: + print_usage(argv[0]); + exit(EXIT_FAILURE); + } + } + + if (optind < (argc - 1)) { + err("%s: too many arguments\n", argv[0]); + exit(EXIT_FAILURE); + } + + if (daemonize) { pid_t pID = fork(); if (pID < 0) { // failed to fork err("Failed to fork\n"); - exit(1); + exit(EXIT_FAILURE); } else if (pID > 0) { // parent msg("FORKED!"); - exit(0); + exit(EXIT_SUCCESS); } } struct sigaction sa; sigemptyset(&sa.sa_mask); sa.sa_handler = handle_exit; - PCHK(sigaction(SIGINT, &sa, NULL)); + PCHK(sigaction(SIGINT, &sa, NULL)); + PCHK(sigaction(SIGTERM, &sa, NULL)); libusb_context *ctx; libusb_device_handle *dev; @@ -324,13 +379,12 @@ int main (int argc, char **argv) struct libusb_transfer *transfer0x82 = libusb_alloc_transfer(0); unsigned char buf0x81 [8]; unsigned char buf0x82 [5]; - int i; UCHK(libusb_init(&ctx)); if (!(dev = libusb_open_device_with_vid_pid(ctx, 0x05a4, 0x9881))) { err("%s: No HAMA MCE remote control found.\n", argv[0]); - return -1; + exit(EXIT_FAILURE); } msg("Connected HAMA MCE Remote\n"); @@ -365,17 +419,18 @@ int main (int argc, char **argv) libusb_free_transfer(transfer0x81); libusb_free_transfer(transfer0x82); - /* not needed, if we arrive here, the device has been disconnected.i*/ if (!disconnected) { + // Release the remote back to the system UCHK(libusb_release_interface(dev, 0)); UCHK(libusb_release_interface(dev, 1)); UCHK(libusb_attach_kernel_driver(dev, 0)); UCHK(libusb_attach_kernel_driver(dev, 1)); } + libusb_close(dev); libusb_exit(ctx); - return 0; + exit(EXIT_SUCCESS); } /* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */ diff --git a/hama_mce.rules b/hama_mce.rules index d038598..9d65e44 100644 --- a/hama_mce.rules +++ b/hama_mce.rules @@ -4,4 +4,4 @@ # There is also a LIRC driver available for this, but some buttons are unusable then, since # they send mouse button events instead of key press events. # -BUS=="usb", ACTION=="add", DRIVER=="usb", SYSFS{idVendor}=="05a4", SYSFS{idProduct}=="9881", RUN+="/usr/sbin/hama_mce --fork" +BUS=="usb", ACTION=="add", DRIVER=="usb", SYSFS{idVendor}=="05a4", SYSFS{idProduct}=="9881", RUN+="/usr/sbin/hama_mce -d" -- 2.11.4.GIT