Mercurial > hg > nnchat
comparison ui.c @ 713:93d0e1547842
th-libs now uses stdbool.h if possible, so we need to rename all
BOOL/TRUE/FALSE to bool/true/false.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Dec 2022 13:20:25 +0200 |
parents | ff1af7410bba |
children |
comparison
equal
deleted
inserted
replaced
712:3a3e4fc53ac7 | 713:93d0e1547842 |
---|---|
8 | 8 |
9 | 9 |
10 nn_window_t *chatWindows[SET_MAX_WINDOWS], | 10 nn_window_t *chatWindows[SET_MAX_WINDOWS], |
11 *currWin = NULL; | 11 *currWin = NULL; |
12 | 12 |
13 BOOL appCursesInit = FALSE, appQuitFlag = FALSE; | 13 bool appCursesInit = false, appQuitFlag = false; |
14 int cursorVisible = ERR, | 14 int cursorVisible = ERR, |
15 scrWidth, scrHeight; | 15 scrWidth, scrHeight; |
16 | 16 |
17 | 17 |
18 static void nn_line_free(void *ptr) | 18 static void nn_line_free(void *ptr) |
69 else | 69 else |
70 return NULL; | 70 return NULL; |
71 } | 71 } |
72 | 72 |
73 | 73 |
74 BOOL nnwin_init(int delay) | 74 bool nnwin_init(int delay) |
75 { | 75 { |
76 // Sanity check the terminal size | 76 // Sanity check the terminal size |
77 if (LINES < 0 || LINES > 1000) LINES = 24; | 77 if (LINES < 0 || LINES > 1000) LINES = 24; |
78 if (COLS < 0 || COLS > 1000) COLS = 80; | 78 if (COLS < 0 || COLS > 1000) COLS = 80; |
79 | 79 |
80 // Initialize (n)curses library and terminal settings | 80 // Initialize (n)curses library and terminal settings |
81 initscr(); | 81 initscr(); |
82 raw(); | 82 raw(); |
83 keypad(stdscr, TRUE); | 83 keypad(stdscr, true); |
84 noecho(); | 84 noecho(); |
85 meta(stdscr, TRUE); | 85 meta(stdscr, true); |
86 timeout(delay); | 86 timeout(delay); |
87 scrollok(stdscr, FALSE); | 87 scrollok(stdscr, false); |
88 cursorVisible = curs_set(1); | 88 cursorVisible = curs_set(1); |
89 | 89 |
90 if (has_colors()) | 90 if (has_colors()) |
91 { | 91 { |
92 start_color(); | 92 start_color(); |
107 init_pair(14, COLOR_BLUE, COLOR_RED); | 107 init_pair(14, COLOR_BLUE, COLOR_RED); |
108 init_pair(15, COLOR_MAGENTA, COLOR_RED); | 108 init_pair(15, COLOR_MAGENTA, COLOR_RED); |
109 init_pair(16, COLOR_CYAN, COLOR_RED); | 109 init_pair(16, COLOR_CYAN, COLOR_RED); |
110 } | 110 } |
111 | 111 |
112 appCursesInit = TRUE; | 112 appCursesInit = true; |
113 nnwin_reset(); | 113 nnwin_reset(); |
114 | 114 |
115 #ifdef PDCURSES | 115 #ifdef PDCURSES |
116 PDC_set_title("NNChat v" NN_VERSION); | 116 PDC_set_title("NNChat v" NN_VERSION); |
117 #endif | 117 #endif |
119 memset(chatWindows, 0, sizeof(chatWindows)); | 119 memset(chatWindows, 0, sizeof(chatWindows)); |
120 chatWindows[0] = nn_window_new(NULL); | 120 chatWindows[0] = nn_window_new(NULL); |
121 nn_log_open(chatWindows[0]); | 121 nn_log_open(chatWindows[0]); |
122 currWin = chatWindows[0]; | 122 currWin = chatWindows[0]; |
123 | 123 |
124 return TRUE; | 124 return true; |
125 } | 125 } |
126 | 126 |
127 | 127 |
128 void nnwin_shutdown() | 128 void nnwin_shutdown() |
129 { | 129 { |
163 | 163 |
164 return NULL; | 164 return NULL; |
165 } | 165 } |
166 | 166 |
167 | 167 |
168 BOOL nnwin_open(const char *name, BOOL curwin) | 168 bool nnwin_open(const char *name, bool curwin) |
169 { | 169 { |
170 nn_window_t *res; | 170 nn_window_t *res; |
171 int i; | 171 int i; |
172 | 172 |
173 if (name == NULL) | 173 if (name == NULL) |
174 return FALSE; | 174 return false; |
175 | 175 |
176 if ((res = nn_window_new(name)) == NULL) | 176 if ((res = nn_window_new(name)) == NULL) |
177 return FALSE; | 177 return false; |
178 | 178 |
179 nn_log_open(res); | 179 nn_log_open(res); |
180 | 180 |
181 for (i = 1; i < SET_MAX_WINDOWS; i++) | 181 for (i = 1; i < SET_MAX_WINDOWS; i++) |
182 if (chatWindows[i] == NULL) | 182 if (chatWindows[i] == NULL) |
183 { | 183 { |
184 res->num = i; | 184 res->num = i; |
185 chatWindows[i] = res; | 185 chatWindows[i] = res; |
186 if (curwin) | 186 if (curwin) |
187 currWin = res; | 187 currWin = res; |
188 return TRUE; | 188 return true; |
189 } | 189 } |
190 | 190 |
191 return FALSE; | 191 return false; |
192 } | 192 } |
193 | 193 |
194 | 194 |
195 void nnwin_close(nn_window_t *win) | 195 void nnwin_close(nn_window_t *win) |
196 { | 196 { |
206 return; | 206 return; |
207 } | 207 } |
208 } | 208 } |
209 | 209 |
210 | 210 |
211 static BOOL nnwin_get_color(char const **s, int *col) | 211 static bool nnwin_get_color(char const **s, int *col) |
212 { | 212 { |
213 int val = 0; | 213 int val = 0; |
214 | 214 |
215 while (**s >= '0' && **s <= '9') | 215 while (**s >= '0' && **s <= '9') |
216 { | 216 { |
217 val *= 10; | 217 val *= 10; |
218 val += (**s - '0'); | 218 val += (**s - '0'); |
219 (*s)++; | 219 (*s)++; |
220 } | 220 } |
221 if (**s != '½') | 221 if (**s != '½') |
222 return FALSE; | 222 return false; |
223 | 223 |
224 if (val < 9) | 224 if (val < 9) |
225 *col = A_DIM | COLOR_PAIR(val); | 225 *col = A_DIM | COLOR_PAIR(val); |
226 else if (val < 30) | 226 else if (val < 30) |
227 *col = A_BOLD | COLOR_PAIR(val - 9); | 227 *col = A_BOLD | COLOR_PAIR(val - 9); |
228 | 228 |
229 return TRUE; | 229 return true; |
230 } | 230 } |
231 | 231 |
232 | 232 |
233 #define QPUTCH(ch) nnwin_putch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), col, ch) | 233 #define QPUTCH(ch) nnwin_putch(&(win->line->buf), &(win->line->bufsize), &(win->line->len), col, ch) |
234 | 234 |
235 static BOOL nnwin_putch(int **buf, size_t *bufsize, size_t *len, int color, char ch) | 235 static bool nnwin_putch(int **buf, size_t *bufsize, size_t *len, int color, char ch) |
236 { | 236 { |
237 if (*buf == NULL || *len + 1 >= *bufsize) | 237 if (*buf == NULL || *len + 1 >= *bufsize) |
238 { | 238 { |
239 *bufsize += TH_BUFGROW; | 239 *bufsize += TH_BUFGROW; |
240 *buf = (int *) th_realloc(*buf, *bufsize * sizeof(int)); | 240 *buf = (int *) th_realloc(*buf, *bufsize * sizeof(int)); |
241 if (*buf == NULL) | 241 if (*buf == NULL) |
242 return FALSE; | 242 return false; |
243 } | 243 } |
244 | 244 |
245 (*buf)[*len] = ((unsigned char) ch) | color; | 245 (*buf)[*len] = ((unsigned char) ch) | color; |
246 (*len)++; | 246 (*len)++; |
247 | 247 |
248 return TRUE; | 248 return true; |
249 } | 249 } |
250 | 250 |
251 | 251 |
252 int nnwin_print(nn_window_t *win, const char *fmt) | 252 int nnwin_print(nn_window_t *win, const char *fmt) |
253 { | 253 { |
281 } | 281 } |
282 else if (*s == '\n') | 282 else if (*s == '\n') |
283 { | 283 { |
284 th_ringbuf_add(win->data, win->line); | 284 th_ringbuf_add(win->data, win->line); |
285 win->line = NULL; | 285 win->line = NULL; |
286 win->dirty = TRUE; | 286 win->dirty = true; |
287 } | 287 } |
288 else if (*s != '\r') | 288 else if (*s != '\r') |
289 { | 289 { |
290 QPUTCH((unsigned char) *s == 255 ? ' ' : *s); | 290 QPUTCH((unsigned char) *s == 255 ? ' ' : *s); |
291 } | 291 } |
295 | 295 |
296 return 0; | 296 return 0; |
297 } | 297 } |
298 | 298 |
299 | 299 |
300 static void nnwin_print_str(WINDOW *win, const char *fmt, BOOL clip) | 300 static void nnwin_print_str(WINDOW *win, const char *fmt, bool clip) |
301 { | 301 { |
302 const char *s = fmt; | 302 const char *s = fmt; |
303 int col = 0; | 303 int col = 0; |
304 while (*s) | 304 while (*s) |
305 { | 305 { |
327 } | 327 } |
328 } | 328 } |
329 } | 329 } |
330 | 330 |
331 | 331 |
332 void nnwin_update(BOOL force, BOOL mask, nn_editbuf_t *ebuf, const char *username, const unsigned int usercolor) | 332 void nnwin_update(bool force, bool mask, nn_editbuf_t *ebuf, const char *username, const unsigned int usercolor) |
333 { | 333 { |
334 int sx, sy; | 334 int sx, sy; |
335 BOOL changed = FALSE; | 335 bool changed = false; |
336 | 336 |
337 // Clear screen if forced or main or editbuf are dirty | 337 // Clear screen if forced or main or editbuf are dirty |
338 if (force || (currWin != NULL && currWin->dirty) || (ebuf != NULL && ebuf->dirty)) | 338 if (force || (currWin != NULL && currWin->dirty) || (ebuf != NULL && ebuf->dirty)) |
339 { | 339 { |
340 // Save cursor position | 340 // Save cursor position |
341 getyx(stdscr, sy, sx); | 341 getyx(stdscr, sy, sx); |
342 | 342 |
343 wattrset(stdscr, A_NORMAL); | 343 wattrset(stdscr, A_NORMAL); |
344 wbkgdset(stdscr, COLOR_PAIR(0)); | 344 wbkgdset(stdscr, COLOR_PAIR(0)); |
345 werase(stdscr); | 345 werase(stdscr); |
346 force = TRUE; | 346 force = true; |
347 changed = TRUE; | 347 changed = true; |
348 } | 348 } |
349 else | 349 else |
350 { | 350 { |
351 // Save cursor position | 351 // Save cursor position |
352 getyx(stdscr, sy, sx); | 352 getyx(stdscr, sy, sx); |
356 if (currWin != NULL && (force || currWin->dirty)) | 356 if (currWin != NULL && (force || currWin->dirty)) |
357 { | 357 { |
358 int y, offs; | 358 int y, offs; |
359 th_ringbuf_t *buf = currWin->data; | 359 th_ringbuf_t *buf = currWin->data; |
360 | 360 |
361 changed = TRUE; | 361 changed = true; |
362 | 362 |
363 for (y = scrHeight - 4, offs = buf->size - 1 - currWin->pos; offs >= 0 && y > 0; offs--) | 363 for (y = scrHeight - 4, offs = buf->size - 1 - currWin->pos; offs >= 0 && y > 0; offs--) |
364 { | 364 { |
365 nn_line_t *line = (nn_line_t *) buf->data[offs]; | 365 nn_line_t *line = (nn_line_t *) buf->data[offs]; |
366 if (line != NULL) | 366 if (line != NULL) |
392 for (pos = 0; pos < line->len; pos++) | 392 for (pos = 0; pos < line->len; pos++) |
393 waddch(stdscr, s[pos]); | 393 waddch(stdscr, s[pos]); |
394 } | 394 } |
395 } | 395 } |
396 | 396 |
397 currWin->dirty = FALSE; | 397 currWin->dirty = false; |
398 } | 398 } |
399 | 399 |
400 // Update statusline | 400 // Update statusline |
401 if (changed || force) | 401 if (changed || force) |
402 { | 402 { |
426 #endif | 426 #endif |
427 | 427 |
428 wmove(stdscr, scrHeight - 4, 0); | 428 wmove(stdscr, scrHeight - 4, 0); |
429 wbkgdset(stdscr, COLOR_PAIR(10)); | 429 wbkgdset(stdscr, COLOR_PAIR(10)); |
430 wclrtoeol(stdscr); | 430 wclrtoeol(stdscr); |
431 nnwin_print_str(stdscr, tmpStr, TRUE); | 431 nnwin_print_str(stdscr, tmpStr, true); |
432 | 432 |
433 for (i = 0; i < SET_MAX_WINDOWS; i++) | 433 for (i = 0; i < SET_MAX_WINDOWS; i++) |
434 { | 434 { |
435 if (chatWindows[i] != NULL && chatWindows[i]->dirty) | 435 if (chatWindows[i] != NULL && chatWindows[i]->dirty) |
436 { | 436 { |
451 | 451 |
452 wmove(stdscr, scrHeight - 3, 0); | 452 wmove(stdscr, scrHeight - 3, 0); |
453 wattrset(stdscr, A_NORMAL); | 453 wattrset(stdscr, A_NORMAL); |
454 wbkgdset(stdscr, COLOR_PAIR(0)); | 454 wbkgdset(stdscr, COLOR_PAIR(0)); |
455 | 455 |
456 ebuf->dirty = FALSE; | 456 ebuf->dirty = false; |
457 if (mask) | 457 if (mask) |
458 { | 458 { |
459 size_t i; | 459 size_t i; |
460 for (i = 0; i < ebuf->len; i++) | 460 for (i = 0; i < ebuf->len; i++) |
461 waddch(stdscr, '*'); | 461 waddch(stdscr, '*'); |
468 waddnstr(stdscr, tmp, ebuf->len); | 468 waddnstr(stdscr, tmp, ebuf->len); |
469 th_free(tmp); | 469 th_free(tmp); |
470 } | 470 } |
471 wmove(stdscr, scrHeight - 3 + yoffs, xoffs); | 471 wmove(stdscr, scrHeight - 3 + yoffs, xoffs); |
472 | 472 |
473 changed = TRUE; | 473 changed = true; |
474 } | 474 } |
475 | 475 |
476 if (changed) | 476 if (changed) |
477 wrefresh(stdscr); | 477 wrefresh(stdscr); |
478 } | 478 } |
479 | 479 |
480 | 480 |
481 void nnwin_input_process(nn_editbuf_t *editBuf, nn_editstate_t *editState, | 481 void nnwin_input_process(nn_editbuf_t *editBuf, nn_editstate_t *editState, |
482 BOOL (*callback)(int, nn_editbuf_t *, nn_editstate_t *)) | 482 bool (*callback)(int, nn_editbuf_t *, nn_editstate_t *)) |
483 { | 483 { |
484 int c, cnt = 0; | 484 int c, cnt = 0; |
485 | 485 |
486 // Handle several buffered keypresses at once | 486 // Handle several buffered keypresses at once |
487 do | 487 do |
582 case KEY_RESIZE: | 582 case KEY_RESIZE: |
583 resize_term(0, 0); | 583 resize_term(0, 0); |
584 erase(); | 584 erase(); |
585 timeout(SET_DELAY); | 585 timeout(SET_DELAY); |
586 nnwin_reset(); | 586 nnwin_reset(); |
587 editState->update = TRUE; | 587 editState->update = true; |
588 break; | 588 break; |
589 #endif | 589 #endif |
590 | 590 |
591 case 0x204: // ctrl+left arrow = Skip words left | 591 case 0x204: // ctrl+left arrow = Skip words left |
592 case 0x20b: | 592 case 0x20b: |
593 while (editBuf->pos > 0 && isspace((int) editBuf->data[editBuf->pos - 1])) | 593 while (editBuf->pos > 0 && isspace((int) editBuf->data[editBuf->pos - 1])) |
594 editBuf->pos--; | 594 editBuf->pos--; |
595 while (editBuf->pos > 0 && !isspace((int) editBuf->data[editBuf->pos - 1])) | 595 while (editBuf->pos > 0 && !isspace((int) editBuf->data[editBuf->pos - 1])) |
596 editBuf->pos--; | 596 editBuf->pos--; |
597 editBuf->dirty = TRUE; | 597 editBuf->dirty = true; |
598 break; | 598 break; |
599 | 599 |
600 case 0x206: // ctrl+right arrow = Skip words right | 600 case 0x206: // ctrl+right arrow = Skip words right |
601 case 0x210: | 601 case 0x210: |
602 while (editBuf->pos < editBuf->len && isspace((int) editBuf->data[editBuf->pos])) | 602 while (editBuf->pos < editBuf->len && isspace((int) editBuf->data[editBuf->pos])) |
603 editBuf->pos++; | 603 editBuf->pos++; |
604 while (editBuf->pos < editBuf->len && !isspace((int) editBuf->data[editBuf->pos])) | 604 while (editBuf->pos < editBuf->len && !isspace((int) editBuf->data[editBuf->pos])) |
605 editBuf->pos++; | 605 editBuf->pos++; |
606 editBuf->dirty = TRUE; | 606 editBuf->dirty = true; |
607 break; | 607 break; |
608 | 608 |
609 case KEY_HOME: | 609 case KEY_HOME: |
610 nn_editbuf_setpos(editBuf, 0); | 610 nn_editbuf_setpos(editBuf, 0); |
611 break; | 611 break; |
640 case KEY_F(2): // F2 = Clear editbuffer | 640 case KEY_F(2): // F2 = Clear editbuffer |
641 nn_editbuf_clear(editBuf); | 641 nn_editbuf_clear(editBuf); |
642 break; | 642 break; |
643 | 643 |
644 case 0x0c: // Ctrl + L | 644 case 0x0c: // Ctrl + L |
645 editState->update = TRUE; | 645 editState->update = true; |
646 break; | 646 break; |
647 | 647 |
648 case ERR: | 648 case ERR: |
649 // Ignore | 649 // Ignore |
650 break; | 650 break; |
669 while (c != ERR && ++cnt < 10); | 669 while (c != ERR && ++cnt < 10); |
670 } | 670 } |
671 | 671 |
672 | 672 |
673 | 673 |
674 char *nnwin_prompt_requester(BOOL allowEmpty, nn_editstate_t *editState, | 674 char *nnwin_prompt_requester(bool allowEmpty, nn_editstate_t *editState, |
675 BOOL (*callback)(int, nn_editbuf_t *, nn_editstate_t *), | 675 bool (*callback)(int, nn_editbuf_t *, nn_editstate_t *), |
676 void (*update)(nn_editbuf_t *, nn_editstate_t *)) | 676 void (*update)(nn_editbuf_t *, nn_editstate_t *)) |
677 { | 677 { |
678 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); | 678 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); |
679 char *res; | 679 char *res; |
680 | 680 |
681 editState->done = FALSE; | 681 editState->done = false; |
682 while (!editState->isError && !appQuitFlag && !editState->done) | 682 while (!editState->isError && !appQuitFlag && !editState->done) |
683 { | 683 { |
684 nnwin_input_process(editBuf, editState, callback); | 684 nnwin_input_process(editBuf, editState, callback); |
685 update(editBuf, editState); | 685 update(editBuf, editState); |
686 } | 686 } |