Skip to content

Commit

Permalink
add --deviceglob/-g option to match device files with glob
Browse files Browse the repository at this point in the history
  • Loading branch information
wertarbyte committed Sep 6, 2016
1 parent 70fd833 commit c9b6556
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 21 additions & 4 deletions thd.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <glob.h>

#include <stdlib.h>
#include <sys/wait.h>
Expand Down Expand Up @@ -214,6 +215,7 @@ static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"uinput", required_argument, 0, '<'},
{"listevents", no_argument, 0, 'l'},
{"deviceglob", required_argument, 0, 'g'},
{0,0,0,0} /* end of list */
};

Expand All @@ -231,6 +233,7 @@ void show_help(void) {
printf( " --ignore <event> Ignore key events with name <event>\n" );
printf( " --normalize Normalize relative movement events\n" );
printf( " --user <name> Drop privileges to <name> after opening devices\n" );
printf( " --deviceglob <p> Open device files matching pattern <p>\n" );
}

static void list_event_table(int type, int max) {
Expand Down Expand Up @@ -289,8 +292,8 @@ static void handle_signal(int sig) {
}
}

int start_readers(int argc, char *argv[], int start) {
if (argc-start < 1 && cmd_file == NULL) {
int start_readers(int argc, char *argv[], int start, char *dev_glob) {
if (argc-start < 1 && cmd_file == NULL && dev_glob == NULL) {
fprintf(stderr, "No input device files or command pipe specified.\n");
return 1;
}
Expand All @@ -315,6 +318,16 @@ int start_readers(int argc, char *argv[], int start) {
char *tag_dev = NULL;
add_device( dev, -1, grab_dev, tag_dev );
}
/* check device glob */
if (dev_glob) {
glob_t globbuf;
glob(dev_glob, GLOB_NOSORT, NULL, &globbuf);
for (i=0; i<globbuf.gl_pathc; i++) {
int grab_dev = 0;
char *tag_dev = NULL;
add_device(globbuf.gl_pathv[i], -1, grab_dev, tag_dev);
}
}
if (run_as_daemon) {
int err = daemon(0,0);
if (err) {
Expand Down Expand Up @@ -359,10 +372,11 @@ int start_readers(int argc, char *argv[], int start) {

int main(int argc, char *argv[]) {
signal(SIGCHLD, SIG_IGN);
char *dev_glob = NULL;
int option_index = 0;
int c;
while (1) {
c = getopt_long (argc, argv, "t:s:dhpni:u:", long_options, &option_index);
c = getopt_long (argc, argv, "t:s:dhpni:u:g:", long_options, &option_index);
if (c == -1) {
break;
}
Expand Down Expand Up @@ -405,6 +419,9 @@ int main(int argc, char *argv[]) {
case '<':
uinput_dev = optarg;
break;
case 'g':
dev_glob = optarg;
break;
case '?':
default:
return 1;
Expand Down Expand Up @@ -432,7 +449,7 @@ int main(int argc, char *argv[]) {
sigaction(SIGTERM,&handler,0);
sigaction(SIGHUP,&handler,0);

int status = start_readers(argc, argv, optind);
int status = start_readers(argc, argv, optind, dev_glob);
cleanup();
return status;
}
Expand Down
6 changes: 5 additions & 1 deletion thd.pod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ thd -- triggerhappy global hotkey daemon

=head1 SYNOPSIS

B<thd> [B<--help>] [B<--user> I<name>] [B<--listevents>] [B<--dump>] [B<--socket> I<socket>] [B<--triggers> I<config>] [B<--daemon>] [B<--pidfile> I<file>] [B<--uinput> I<device>][B<--ignore> I<event>] [I<devices...>]
B<thd> [B<--help>] [B<--user> I<name>] [B<--listevents>] [B<--dump>] [B<--socket> I<socket>] [B<--triggers> I<config>] [B<--daemon>] [B<--pidfile> I<file>] [B<--uinput> I<device>][B<--ignore> I<event>] [B<--deviceglob> I<pattern>] [I<devices...>]

=head1 DESCRIPTION

Expand Down Expand Up @@ -65,6 +65,10 @@ Normalize REL and ABS events. If this option is enabled, the values of axis move

Change to user id I<name> after opening files. This usually prevents thd from opening additional input devices, unless they are opened by the th-cmd program and their file descriptor are passed to the daemon.

=item B<--deviceglob> I<pattern>

Open device files matching the glob I<pattern>.

=back

Additional command line arguments are considered filenames of input devices.
Expand Down

0 comments on commit c9b6556

Please sign in to comment.