termo/demo-glib.c

73 lines
1.3 KiB
C
Raw Normal View History

#include <stdio.h>
#include <glib.h>
#include <unistd.h>
#include <locale.h>
2014-10-14 00:08:15 +02:00
#include "termo.h"
2014-10-14 00:08:15 +02:00
static termo_t *tk;
static int timeout_id;
static void
2014-10-14 00:08:15 +02:00
on_key (termo_t *tk, termo_key_t *key)
{
char buffer[50];
2014-10-14 00:08:15 +02:00
termo_strfkey (tk, buffer, sizeof buffer, key, TERMO_FORMAT_VIM);
printf ("%s\n", buffer);
}
static gboolean
key_timer (gpointer data)
{
2014-10-14 00:08:15 +02:00
termo_key_t key;
if (termo_getkey_force (tk, &key) == TERMO_RES_KEY)
on_key (tk, &key);
return FALSE;
}
static gboolean
stdin_io (GIOChannel *source, GIOCondition condition, gpointer data)
{
2016-11-03 02:24:35 +01:00
if (condition & G_IO_IN)
{
if (timeout_id)
g_source_remove (timeout_id);
2014-10-14 00:08:15 +02:00
termo_advisereadable (tk);
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)
on_key (tk, &key);
2014-10-14 00:08:15 +02:00
if (ret == TERMO_RES_AGAIN)
timeout_id = g_timeout_add
2014-10-14 00:08:15 +02:00
(termo_get_waittime (tk), key_timer, NULL);
}
return TRUE;
}
int
main (int argc, char *argv[])
{
(void) argc;
(void) argv;
2014-10-14 00:08:15 +02:00
TERMO_CHECK_VERSION;
setlocale (LC_CTYPE, "");
2014-10-14 00:08:15 +02:00
tk = termo_new (STDIN_FILENO, NULL, 0);
if (!tk)
{
2014-10-14 00:08:15 +02:00
fprintf (stderr, "Cannot allocate termo instance\n");
exit (1);
}
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
g_io_add_watch (g_io_channel_unix_new (STDIN_FILENO),
G_IO_IN, stdin_io, NULL);
g_main_loop_run (loop);
2014-10-14 00:08:15 +02:00
termo_destroy (tk);
}