Skip to content

Commit

Permalink
read configuration from directory
Browse files Browse the repository at this point in the history
  • Loading branch information
wertarbyte committed Oct 27, 2010
1 parent 367a5ac commit 0ff62c2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion thd.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void cleanup(void) {
static int reload_triggerfile(void) {
clear_triggers();
if (triggerfile) {
int err = read_triggerfile(triggerfile);
int err = read_triggers(triggerfile);
if (err) {
fprintf(stderr, "Error loading triggerfile '%s'\n", triggerfile);
return 1;
Expand Down
13 changes: 7 additions & 6 deletions 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<--dump>] [B<--socket> I<socket>] [B<--trigger> I<config>] [B<--daemon>] [B<--pidfile> I<file>] [B<--ignore> I<event>] [I<devices...>]
B<thd> [B<--help>] [B<--dump>] [B<--socket> I<socket>] [B<--triggers> I<config>] [B<--daemon>] [B<--pidfile> I<file>] [B<--ignore> I<event>] [I<devices...>]

=head1 DESCRIPTION

Expand All @@ -25,7 +25,7 @@ Shows usage instructions

=item B<--triggers> F<conf>

Read trigger definitions from F<file>
Read trigger definitions from F<conf>, which can either be a file or a directory. If a directory is specified, all its files are loaded.

=item B<--dump>

Expand Down Expand Up @@ -75,9 +75,9 @@ B<thd --dump /dev/input/event*>

Dump all events processable by thd to the console; this is useful to find out the correct event name for a specific key.

B<thd --triggers /etc/triggerhappy/triggers.conf /dev/input/event*>
B<thd --triggers /etc/triggerhappy/triggers.d/ /dev/input/event*>

Read from all currently connected input devices and process events according to the file F</etc/triggerhappy/triggers.conf>.
Read from all currently connected input devices and process events according to the files in F</etc/triggerhappy/triggers.d/>.

B<thd --triggers /etc/triggerhappy/triggers.conf --socket /var/run/thd.socket>

Expand All @@ -87,12 +87,13 @@ Do not open any input devices yet, but bind the socket F</var/run/thd.socket> fo

Any number of event handlers can be placed in the configuration file:

# /etc/triggerhappy/triggers.conf
# /etc/triggerhappy/triggers.d/suspend.conf
#
# Suspend the system
KEY_SLEEP 1 /usr/sbin/hibernate-ram
KEY_SLEEP+KEY_LEFTSHIFT 1 /usr/sbin/hibernate-disk


# /etc/triggerhappy/triggers.d/audio.conf
# Change mixer volume when pressing the appropiate keys (or holding them)
KEY_VOLUMEUP 1 /usr/bin/amixer set Master 5%+
KEY_VOLUMEUP 2 /usr/bin/amixer set Master 5%+
Expand Down
46 changes: 45 additions & 1 deletion trigger.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "eventnames.h"
#include "keystate.h"
#include "trigger.h"
Expand Down Expand Up @@ -85,7 +89,7 @@ void append_trigger(trigger *t) {
*p = t;
}

int read_triggerfile(const char *filename) {
static int read_triggerfile(const char *filename) {
trigger **p = &TRIGGER_LIST;
FILE *conf;
int len = 0;
Expand All @@ -108,6 +112,46 @@ int read_triggerfile(const char *filename) {
return 0;
}

int read_triggers(const char *path) {
/* check whether filename is a directory */
struct stat sb;
if (stat(path, &sb) == -1) {
perror("stat");
return 1;
}
if (S_ISDIR(sb.st_mode)) {
/* dive into it */
struct dirent **namelist;
int n;
n = scandir(path, &namelist, 0, alphasort);
if ( n < 0) {
perror("scandir");
} else {
while (n--) {
struct stat sf;
char *file = namelist[n]->d_name;
char *sep = "/";
char fpath[strlen(path)+strlen(sep)+strlen(file) + 1];
strcpy(fpath, path);
strcat(fpath, sep);
strcat(fpath, file);
if (stat(fpath, &sf) == -1) {
perror("stat");
}
if (S_ISREG(sf.st_mode)) {
read_triggerfile(fpath);
}
free(namelist[n]);
}
free(namelist);
return 0;
}
} else {
read_triggerfile( path );
}
}


static int mods_equal( keystate_holder ksh, trigger_modifier tm ) {
int n = 0;
while ( n < TRIGGER_MODIFIERS_MAX ) {
Expand Down
2 changes: 1 addition & 1 deletion trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef struct trigger {

trigger* parse_trigger(char* line);
void append_trigger(trigger *t);
int read_triggerfile(const char *filename);
int read_triggers(const char *filename);
void run_triggers(int type, int code, int value, keystate_holder ksh);
void clear_triggers();

Expand Down

0 comments on commit 0ff62c2

Please sign in to comment.