forked from aglasgall/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.c
607 lines (519 loc) · 14.8 KB
/
window.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#include "owl.h"
struct _owl_window { /*noproto*/
GObject object;
/* hierarchy information */
owl_window *parent;
owl_window *child;
owl_window *next, *prev;
/* flags */
int dirty : 1;
int dirty_subtree : 1;
int shown : 1;
int is_screen : 1;
/* window information */
WINDOW *win;
PANEL *pan;
int nlines, ncols;
int begin_y, begin_x;
};
enum {
REDRAW,
RESIZED,
LAST_SIGNAL
};
static guint window_signals[LAST_SIGNAL] = { 0 };
static void owl_window_dispose(GObject *gobject);
static void owl_window_finalize(GObject *gobject);
static owl_window *_owl_window_new(owl_window *parent, int nlines, int ncols, int begin_y, int begin_x);
static void _owl_window_link(owl_window *w, owl_window *parent);
static void _owl_window_create_curses(owl_window *w);
static void _owl_window_destroy_curses(owl_window *w);
static void _owl_window_realize(owl_window *w);
static void _owl_window_unrealize(owl_window *w);
static owl_window *cursor_owner;
static owl_window *default_cursor;
/* clang gets upset about the glib argument chopping hack because it manages to
* inline owl_window_children_foreach. user_data should be a pointer to a
* FuncOneArg. */
typedef void (*FuncOneArg)(void *);
static void first_arg_only(gpointer data, gpointer user_data)
{
FuncOneArg *func = user_data;
(*func)(data);
}
G_DEFINE_TYPE (OwlWindow, owl_window, G_TYPE_OBJECT)
static void owl_window_class_init (OwlWindowClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
/* Set up the vtabl */
gobject_class->dispose = owl_window_dispose;
gobject_class->finalize = owl_window_finalize;
klass->redraw = NULL;
klass->resized = NULL;
/* Create the signals, remember IDs */
window_signals[REDRAW] =
g_signal_new("redraw",
G_TYPE_FROM_CLASS(gobject_class),
G_SIGNAL_RUN_CLEANUP,
G_STRUCT_OFFSET(OwlWindowClass, redraw),
NULL, NULL,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE,
1,
G_TYPE_POINTER, NULL);
/* TODO: maybe type should be VOID__INT_INT_INT_INT; will need to generate a
* marshaller */
window_signals[RESIZED] =
g_signal_new("resized",
G_TYPE_FROM_CLASS(gobject_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET(OwlWindowClass, resized),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE,
0,
NULL);
}
static void owl_window_dispose (GObject *object)
{
owl_window *w = OWL_WINDOW (object);
/* Unmap the window */
owl_window_hide (w);
/* Unlink and unref all children */
while (w->child) {
owl_window *child = w->child;
owl_window_unlink (child);
}
/* Remove from hierarchy */
owl_window_unlink (w);
G_OBJECT_CLASS (owl_window_parent_class)->dispose (object);
}
static void owl_window_finalize (GObject *object)
{
owl_window *w = OWL_WINDOW(object);
if (w->pan) {
del_panel(w->pan);
w->pan = NULL;
}
G_OBJECT_CLASS (owl_window_parent_class)->finalize (object);
}
static void owl_window_init (owl_window *w)
{
}
/** singletons **/
static WINDOW *_dummy_window(void)
{
static WINDOW *dummy = NULL;
if (!dummy) {
dummy = newwin(1, 1, 0, 0);
}
return dummy;
}
owl_window *owl_window_get_screen(void)
{
static owl_window *screen = NULL;
if (!screen) {
/* The screen is magical. It's 'shown', but the only mean of it going
* invisible is if we're tore down curses (i.e. screen resize) */
screen = _owl_window_new(NULL, g.lines, g.cols, 0, 0);
screen->is_screen = 1;
owl_window_show(screen);
}
return screen;
}
/** Creation and Destruction **/
owl_window *owl_window_new(owl_window *parent)
{
if (!parent)
parent = owl_window_get_screen();
return _owl_window_new(parent, 0, 0, 0, 0);
}
static owl_window *_owl_window_new(owl_window *parent, int nlines, int ncols, int begin_y, int begin_x)
{
owl_window *w;
w = g_object_new (OWL_TYPE_WINDOW, NULL);
if (w == NULL) g_error("Failed to create owl_window instance");
w->nlines = nlines;
w->ncols = ncols;
w->begin_y = begin_y;
w->begin_x = begin_x;
_owl_window_link(w, parent);
if (parent && parent->is_screen) {
w->pan = new_panel(_dummy_window());
set_panel_userptr(w->pan, w);
}
return w;
}
/** Hierarchy **/
void owl_window_unlink(owl_window *w)
{
/* make sure the window is unmapped first */
_owl_window_unrealize(w);
/* unlink parent/child information */
if (w->parent) {
if (w->prev)
w->prev->next = w->next;
if (w->next)
w->next->prev = w->prev;
if (w->parent->child == w)
w->parent->child = w->next;
w->parent = NULL;
g_object_unref (w);
}
}
static void _owl_window_link(owl_window *w, owl_window *parent)
{
if (w->parent == parent)
return;
owl_window_unlink(w);
if (parent) {
w->parent = parent;
w->next = parent->child;
if (w->next)
w->next->prev = w;
parent->child = w;
g_object_ref (w);
}
}
/* mimic g_list_foreach for consistency */
void owl_window_children_foreach(owl_window *parent, GFunc func, gpointer user_data)
{
owl_window *w;
for (w = parent->child;
w != NULL;
w = w->next) {
func(w, user_data);
}
}
owl_window *owl_window_parent(owl_window *w)
{
return w->parent;
}
owl_window *owl_window_first_child(owl_window *w)
{
return w->child;
}
owl_window *owl_window_next_sibling(owl_window *w)
{
return w->next;
}
owl_window *owl_window_previous_sibling(owl_window *w)
{
return w->prev;
}
/** Internal window management **/
static void _owl_window_create_curses(owl_window *w)
{
if (w->is_screen) {
resizeterm(w->nlines, w->ncols);
w->win = stdscr;
} else {
/* Explicitly disallow realizing an unlinked non-root */
if (w->parent == NULL || w->parent->win == NULL)
return;
if (w->pan) {
w->win = newwin(w->nlines, w->ncols, w->begin_y, w->begin_x);
replace_panel(w->pan, w->win);
} else {
w->win = derwin(w->parent->win, w->nlines, w->ncols, w->begin_y, w->begin_x);
}
}
}
static void _owl_window_destroy_curses(owl_window *w)
{
if (w->is_screen) {
/* don't deallocate the dummy */
w->win = NULL;
} else {
if (w->pan) {
/* panels assume their windows always exist, so we put in a fake one */
replace_panel(w->pan, _dummy_window());
}
if (w->win) {
/* and destroy our own window */
delwin(w->win);
w->win = NULL;
}
}
}
void owl_window_show(owl_window *w)
{
w->shown = 1;
_owl_window_realize(w);
if (w->pan)
show_panel(w->pan);
}
void owl_window_show_all(owl_window *w)
{
FuncOneArg ptr = (FuncOneArg)owl_window_show;
owl_window_show(w);
owl_window_children_foreach(w, first_arg_only, &ptr);
}
void owl_window_hide(owl_window *w)
{
/* you can't unmap the screen */
if (w->is_screen)
return;
w->shown = 0;
if (w->pan)
hide_panel(w->pan);
_owl_window_unrealize(w);
}
bool owl_window_is_shown(owl_window *w)
{
return w->shown;
}
bool owl_window_is_realized(owl_window *w)
{
return w->win != NULL;
}
bool owl_window_is_toplevel(owl_window *w)
{
return w->pan != NULL;
}
bool owl_window_is_subwin(owl_window *w)
{
return w->pan == NULL && !w->is_screen;
}
static bool _owl_window_should_realize(owl_window *w)
{
return owl_window_is_shown(w) &&
(!w->parent || owl_window_is_realized(w->parent));
}
static void _owl_window_realize_later(owl_window *w)
{
if (owl_window_is_realized(w) || !_owl_window_should_realize(w))
return;
owl_window_dirty(w);
}
static void _owl_window_realize(owl_window *w)
{
FuncOneArg ptr = (FuncOneArg)_owl_window_realize_later;
/* check if we can create a window */
if (owl_window_is_realized(w) || !_owl_window_should_realize(w))
return;
if (w->nlines <= 0 || w->ncols <= 0)
return;
_owl_window_create_curses(w);
if (w->win == NULL)
return;
/* schedule a repaint */
owl_window_dirty(w);
/* map the children */
owl_window_children_foreach(w, first_arg_only, &ptr);
}
static void _owl_window_unrealize(owl_window *w)
{
FuncOneArg ptr = (FuncOneArg)_owl_window_unrealize;
if (w->win == NULL)
return;
/* unmap all the children */
owl_window_children_foreach(w, first_arg_only, &ptr);
_owl_window_destroy_curses(w);
w->dirty = w->dirty_subtree = 0;
/* subwins leave a mess in the parent; dirty it */
if (w->parent)
owl_window_dirty(w->parent);
}
/** Painting and book-keeping **/
void owl_window_set_cursor(owl_window *w)
{
if (cursor_owner)
g_object_remove_weak_pointer(G_OBJECT(cursor_owner), (gpointer*) &cursor_owner);
cursor_owner = w;
if (w)
g_object_add_weak_pointer(G_OBJECT(w), (gpointer*) &cursor_owner);
owl_window_dirty(owl_window_get_screen());
}
void owl_window_set_default_cursor(owl_window *w)
{
if (default_cursor)
g_object_remove_weak_pointer(G_OBJECT(default_cursor), (gpointer*) &default_cursor);
default_cursor = w;
if (w)
g_object_add_weak_pointer(G_OBJECT(w), (gpointer*) &default_cursor);
owl_window_dirty(owl_window_get_screen());
}
static owl_window *_get_cursor(bool *is_default)
{
*is_default = false;
if (cursor_owner && owl_window_is_realized(cursor_owner))
return cursor_owner;
*is_default = true;
if (default_cursor && owl_window_is_realized(default_cursor))
return default_cursor;
return owl_window_get_screen();
}
void owl_window_dirty(owl_window *w)
{
if (!_owl_window_should_realize(w))
return;
if (!w->dirty) {
w->dirty = 1;
while (w && !w->dirty_subtree) {
w->dirty_subtree = 1;
w = w->parent;
}
}
}
void owl_window_dirty_children(owl_window *w)
{
FuncOneArg ptr = (FuncOneArg)owl_window_dirty;
owl_window_children_foreach(w, first_arg_only, &ptr);
}
static void _owl_window_redraw(owl_window *w)
{
if (!w->dirty) return;
_owl_window_realize(w);
if (w->win && !w->is_screen) {
if (owl_window_is_subwin(w)) {
/* If a subwin, we might have gotten random touched lines from wsyncup or
* past drawing. That information is useless, so we discard it all */
untouchwin(w->win);
}
g_signal_emit(w, window_signals[REDRAW], 0, w->win);
wsyncup(w->win);
}
w->dirty = 0;
}
static bool _owl_window_is_subtree_dirty(owl_window *w)
{
owl_window *child;
if (w->dirty)
return true;
for (child = w->child;
child != NULL;
child = child->next) {
if (child->dirty_subtree)
return true;
}
return false;
}
static void _owl_window_redraw_subtree(owl_window *w)
{
FuncOneArg ptr = (FuncOneArg)_owl_window_redraw_subtree;
if (!w->dirty_subtree)
return;
_owl_window_redraw(w);
owl_window_children_foreach(w, first_arg_only, &ptr);
/* Clear the dirty_subtree bit, unless a child doesn't have it
* cleared because we dirtied a window in redraw. Dirtying a
* non-descendant window during a redraw handler is
* discouraged. Redraw will not break, but it is undefined whether
* the dirty is delayed to the next event loop iteration. */
if (_owl_window_is_subtree_dirty(w)) {
owl_function_debugmsg("subtree still dirty after one iteration!");
} else {
w->dirty_subtree = 0;
}
}
/*
Redraw all the windows with scheduled redraws.
NOTE: This function shouldn't be called outside the event loop
*/
void owl_window_redraw_scheduled(void)
{
owl_window *cursor;
owl_window *screen = owl_window_get_screen();
bool default_cursor;
if (!screen->dirty_subtree)
return;
_owl_window_redraw_subtree(screen);
update_panels();
cursor = _get_cursor(&default_cursor);
if (cursor && cursor->win) {
/* If supported, hide the default cursor. */
curs_set(default_cursor ? 0 : 1);
/* untouch it to avoid drawing; window should be clean now, but we must
* clean up in case there was junk left over on a subwin (cleaning up after
* subwin drawing isn't sufficient because a wsyncup messes up subwin
* ancestors */
untouchwin(cursor->win);
wnoutrefresh(cursor->win);
}
doupdate();
}
/** Window position **/
void owl_window_get_position(owl_window *w, int *nlines, int *ncols, int *begin_y, int *begin_x)
{
if (nlines) *nlines = w->nlines;
if (ncols) *ncols = w->ncols;
if (begin_y) *begin_y = w->begin_y;
if (begin_x) *begin_x = w->begin_x;
}
void owl_window_move(owl_window *w, int begin_y, int begin_x)
{
/* It is possible to move a window efficiently with move_panel and mvderwin,
* but begy and begx are then wrong. Currently, this only effects the
* wnoutrefresh to move cursor. TODO: fix that and reinstate that
* optimization if it's worth the trouble */
owl_window_set_position(w, w->nlines, w->ncols, begin_y, begin_x);
}
void owl_window_set_position(owl_window *w, int nlines, int ncols, int begin_y, int begin_x)
{
int resized;
if (w->nlines == nlines && w->ncols == ncols
&& w->begin_y == begin_y && w->begin_x == begin_x) {
return;
}
resized = w->nlines != nlines || w->ncols != ncols;
_owl_window_unrealize(w);
w->begin_y = begin_y;
w->begin_x = begin_x;
w->nlines = nlines;
w->ncols = ncols;
if (resized)
g_signal_emit(w, window_signals[RESIZED], 0);
if (w->shown) {
/* ncurses is screwy: give up and recreate windows at the right place */
_owl_window_realize_later(w);
}
}
void owl_window_resize(owl_window *w, int nlines, int ncols)
{
owl_window_set_position(w, nlines, ncols, w->begin_y, w->begin_x);
}
/** Redrawing main loop hooks **/
static bool _owl_window_should_redraw(void) {
return g.resizepending || owl_window_get_screen()->dirty_subtree;
}
static gboolean _owl_window_redraw_prepare(GSource *source, int *timeout) {
*timeout = -1;
return _owl_window_should_redraw();
}
static gboolean _owl_window_redraw_check(GSource *source) {
return _owl_window_should_redraw();
}
static gboolean _owl_window_redraw_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
owl_colorpair_mgr *cpmgr;
/* if a resize has been scheduled, deal with it */
owl_global_check_resize(&g);
/* update the terminal if we need to */
owl_window_redraw_scheduled();
/* On colorpair shortage, reset and redraw /everything/. NOTE: if we
* still overflow, this be useless work. With 8-colors, we get 64
* pairs. With 256-colors, we get 32768 pairs with ext-colors
* support and 256 otherwise. */
cpmgr = owl_global_get_colorpair_mgr(&g);
if (cpmgr->overflow) {
owl_function_debugmsg("colorpairs: used all %d pairs; reset pairs and redraw.",
owl_util_get_colorpairs());
owl_fmtext_reset_colorpairs(cpmgr);
owl_function_full_redisplay();
owl_window_redraw_scheduled();
}
return TRUE;
}
static GSourceFuncs redraw_funcs = {
_owl_window_redraw_prepare,
_owl_window_redraw_check,
_owl_window_redraw_dispatch,
NULL
};
CALLER_OWN GSource *owl_window_redraw_source_new(void)
{
GSource *source;
source = g_source_new(&redraw_funcs, sizeof(GSource));
/* TODO: priority?? */
return source;
}