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-14 00:08:15 +02:00
|
|
|
#include "termo.h"
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_t *tk;
|
2012-04-01 21:04:01 +02:00
|
|
|
static int timeout_id;
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
2014-10-14 00:08:15 +02:00
|
|
|
on_key (termo_t *tk, termo_key_t *key)
|
2012-04-01 21:04:01 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
char buffer[50];
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_strfkey (tk, buffer, sizeof buffer, key, TERMO_FORMAT_VIM);
|
2014-09-23 01:38:08 +02:00
|
|
|
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-10-14 00:08:15 +02:00
|
|
|
termo_key_t key;
|
|
|
|
if (termo_getkey_force (tk, &key) == TERMO_RES_KEY)
|
2014-09-23 01:38:08 +02:00
|
|
|
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-10-14 00:08:15 +02:00
|
|
|
termo_advisereadable (tk);
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t ret;
|
|
|
|
termo_key_t key;
|
|
|
|
while ((ret = termo_getkey (tk, &key)) == TERMO_RES_KEY)
|
2014-09-23 01:38:08 +02:00
|
|
|
on_key (tk, &key);
|
2012-04-01 21:04:01 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (ret == TERMO_RES_AGAIN)
|
2014-09-23 01:38:08 +02:00
|
|
|
timeout_id = g_timeout_add
|
2014-10-14 00:08:15 +02:00
|
|
|
(termo_get_waittime (tk), key_timer, NULL);
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
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-10-14 00:08:15 +02:00
|
|
|
TERMO_CHECK_VERSION;
|
2014-09-28 03:51:45 +02:00
|
|
|
setlocale (LC_CTYPE, "");
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
tk = termo_new (STDIN_FILENO, NULL, 0);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
fprintf (stderr, "Cannot allocate termo instance\n");
|
2014-09-23 01:38:08 +02:00
|
|
|
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);
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_destroy (tk);
|
2012-04-01 21:04:01 +02:00
|
|
|
}
|