summaryrefslogtreecommitdiff
path: root/modules/wincons/wincons.c
blob: f8e13404d3f1bb835b1e06630f3b50fb292d299b (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
 * @file wincons.c  Windows console input
 *
 * Copyright (C) 2010 Creytiv.com
 */

#include <winsock2.h>
#include <re.h>
#include <baresip.h>


/**
 * @defgroup wincons wincons
 *
 * User-Interface (UI) module for Windows Console
 */


/** Local constants */
enum {
	RELEASE_VAL = 250  /**< Key release value in [ms] */
};

struct ui_st {
	struct tmr tmr;
	struct mqueue *mq;
	HANDLE hThread;
	bool run;
	HANDLE hstdin;
	DWORD  mode;
};


static struct ui_st *wincons;


static void destructor(void *arg)
{
	struct ui_st *st = arg;

	/* Restore the console to its previous state */
	if (st->mode)
		SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), st->mode);

	st->run = false;
	CloseHandle(st->hThread);

	tmr_cancel(&st->tmr);
	mem_deref(st->mq);
}


static int print_handler(const char *p, size_t size, void *arg)
{
	(void)arg;

	return 1 == fwrite(p, size, 1, stderr) ? 0 : ENOMEM;
}


static void report_key(struct ui_st *ui, char key)
{
	static struct re_printf pf_stderr = {print_handler, NULL};
	(void)ui;

	ui_input_key(key, &pf_stderr);
}


static void timeout(void *arg)
{
	struct ui_st *st = arg;

	/* Emulate key-release */
	report_key(st, 0x00);
}


static DWORD WINAPI input_thread(LPVOID arg)
{
	struct ui_st *st = arg;

	/* Switch to raw mode */
	SetConsoleMode(st->hstdin, 0);

	while (st->run) {

		char buf[4];
		DWORD i, count = 0;

		ReadConsole(st->hstdin, buf, sizeof(buf), &count, NULL);

		for (i=0; i<count; i++) {
			int ch = buf[i];

			if (ch == '\r')
				ch = '\n';

			/*
			 * The keys are read from a thread so we have
			 * to send them to the RE main event loop via
			 * a message queue
			 */
			mqueue_push(st->mq, ch, 0);
		}
	}

	return 0;
}


static void mqueue_handler(int id, void *data, void *arg)
{
	struct ui_st *st = arg;
	(void)data;

	tmr_start(&st->tmr, RELEASE_VAL, timeout, st);
	report_key(st, id);
}


static int ui_alloc(struct ui_st **stp)
{
	struct ui_st *st;
	DWORD threadID;
	int err = 0;

	if (!stp)
		return EINVAL;

	st = mem_zalloc(sizeof(*st), destructor);
	if (!st)
		return ENOMEM;

	tmr_init(&st->tmr);

	err = mqueue_alloc(&st->mq, mqueue_handler, st);
	if (err)
		goto out;

	st->hstdin = GetStdHandle(STD_INPUT_HANDLE);

	/* save the current console mode */
	GetConsoleMode(st->hstdin, &st->mode);

	st->run = true;
	st->hThread = CreateThread(NULL, 0, input_thread, st, 0, &threadID);
	if (!st->hThread) {
		st->run = false;
		err = ENOMEM;
		goto out;
	}

 out:
	if (err)
		mem_deref(st);
	else
		*stp = st;

	return err;
}


static int output_handler(const char *str)
{
	return print_handler(str, str_len(str), NULL);
}


static struct ui ui_wincons = {
	LE_INIT,
	"wincons",
	output_handler
};


static int module_init(void)
{
	int err;

	err = ui_alloc(&wincons);
	if (err)
		return err;

	ui_register(&ui_wincons);

	return 0;
}


static int module_close(void)
{
	ui_unregister(&ui_wincons);
	wincons = mem_deref(wincons);
	return 0;
}


const struct mod_export DECL_EXPORTS(wincons) = {
	"wincons",
	"ui",
	module_init,
	module_close
};