Compare commits

...

2 Commits

  1. 8
      hkd.1
  2. 58
      hkd.c

@ -7,6 +7,7 @@ hkd \- simple HotKey Daemon
.SY hkd .SY hkd
.OP \-v .OP \-v
.OP \-d .OP \-d
.OP \-h
.OP \-c file .OP \-c file
.YS .YS
@ -27,6 +28,8 @@ change acting as a crude keylogger :^)
.IP \-d .IP \-d
dump, used for debugging, it prints the whole hotkey list along with the dump, used for debugging, it prints the whole hotkey list along with the
information about the hotkeys (matching type, keys and associated command). information about the hotkeys (matching type, keys and associated command).
.IP \-h
prints help message and exits
.IP "\-c file" .IP "\-c file"
override default configuration file location, instead using the specified override default configuration file location, instead using the specified
.I file .I file
@ -77,6 +80,11 @@ Key names are always capitalized and do not differenciate between upper or
lower case, as such hotkeys that require a capitalized letter need to include lower case, as such hotkeys that require a capitalized letter need to include
RIGHTSHIFT or LEFTSHIFT in the keys section. RIGHTSHIFT or LEFTSHIFT in the keys section.
Keys are intended as a list of comma separated strings. Keys are intended as a list of comma separated strings.
.PP
hkd can live reload the configuration file by signaling it with
.I SIGUSR1
for example with the command "$ pkill -USR1 -x hkd", for easier use one could add
an hotkey to execute that command.
.SH EXAMPLES .SH EXAMPLES
This is a valid config file example This is a valid config file example

58
hkd.c

@ -249,6 +249,7 @@ void parse_config_file (void);
void update_descriptors_list (int **, int *); void update_descriptors_list (int **, int *);
void remove_lock (void); void remove_lock (void);
void die (const char *, ...); void die (const char *, ...);
void usage (void);
int prepare_epoll (int *, int, int); int prepare_epoll (int *, int, int);
unsigned short key_to_code (char *); unsigned short key_to_code (char *);
const char * code_to_name (unsigned int); const char * code_to_name (unsigned int);
@ -270,26 +271,8 @@ int main (int argc, char *argv[])
struct input_event event; struct input_event event;
struct key_buffer pb = {{0}, 0}; // Pressed keys buffer struct key_buffer pb = {{0}, 0}; // Pressed keys buffer
/* Check if hkd is already running */
lock_file_descriptor = open(LOCK_FILE, O_RDWR | O_CREAT, 0600);
if (lock_file_descriptor < 0)
die("Can't open lock file:");
fl.l_start = 0;
fl.l_len = 0;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
if (fcntl(lock_file_descriptor, F_SETLK, &fl) < 0)
die("hkd is already running");
atexit(remove_lock);
/* Handle SIGINT */
dead = 0;
memset(&action, 0, sizeof(action));
action.sa_handler = int_handler;
sigaction(SIGINT, &action, NULL);
/* Parse command line arguments */ /* Parse command line arguments */
while ((opc = getopt(argc, argv, "vc:d")) != -1) { while ((opc = getopt(argc, argv, "vc:dh")) != -1) {
switch (opc) { switch (opc) {
case 'v': case 'v':
vflag = 1; vflag = 1;
@ -303,13 +286,35 @@ int main (int argc, char *argv[])
case 'd': case 'd':
dump = 1; dump = 1;
break; break;
case 'h':
usage();
break;
break; break;
} }
} }
/* Handle SIGINT */
dead = 0;
memset(&action, 0, sizeof(action));
action.sa_handler = int_handler;
sigaction(SIGINT, &action, NULL);
sigaction(SIGUSR1, &action, NULL);
/* Parse config file */ /* Parse config file */
parse_config_file(); parse_config_file();
/* Check if hkd is already running */
lock_file_descriptor = open(LOCK_FILE, O_RDWR | O_CREAT, 0600);
if (lock_file_descriptor < 0)
die("Can't open lock file:");
fl.l_start = 0;
fl.l_len = 0;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
if (fcntl(lock_file_descriptor, F_SETLK, &fl) < 0)
die("hkd is already running");
atexit(remove_lock);
/* If a dump is requested print the hotkey list then exit */ /* If a dump is requested print the hotkey list then exit */
if (dump) { if (dump) {
printf("DUMPING HOTKEY LIST\n\n"); printf("DUMPING HOTKEY LIST\n\n");
@ -475,6 +480,9 @@ void int_handler (int signum)
printf(yellow("Received interrupt signal, exiting gracefully...\n")); printf(yellow("Received interrupt signal, exiting gracefully...\n"));
dead = 1; dead = 1;
break; break;
case SIGUSR1:
parse_config_file();
break;
} }
} }
@ -721,6 +729,8 @@ void parse_config_file (void)
if (!fd) if (!fd)
die("Could not open any config files, check the log for more details"); die("Could not open any config files, check the log for more details");
} }
hotkey_list_destroy(hotkey_list);
while (exit_state >= 0) { while (exit_state >= 0) {
int tmp = 0; int tmp = 0;
memset(block, 0, BLOCK_SIZE + 1); memset(block, 0, BLOCK_SIZE + 1);
@ -947,3 +957,13 @@ const char * code_to_name (unsigned int code)
} }
return "Key not recognized"; return "Key not recognized";
} }
void usage (void)
{
puts("Usage: hkd [-vdh] [-c file]\n"
"\t-v verbose, prints all the key presses and debug information\n"
"\t-d dump, dumps the hotkey list and exits\n"
"\t-h prints this help message\n"
"\t-f file uses the specified file as config\n");
exit(EXIT_SUCCESS);
}

Loading…
Cancel
Save