Skip to content

Commit

Permalink
Merge branch 'github-pull-102'
Browse files Browse the repository at this point in the history
Add parameter to allow to change timeout.

Link: #102
Signed-off-by: Alexey Gladkov <[email protected]>
  • Loading branch information
legionus committed Aug 16, 2023
2 parents fa11730 + 8abc015 commit 02ad318
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/showkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,20 @@ usage(int rc, const struct kbd_help *options)

int main(int argc, char *argv[])
{
const char *short_opts = "haskV";
const char *short_opts = "haskVt:";
const struct option long_opts[] = {
{ "help", no_argument, NULL, 'h' },
{ "ascii", no_argument, NULL, 'a' },
{ "scancodes", no_argument, NULL, 's' },
{ "keycodes", no_argument, NULL, 'k' },
{ "timeout", required_argument, NULL, 't' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
int c;
int show_keycodes = 1;
int print_ascii = 0;
int timeout = 10;

struct termios new = { 0 };
unsigned char buf[18]; /* divisible by 3 */
Expand All @@ -142,6 +144,7 @@ int main(int argc, char *argv[])
{ "-a, --ascii", _("display the decimal/octal/hex values of the keys.") },
{ "-s, --scancodes", _("display only the raw scan-codes.") },
{ "-k, --keycodes", _("display only the interpreted keycodes (default).") },
{ "-t, --timeout", _("set timeout, default 10") },
{ "-h, --help", _("print this usage message.") },
{ "-V, --version", _("print version number.") },
{ NULL, NULL }
Expand All @@ -164,6 +167,12 @@ int main(int argc, char *argv[])
case 'h':
usage(EXIT_SUCCESS, opthelp);
break;
case 't':
timeout = atoi(optarg);
/* in anycase if conversion wrong */
if (timeout < 1)
timeout = 10;
break;
case '?':
usage(EX_USAGE, opthelp);
break;
Expand Down Expand Up @@ -258,12 +267,12 @@ int main(int argc, char *argv[])
kbd_error(EXIT_FAILURE, errno, "ioctl KDSKBMODE");
}

printf(_("press any key (program terminates 10s after last keypress)...\n"));
printf(_("press any key (program terminates %ds after last keypress)...\n"), timeout);

/* show scancodes */
if (!show_keycodes) {
while (1) {
alarm(10);
alarm(timeout);
n = read(fd, buf, sizeof(buf));
for (i = 0; i < n; i++)
printf("0x%02x ", buf[i]);
Expand All @@ -275,7 +284,7 @@ int main(int argc, char *argv[])

/* show keycodes - 2.6 allows 3-byte reports */
while (1) {
alarm(10);
alarm(timeout);
n = read(fd, buf, sizeof(buf));
i = 0;
while (i < n) {
Expand Down

0 comments on commit 02ad318

Please sign in to comment.