Compare commits
No commits in common. "213db3cee5a4d0cd7acf67f8589ce2ffd749addb" and "122ba9b44bb2c11a4c94f10e0f2f7bd7436cfa7e" have entirely different histories.
213db3cee5
...
122ba9b44b
4
hkd.1
4
hkd.1
@ -6,7 +6,6 @@ hkd \- simple HotKey Daemon
|
|||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
.SY hkd
|
.SY hkd
|
||||||
.OP \-v
|
.OP \-v
|
||||||
.OP \-d
|
|
||||||
.OP \-c file
|
.OP \-c file
|
||||||
.YS
|
.YS
|
||||||
|
|
||||||
@ -24,9 +23,6 @@ correctly.
|
|||||||
.IP \-v
|
.IP \-v
|
||||||
verbose, prints debug information and the internal pressed key buffer upon
|
verbose, prints debug information and the internal pressed key buffer upon
|
||||||
change acting as a crude keylogger :^)
|
change acting as a crude keylogger :^)
|
||||||
.IP \-d
|
|
||||||
dump, used for debugging, it prints the whole hotkey list along with the
|
|
||||||
information about the hotkeys (matching type, keys and associated command).
|
|
||||||
.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
|
||||||
|
131
hkd.c
131
hkd.c
@ -235,7 +235,7 @@ char *ext_config_file = NULL;
|
|||||||
/* Flags */
|
/* Flags */
|
||||||
int vflag = 0;
|
int vflag = 0;
|
||||||
int dead = 0; // exit flag
|
int dead = 0; // exit flag
|
||||||
int dump = 0;
|
|
||||||
/* key buffer operations */
|
/* key buffer operations */
|
||||||
int key_buffer_add (struct key_buffer*, unsigned short);
|
int key_buffer_add (struct key_buffer*, unsigned short);
|
||||||
int key_buffer_remove (struct key_buffer*, unsigned short);
|
int key_buffer_remove (struct key_buffer*, unsigned short);
|
||||||
@ -251,7 +251,6 @@ void remove_lock (void);
|
|||||||
void die (const char *, ...);
|
void die (const char *, ...);
|
||||||
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);
|
|
||||||
/* hotkey list operations */
|
/* hotkey list operations */
|
||||||
void hotkey_list_add (struct hotkey_list_e *, struct key_buffer *, char *, int);
|
void hotkey_list_add (struct hotkey_list_e *, struct key_buffer *, char *, int);
|
||||||
void hotkey_list_destroy (struct hotkey_list_e *);
|
void hotkey_list_destroy (struct hotkey_list_e *);
|
||||||
@ -289,7 +288,7 @@ int main (int argc, char *argv[])
|
|||||||
sigaction(SIGINT, &action, NULL);
|
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:")) != -1) {
|
||||||
switch (opc) {
|
switch (opc) {
|
||||||
case 'v':
|
case 'v':
|
||||||
vflag = 1;
|
vflag = 1;
|
||||||
@ -300,30 +299,12 @@ int main (int argc, char *argv[])
|
|||||||
die("malloc in main():");
|
die("malloc in main():");
|
||||||
strcpy(ext_config_file, optarg);
|
strcpy(ext_config_file, optarg);
|
||||||
break;
|
break;
|
||||||
case 'd':
|
|
||||||
dump = 1;
|
|
||||||
break;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse config file */
|
/* Parse config file */
|
||||||
parse_config_file();
|
parse_config_file();
|
||||||
|
|
||||||
/* If a dump is requested print the hotkey list then exit */
|
|
||||||
if (dump) {
|
|
||||||
printf("DUMPING HOTKEY LIST\n\n");
|
|
||||||
for (struct hotkey_list_e *tmp = hotkey_list; tmp; tmp = tmp->next) {
|
|
||||||
printf("Hotkey\n");
|
|
||||||
printf("\tKeys: ");
|
|
||||||
for (unsigned int i = 0; i < tmp->kb.size; i++)
|
|
||||||
printf("%s ", code_to_name(tmp->kb.buf[i]));
|
|
||||||
printf("\n\tMatching: %s\n", tmp->fuzzy ? "fuzzy" : "ordered");
|
|
||||||
printf("\tCommand: %s\n\n", tmp->command);
|
|
||||||
}
|
|
||||||
exit(EXIT_SUCCESS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Load descriptors */
|
/* Load descriptors */
|
||||||
update_descriptors_list(&fds, &fd_num);
|
update_descriptors_list(&fds, &fd_num);
|
||||||
|
|
||||||
@ -394,9 +375,17 @@ int main (int argc, char *argv[])
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (vflag) {
|
if (vflag) {
|
||||||
|
int ci;
|
||||||
printf("Pressed keys: ");
|
printf("Pressed keys: ");
|
||||||
for (unsigned int i = 0; i < pb.size; i++)
|
for (unsigned int i = 0; i < pb.size; i++) {
|
||||||
printf("%s ", code_to_name(pb.buf[i]));
|
ci = i;
|
||||||
|
while (pb.buf[i] != key_conversion_table[ci + 1].value) {
|
||||||
|
if (ci >= array_size_const(key_conversion_table) - 2)
|
||||||
|
break;
|
||||||
|
ci++;
|
||||||
|
}
|
||||||
|
printf("%s ", key_conversion_table[ci + 1].name);
|
||||||
|
}
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -662,13 +651,12 @@ void parse_config_file (void)
|
|||||||
{
|
{
|
||||||
wordexp_t result = {0};
|
wordexp_t result = {0};
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
|
int remaining = 0;
|
||||||
// 0: normal, 1: skip line 2: get directive 3: get keys 4: get command 5: output
|
// 0: normal, 1: skip line 2: get directive 3: get keys 4: get command 5: output
|
||||||
int state = 0;
|
int state = 0;
|
||||||
int alloc_tmp = 0, alloc_size = 0;
|
int alloc_tmp = 0, alloc_size = 0;
|
||||||
int fuzzy = 0;
|
int fuzzy = 0;
|
||||||
int i_tmp = 0, linenum = 1;
|
int i_tmp = 0, done = 0, linenum = 1;
|
||||||
// 0: aok, 1: request block, 2: last block, -1: exit
|
|
||||||
int exit_state = 0;
|
|
||||||
char block[BLOCK_SIZE + 1] = {0};
|
char block[BLOCK_SIZE + 1] = {0};
|
||||||
char *bb = NULL;
|
char *bb = NULL;
|
||||||
char *keys = NULL;
|
char *keys = NULL;
|
||||||
@ -721,37 +709,31 @@ 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");
|
||||||
}
|
}
|
||||||
while (exit_state >= 0) {
|
while (!done) {
|
||||||
int tmp = 0;
|
memset(block, 0, BLOCK_SIZE);
|
||||||
memset(block, 0, BLOCK_SIZE + 1);
|
remaining = fread(block, sizeof(char), BLOCK_SIZE, fd);
|
||||||
tmp = fread(block, sizeof(char), BLOCK_SIZE, fd);
|
if (!remaining)
|
||||||
if (!tmp)
|
|
||||||
break;
|
break;
|
||||||
if (tmp < BLOCK_SIZE || feof(fd))
|
|
||||||
exit_state = 2;
|
|
||||||
else
|
|
||||||
exit_state = 0;
|
|
||||||
bb = block;
|
bb = block;
|
||||||
|
|
||||||
while (exit_state == 0 || exit_state == 2) {
|
while (remaining > 0 && !done) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
// First state
|
// First state
|
||||||
case 0:
|
case 0:
|
||||||
// remove whitespaces
|
// remove whitespaces
|
||||||
while (isblank(*bb))
|
while (isblank(*bb) && remaining > 0)
|
||||||
bb++;
|
bb++, remaining--;
|
||||||
|
if (remaining <= 0)
|
||||||
|
break;
|
||||||
// get state
|
// get state
|
||||||
switch (*bb) {
|
switch (*bb) {
|
||||||
case EOF:
|
|
||||||
case '\0':
|
|
||||||
// If it is the end of the last block exit
|
|
||||||
if (exit_state > 1)
|
|
||||||
exit_state = -1;
|
|
||||||
break;
|
|
||||||
case '\n':
|
case '\n':
|
||||||
case '#':
|
case '#':
|
||||||
state = 1;
|
state = 1;
|
||||||
break;
|
break;
|
||||||
|
case '\0':
|
||||||
|
done = 1;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
state = 2;
|
state = 2;
|
||||||
break;
|
break;
|
||||||
@ -759,15 +741,12 @@ void parse_config_file (void)
|
|||||||
break;
|
break;
|
||||||
// Skip line (comment)
|
// Skip line (comment)
|
||||||
case 1:
|
case 1:
|
||||||
while (*bb != '\n' && *bb)
|
while (*bb != '\n' && remaining > 0)
|
||||||
bb++;
|
bb++, remaining--;
|
||||||
if (*bb) {
|
bb++, remaining--;
|
||||||
bb++;
|
|
||||||
linenum++;
|
linenum++;
|
||||||
|
if (remaining > 0)
|
||||||
state = 0;
|
state = 0;
|
||||||
} else {
|
|
||||||
exit_state = 1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
// Get compairson method
|
// Get compairson method
|
||||||
case 2:
|
case 2:
|
||||||
@ -784,7 +763,7 @@ void parse_config_file (void)
|
|||||||
linenum);
|
linenum);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
bb++;
|
bb++, remaining--;
|
||||||
state = 3;
|
state = 3;
|
||||||
break;
|
break;
|
||||||
// Get keys
|
// Get keys
|
||||||
@ -793,27 +772,26 @@ void parse_config_file (void)
|
|||||||
if (!(keys = malloc(alloc_size = (sizeof(char) * 64))))
|
if (!(keys = malloc(alloc_size = (sizeof(char) * 64))))
|
||||||
die("malloc for keys in parse_config_file():");
|
die("malloc for keys in parse_config_file():");
|
||||||
memset(keys, 0, alloc_size);
|
memset(keys, 0, alloc_size);
|
||||||
|
alloc_tmp = 0;
|
||||||
} else if (alloc_tmp >= alloc_size) {
|
} else if (alloc_tmp >= alloc_size) {
|
||||||
if (!(keys = realloc(keys, alloc_size = alloc_size * 2)))
|
if (!(keys = realloc(keys, alloc_size *= 2)))
|
||||||
die("realloc for keys in parse_config_file():");
|
die("realloc for keys in parse_config_file():");
|
||||||
memset(&keys[alloc_size / 2], 0, alloc_size / 2);
|
memset(&keys[alloc_size / 2], 0, alloc_size / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (alloc_tmp = 0; bb[alloc_tmp] &&
|
for (; remaining > 0 &&
|
||||||
bb[alloc_tmp] != ':' && bb[alloc_tmp] != '\n' &&
|
(bb[alloc_tmp] != ':' && bb[alloc_tmp] != '\n') &&
|
||||||
alloc_tmp < alloc_size; alloc_tmp++);
|
alloc_tmp < alloc_size;
|
||||||
|
remaining--, alloc_tmp++);
|
||||||
|
|
||||||
if (!bb[alloc_tmp] || alloc_tmp == alloc_size) {
|
if (remaining <= 0 || alloc_tmp == alloc_size) {
|
||||||
strncat(keys, bb, alloc_tmp);
|
strncat(keys, bb, alloc_tmp);
|
||||||
bb += alloc_tmp;
|
bb += alloc_tmp;
|
||||||
if (exit_state > 1)
|
|
||||||
die("Keys not finished before end of file");
|
|
||||||
else
|
|
||||||
exit_state = 1;
|
|
||||||
break;
|
break;
|
||||||
} else if (bb[alloc_tmp] == ':') {
|
} else if (bb[alloc_tmp] == ':') {
|
||||||
strncat(keys, bb, alloc_tmp);
|
strncat(keys, bb, alloc_tmp);
|
||||||
bb += alloc_tmp + 1;
|
bb += alloc_tmp + 1;
|
||||||
|
remaining--;
|
||||||
state = 4;
|
state = 4;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
@ -828,35 +806,33 @@ void parse_config_file (void)
|
|||||||
if (!(cmd = malloc(alloc_size = (sizeof(char) * 128))))
|
if (!(cmd = malloc(alloc_size = (sizeof(char) * 128))))
|
||||||
die("malloc for cmd in parse_config_file():");
|
die("malloc for cmd in parse_config_file():");
|
||||||
memset(cmd, 0, alloc_size);
|
memset(cmd, 0, alloc_size);
|
||||||
|
alloc_tmp = 0;
|
||||||
} else if (alloc_tmp >= alloc_size) {
|
} else if (alloc_tmp >= alloc_size) {
|
||||||
if (!(cmd = realloc(cmd, alloc_size = alloc_size * 2)))
|
if (!(cmd = realloc(cmd, alloc_size *= 2)))
|
||||||
die("realloc for cmd in parse_config_file():");
|
die("realloc for cmd in parse_config_file():");
|
||||||
memset(&cmd[alloc_size / 2], 0, alloc_size / 2);
|
memset(&cmd[alloc_size / 2], 0, alloc_size / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (alloc_tmp = 0; bb[alloc_tmp] && bb[alloc_tmp] != '\n' &&
|
for (; remaining > 0 &&
|
||||||
alloc_tmp < alloc_size; alloc_tmp++);
|
bb[alloc_tmp] != '\n' &&
|
||||||
|
alloc_tmp < alloc_size;
|
||||||
|
remaining--, alloc_tmp++);
|
||||||
|
|
||||||
if (!bb[alloc_tmp] || alloc_tmp == alloc_size) {
|
if (remaining <= 0 || alloc_tmp == alloc_size) {
|
||||||
strncat(cmd, bb, alloc_tmp);
|
strncat(cmd, bb, alloc_tmp);
|
||||||
bb += alloc_tmp;
|
bb += alloc_tmp;
|
||||||
if (exit_state > 1)
|
|
||||||
die("Command not finished before end of file");
|
|
||||||
else
|
|
||||||
exit_state = 1;
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
strncat(cmd, bb, alloc_tmp);
|
strncat(cmd, bb, alloc_tmp);
|
||||||
if (!(bb[alloc_tmp - 1] == '\\'))
|
if (!(bb[alloc_tmp - 1] == '\\'))
|
||||||
state = 5;
|
state = 5;
|
||||||
bb += alloc_tmp + 1;
|
bb += alloc_tmp + 1;
|
||||||
|
remaining--;
|
||||||
linenum++;
|
linenum++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
if (!keys)
|
|
||||||
die("error");
|
|
||||||
i_tmp = strlen(keys);
|
i_tmp = strlen(keys);
|
||||||
for (int i = 0; i < i_tmp; i++) {
|
for (int i = 0; i < i_tmp; i++) {
|
||||||
if (isblank(keys[i])) {
|
if (isblank(keys[i])) {
|
||||||
@ -888,13 +864,13 @@ void parse_config_file (void)
|
|||||||
|
|
||||||
hotkey_list_add(hotkey_list, &kb, cp_tmp, fuzzy);
|
hotkey_list_add(hotkey_list, &kb, cp_tmp, fuzzy);
|
||||||
hotkey_size_mask |= 1 << (kb.size - 1);
|
hotkey_size_mask |= 1 << (kb.size - 1);
|
||||||
|
// DO STUFF
|
||||||
|
|
||||||
key_buffer_reset(&kb);
|
key_buffer_reset(&kb);
|
||||||
free(keys);
|
free(keys);
|
||||||
free(cmd);
|
free(cmd);
|
||||||
cp_tmp = keys = cmd = NULL;
|
cp_tmp = keys = cmd = NULL;
|
||||||
i_tmp = 0;
|
i_tmp = state = 0;
|
||||||
state = 0;
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
die("Unknown state in parse_config_file");
|
die("Unknown state in parse_config_file");
|
||||||
@ -938,12 +914,3 @@ void die(const char *fmt, ...)
|
|||||||
va_end(ap);
|
va_end(ap);
|
||||||
exit(errno ? errno : 1);
|
exit(errno ? errno : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * code_to_name (unsigned int code)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < array_size_const(key_conversion_table); i++) {
|
|
||||||
if (key_conversion_table[i].value == code)
|
|
||||||
return key_conversion_table[i].name;
|
|
||||||
}
|
|
||||||
return "Key not recognized";
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user