-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.c
753 lines (671 loc) · 22.5 KB
/
frontend.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
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/* Collascii - a canvas-based collaborative ascii text editor.
*
* ___ ___ _ _ _ ___ ___ ___ ___
* / __/ _ \| | | | /_\ / __|/ __|_ _|_ _|
* | (_| (_) | |__| |__ / _ \\__ \ (__ | | | |
* \___\___/|____|____/_/ \_\___/\___|___|___|
*
* The Future Editor of Yesterday, Tomorrow!
*
* By Matthew Beaudouin-Lafon, Evan New-Schmidt, Adam Novotny
*
* General layout of interface:
* +------------------------------------------+ canvas_win
* | 0 -- X, COLS |
* | | |
* | Y, ROWS |
* | |
* | |
* | |
* | |
* | |
* | |
* +------------------------------------------+ status_interface/status_win:
* |Saved to file "art.txt" [INSERT](2,6)WxH| msg_win info_win
* |step: ON, transparent: OFF | mode_win
* +------------------------------------------+
*
* What all these "wins" are:
* - canvas_win: where the ascii canvas is drawn
* - status_interface: grouping of all status-related wins, which live in
* - status_win:
* - msg_win: displays general messages to user
* - info_win: state-specific info (see update_info_win)
* - mode_win: mode-specific information
*/
#include "frontend.h"
#include <argp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "canvas.h"
#include "cursor.h"
#include "fe_modes.h"
#include "mode_id.h"
#include "network.h"
#include "state.h"
#include "util.h"
WINDOW *canvas_win;
status_interface_t *status_interface;
Cursor *cursor;
View *view;
bool networked = false;
Net_cfg *net_cfg = NULL;
int INFO_WIDTH = 24; // max width of the info window
// initial canvas dimensions
const int DEFAULT_WIDTH = 100;
const int DEFAULT_HEIGHT = 100;
// save filepath
char *DEFAULT_FILEPATH = "art.txt";
// signals to be caught with finish() function
int caught_signals[] = {SIGINT, SIGTERM, SIGKILL, SIGABRT,
SIGSEGV, SIGQUIT, SIGSTOP};
// #define LOG_TO_FILE // redirect stderr to "out.txt"
// mouse movements options (only use one)
// #define ENABLE_MOUSE_MOVEMENT 1003 // all move updates
#define ENABLE_MOUSE_MOVEMENT 1002 // move updates when buttons are pressed
// #define ENABLE_MOUSE_MOVEMENT 0 // don't send any charcodes
#ifdef DEBUG
#define LOG_TO_FILE
#endif
#ifdef LOG_TO_FILE
char *logfile_path = "out.txt";
FILE *logfile = NULL;
#endif
// if version isn't defined by the Makefile
#ifndef VERSION
#define VERSION "unknown"
#endif
//// ARGP CONFIG
// globals for argp
const char *argp_program_version = "collascii-" VERSION;
const char *argp_program_bug_address =
"<https://github.com/olin/collascii/issues/>";
// doc strings
static char doc[] =
"collascii -- a collaborative ascii editor\v"
// everything after the vertical tab comes after the usage info
"COLLASCII\n\n"
"\"The Future Editor of Yesterday, Tomorrow!\"\n\n"
"By Matthew Beaudouin-Lafon, Evan New-Schmidt, and Adam Novotny\n\n"
"A brief introduction to the interface:\n"
"- arrow keys to move the cursor\n"
"- enter a character by typing it, unless the mode specifies otherwise\n"
"- <TAB> to switch between modes\n"
"- <CTRL-C> to quit\n"
"- <CTRL-R> to read from the file\n"
"- <CTRL-S> to write to the file\n"
"- <PGUP>/<PGDOWN> move up/down a screen height\n"
"- <SHIFT-LEFT>/<SHIFT-RIGHT> move left/right a screen width\n";
static char args_doc[] = "[FILE]"; // optional file argument usage string
// commandline options
static struct argp_option options[] = {
// filename (not a flag)
{"FILE", 0, 0, OPTION_DOC,
"filepath for read/write ('-' to read from stdin)", 1},
// canvas dimensions
{"width", 'w', "WIDTH", 0, "initial width of canvas", 2},
{"height", 'h', "HEIGHT", 0, "initial height of canvas"},
// remote connections
{"server", 's', "SERVER", 0, "address of server to connect to", 3},
{"port", 'p', "PORT", 0, "port of server to connect to (default 5000)"},
{0} // terminating entry
};
// representation of desired state from the cmdline interface
// see the creatively-named "arguments" struct in `main` for the defaults
typedef struct {
int height, width; // dimensions of initial canvas (use `DEFAULT_` if 0)
int x, y; // initial cursor position
bool load_file; // read from file at start
bool use_stdin; // read from stdin instead of file_name
char *filename; // file to save to/read from
bool connect_remote; // connect to a remote server
char *remote_host;
char *remote_port;
} arguments_t;
/* Get a positive int from a string.
*
* Stores the parsed value from arg in val.
*
* Return values are meant for an argp parser function.
*/
int parse_int(int *val, const char *const arg) {
// set errno to 0 to know if strtol sets it (recommended by 9/10 docs)
errno = 0;
long res = strtol(arg, NULL, 10);
// return strtol errors
if (errno != 0) {
return errno;
}
// break if negative value is parsed
if (res < 0) {
return EINVAL;
}
// otherwise store and return normally
*val = (int)res;
return 0;
}
/* Make sure the arguments struct is valid and in a non-conflicting state.
*
* Return values are meant for an argp parser function.
*
* TODO: fill this out
*/
int validate_arguments(arguments_t *arguments) { return 0; }
// function for parse loop
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
// extract arguments from state
arguments_t *arguments = state->input;
switch (key) {
case 'w':
return parse_int(&(arguments->width), arg);
case 'h':
return parse_int(&(arguments->height), arg);
case 's':
arguments->remote_host = arg;
arguments->connect_remote = true;
return 0;
case 'p':
arguments->remote_port = arg;
return 0;
case ARGP_KEY_ARG:
if (strcmp(arg, "-") == 0) {
arguments->use_stdin = true;
} else {
arguments->filename = arg;
arguments->load_file = true;
}
return 0;
case ARGP_KEY_END:
return validate_arguments(arguments);
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {options, parse_opt, args_doc, doc};
/* Initialize state based on arguments.
*
* In the process it also initializes the following global values:
* - canvas_win
* - view
* - cursor
* - networked
* - net_cfg
*
* This should be run before ncurses init so stdin can be read correctly.
*/
void init_state(State *state, const arguments_t *const arguments) {
// load canvas from network, file, or blank
Canvas *canvas;
if (arguments->connect_remote) {
networked = true;
canvas = net_init(arguments->remote_host, arguments->remote_port);
net_cfg = net_getcfg();
} else if (arguments->use_stdin) {
// read from stdin if specified
logd("Reading from stdin\n");
canvas = canvas_readf_norewind(stdin);
// reopen stdin b/c EOF has been sent
// `/dev/tty` points to current terminal
// note that this is NOT portable
freopen("/dev/tty", "rw", stdin);
} else if (arguments->load_file) {
char *in_filename = arguments->filename;
FILE *f = fopen(in_filename, "r");
logd("Reading from '%s'\n", in_filename);
if (f == NULL) {
eprintf("Cannot read file %s\n", in_filename);
exit(1);
}
canvas = canvas_readf(f);
fclose(f);
// resize if arguments were given
if (arguments->height != 0 || arguments->width != 0) {
int res = canvas_resize(
&canvas,
arguments->height == 0 ? canvas->num_rows : arguments->height,
arguments->width == 0 ? canvas->num_cols : arguments->width);
if (res == 1) {
logd("Canvas truncated on resize\n"); // TODO: display this to user
}
}
} else {
canvas = canvas_new_blank(
arguments->height == 0 ? DEFAULT_HEIGHT : arguments->height,
arguments->width == 0 ? DEFAULT_WIDTH : arguments->width);
}
// init globals
view = view_new_startpos(canvas, 0, 0);
cursor = cursor_new();
// init state
State new_state = {
.ch_in = OK,
.cursor = cursor,
// .current_mode = MODE_PICKER,
.current_mode = MODE_INSERT,
// .current_mode = MODE_FREE_LINE,
// .current_mode = MODE_BRUSH,
.last_arrow_direction = KEY_RIGHT,
.last_canvas_mode = MODE_INSERT,
.view = view,
.last_cursor = cursor_newyx(arguments->y, arguments->x),
.filepath = arguments->filename,
.collab_list = collab_list_create(NUM_COLLAB),
};
*state = new_state;
}
int main(int argc, char *argv[]) {
// initialize non-ncurses data structures here
// setup finish() signal handler
for (int i = 0; i < sizeof(caught_signals) / sizeof(int); i++) {
signal(caught_signals[i], finish);
}
#ifdef LOG_TO_FILE
logfile = fopen(logfile_path, "a");
if (logfile == NULL) {
perror("logfile fopen:");
exit(1);
}
if (-1 == dup2(fileno(logfile), fileno(stderr))) {
perror("stderr dup2:");
exit(1);
}
#endif
// struct of arg state, set with defaults
arguments_t arguments = {
.height = 0,
.width = 0,
.x = 0,
.y = 0,
.load_file = false,
.use_stdin = false,
.filename = DEFAULT_FILEPATH,
.connect_remote = false,
.remote_host = "",
.remote_port = "",
};
// parse arguments, init state
argp_parse(&argp, argc, argv, 0, 0, &arguments);
State *state = malloc(sizeof(State));
init_state(state, &arguments);
// initializing ncurses
logd("Starting frontend\n");
(void)initscr(); /* initialize the curses library */
keypad(stdscr, TRUE); /* enable keyboard mapping */
(void)nonl(); /* tell curses not to do NL->CR/NL on output */
(void)cbreak(); /* take input chars one at a time, no wait for \n */
(void)noecho(); /* don't print on getch() */
curs_set(2);
define_key("\r", KEY_ENTER); // Bind the <Enter> key properly
if (has_colors()) {
setup_colors();
}
// ENABLE MOUSE INPUT
// grab only mouse movement and left mouse press/release
mmask_t mmask = BUTTON1_PRESSED | BUTTON1_RELEASED;
#ifdef ENABLE_MOUSE_MOVEMENT
mmask |= REPORT_MOUSE_POSITION;
#endif
mmask_t return_mask = mousemask(mmask, NULL);
logd("Returned mouse mask: %li\n", return_mask);
// get mouse updates faster at the expense of not registering "clicks"
mouseinterval(0);
#ifdef ENABLE_MOUSE_MOVEMENT
// Make the terminal report mouse movement events, in a not-great way.
// Printing the escape code should bump it into `1003` mode, where an update
// is sent whenever the mouse moves between cells. Also of note: `1002` only
// sends movement updates when a mouse button is held down. Supposedly you can
// configure this in xterm, but I couldn't get it working.
// The $TERM variable should show some info about what mode is emulated.
// Running `cat -v` in a shell will show the escaped character codes that are
// sent and reveal if your shell is configured to send mouse input.
// https://gist.github.com/sylt/93d3f7b77e7f3a881603
// https://stackoverflow.com/q/29020638
// https://stackoverflow.com/q/7462850
//
// disabling mouse event charcodes is done in finish()
if (ENABLE_MOUSE_MOVEMENT == 1002) {
printf("\033[?1002h\n");
} else if (ENABLE_MOUSE_MOVEMENT == 1003) {
printf("\033[?1003h\n");
}
#endif
canvas_win = create_canvas_win();
status_interface = create_status_interface();
// Enable keyboard mapping
keypad(canvas_win, TRUE);
keypad(status_interface->status_win, TRUE);
//// Main loop
// update the screen size first. This clears the status window on any changes
// (including the first time it's run), so refreshing after updating the
// status will clear it otherwise
update_screen_size();
#ifdef DEBUG
mvwprintw(status_interface->msg_win, 0, 0, "MSG (debug)");
mvwprintw(status_interface->info_win, 0, 0, "INFO (debug)");
mvwprintw(status_interface->mode_win, 0, 0, "MODE (debug)");
#endif
char welcome_msg[] = "COLLASCII: <TAB> to switch modes, <CTRL-C> to quit";
print_msg_win(welcome_msg);
// bootstrap initial UI
call_mode(state->current_mode, START, state);
update_info_win_state(state);
// Move cursor to starting location and redraw canvases
refresh_screen();
// If connected to server, check both network and keyboard streams
if (networked) {
logd("Running networked loop\n");
int fd;
fd_set testfds;
while (1) {
testfds = net_cfg->clientfds;
select(FD_SETSIZE, &testfds, NULL, NULL, NULL);
for (fd = 0; fd < FD_SETSIZE; fd++) {
if (FD_ISSET(fd, &testfds)) {
if (networked &&
fd == net_cfg->sockfd) { // Accept data from open socket
logd("recv network\n");
// If server disconnects
if (net_handler(state) != 0) {
networked = false;
print_msg_win("Server Disconnect!");
};
redraw_canvas_win(); // TODO: draw single char update
refresh_screen();
} else if (fd == 0) { // process keyboard activity
master_handler(state, canvas_win, status_interface->info_win);
draw_collab_cursors(state);
refresh_screen();
net_update_pos(state);
}
}
}
}
// If local, process keyboard stream
} else {
logd("Running local loop\n");
while (1) {
master_handler(state, canvas_win, status_interface->info_win);
refresh_screen();
}
}
// Cleanup
cursor_free(cursor);
destroy_status_interface(status_interface);
destroy_win(canvas_win);
finish(0);
}
void setup_colors() {
start_color();
// TODO: Use #define to get colors for standard uses
// Assign color codes
init_pair(1, COLOR_WHITE, COLOR_BLUE);
init_pair(2, COLOR_WHITE, COLOR_GREEN);
init_pair(3, COLOR_WHITE, COLOR_RED);
init_pair(4, COLOR_WHITE, COLOR_CYAN);
init_pair(5, COLOR_WHITE, COLOR_MAGENTA);
init_pair(6, COLOR_WHITE, COLOR_YELLOW);
init_pair(7, COLOR_BLACK, COLOR_WHITE);
}
/* Update canvas with character at cursor current position.
*
* Changes the canvas and updates the ncurses `canvas_win` with the change.
*/
void front_setcharcursor(char ch) {
canvas_scharyx(view->canvas, cursor->y + view->y, cursor->x + view->x, ch);
mvwaddch(canvas_win, cursor_y_to_canvas(cursor), cursor_x_to_canvas(cursor),
ch);
if (networked) {
net_send_char(cursor->y + view->y, cursor->x + view->x, ch);
}
}
void redraw_canvas_win() {
// find max ranges to draw canvas
int max_x = min(view_max_x, view->canvas->num_cols - view->x);
int max_y = min(view_max_y, view->canvas->num_rows - view->y);
// draw canvas onto window
for (int x = 0; x < max_x; x++) {
for (int y = 0; y < max_y; y++) {
mvwaddch(canvas_win, y + 1, x + 1,
canvas_gcharyx(view->canvas, y + view->y, x + view->x));
}
}
// draw fill in rest of window
for (int x = max(0, max_x); x < view_max_x; x++) {
for (int y = 0; y < view_max_y; y++) {
mvwaddch(canvas_win, y + 1, x + 1, ACS_CKBOARD);
}
}
for (int y = max(0, max_y); y < view_max_y; y++) {
for (int x = 0; x < max_x; x++) {
mvwaddch(canvas_win, y + 1, x + 1, ACS_CKBOARD);
}
}
}
void draw_collab_cursors(State *state) {
collab_t *c = NULL;
const int min_x = view->x;
const int min_y = view->y;
const int max_x = min(view_max_x, view->canvas->num_cols - view->x);
const int max_y = min(view_max_y, view->canvas->num_rows - view->y);
for (int i = 0; i < state->collab_list->len; i++) {
c = state->collab_list->list[i];
if (c != NULL && (c->x >= min_x && c->y <= max_x) &&
(c->y >= min_y && c->y <= max_y)) {
logd("Drawing collab %i\n", c->uid);
const int color = has_colors() ? (i % 6) + 1 : 0;
// TODO: blink cursor with A_BLINK attribute (needs to pause between
// updates/only move on changes?)
mvwchgat(canvas_win, c->y - view->y + 1, c->x - view->x + 1, 1, 0, color,
NULL);
}
}
}
void refresh_screen() {
update_screen_size();
wmove(canvas_win, cursor_y_to_canvas(cursor), cursor_x_to_canvas(cursor));
wrefresh(status_interface->msg_win);
wrefresh(status_interface->info_win);
wrefresh(status_interface->mode_win);
wrefresh(canvas_win); // Refresh Canvas last so it gets the cursor
}
void update_screen_size() {
static int window_h_old, window_w_old;
int window_h_new, window_w_new;
getmaxyx(stdscr, window_h_new, window_w_new);
if (window_h_new != window_h_old || window_w_new != window_w_old) {
window_h_old = window_h_new;
window_w_old = window_w_new;
wresize(canvas_win, window_h_new - (STATUS_HEIGHT + 1), window_w_new);
wclear(stdscr);
wclear(canvas_win);
destroy_status_interface(status_interface);
status_interface = create_status_interface();
// Redraw borders
wborder(status_interface->status_win, ACS_VLINE, ACS_VLINE, ACS_HLINE,
ACS_HLINE, // Sides: ls, rs, ts, bs,
ACS_LTEE, ACS_RTEE, ACS_LLCORNER,
ACS_LRCORNER); // Corners: tl, tr, bl, br
wborder(canvas_win, ACS_VLINE, ACS_VLINE, ACS_HLINE,
ACS_HLINE, // Sides: ls, rs, ts, bs,
ACS_ULCORNER, ACS_URCORNER, ACS_LTEE,
ACS_RTEE); // Corners: tl, tr, bl, br
// Move cursor inside the canvas
if (cursor->x >= view_max_x) {
cursor->x = view_max_x;
}
if (cursor->y >= view_max_y) {
cursor->y = view_max_y;
}
redraw_canvas_win();
}
}
WINDOW *create_canvas_win() {
WINDOW *local_win;
// + 1 due to bottom border
local_win = newwin(LINES - (STATUS_HEIGHT + 1), COLS, 0, 0);
wborder(local_win, ACS_VLINE, ACS_VLINE, ACS_HLINE,
ACS_HLINE, // Sides: ls, rs, ts, bs,
ACS_ULCORNER, ACS_URCORNER, ACS_LTEE,
ACS_RTEE); // Corners: tl, tr, bl, br
wrefresh(local_win);
return local_win;
}
WINDOW *create_status_win() {
WINDOW *local_win;
// + 2 due to horizontal borders
local_win = newwin(STATUS_HEIGHT + 2, COLS, LINES - (STATUS_HEIGHT + 2), 0);
wborder(local_win, ACS_VLINE, ACS_VLINE, ACS_HLINE,
ACS_HLINE, // Sides: ls, rs, ts, bs,
ACS_LTEE, ACS_RTEE, ACS_LLCORNER,
ACS_LRCORNER); // Corners: tl, tr, bl, br
wrefresh(local_win);
return local_win;
}
WINDOW *create_msg_win(WINDOW *status_win) {
int sw = getmaxx(status_win) + 1;
int x, y;
getbegyx(status_win, y, x);
return newwin(1, sw - 2 - INFO_WIDTH, y + 1, x + 1);
}
WINDOW *create_info_win(WINDOW *status_win) {
int sw = getmaxx(status_win) + 1;
int x, y;
getbegyx(status_win, y, x);
return newwin(1, INFO_WIDTH, y + 1, x + 1 + sw - INFO_WIDTH - 3);
}
WINDOW *create_mode_win(WINDOW *status_win) {
int sw = getmaxx(status_win) + 1;
int x, y;
getbegyx(status_win, y, x);
return newwin(1, sw - 3, y + 2, x + 1);
}
status_interface_t *create_status_interface() {
WINDOW *sw = create_status_win();
status_interface_t *si = malloc(sizeof(status_interface_t));
*si = (status_interface_t){
.status_win = sw,
.msg_win = create_msg_win(sw),
.info_win = create_info_win(sw),
.mode_win = create_mode_win(sw),
};
wborder(si->status_win, ACS_VLINE, ACS_VLINE, ACS_HLINE,
ACS_HLINE, // Sides: ls, rs, ts, bs,
ACS_LTEE, ACS_RTEE, ACS_LLCORNER,
ACS_LRCORNER); // Corners: tl, tr, bl, br
return si;
}
void destroy_status_interface(status_interface_t *si) {
destroy_win(si->msg_win);
destroy_win(si->info_win);
destroy_win(si->mode_win);
destroy_win(si->status_win);
free(si);
}
void destroy_win(WINDOW *local_win) {
// Clear borders explicitly
wborder(local_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(local_win);
delwin(local_win);
}
/* Prints to status_win, similar to printf
*/
int print_msg_win(char *format, ...) {
// there isn't a va_list version of mvw_printw, so move to status_win first
// and then use vw_printw
WINDOW *mw = status_interface->msg_win;
wclear(mw);
wmove(mw, 0, 0);
va_list argp;
va_start(argp, format);
int res = vw_printw(mw, format, argp);
va_end(argp);
return res;
}
/* Prints to status_win, similar to printf
*/
int print_info_win(char *format, ...) {
// there isn't a va_list version of mvw_printw, so move to status_win first
// and then use vw_printw
WINDOW *mw = status_interface->info_win;
wclear(mw);
wmove(mw, 0, 0);
va_list argp;
va_start(argp, format);
int res = vw_printw(mw, format, argp);
va_end(argp);
return res;
}
/* Prints to status_win, similar to printf
*/
int print_mode_win(char *format, ...) {
// there isn't a va_list version of mvw_printw, so move to status_win first
// and then use vw_printw
WINDOW *mw = status_interface->mode_win;
wclear(mw);
wmove(mw, 0, 0);
va_list argp;
va_start(argp, format);
int res = vw_printw(mw, format, argp);
va_end(argp);
return res;
}
/* Update the info subwindow in status_win with relevant info.
*
* Prints the current mode, cursor coordinates, and canvas dimensions
* right-aligned.
*
* "[MODE] (X,Y) WxH"
*/
void update_info_win(const Mode_ID current_mode, const int x, const int y,
const int w, const int h) {
WINDOW *mw = status_interface->info_win;
char buffer[INFO_WIDTH + 1];
const char *mode_name = modes[current_mode].name;
int width = snprintf(buffer, INFO_WIDTH + 1, "[%s](%i,%i)%ix%i", mode_name, x,
y, w, h);
wclear(mw);
wmove(mw, 0, INFO_WIDTH - width);
waddnstr(mw, buffer, INFO_WIDTH);
}
/* Signal handler for exiting
*
* Turns of ncurses, disables mouse moves commands, closes the logfile.
*
* If sig is 0 or SIGINT, exits normally, otherwise prints the signal
* information to stderr and exits with sig.
*/
void finish(int sig) {
endwin();
// Disable mouse events charcode
#ifdef ENABLE_MOUSE_MOVEMENT
if (ENABLE_MOUSE_MOVEMENT == 1002) {
printf("\033[?1002l\n");
} else if (ENABLE_MOUSE_MOVEMENT == 1003) {
printf("\033[?1003l\n");
}
#endif
/* do your non-curses wrapup here */
#ifdef LOG_TO_FILE
if (logfile != NULL) {
fclose(logfile);
}
#endif
// decide how to exit based on signal
switch (sig) {
case 0: // normal
case SIGINT: // user CTRL-C
eprintf("Exiting\n");
exit(0);
break;
default: // problems
eprintf("Exited with signal %d (%s)\n", sig, strsignal(sig));
exit(sig);
break;
}
}