Skip to content

Commit

Permalink
monitor: Add -t flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rvaiya committed Sep 2, 2022
1 parent 52ab139 commit 5fc6cde
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/keyd.scdoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keyd(1)

# COMMANDS

*monitor*
*monitor [-t] [-h]*
Print key events. Useful for discovering key names and debugging.

*bind <binding> [<binding>...]*
Expand Down
2 changes: 1 addition & 1 deletion src/keyd.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static int help(int argc, char *argv[])
{
printf("usage: keyd [-v] [-h] [command] [<args>]\n\n"
"Commands:\n"
" monitor Print key events in real time.\n"
" monitor [-t] Print key events in real time.\n"
" list-keys Print a list of valid key names.\n"
" reload Trigger a reload .\n"
" listen Print layer state changes of the running keyd daemon to stdout.\n"
Expand Down
24 changes: 24 additions & 0 deletions src/monitor.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "keyd.h"

static int time_flag = 0;

static void set_tflags(tcflag_t flags, int val)
{
if (!isatty(0))
Expand Down Expand Up @@ -28,8 +30,18 @@ static void cleanup()
set_tflags(ICANON|ECHO, 1);
}

static long get_time_ms()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1E3 + ts.tv_nsec / 1E6;
}

int event_handler(struct event *ev)
{
static long last_time;
long ctime = get_time_ms();

switch (ev->type) {
const char *name;

Expand All @@ -47,6 +59,9 @@ int event_handler(struct event *ev)
if (ev->devev->type == DEV_KEY) {
name = keycode_table[ev->devev->code].name;

if (time_flag)
printf("+%ld ms\t", ctime - last_time);

printf("%s\t%04x:%04x\t%s %s\n",
ev->dev->name,
ev->dev->vendor_id,
Expand All @@ -63,11 +78,20 @@ int event_handler(struct event *ev)
fflush(stdout);
fflush(stderr);

last_time = ctime;
return 0;
}

int monitor(int argc, char *argv[])
{
if (argc == 1 && !strcmp(argv[0], "-h")) {
printf("Usage: keyd monitor [-t]\n\n\t-t: Print the time in milliseconds between events.\n");
return 0;
}

if (argc == 1 && !strcmp(argv[0], "-t"))
time_flag = 1;

if (isatty(1))
set_tflags(ECHO, 0);

Expand Down

0 comments on commit 5fc6cde

Please sign in to comment.