1 #include "tickit.h"
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7
8 static struct {
9 int vis : 1;
10 int blink : 1;
11 unsigned int shape : 2;
12 } modes;
13
render_modes(TickitTerm * tt)14 static void render_modes(TickitTerm *tt)
15 {
16 tickit_term_goto(tt, 5, 3);
17 tickit_term_print(tt, "Cursor visible: ");
18 tickit_term_print(tt, modes.vis ? "| >On< | Off |" : "| On | >Off< |");
19
20 tickit_term_goto(tt, 7, 3);
21 tickit_term_print(tt, "Cursor blink: ");
22 tickit_term_print(tt, modes.blink ? "| >On< | Off |" : "| On | >Off< |");
23
24 tickit_term_goto(tt, 9, 3);
25 tickit_term_print(tt, "Cursor shape: ");
26 tickit_term_print(tt, modes.shape == TICKIT_CURSORSHAPE_BLOCK ? "| >Block< | Under | Bar |" :
27 modes.shape == TICKIT_CURSORSHAPE_UNDER ? "| Block | >Under< | Bar |" :
28 "| Block | Under | >Bar< |");
29
30 tickit_term_goto(tt, 20, 10);
31 tickit_term_print(tt, "Cursor > <");
32 tickit_term_goto(tt, 20, 20);
33
34 tickit_term_flush(tt);
35 }
36
event(TickitTerm * tt,TickitEventFlags flags,void * _info,void * data)37 static int event(TickitTerm *tt, TickitEventFlags flags, void *_info, void *data)
38 {
39 TickitMouseEventInfo *info = _info;
40
41 if(info->type != TICKIT_MOUSEEV_PRESS || info->button != 1)
42 return 0;
43
44 if(info->line == 5) {
45 if(info->col >= 21 && info->col <= 26)
46 modes.vis = 1;
47 else if(info->col >= 28 && info->col <= 34)
48 modes.vis = 0;
49 else
50 return 0;
51
52 tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORVIS, modes.vis);
53 }
54
55 if(info->line == 7) {
56 if(info->col >= 21 && info->col <= 26)
57 modes.blink = 1;
58 else if(info->col >= 28 && info->col <= 34)
59 modes.blink = 0;
60 else
61 return 0;
62
63 tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORBLINK, modes.blink);
64 }
65
66 if(info->line == 9) {
67 if(info->col >= 21 && info->col <= 29)
68 modes.shape = TICKIT_CURSORSHAPE_BLOCK;
69 else if(info->col >= 31 && info->col <= 39)
70 modes.shape = TICKIT_CURSORSHAPE_UNDER;
71 else if(info->col >= 40 && info->col <= 47)
72 modes.shape = TICKIT_CURSORSHAPE_LEFT_BAR;
73 else
74 return 0;
75
76 tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORSHAPE, modes.shape);
77 }
78
79 render_modes(tt);
80
81 return 1;
82 }
83
later(Tickit * t,TickitEventFlags flags,void * _info,void * data)84 static int later(Tickit *t, TickitEventFlags flags, void *_info, void *data)
85 {
86 TickitTerm *tt = tickit_get_term(t);
87
88 render_modes(tt);
89
90 tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORVIS, modes.vis);
91
92 return 1;
93 }
94
main(int argc,char * argv[])95 int main(int argc, char *argv[])
96 {
97 Tickit *t = tickit_new_stdtty();
98
99 TickitTerm *tt = tickit_get_term(t);
100 if(!tt) {
101 fprintf(stderr, "Cannot create TickitTerm - %s\n", strerror(errno));
102 return 1;
103 }
104
105 tickit_term_setctl_int(tt, TICKIT_TERMCTL_MOUSE, TICKIT_TERM_MOUSEMODE_CLICK);
106
107 tickit_term_bind_event(tt, TICKIT_TERM_ON_MOUSE, 0, event, NULL);
108
109 tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORVIS, (modes.vis = 1));
110 tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORBLINK, (modes.blink = 1));
111 tickit_term_setctl_int(tt, TICKIT_TERMCTL_CURSORSHAPE, (modes.shape = TICKIT_CURSORSHAPE_BLOCK));
112
113 tickit_watch_later(t, 0, &later, NULL);
114
115 tickit_run(t);
116
117 tickit_unref(t);
118
119 return 0;
120 }
121