2012-04-01 21:04:01 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <glib.h>
|
2014-09-28 03:51:45 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <locale.h>
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-10-09 23:47:24 +02:00
|
|
|
#include "termkey2.h"
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static termkey_t *tk;
|
2012-04-01 21:04:01 +02:00
|
|
|
static int timeout_id;
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
|
|
|
on_key (termkey_t *tk, termkey_key_t *key)
|
2012-04-01 21:04:01 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
char buffer[50];
|
|
|
|
termkey_strfkey (tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM);
|
|
|
|
printf ("%s\n", buffer);
|
2012-04-01 21:04:01 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static gboolean
|
|
|
|
key_timer (gpointer data)
|
2012-04-01 21:04:01 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
termkey_key_t key;
|
|
|
|
if (termkey_getkey_force (tk, &key) == TERMKEY_RES_KEY)
|
|
|
|
on_key (tk, &key);
|
|
|
|
return FALSE;
|
2012-04-01 21:04:01 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static gboolean
|
|
|
|
stdin_io (GIOChannel *source, GIOCondition condition, gpointer data)
|
2012-04-01 21:04:01 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (condition && G_IO_IN)
|
|
|
|
{
|
|
|
|
if (timeout_id)
|
|
|
|
g_source_remove (timeout_id);
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
termkey_advisereadable (tk);
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
termkey_result_t ret;
|
|
|
|
termkey_key_t key;
|
|
|
|
while ((ret = termkey_getkey (tk, &key)) == TERMKEY_RES_KEY)
|
|
|
|
on_key (tk, &key);
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (ret == TERMKEY_RES_AGAIN)
|
|
|
|
timeout_id = g_timeout_add
|
|
|
|
(termkey_get_waittime (tk), key_timer, NULL);
|
|
|
|
}
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
return TRUE;
|
2012-04-01 21:04:01 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2012-04-01 21:04:01 +02:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
TERMKEY_CHECK_VERSION;
|
2014-09-28 03:51:45 +02:00
|
|
|
setlocale (LC_CTYPE, "");
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
tk = termkey_new (STDIN_FILENO, NULL, 0);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "Cannot allocate termkey instance\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
|
2014-09-28 03:51:45 +02:00
|
|
|
g_io_add_watch (g_io_channel_unix_new (STDIN_FILENO),
|
|
|
|
G_IO_IN, stdin_io, NULL);
|
2014-09-23 01:38:08 +02:00
|
|
|
g_main_loop_run (loop);
|
|
|
|
termkey_destroy (tk);
|
2012-04-01 21:04:01 +02:00
|
|
|
}
|