Compare commits
No commits in common. "05a18e88747fd301c3f68009e0b47ad89400c8a4" and "dd319e08b74ee23035f86ff1ecabad5fe189457d" have entirely different histories.
05a18e8874
...
dd319e08b7
103
hkd.c
103
hkd.c
@ -14,7 +14,6 @@
|
|||||||
#include <linux/input.h>
|
#include <linux/input.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
#include <sys/inotify.h>
|
#include <sys/inotify.h>
|
||||||
#include <wordexp.h>
|
|
||||||
|
|
||||||
#define FILE_NAME_MAX_LENGTH 255
|
#define FILE_NAME_MAX_LENGTH 255
|
||||||
#define KEY_BUFFER_SIZE 16
|
#define KEY_BUFFER_SIZE 16
|
||||||
@ -31,7 +30,7 @@
|
|||||||
#define green(str) (ANSI_COLOR_GREEN str ANSI_COLOR_RESET)
|
#define green(str) (ANSI_COLOR_GREEN str ANSI_COLOR_RESET)
|
||||||
#define red(str) (ANSI_COLOR_RED str ANSI_COLOR_RESET)
|
#define red(str) (ANSI_COLOR_RED str ANSI_COLOR_RESET)
|
||||||
#define test_bit(yalv, abs_b) ((((char *)abs_b)[yalv/8] & (1<<yalv%8)) > 0)
|
#define test_bit(yalv, abs_b) ((((char *)abs_b)[yalv/8] & (1<<yalv%8)) > 0)
|
||||||
#define die(str) {fputs(ANSI_COLOR_RED, stderr); perror(str); fputs(ANSI_COLOR_RESET, stderr); exit(errno);}
|
#define die(str) {perror(red(str)); exit(errno);}
|
||||||
#define array_size(val) (val ? sizeof(val)/sizeof(val[0]) : 0)
|
#define array_size(val) (val ? sizeof(val)/sizeof(val[0]) : 0)
|
||||||
|
|
||||||
#define EVENT_SIZE (sizeof(struct inotify_event))
|
#define EVENT_SIZE (sizeof(struct inotify_event))
|
||||||
@ -188,12 +187,12 @@ const char evdev_root_dir[] = "/dev/input/";
|
|||||||
|
|
||||||
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);
|
||||||
int key_buffer_compare_random (struct key_buffer *, struct key_buffer *);
|
int key_buffer_compare (struct key_buffer *haystack, struct key_buffer *needle);
|
||||||
int key_buffer_compare_ordered (struct key_buffer *, struct key_buffer *);
|
|
||||||
void int_handler (int signum);
|
void int_handler (int signum);
|
||||||
void exec_command (char *);
|
void exec_command (char *);
|
||||||
void update_descriptors_list (int **, int *);
|
void update_descriptors_list (int **, int *);
|
||||||
int prepare_epoll (int *, int, int);
|
int prepare_epoll (int *, int, int);
|
||||||
|
void str_to_argv (char ***, const char *);
|
||||||
|
|
||||||
// TODO: use getopts() to parse command line options
|
// TODO: use getopts() to parse command line options
|
||||||
int main (void)
|
int main (void)
|
||||||
@ -261,11 +260,11 @@ int main (void)
|
|||||||
) {
|
) {
|
||||||
switch (event.value) {
|
switch (event.value) {
|
||||||
/* Key released */
|
/* Key released */
|
||||||
case 0:
|
case (0):
|
||||||
key_buffer_remove(&pb, event.code);
|
key_buffer_remove(&pb, event.code);
|
||||||
break;
|
break;
|
||||||
/* Key pressed */
|
/* Key pressed */
|
||||||
case 1:
|
case (1):
|
||||||
key_buffer_add(&pb, event.code);
|
key_buffer_add(&pb, event.code);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -274,14 +273,14 @@ int main (void)
|
|||||||
|
|
||||||
struct key_buffer comb1 = {{KEY_LEFTALT, KEY_S}, 2};
|
struct key_buffer comb1 = {{KEY_LEFTALT, KEY_S}, 2};
|
||||||
|
|
||||||
if (pb.size > prev_size) {
|
if (pb.size != prev_size) {
|
||||||
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("%d ", pb.buf[i]);
|
printf("%d ", pb.buf[i]);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
if (key_buffer_compare_ordered(&pb, &comb1))
|
if (key_buffer_compare(&pb, &comb1))
|
||||||
exec_command("ls -l [a-z]*");
|
exec_command("ufetch");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -337,42 +336,31 @@ void int_handler (int signum)
|
|||||||
dead = 1;
|
dead = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Executes a command from a string */
|
void exec_command (char *path)
|
||||||
void exec_command (char *command)
|
|
||||||
{
|
{
|
||||||
static wordexp_t result;
|
char **argv = NULL;
|
||||||
|
str_to_argv(&argv, path);
|
||||||
|
|
||||||
/* Expand the string for the program to run */
|
switch (fork()) {
|
||||||
switch (wordexp (command, &result, 0)) {
|
|
||||||
case 0:
|
|
||||||
break;
|
|
||||||
case WRDE_NOSPACE:
|
|
||||||
/* If the error was WRDE_NOSPACE,
|
|
||||||
* then perhaps part of the result was allocated */
|
|
||||||
wordfree (&result);
|
|
||||||
return;
|
|
||||||
default:
|
|
||||||
/* Some other error */
|
|
||||||
fprintf(stderr, "Could not parse, %s is not valid", command);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pid_t cpid;
|
|
||||||
switch (cpid = fork()) {
|
|
||||||
case -1:
|
case -1:
|
||||||
fprintf(stderr, "Could not create child process: %s", strerror(errno));
|
die("Could not fork");
|
||||||
wordfree(&result);
|
|
||||||
break;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
/* This is the child process, execute the command */
|
/* we are the child */
|
||||||
execvp(result.we_wordv[0], result.we_wordv);
|
if (!argv) {
|
||||||
die("Could not run command");
|
printf(red("No command to execute\n"));
|
||||||
break;
|
exit(1);
|
||||||
default:
|
}
|
||||||
while (waitpid(cpid, NULL, WNOHANG) == -1) {}
|
if(execvp(path, argv) < 0) {
|
||||||
wordfree(&result);
|
/* execv only returns if an error occured, so we exit
|
||||||
|
* otherwise we duplicate the process */
|
||||||
|
fprintf(stderr, red("Could not run %s\n"), path);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
/* we shouldn't be here */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// TODO: communication between parent and child about process status/errors/etc
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_descriptors_list (int **fds, int *fd_num)
|
void update_descriptors_list (int **fds, int *fd_num)
|
||||||
@ -455,8 +443,7 @@ int prepare_epoll (int *fds, int fd_num, int event_watcher)
|
|||||||
return ev_fd;
|
return ev_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Checks if two key buffers contain the same keys in no specified order */
|
int key_buffer_compare (struct key_buffer *haystack, struct key_buffer *needle)
|
||||||
int key_buffer_compare_random (struct key_buffer *haystack, struct key_buffer *needle)
|
|
||||||
{
|
{
|
||||||
if (haystack->size != needle->size)
|
if (haystack->size != needle->size)
|
||||||
return 0;
|
return 0;
|
||||||
@ -471,14 +458,34 @@ int key_buffer_compare_random (struct key_buffer *haystack, struct key_buffer *n
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Checks if two key buffers are the same (same order) */
|
void str_to_argv (char ***argv, const char *path)
|
||||||
int key_buffer_compare_ordered (struct key_buffer *haystack, struct key_buffer *needle)
|
|
||||||
{
|
{
|
||||||
if (haystack->size != needle->size)
|
char * str = NULL;
|
||||||
return 0;
|
if (!(str = malloc(sizeof(path))))
|
||||||
for (int i = 0; i < needle->size; i++) {
|
die("malloc in str_to_argv()");
|
||||||
if (needle->buf[i] != haystack->buf[i])
|
strcpy(str, path);
|
||||||
return 0;
|
|
||||||
|
char *token = NULL;
|
||||||
|
token = strtok(str, " ");
|
||||||
|
if (!token) {
|
||||||
|
if (!(*argv = realloc(*argv, sizeof(char *))))
|
||||||
|
die("realloc in str_to_argv()");
|
||||||
|
*argv[0] = malloc(sizeof(str));
|
||||||
|
strcpy(*argv[0], str);
|
||||||
|
goto end_return;
|
||||||
|
} else {
|
||||||
|
if (!(*argv = realloc(*argv, sizeof(char *))))
|
||||||
|
die("realloc in str_to_argv()");
|
||||||
|
*argv[0] = malloc(sizeof(token));
|
||||||
|
strcpy(*argv[0], token);
|
||||||
}
|
}
|
||||||
return 1;
|
for (int i = 1; (token = strtok(NULL, " ")); i++) {
|
||||||
|
if (!(*argv = realloc(*argv, sizeof(char *) * (i + 1))))
|
||||||
|
die("realloc in str_to_argv()");
|
||||||
|
*argv[i] = malloc(sizeof(token));
|
||||||
|
strcpy(*argv[i], token);
|
||||||
|
}
|
||||||
|
end_return:
|
||||||
|
free(str);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user