This file is indexed.

/usr/include/tickit.h is in libtickit-dev 0.2-2ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#ifdef __cplusplus
extern "C" {
#endif

#ifndef __TICKIT_H__
#define __TICKIT_H__

/* We'd quite like the timer*() functions */
#define _DEFAULT_SOURCE

#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>

#include <sys/time.h>

#ifdef __GNUC__
# define DEPRECATED __attribute__((deprecated))
#else
# define DEPRECATED
#endif

/*
 * Top-level object / structure types
 */

typedef struct TickitPen TickitPen;
typedef struct TickitRectSet TickitRectSet;
typedef struct TickitRenderBuffer TickitRenderBuffer;
typedef struct TickitString TickitString;
typedef struct TickitTerm TickitTerm;
typedef struct TickitWindow TickitWindow;

typedef struct Tickit Tickit;

typedef struct {
  int top;
  int left;
  int lines;
  int cols;
} TickitRect;

/*
 * Enumerations
 */

typedef enum {
  TICKIT_BIND_FIRST   = 1<<0,
  TICKIT_BIND_UNBIND  = 1<<1,
  TICKIT_BIND_DESTROY = 1<<2,
} TickitBindFlags;

typedef enum {
  TICKIT_CURSORSHAPE_BLOCK = 1,
  TICKIT_CURSORSHAPE_UNDER,
  TICKIT_CURSORSHAPE_LEFT_BAR,
} TickitCursorShape;

typedef enum {
  TICKIT_LINECAP_START = 0x01,
  TICKIT_LINECAP_END   = 0x02,
  TICKIT_LINECAP_BOTH  = 0x03,
} TickitLineCaps;

typedef enum {
  TICKIT_LINE_SINGLE = 1,
  TICKIT_LINE_DOUBLE = 2,
  TICKIT_LINE_THICK  = 3,
} TickitLineStyle;

typedef enum {
  TICKIT_NO    =  0,
  TICKIT_YES   =  1,
  TICKIT_MAYBE = -1,
} TickitMaybeBool;

typedef enum {
  TICKIT_PEN_FG,         /* colour */
  TICKIT_PEN_BG,         /* colour */
  TICKIT_PEN_BOLD,       /* bool */
  TICKIT_PEN_UNDER,      /* bool: TODO - number? */
  TICKIT_PEN_ITALIC,     /* bool */
  TICKIT_PEN_REVERSE,    /* bool */
  TICKIT_PEN_STRIKE,     /* bool */
  TICKIT_PEN_ALTFONT,    /* number */
  TICKIT_PEN_BLINK,      /* bool */

  TICKIT_N_PEN_ATTRS
} TickitPenAttr;

typedef enum {
  TICKIT_PENTYPE_BOOL,
  TICKIT_PENTYPE_INT,
  TICKIT_PENTYPE_COLOUR,
} TickitPenAttrType;

typedef enum {
  /* This is part of the API so additions must go at the end only */
  TICKIT_TERMCTL_ALTSCREEN = 1,
  TICKIT_TERMCTL_CURSORVIS,
  TICKIT_TERMCTL_MOUSE,
  TICKIT_TERMCTL_CURSORBLINK,
  TICKIT_TERMCTL_CURSORSHAPE,
  TICKIT_TERMCTL_ICON_TEXT,
  TICKIT_TERMCTL_TITLE_TEXT,
  TICKIT_TERMCTL_ICONTITLE_TEXT,
  TICKIT_TERMCTL_KEYPAD_APP,
  TICKIT_TERMCTL_COLORS, // read-only

  TICKIT_N_TERMCTLS
} TickitTermCtl;

typedef enum {
  TICKIT_TERM_MOUSEMODE_OFF,
  TICKIT_TERM_MOUSEMODE_CLICK,
  TICKIT_TERM_MOUSEMODE_DRAG,
  TICKIT_TERM_MOUSEMODE_MOVE,
} TickitTermMouseMode;

typedef enum {
  TICKIT_WINDOW_HIDDEN      = 1<<0,
  TICKIT_WINDOW_LOWEST      = 1<<1,
  TICKIT_WINDOW_ROOT_PARENT = 1<<2,
  TICKIT_WINDOW_STEAL_INPUT = 1<<3,

  // Composite flag
  TICKIT_WINDOW_POPUP = TICKIT_WINDOW_ROOT_PARENT|TICKIT_WINDOW_STEAL_INPUT,
} TickitWindowFlags;

// TODO: this wants a name surely?
enum {
  TICKIT_MOD_SHIFT = 0x01,
  TICKIT_MOD_ALT   = 0x02,
  TICKIT_MOD_CTRL  = 0x04,
};

/*
 * Secondary structures
 */

typedef struct {
  size_t bytes;
  int    codepoints;
  int    graphemes;
  int    columns;
} TickitStringPos;

/*
 * Event types
 */

typedef enum {
  TICKIT_EV_FIRE = (1 << 0),
  TICKIT_EV_UNBIND = (1 << 1),
  TICKIT_EV_DESTROY = (1 << 2),
} TickitEventFlags;

typedef struct {
  int lines, cols;
} TickitResizeEventInfo;

typedef enum {
  TICKIT_KEYEV_KEY = 1,
  TICKIT_KEYEV_TEXT,
} TickitKeyEventType;

typedef struct {
  TickitKeyEventType type;
  int mod;
  const char *str;
} TickitKeyEventInfo;

typedef enum {
  TICKIT_MOUSEEV_PRESS = 1,
  TICKIT_MOUSEEV_DRAG,
  TICKIT_MOUSEEV_RELEASE,
  TICKIT_MOUSEEV_WHEEL,

  TICKIT_MOUSEEV_DRAG_START = 0x101,
  TICKIT_MOUSEEV_DRAG_OUTSIDE,
  TICKIT_MOUSEEV_DRAG_DROP,
  TICKIT_MOUSEEV_DRAG_STOP,
} TickitMouseEventType;

enum {
  TICKIT_MOUSEWHEEL_UP = 1,
  TICKIT_MOUSEWHEEL_DOWN,
};

typedef struct {
  TickitMouseEventType type;
  int button;
  int mod;
  int line, col;
} TickitMouseEventInfo;

typedef struct {
  TickitRect rect;
  TickitRect oldrect;
} TickitGeomchangeEventInfo;

typedef struct {
  TickitRect rect;
  TickitRenderBuffer *rb;
} TickitExposeEventInfo;

typedef enum {
  TICKIT_FOCUSEV_IN = 1,
  TICKIT_FOCUSEV_OUT,
} TickitFocusEventType;

typedef struct {
  TickitFocusEventType type;
  TickitWindow *win;
} TickitFocusEventInfo;

/*
 * Functions
 */

/* TickitPen */

TickitPen *tickit_pen_new(void);
TickitPen *tickit_pen_new_attrs(TickitPenAttr attr, ...);
TickitPen *tickit_pen_clone(const TickitPen *orig);

TickitPen *tickit_pen_ref(TickitPen *pen);
void       tickit_pen_unref(TickitPen *pen);

bool tickit_pen_has_attr(const TickitPen *pen, TickitPenAttr attr);
bool tickit_pen_is_nonempty(const TickitPen *pen);
bool tickit_pen_nondefault_attr(const TickitPen *pen, TickitPenAttr attr);
bool tickit_pen_is_nondefault(const TickitPen *pen);

bool tickit_pen_get_bool_attr(const TickitPen *pen, TickitPenAttr attr);
void tickit_pen_set_bool_attr(TickitPen *pen, TickitPenAttr attr, bool val);

int  tickit_pen_get_int_attr(const TickitPen *pen, TickitPenAttr attr);
void tickit_pen_set_int_attr(TickitPen *pen, TickitPenAttr attr, int val);

int  tickit_pen_get_colour_attr(const TickitPen *pen, TickitPenAttr attr);
void tickit_pen_set_colour_attr(TickitPen *pen, TickitPenAttr attr, int value);

typedef struct {
  uint8_t r, g, b;
} TickitPenRGB8;

bool tickit_pen_has_colour_attr_rgb8(const TickitPen *pen, TickitPenAttr attr);
TickitPenRGB8 tickit_pen_get_colour_attr_rgb8(const TickitPen *pen, TickitPenAttr attr);
void tickit_pen_set_colour_attr_rgb8(TickitPen *pen, TickitPenAttr attr, TickitPenRGB8 value);

bool tickit_pen_set_colour_attr_desc(TickitPen *pen, TickitPenAttr attr, const char *value);

void tickit_pen_clear_attr(TickitPen *pen, TickitPenAttr attr);
void tickit_pen_clear(TickitPen *pen);

bool tickit_pen_equiv_attr(const TickitPen *a, const TickitPen *b, TickitPenAttr attr);
bool tickit_pen_equiv(const TickitPen *a, const TickitPen *b);

void tickit_pen_copy_attr(TickitPen *dst, const TickitPen *src, TickitPenAttr attr);
void tickit_pen_copy(TickitPen *dst, const TickitPen *src, bool overwrite);

typedef int TickitPenEventFn(TickitPen *tt, TickitEventFlags flags, void *info, void *user);

typedef enum {
  TICKIT_PEN_ON_DESTROY,
  TICKIT_PEN_ON_CHANGE,
} TickitPenEvent;

int  tickit_pen_bind_event(TickitPen *tt, TickitPenEvent ev, TickitBindFlags flags,
    TickitPenEventFn *fn, void *user);
void tickit_pen_unbind_event_id(TickitPen *tt, int id);

TickitPenAttrType tickit_pen_attrtype(TickitPenAttr attr);
const char *tickit_pen_attrname(TickitPenAttr attr);
TickitPenAttr tickit_pen_lookup_attr(const char *name);

/* TickitRect */

void tickit_rect_init_sized(TickitRect *rect, int top, int left, int lines, int cols);
void tickit_rect_init_bounded(TickitRect *rect, int top, int left, int bottom, int right);

static inline int tickit_rect_bottom(const TickitRect *rect)
{ return rect->top + rect->lines; }

static inline int tickit_rect_right (const TickitRect *rect)
{ return rect->left + rect->cols; }

void tickit_rect_translate(TickitRect *rect, int downward, int rightward);

bool tickit_rect_intersect(TickitRect *dst, const TickitRect *a, const TickitRect *b);

bool tickit_rect_intersects(const TickitRect *a, const TickitRect *b);
bool tickit_rect_contains(const TickitRect *large, const TickitRect *small);

int tickit_rect_add(TickitRect ret[3], const TickitRect *a, const TickitRect *b);
int tickit_rect_subtract(TickitRect ret[4], const TickitRect *orig, const TickitRect *hole);

/* TickitRectSet */

TickitRectSet *tickit_rectset_new(void);
void tickit_rectset_destroy(TickitRectSet *trs);

void tickit_rectset_clear(TickitRectSet *trs);

size_t tickit_rectset_rects(const TickitRectSet *trs);
size_t tickit_rectset_get_rect(const TickitRectSet *trs, size_t i, TickitRect *rects);
size_t tickit_rectset_get_rects(const TickitRectSet *trs, TickitRect rects[], size_t n);

void tickit_rectset_add(TickitRectSet *trs, const TickitRect *rect);
void tickit_rectset_subtract(TickitRectSet *trs, const TickitRect *rect);

void tickit_rectset_translate(TickitRectSet *trs, int downward, int rightward);

bool tickit_rectset_intersects(const TickitRectSet *trs, const TickitRect *rect);
bool tickit_rectset_contains(const TickitRectSet *trs, const TickitRect *rect);

/* TickitString */

TickitString *tickit_string_new(const char *str, size_t len);
TickitString *tickit_string_ref(TickitString *s);
void tickit_string_unref(TickitString *s);
const char *tickit_string_get(const TickitString *s);
size_t tickit_string_len(const TickitString *s);

/* TickitTerm */

TickitTerm *tickit_term_new(void);
TickitTerm *tickit_term_new_for_termtype(const char *termtype);
void tickit_term_destroy(TickitTerm *tt);

TickitTerm *tickit_term_ref(TickitTerm *tt);
void        tickit_term_unref(TickitTerm *tt);

TickitTerm *tickit_term_open_stdio(void);

const char *tickit_term_get_termtype(TickitTerm *tt);

typedef void TickitTermOutputFunc(TickitTerm *tt, const char *bytes, size_t len, void *user);

void tickit_term_set_output_fd(TickitTerm *tt, int fd);
int  tickit_term_get_output_fd(const TickitTerm *tt);
void tickit_term_set_output_func(TickitTerm *tt, TickitTermOutputFunc *fn, void *user);
void tickit_term_set_output_buffer(TickitTerm *tt, size_t len);

void tickit_term_await_started_msec(TickitTerm *tt, long msec);
void tickit_term_await_started_tv(TickitTerm *tt, const struct timeval *timeout);
void tickit_term_flush(TickitTerm *tt);

void tickit_term_pause(TickitTerm *tt);
void tickit_term_resume(TickitTerm *tt);

/* fd is allowed to be unset (-1); works abstractly */
void tickit_term_set_input_fd(TickitTerm *tt, int fd);
int  tickit_term_get_input_fd(const TickitTerm *tt);

TickitMaybeBool tickit_term_get_utf8(const TickitTerm *tt);
void tickit_term_set_utf8(TickitTerm *tt, bool utf8);

void tickit_term_input_push_bytes(TickitTerm *tt, const char *bytes, size_t len);
void tickit_term_input_readable(TickitTerm *tt);
int  tickit_term_input_check_timeout_msec(TickitTerm *tt);
void tickit_term_input_wait_msec(TickitTerm *tt, long msec);
void tickit_term_input_wait_tv(TickitTerm *tt, const struct timeval *timeout);

void tickit_term_get_size(const TickitTerm *tt, int *lines, int *cols);
void tickit_term_set_size(TickitTerm *tt, int lines, int cols);
void tickit_term_refresh_size(TickitTerm *tt);

void tickit_term_observe_sigwinch(TickitTerm *tt, bool observe);

typedef int TickitTermEventFn(TickitTerm *tt, TickitEventFlags flags, void *info, void *user);

typedef enum {
  TICKIT_TERM_ON_DESTROY,
  TICKIT_TERM_ON_RESIZE,
  TICKIT_TERM_ON_KEY,
  TICKIT_TERM_ON_MOUSE,
} TickitTermEvent;

int  tickit_term_bind_event(TickitTerm *tt, TickitTermEvent ev, TickitBindFlags flags,
    TickitTermEventFn *fn, void *user);
void tickit_term_unbind_event_id(TickitTerm *tt, int id);

void tickit_term_print(TickitTerm *tt, const char *str);
void tickit_term_printn(TickitTerm *tt, const char *str, size_t len);
void tickit_term_printf(TickitTerm *tt, const char *fmt, ...);
void tickit_term_vprintf(TickitTerm *tt, const char *fmt, va_list args);
bool tickit_term_goto(TickitTerm *tt, int line, int col);
void tickit_term_move(TickitTerm *tt, int downward, int rightward);
bool tickit_term_scrollrect(TickitTerm *tt, TickitRect rect, int downward, int rightward);

void tickit_term_chpen(TickitTerm *tt, const TickitPen *pen);
void tickit_term_setpen(TickitTerm *tt, const TickitPen *pen);

void tickit_term_clear(TickitTerm *tt);
void tickit_term_erasech(TickitTerm *tt, int count, TickitMaybeBool moveend);

bool tickit_term_getctl_int(TickitTerm *tt, TickitTermCtl ctl, int *value);
bool tickit_term_setctl_int(TickitTerm *tt, TickitTermCtl ctl, int value);
bool tickit_term_setctl_str(TickitTerm *tt, TickitTermCtl ctl, const char *value);

void tickit_term_emit_key(TickitTerm *tt, TickitKeyEventInfo *info);
void tickit_term_emit_mouse(TickitTerm *tt, TickitMouseEventInfo *info);

const char *tickit_term_ctlname(TickitTermCtl ctl);
TickitTermCtl tickit_term_lookup_ctl(const char *name);

/* String handling utilities */

int tickit_utf8_seqlen(long codepoint);
DEPRECATED int tickit_string_seqlen(long codepoint); /* use tickit_utf8_seqlen() */

/* Does NOT NUL-terminate the buffer */
size_t tickit_utf8_put(char *str, size_t len, long codepoint);
DEPRECATED size_t tickit_string_putchar(char *str, size_t len, long codepoint); /* use tickit_utf8_put() */

size_t tickit_utf8_count(const char *str, TickitStringPos *pos, const TickitStringPos *limit);
size_t tickit_utf8_countmore(const char *str, TickitStringPos *pos, const TickitStringPos *limit);
size_t tickit_utf8_ncount(const char *str, size_t len, TickitStringPos *pos, const TickitStringPos *limit);
size_t tickit_utf8_ncountmore(const char *str, size_t len, TickitStringPos *pos, const TickitStringPos *limit);

DEPRECATED size_t tickit_string_count(const char *str, TickitStringPos *pos, const TickitStringPos *limit);
DEPRECATED size_t tickit_string_countmore(const char *str, TickitStringPos *pos, const TickitStringPos *limit);
DEPRECATED size_t tickit_string_ncount(const char *str, size_t len, TickitStringPos *pos, const TickitStringPos *limit);
DEPRECATED size_t tickit_string_ncountmore(const char *str, size_t len, TickitStringPos *pos, const TickitStringPos *limit);

// Some convenient mutators for TickitStringPos structs

static inline void tickit_stringpos_zero(TickitStringPos *pos) {
  pos->bytes = pos->codepoints = pos->graphemes = pos->columns = 0;
}

#define INIT_TICKIT_STRINGPOS_LIMIT_NONE { .bytes = -1, .codepoints = -1, .graphemes = -1, .columns = -1 }
static inline void tickit_stringpos_limit_none(TickitStringPos *pos)
{
  pos->bytes = pos->codepoints = pos->graphemes = pos->columns = -1;
}

#define INIT_TICKIT_STRINGPOS_LIMIT_BYTES(v) { .bytes = (v), .codepoints = -1, .graphemes = -1, .columns = -1 }
static inline void tickit_stringpos_limit_bytes(TickitStringPos *pos, size_t bytes) {
  pos->codepoints = pos->graphemes = pos->columns = -1;
  pos->bytes = bytes;
}

#define INIT_TICKIT_STRINGPOS_LIMIT_CODEPOINTS(v) { .bytes = -1, .codepoints = (v), .graphemes = -1, .columns = -1 }
static inline void tickit_stringpos_limit_codepoints(TickitStringPos *pos, int codepoints) {
  pos->bytes = pos->graphemes = pos->columns = -1;
  pos->codepoints = codepoints;
}

#define INIT_TICKIT_STRINGPOS_LIMIT_GRAPHEMES(v) { .bytes = -1, .codepoints = -1, .graphemes = (v), .columns = -1 }
static inline void tickit_stringpos_limit_graphemes(TickitStringPos *pos, int graphemes) {
  pos->bytes = pos->codepoints = pos->columns = -1;
  pos->graphemes = graphemes;
}

#define INIT_TICKIT_STRINGPOS_LIMIT_COLUMNS(v) { .bytes = -1, .codepoints = -1, .graphemes = -1, .columns = (v) }
static inline void tickit_stringpos_limit_columns(TickitStringPos *pos, int columns) {
  pos->bytes = pos->codepoints = pos->graphemes = -1;
  pos->columns = columns;
}

DEPRECATED int    tickit_string_mbswidth(const char *str);
DEPRECATED int    tickit_string_byte2col(const char *str, size_t byte);
DEPRECATED size_t tickit_string_col2byte(const char *str, int col);

int    tickit_utf8_mbswidth(const char *str);
int    tickit_utf8_byte2col(const char *str, size_t byte);
size_t tickit_utf8_col2byte(const char *str, int col);

/* TickitRenderBuffer */

TickitRenderBuffer *tickit_renderbuffer_new(int lines, int cols);
void tickit_renderbuffer_destroy(TickitRenderBuffer *rb);

TickitRenderBuffer *tickit_renderbuffer_ref(TickitRenderBuffer *rb);
void                tickit_renderbuffer_unref(TickitRenderBuffer *rb);

void tickit_renderbuffer_get_size(const TickitRenderBuffer *rb, int *lines, int *cols);

void tickit_renderbuffer_translate(TickitRenderBuffer *rb, int downward, int rightward);
void tickit_renderbuffer_clip(TickitRenderBuffer *rb, TickitRect *rect);
void tickit_renderbuffer_mask(TickitRenderBuffer *rb, TickitRect *mask);

bool tickit_renderbuffer_has_cursorpos(const TickitRenderBuffer *rb);
void tickit_renderbuffer_get_cursorpos(const TickitRenderBuffer *rb, int *line, int *col);
void tickit_renderbuffer_goto(TickitRenderBuffer *rb, int line, int col);
void tickit_renderbuffer_ungoto(TickitRenderBuffer *rb);

void tickit_renderbuffer_setpen(TickitRenderBuffer *rb, const TickitPen *pen);

void tickit_renderbuffer_reset(TickitRenderBuffer *rb);

void tickit_renderbuffer_save(TickitRenderBuffer *rb);
void tickit_renderbuffer_savepen(TickitRenderBuffer *rb);
void tickit_renderbuffer_restore(TickitRenderBuffer *rb);

void tickit_renderbuffer_skip_at(TickitRenderBuffer *rb, int line, int col, int cols);
void tickit_renderbuffer_skip(TickitRenderBuffer *rb, int cols);
void tickit_renderbuffer_skip_to(TickitRenderBuffer *rb, int col);
void tickit_renderbuffer_skiprect(TickitRenderBuffer *rb, TickitRect *rect);
int tickit_renderbuffer_text_at(TickitRenderBuffer *rb, int line, int col, const char *text);
int tickit_renderbuffer_textn_at(TickitRenderBuffer *rb, int line, int col, const char *text, size_t len);
int tickit_renderbuffer_text(TickitRenderBuffer *rb, const char *text);
int tickit_renderbuffer_textn(TickitRenderBuffer *rb, const char *text, size_t len);
int tickit_renderbuffer_textf_at(TickitRenderBuffer *rb, int line, int col, const char *fmt, ...);
int tickit_renderbuffer_vtextf_at(TickitRenderBuffer *rb, int line, int col, const char *fmt, va_list args);
int tickit_renderbuffer_textf(TickitRenderBuffer *rb, const char *fmt, ...);
int tickit_renderbuffer_vtextf(TickitRenderBuffer *rb, const char *fmt, va_list args);
void tickit_renderbuffer_erase_at(TickitRenderBuffer *rb, int line, int col, int cols);
void tickit_renderbuffer_erase(TickitRenderBuffer *rb, int cols);
void tickit_renderbuffer_erase_to(TickitRenderBuffer *rb, int col);
void tickit_renderbuffer_eraserect(TickitRenderBuffer *rb, TickitRect *rect);
void tickit_renderbuffer_clear(TickitRenderBuffer *rb);
void tickit_renderbuffer_char_at(TickitRenderBuffer *rb, int line, int col, long codepoint);
void tickit_renderbuffer_char(TickitRenderBuffer *rb, long codepoint);

void tickit_renderbuffer_hline_at(TickitRenderBuffer *rb, int line, int startcol, int endcol,
    TickitLineStyle style, TickitLineCaps caps);
void tickit_renderbuffer_vline_at(TickitRenderBuffer *rb, int startline, int endline, int col,
    TickitLineStyle style, TickitLineCaps caps);

void tickit_renderbuffer_copyrect(TickitRenderBuffer *rb, const TickitRect *dest, const TickitRect *src);
void tickit_renderbuffer_moverect(TickitRenderBuffer *rb, const TickitRect *dest, const TickitRect *src);

void tickit_renderbuffer_flush_to_term(TickitRenderBuffer *rb, TickitTerm *tt);

void tickit_renderbuffer_blit(TickitRenderBuffer *dst, TickitRenderBuffer *src);

// This API is still somewhat experimental

typedef struct {
  char north;
  char south;
  char east;
  char west;
} TickitRenderBufferLineMask;

int tickit_renderbuffer_get_cell_active(TickitRenderBuffer *rb, int line, int col);
size_t tickit_renderbuffer_get_cell_text(TickitRenderBuffer *rb, int line, int col, char *buffer, size_t len);
TickitRenderBufferLineMask tickit_renderbuffer_get_cell_linemask(TickitRenderBuffer *rb, int line, int col);

// returns a direct pointer - do not free or modify
TickitPen *tickit_renderbuffer_get_cell_pen(TickitRenderBuffer *rb, int line, int col);

struct TickitRenderBufferSpanInfo {
  bool is_active;
  int n_columns;
  char *text;
  size_t len;
  TickitPen *pen;
};

// returns the text length or -1 on error
size_t tickit_renderbuffer_get_span(TickitRenderBuffer *rb, int line, int startcol, struct TickitRenderBufferSpanInfo *info, char *buffer, size_t len);

/* Window */

TickitWindow *tickit_window_new_root(TickitTerm *term);
TickitWindow *tickit_window_new(TickitWindow *parent, TickitRect rect, TickitWindowFlags flags);

TickitWindow *tickit_window_parent(const TickitWindow *win);
TickitWindow *tickit_window_root(const TickitWindow *win);

size_t tickit_window_children(const TickitWindow *win);
size_t tickit_window_get_children(const TickitWindow *win, TickitWindow *children[], size_t n);

TickitTerm *tickit_window_get_term(const TickitWindow *win);

void tickit_window_close(TickitWindow *win);

void tickit_window_destroy(TickitWindow *win);

TickitWindow *tickit_window_ref(TickitWindow *win);
void          tickit_window_unref(TickitWindow *win);

typedef int TickitWindowEventFn(TickitWindow *win, TickitEventFlags flags, void *info, void *user);

typedef enum {
  TICKIT_WINDOW_ON_DESTROY,
  TICKIT_WINDOW_ON_GEOMCHANGE,
  TICKIT_WINDOW_ON_EXPOSE,
  TICKIT_WINDOW_ON_FOCUS,
  TICKIT_WINDOW_ON_KEY,
  TICKIT_WINDOW_ON_MOUSE,
} TickitWindowEvent;

int  tickit_window_bind_event(TickitWindow *win, TickitWindowEvent ev, TickitBindFlags flags,
    TickitWindowEventFn *fn, void *user);
void tickit_window_unbind_event_id(TickitWindow *win, int id);

void tickit_window_raise(TickitWindow *win);
void tickit_window_raise_to_front(TickitWindow *win);
void tickit_window_lower(TickitWindow *win);
void tickit_window_lower_to_back(TickitWindow *win);

void tickit_window_show(TickitWindow *win);
void tickit_window_hide(TickitWindow *win);
bool tickit_window_is_visible(TickitWindow *win);

TickitRect tickit_window_get_geometry(const TickitWindow *win);
TickitRect tickit_window_get_abs_geometry(const TickitWindow *win);

#define tickit_window_top(win)   (tickit_window_get_geometry(win)).top
#define tickit_window_left(win)  (tickit_window_get_geometry(win)).left
#define tickit_window_lines(win) (tickit_window_get_geometry(win)).lines
#define tickit_window_cols(win)  (tickit_window_get_geometry(win)).cols

int tickit_window_bottom(const TickitWindow *win);
int tickit_window_right(const TickitWindow *win);

void tickit_window_resize(TickitWindow *win, int lines, int cols);
void tickit_window_reposition(TickitWindow *win, int top, int left);
void tickit_window_set_geometry(TickitWindow *win, TickitRect rect);

TickitPen *tickit_window_get_pen(const TickitWindow *win);
void tickit_window_set_pen(TickitWindow *win, TickitPen *pen);
void tickit_window_expose(TickitWindow *win, const TickitRect *exposed);
void tickit_window_flush(TickitWindow *win);

bool tickit_window_scrollrect(TickitWindow *win, const TickitRect *rect, int downward, int rightward, TickitPen *pen);
bool tickit_window_scroll(TickitWindow *win, int downward, int rightward);
bool tickit_window_scroll_with_children(TickitWindow *win, int downward, int rightward);

void tickit_window_set_cursor_position(TickitWindow *win, int line, int col);
void tickit_window_set_cursor_visible(TickitWindow *win, bool visible);
void tickit_window_set_cursor_shape(TickitWindow *win, TickitCursorShape shape);

void tickit_window_take_focus(TickitWindow *win);
bool tickit_window_is_focused(const TickitWindow *win);
void tickit_window_set_focus_child_notify(TickitWindow *win, bool notify);

bool tickit_window_is_steal_input(const TickitWindow *win);
void tickit_window_set_steal_input(TickitWindow *win, bool steal);

/* Main object */

typedef int TickitCallbackFn(Tickit *t, TickitEventFlags flags, void *user);

Tickit *tickit_new_for_term(TickitTerm *tt);
Tickit *tickit_new_stdio(void);

Tickit *tickit_ref(Tickit *t);
void    tickit_unref(Tickit *t);

TickitTerm *tickit_get_term(Tickit *t);

TickitWindow *tickit_get_rootwin(Tickit *t);

void tickit_run(Tickit *t);
void tickit_stop(Tickit *t);

int tickit_timer_after_msec(Tickit *t, int msec, TickitBindFlags flags, TickitCallbackFn *fn, void *user);
int tickit_timer_after_tv(Tickit *t, const struct timeval *after, TickitBindFlags flags, TickitCallbackFn *fn, void *user);
void tickit_timer_cancel(Tickit *t, int id);

int tickit_later(Tickit *t, TickitBindFlags flags, TickitCallbackFn *fn, void *user);

/* Debug support */

void tickit_debug_init(void);

extern bool tickit_debug_enabled;

void tickit_debug_logf(const char *flag, const char *fmt, ...);
void tickit_debug_vlogf(const char *flag, const char *fmt, va_list args);

void tickit_debug_set_func(void (*func)(const char *str, void *data), void *data);
void tickit_debug_set_fh(FILE *fh);
bool tickit_debug_open(const char *path);

#endif

#ifdef __cplusplus
}
#endif