Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

Commit 3b49acb

Browse files
committed
Created a demo to show libtermkey in a GLib-based program
1 parent 9e9ec58 commit 3b49acb

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

.bzrignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
termkey.h
22
demo
33
demo-async
4+
demo-glib
45
termkey_getkey.3
56
termkey_waitkey.3
67
*.lo

Makefile

+13-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ endif
3232
OBJECTS=termkey.lo driver-csi.lo driver-ti.lo
3333
LIBRARY=libtermkey.la
3434

35+
DEMOS=demo demo-async
36+
37+
ifeq ($(shell pkg-config glib-2.0 && echo 1),1)
38+
DEMOS+=demo-glib
39+
endif
40+
3541
TESTSOURCES=$(wildcard t/[0-9]*.c)
3642
TESTFILES=$(TESTSOURCES:.c=.t)
3743

@@ -49,7 +55,7 @@ MANDIR=$(PREFIX)/share/man
4955
MAN3DIR=$(MANDIR)/man3
5056
MAN7DIR=$(MANDIR)/man7
5157

52-
all: $(LIBRARY) demo demo-async
58+
all: $(LIBRARY) $(DEMOS)
5359

5460
%.lo: %.c termkey.h termkey-internal.h
5561
$(LIBTOOL) --mode=compile --tag=CC gcc $(CFLAGS) -o $@ -c $<
@@ -63,6 +69,12 @@ demo: $(LIBRARY) demo.lo
6369
demo-async: $(LIBRARY) demo-async.lo
6470
$(LIBTOOL) --mode=link --tag=CC gcc -o $@ $^
6571

72+
demo-glib.lo: demo-glib.c termkey.h
73+
$(LIBTOOL) --mode=compile --tag=CC gcc -o $@ -c $< $(shell pkg-config glib-2.0 --cflags)
74+
75+
demo-glib: $(LIBRARY) demo-glib.lo
76+
$(LIBTOOL) --mode=link --tag=CC gcc -o $@ $^ $(shell pkg-config glib-2.0 --libs)
77+
6678
t/%.t: t/%.c $(LIBRARY) t/taplib.lo
6779
$(LIBTOOL) --mode=link --tag=CC gcc -o $@ $^
6880

demo-glib.c

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <stdio.h>
2+
#include <glib.h>
3+
4+
#include "termkey.h"
5+
6+
static TermKey *tk;
7+
static int timeout_id;
8+
9+
static void on_key(TermKey *tk, TermKeyKey *key)
10+
{
11+
char buffer[50];
12+
termkey_strfkey(tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM);
13+
printf("%s\n", buffer);
14+
}
15+
16+
static gboolean key_timer(gpointer data)
17+
{
18+
TermKeyKey key;
19+
20+
if(termkey_getkey_force(tk, &key) == TERMKEY_RES_KEY)
21+
on_key(tk, &key);
22+
23+
return FALSE;
24+
}
25+
26+
static gboolean stdin_io(GIOChannel *source, GIOCondition condition, gpointer data)
27+
{
28+
if(condition && G_IO_IN) {
29+
if(timeout_id)
30+
g_source_remove(timeout_id);
31+
32+
termkey_advisereadable(tk);
33+
34+
TermKeyResult ret;
35+
TermKeyKey key;
36+
while((ret = termkey_getkey(tk, &key)) == TERMKEY_RES_KEY) {
37+
on_key(tk, &key);
38+
}
39+
40+
if(ret == TERMKEY_RES_AGAIN)
41+
timeout_id = g_timeout_add(termkey_get_waittime(tk), key_timer, NULL);
42+
}
43+
44+
return TRUE;
45+
}
46+
47+
int main(int argc, char *argv[])
48+
{
49+
TERMKEY_CHECK_VERSION;
50+
51+
tk = termkey_new(0, 0);
52+
53+
if(!tk) {
54+
fprintf(stderr, "Cannot allocate termkey instance\n");
55+
exit(1);
56+
}
57+
58+
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
59+
60+
g_io_add_watch(g_io_channel_unix_new(0), G_IO_IN, stdin_io, NULL);
61+
62+
g_main_loop_run(loop);
63+
64+
termkey_destroy(tk);
65+
}

0 commit comments

Comments
 (0)