-
Notifications
You must be signed in to change notification settings - Fork 1
/
emui.c
328 lines (277 loc) · 7.43 KB
/
emui.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright (c) 2015 Jakub Filipowicz <jakubf@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <stdio.h>
#include <sys/select.h>
#include <ncurses.h>
#include <sys/time.h>
#include <errno.h>
#include "dbg.h"
#include "event.h"
#include "tiles.h"
#include "style.h"
#include "focus.h"
#define EMUI_FPS_CAP 60
static SCREEN *s;
static EMTILE *layout;
static int fps_min;
static int fps_frame_mod;
static float fps_current;
static unsigned long frame_current;
static volatile int terminal_resized;
// -----------------------------------------------------------------------
static void _emui_sigwinch_handler(int signum)
{
if (signum == SIGWINCH) {
terminal_resized = 1;
}
while (signal(SIGWINCH, _emui_sigwinch_handler));
}
// -----------------------------------------------------------------------
EMTILE * emui_init(unsigned fps)
{
// initialize ncurses
s = newterm(NULL, stdout, stdin);
set_term(s);
//initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
timeout(0);
curs_set(0);
set_escdelay(100);
start_color();
emui_style_init(NULL);
// initialize emui
if (fps > EMUI_FPS_CAP) {
fps_min = EMUI_FPS_CAP;
} else {
fps_min = fps;
}
// modulo for fps counter
fps_frame_mod = fps_min / 4;
if (fps_frame_mod <= 0) {
fps_frame_mod = 1;
}
layout = emui_screen();
if (signal(SIGWINCH, _emui_sigwinch_handler) == SIG_ERR) {
return NULL;
}
return layout;
}
// -----------------------------------------------------------------------
void emui_destroy()
{
_emtile_really_delete(layout);
endwin();
//_nc_free_and_exit();
delscreen(s);
emui_evq_clear();
}
// -----------------------------------------------------------------------
static void emui_draw(EMTILE *t)
{
EMTILE *focused_child = NULL;
// physically delete tile marked P_DELETED
if (t->properties & P_DELETED) {
_emtile_really_delete(t);
return;
}
// update tile geometry
int geometry_changed = t->geometry_changed;
if (geometry_changed) {
EDBG(t, 0, "Tile geometry changed");
emtile_fit(t);
// if the focused tile is hidden after geometry change,
// and there is no scroll handler in tile's focus group,
// search for a new unhidden tile:
// go up, then left, then from the beggining of focus group
EMTILE *f = emui_focus_get();
if (f->properties & P_HIDDEN) {
if (f->fg->drv->scroll_handler) {
// TODO: ???
} else {
EDBG(f, 0, "Tile is hidden after geometry change, moving focus");
f = emtile_get_physical_neighbour(f->fg, FC_ABOVE, P_INTERACTIVE, P_HIDDEN);
if (f->properties & P_HIDDEN) {
f = emtile_get_physical_neighbour(f->fg, FC_LEFT, P_INTERACTIVE, P_HIDDEN);
if (f->properties & P_HIDDEN) {
f = emtile_get_list_neighbour(f->fg, FC_FIRST, P_INTERACTIVE, P_HIDDEN);
}
}
emui_focus(f);
}
}
}
// draw the tile
emtile_draw(t);
// draw tile's children
EMTILE *child = t->ch_first;
while (child) {
child->geometry_changed |= geometry_changed;
// store focused tile to draw it later
if (!emui_has_focus(child)) {
emui_draw(child);
} else {
focused_child = child;
}
child = child->ch_next;
}
// draw focused tile last, so it's always on top
if (focused_child) {
emui_draw(focused_child);
}
}
// -----------------------------------------------------------------------
static void emui_update_screen()
{
struct timeval work_start;
static struct timeval work_start_old;
const long resolution = 1000000L;
// calculate the real fps
gettimeofday(&work_start, NULL);
if (frame_current % fps_frame_mod == 0) {
long frame_time = resolution * (work_start.tv_sec - work_start_old.tv_sec);
frame_time += work_start.tv_usec - work_start_old.tv_usec;
fps_current = (float) fps_frame_mod * resolution / frame_time;
work_start_old = work_start;
}
if (terminal_resized > 0) {
terminal_resized = 0;
layout->geometry_changed = 1;
}
emui_draw(layout);
doupdate();
frame_current++;
}
// -----------------------------------------------------------------------
static int emui_process_event(struct emui_event *ev)
{
EMTILE *t = emui_focus_get();
while (t) {
if (emtile_event(t, ev) == E_HANDLED) {
return E_HANDLED;
}
t = t->parent;
}
// TODO: temporary
if ((ev->type == EV_KEY) && (ev->sender == 'q')) {
struct emui_event *ev = malloc(sizeof(struct emui_event));
ev->type = EV_QUIT;
emui_evq_prepend(ev);
return E_HANDLED;
}
return E_UNHANDLED;
}
// -----------------------------------------------------------------------
static int emui_process_queue()
{
struct emui_event *ev;
while ((ev = emui_evq_get())) {
if (ev->type != EV_QUIT) {
emui_process_event(ev);
free(ev);
} else {
free(ev);
return -1;
}
}
return 0;
}
// -----------------------------------------------------------------------
static int emui_do_events(int fps)
{
static fd_set rfds;
int retval;
int ch;
struct emui_event *ev = NULL;
struct timeval work_start, work_end;
// wait up to 1000000/fps usec for an event
struct timeval tv;
int ftime = 1000000/fps;
tv.tv_sec = 0;
tv.tv_usec = ftime;
// respond to evens in 1000000/61 usec max...
while (1000000L*tv.tv_sec + tv.tv_usec > (ftime-(1000000L/61))) {
FD_ZERO(&rfds);
FD_SET(0, &rfds);
// ...but wait up to 1000000/fps usec for an event
retval = select(1, &rfds, NULL, NULL, &tv);
// nothing to do (timeout)
if (retval == 0) {
continue;
}
gettimeofday(&work_start, NULL);
ev = calloc(1, sizeof(struct emui_event));
// we have a keypress
if (retval > 0) {
ch = getch();
ev->type = EV_KEY;
ev->sender = ch;
// error
} else {
ev->type = EV_ERROR;
ev->sender = errno;
}
// append the event
emui_evq_append(ev);
// process all events (processing an event may cause enqueuing another event)
if (emui_process_queue()) {
return -1;
}
// subtract time wasted on processing
gettimeofday(&work_end, NULL);
long work_time = 1000000L * (work_end.tv_usec - work_start.tv_usec) + work_end.tv_usec - work_start.tv_usec;
tv.tv_usec -= work_time;
}
return 0;
}
// -----------------------------------------------------------------------
void emui_loop()
{
emui_focus(layout);
while (1) {
emui_update_screen();
if (emui_do_events(fps_min)) {
break;
}
}
EDBG(layout, 0, "QUIT");
}
// -----------------------------------------------------------------------
EMTILE * emui_get_layout()
{
return layout;
}
// -----------------------------------------------------------------------
unsigned emui_get_target_fps()
{
return fps_min;
}
// -----------------------------------------------------------------------
float emui_get_current_fps()
{
return fps_current;
}
// -----------------------------------------------------------------------
unsigned long emui_get_current_frame()
{
return frame_current;
}
// vim: tabstop=4 shiftwidth=4 autoindent