aboutsummaryrefslogtreecommitdiff
path: root/demo-glib.c
blob: 01620424211b2c5a9ae97ffe23ef4a4b6a3bbc28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <glib.h>
#include <unistd.h>
#include <locale.h>

#include "termkey.h"

static termkey_t *tk;
static int timeout_id;

static void
on_key (termkey_t *tk, termkey_key_t *key)
{
	char buffer[50];
	termkey_strfkey (tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM);
	printf ("%s\n", buffer);
}

static gboolean
key_timer (gpointer data)
{
	termkey_key_t key;
	if (termkey_getkey_force (tk, &key) == TERMKEY_RES_KEY)
		on_key (tk, &key);
	return FALSE;
}

static gboolean
stdin_io (GIOChannel *source, GIOCondition condition, gpointer data)
{
	if (condition && G_IO_IN)
	{
		if (timeout_id)
			g_source_remove (timeout_id);

		termkey_advisereadable (tk);

		termkey_result_t ret;
		termkey_key_t key;
		while ((ret = termkey_getkey (tk, &key)) == TERMKEY_RES_KEY)
			on_key (tk, &key);

		if (ret == TERMKEY_RES_AGAIN)
			timeout_id = g_timeout_add
				(termkey_get_waittime (tk), key_timer, NULL);
	}

	return TRUE;
}

int
main (int argc, char *argv[])
{
	(void) argc;
	(void) argv;

	TERMKEY_CHECK_VERSION;
	setlocale (LC_CTYPE, "");

	tk = termkey_new (STDIN_FILENO, NULL, 0);
	if (!tk)
	{
		fprintf (stderr, "Cannot allocate termkey 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);
	termkey_destroy (tk);
}