comparison main.c @ 422:8eb2839a565c

Rename various functions.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 24 May 2012 08:11:34 +0300
parents 4b7cd24dc943
children aeb24b1b5e77
comparison
equal deleted inserted replaced
421:4b7cd24dc943 422:8eb2839a565c
233 int compareUsername(const void *s1, const void *s2) 233 int compareUsername(const void *s1, const void *s2)
234 { 234 {
235 return th_strcasecmp((char *) s1, (char *) s2); 235 return th_strcasecmp((char *) s1, (char *) s2);
236 } 236 }
237 237
238 nn_window_t *findWindow(const char *id) 238 nn_window_t *nnwin_find(const char *id)
239 { 239 {
240 int i; 240 int i;
241 241
242 for (i = 0; i < SET_MAX_WINDOWS; i++) 242 for (i = 0; i < SET_MAX_WINDOWS; i++)
243 if (chatWindows[i] != NULL && 243 if (chatWindows[i] != NULL &&
247 247
248 return NULL; 248 return NULL;
249 } 249 }
250 250
251 251
252 BOOL openWindow(const char *name, BOOL curwin) 252 BOOL nnwin_open(const char *name, BOOL curwin)
253 { 253 {
254 int i; 254 int i;
255 nn_window_t *res; 255 nn_window_t *res;
256 if (name == NULL) 256 if (name == NULL)
257 return FALSE; 257 return FALSE;
271 271
272 return FALSE; 272 return FALSE;
273 } 273 }
274 274
275 275
276 void closeWindow(nn_window_t *win) 276 void nnwin_close(nn_window_t *win)
277 { 277 {
278 int i; 278 int i;
279 if (win == NULL) return; 279 if (win == NULL) return;
280 280
281 for (i = 1; i < SET_MAX_WINDOWS; i++) 281 for (i = 1; i < SET_MAX_WINDOWS; i++)
286 return; 286 return;
287 } 287 }
288 } 288 }
289 289
290 290
291 void updateStatus(void) 291 void nnwin_update_statusline(void)
292 { 292 {
293 char tmpStr[128]; 293 char tmpStr[128];
294 int i; 294 int i;
295 295
296 if (statusWin == NULL) return; 296 if (statusWin == NULL) return;
336 336
337 wrefresh(statusWin); 337 wrefresh(statusWin);
338 } 338 }
339 339
340 340
341 void printEditBuf(nn_editbuf_t *buf) 341 void nnwin_update_editbuf(nn_editbuf_t *buf)
342 { 342 {
343 char *tmp; 343 char *tmp;
344 if (editWin == NULL || buf == NULL) return; 344 if (editWin == NULL || buf == NULL) return;
345 345
346 buf->data[buf->len] = 0; 346 buf->data[buf->len] = 0;
368 wrefresh(editWin); 368 wrefresh(editWin);
369 th_free(tmp); 369 th_free(tmp);
370 } 370 }
371 371
372 372
373 int printWin(WINDOW *win, const char *fmt) 373 int nnwin_print(WINDOW *win, const char *fmt)
374 { 374 {
375 const char *s = fmt; 375 const char *s = fmt;
376 int col = 0; 376 int col = 0;
377 377
378 while (*s) 378 while (*s)
401 } 401 }
402 402
403 403
404 #define QPUTCH(ch) th_vputch(&(win->buf), &(win->bufsize), &(win->len), ch) 404 #define QPUTCH(ch) th_vputch(&(win->buf), &(win->bufsize), &(win->len), ch)
405 405
406 int nn_window_print(nn_window_t *win, const char *fmt) 406 int nnwin_print_buf(nn_window_t *win, const char *fmt)
407 { 407 {
408 const char *s = fmt; 408 const char *s = fmt;
409 int col = 0; 409 int col = 0;
410 while (*s) 410 while (*s)
411 { 411 {
463 463
464 return 0; 464 return 0;
465 } 465 }
466 466
467 467
468 BOOL updateMainWin(BOOL force) 468 BOOL nnwin_update_main(BOOL force)
469 { 469 {
470 int h, offs; 470 int h, offs;
471 qringbuf_t *buf; 471 qringbuf_t *buf;
472 472
473 // Check pointers 473 // Check pointers
486 werase(mainWin); 486 werase(mainWin);
487 scrollok(mainWin, 1); 487 scrollok(mainWin, 1);
488 for (offs = buf->size - h - currWin->pos; offs >= 0 && offs < buf->size - currWin->pos && offs < buf->size; offs++) 488 for (offs = buf->size - h - currWin->pos; offs >= 0 && offs < buf->size - currWin->pos && offs < buf->size; offs++)
489 { 489 {
490 if (buf->data[offs] != NULL) 490 if (buf->data[offs] != NULL)
491 printWin(mainWin, buf->data[offs]); 491 nnwin_print(mainWin, buf->data[offs]);
492 } 492 }
493 493
494 currWin->dirty = FALSE; 494 currWin->dirty = FALSE;
495 wrefresh(mainWin); 495 wrefresh(mainWin);
496 return TRUE; 496 return TRUE;
547 } 547 }
548 548
549 if (!optDaemon && (flags & LOG_WINDOW)) 549 if (!optDaemon && (flags & LOG_WINDOW))
550 { 550 {
551 nn_window_t *tmp = win != NULL ? win : chatWindows[0]; 551 nn_window_t *tmp = win != NULL ? win : chatWindows[0];
552 if (flags & LOG_STAMP) nn_window_print(tmp, tmpStr); 552 if (flags & LOG_STAMP) nnwin_print_buf(tmp, tmpStr);
553 nn_window_print(tmp, buf); 553 nnwin_print_buf(tmp, buf);
554 } 554 }
555 555
556 th_free(buf); 556 th_free(buf);
557 } 557 }
558 558
699 else 699 else
700 in_msg = ""; 700 in_msg = "";
701 *tmp = 0; 701 *tmp = 0;
702 702
703 isIgnored = setIgnoreMode && checkIgnoreList(in_name); 703 isIgnored = setIgnoreMode && checkIgnoreList(in_name);
704 win = findWindow(in_name); 704 win = nnwin_find(in_name);
705 705
706 if (win != NULL) 706 if (win != NULL)
707 { 707 {
708 printMsgF(win, isIgnored ? 0 : LOG_WINDOW, 708 printMsgF(win, isIgnored ? 0 : LOG_WINDOW,
709 "½5½<½%d½%s½5½>½0½ %s\n", 709 "½5½<½%d½%s½5½>½0½ %s\n",
779 *s = 0; 779 *s = 0;
780 780
781 p = nn_dbldecode_str(str); 781 p = nn_dbldecode_str(str);
782 if (!p) return -1; 782 if (!p) return -1;
783 783
784 win = findWindow(p); 784 win = nnwin_find(p);
785 nn_userhash_insert(nnUsers, nn_username_encode(p)); 785 nn_userhash_insert(nnUsers, nn_username_encode(p));
786 786
787 printMsg(NULL, "! ½3½%s½0½ ½2½ADDED.½0½\n", p); 787 printMsg(NULL, "! ½3½%s½0½ ½2½ADDED.½0½\n", p);
788 if (win != NULL) 788 if (win != NULL)
789 printMsg(win, "! ½3½%s½0½ ½2½joined the chat.½0½\n", p); 789 printMsg(win, "! ½3½%s½0½ ½2½joined the chat.½0½\n", p);
803 *s = 0; 803 *s = 0;
804 804
805 p = nn_dbldecode_str(str); 805 p = nn_dbldecode_str(str);
806 if (!p) return -1; 806 if (!p) return -1;
807 807
808 win = findWindow(p); 808 win = nnwin_find(p);
809 nn_userhash_delete(nnUsers, nn_username_encode(p)); 809 nn_userhash_delete(nnUsers, nn_username_encode(p));
810 810
811 printMsg(NULL, "! ½3½%s½0½ ½1½DELETED.½0½\n", p); 811 printMsg(NULL, "! ½3½%s½0½ ½1½DELETED.½0½\n", p);
812 if (win != NULL) 812 if (win != NULL)
813 printMsg(win, "! ½3½%s½0½ ½1½left the chat.½0½\n", p); 813 printMsg(win, "! ½3½%s½0½ ½1½left the chat.½0½\n", p);
962 nn_user_t *user = nn_user_find(nnUsers, nn_username_encode(name)); 962 nn_user_t *user = nn_user_find(nnUsers, nn_username_encode(name));
963 if (user != NULL) 963 if (user != NULL)
964 { 964 {
965 name = nn_username_decode(th_strdup(user->name)); 965 name = nn_username_decode(th_strdup(user->name));
966 printMsgQ(currWin, "Opening PRV query for '%s'.\n", name); 966 printMsgQ(currWin, "Opening PRV query for '%s'.\n", name);
967 if (openWindow(name, TRUE)) 967 if (nnwin_open(name, TRUE))
968 printMsgQ(currWin, "In PRV query with '%s'.\n", name); 968 printMsgQ(currWin, "In PRV query with '%s'.\n", name);
969 th_free(name); 969 th_free(name);
970 } 970 }
971 } 971 }
972 else 972 else
1005 else if (!th_strncasecmp(buf, "/close", 6)) 1005 else if (!th_strncasecmp(buf, "/close", 6))
1006 { 1006 {
1007 char *name = str_trim_left(buf + 6); 1007 char *name = str_trim_left(buf + 6);
1008 if (strlen(name) > 0) 1008 if (strlen(name) > 0)
1009 { 1009 {
1010 nn_window_t *win = findWindow(name); 1010 nn_window_t *win = nnwin_find(name);
1011 if (win != NULL) 1011 if (win != NULL)
1012 { 1012 {
1013 closeWindow(win); 1013 nnwin_close(win);
1014 printMsgQ(currWin, "Closed PRV query to '%s'.\n", name); 1014 printMsgQ(currWin, "Closed PRV query to '%s'.\n", name);
1015 } 1015 }
1016 else 1016 else
1017 { 1017 {
1018 printMsgQ(currWin, "No PRV query by name '%s'.\n", name); 1018 printMsgQ(currWin, "No PRV query by name '%s'.\n", name);
1020 } 1020 }
1021 else 1021 else
1022 { 1022 {
1023 if (currWin != chatWindows[0]) 1023 if (currWin != chatWindows[0])
1024 { 1024 {
1025 closeWindow(currWin); 1025 nnwin_close(currWin);
1026 currWin = chatWindows[0]; 1026 currWin = chatWindows[0];
1027 } 1027 }
1028 } 1028 }
1029 return 0; 1029 return 0;
1030 } 1030 }
1123 1123
1124 return result ? 0 : -1; 1124 return result ? 0 : -1;
1125 } 1125 }
1126 1126
1127 1127
1128 void closeWindows(void) 1128 void nnwin_close_windows(void)
1129 { 1129 {
1130 if (mainWin) delwin(mainWin); 1130 if (mainWin) delwin(mainWin);
1131 if (statusWin) delwin(statusWin); 1131 if (statusWin) delwin(statusWin);
1132 if (editWin) delwin(editWin); 1132 if (editWin) delwin(editWin);
1133 } 1133 }
1134 1134
1135 1135
1136 BOOL initializeWindows(void) 1136 BOOL nnwin_initialize_windows(void)
1137 { 1137 {
1138 int w, h; 1138 int w, h;
1139 1139
1140 getmaxyx(stdscr, h, w); 1140 getmaxyx(stdscr, h, w);
1141 1141
1142 closeWindows(); 1142 nnwin_close_windows();
1143 1143
1144 mainWin = subwin(stdscr, h - 4, w, 0, 0); 1144 mainWin = subwin(stdscr, h - 4, w, 0, 0);
1145 statusWin = subwin(stdscr, 1, w, h - 4, 0); 1145 statusWin = subwin(stdscr, 1, w, h - 4, 0);
1146 editWin = subwin(stdscr, 3, w, h - 3, 0); 1146 editWin = subwin(stdscr, 3, w, h - 3, 0);
1147 1147
1150 1150
1151 return TRUE; 1151 return TRUE;
1152 } 1152 }
1153 1153
1154 1154
1155 void updateWindows(void) 1155 void nnwin_update_all(void)
1156 { 1156 {
1157 if (mainWin) redrawwin(mainWin); 1157 if (mainWin) redrawwin(mainWin);
1158 if (statusWin) redrawwin(statusWin); 1158 if (statusWin) redrawwin(statusWin);
1159 if (editWin) redrawwin(editWin); 1159 if (editWin) redrawwin(editWin);
1160 } 1160 }
1584 init_pair(16, COLOR_CYAN, COLOR_RED); 1584 init_pair(16, COLOR_CYAN, COLOR_RED);
1585 } 1585 }
1586 1586
1587 cursesInit = TRUE; 1587 cursesInit = TRUE;
1588 1588
1589 if (!initializeWindows()) 1589 if (!nnwin_initialize_windows())
1590 goto err_exit; 1590 goto err_exit;
1591 1591
1592 #ifdef PDCURSES 1592 #ifdef PDCURSES
1593 PDC_set_title("NNChat v" NN_VERSION); 1593 PDC_set_title("NNChat v" NN_VERSION);
1594 #endif 1594 #endif
1595 1595
1596 memset(chatWindows, 0, sizeof(chatWindows)); 1596 memset(chatWindows, 0, sizeof(chatWindows));
1597 chatWindows[0] = nn_window_new(NULL); 1597 chatWindows[0] = nn_window_new(NULL);
1598 currWin = chatWindows[0]; 1598 currWin = chatWindows[0];
1599 updateStatus(); 1599 nnwin_update_statusline();
1600 } 1600 }
1601 1601
1602 // Check if we have username and password 1602 // Check if we have username and password
1603 if (cursesInit && (optUserName == NULL || optPassword == NULL)) 1603 if (cursesInit && (optUserName == NULL || optPassword == NULL))
1604 { 1604 {
1605 printWin(editWin, "You can avoid this prompt by issuing '/save' after logging in.\n"); 1605 nnwin_print(editWin, "You can avoid this prompt by issuing '/save' after logging in.\n");
1606 optUserName = promptRequester(editWin, "NN username: ", FALSE); 1606 optUserName = promptRequester(editWin, "NN username: ", FALSE);
1607 optPassword = promptRequester(editWin, "NN password: ", TRUE); 1607 optPassword = promptRequester(editWin, "NN password: ", TRUE);
1608 } 1608 }
1609 1609
1610 if (optUserName == NULL || optPassword == NULL) 1610 if (optUserName == NULL || optPassword == NULL)
1693 { 1693 {
1694 // Initialize rest of interactive UI code 1694 // Initialize rest of interactive UI code
1695 nn_editbuf_clear(editBuf); 1695 nn_editbuf_clear(editBuf);
1696 1696
1697 // First update of screen 1697 // First update of screen
1698 printEditBuf(editBuf); 1698 nnwin_update_editbuf(editBuf);
1699 updateStatus(); 1699 nnwin_update_statusline();
1700 1700
1701 printMsg(NULL, "%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname); 1701 printMsg(NULL, "%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname);
1702 printMsg(NULL, "%s\n", th_prog_author); 1702 printMsg(NULL, "%s\n", th_prog_author);
1703 printMsg(NULL, "%s\n", th_prog_license); 1703 printMsg(NULL, "%s\n", th_prog_license);
1704 } 1704 }
1852 case KEY_RESIZE: 1852 case KEY_RESIZE:
1853 resize_term(0, 0); 1853 resize_term(0, 0);
1854 erase(); 1854 erase();
1855 timeout(SET_DELAY); 1855 timeout(SET_DELAY);
1856 1856
1857 if (!initializeWindows()) 1857 if (!nnwin_initialize_windows())
1858 { 1858 {
1859 errorMsg("Error resizing curses chatWindows\n"); 1859 errorMsg("Error resizing curses chatWindows\n");
1860 isError = TRUE; 1860 isError = TRUE;
1861 } 1861 }
1862 update = updateMain = TRUE; 1862 update = updateMain = TRUE;
2013 performTabCompletion(editBuf); 2013 performTabCompletion(editBuf);
2014 update = TRUE; 2014 update = TRUE;
2015 break; 2015 break;
2016 2016
2017 case 0x0c: // Ctrl + L 2017 case 0x0c: // Ctrl + L
2018 updateWindows(); 2018 nnwin_update_all();
2019 update = updateMain = TRUE; 2019 update = updateMain = TRUE;
2020 break; 2020 break;
2021 2021
2022 case KEY_NPAGE: 2022 case KEY_NPAGE:
2023 case KEY_PPAGE: 2023 case KEY_PPAGE:
2060 break; 2060 break;
2061 } 2061 }
2062 } 2062 }
2063 while (c != ERR && !exitProg && ++cnt < 10); 2063 while (c != ERR && !exitProg && ++cnt < 10);
2064 2064
2065 updateMainWin(updateMain); 2065 nnwin_update_main(updateMain);
2066 2066
2067 if (update || firstUpdate) 2067 if (update || firstUpdate)
2068 { 2068 {
2069 // Update edit line 2069 // Update edit line
2070 updateStatus(); 2070 nnwin_update_statusline();
2071 printEditBuf(editBuf); 2071 nnwin_update_editbuf(editBuf);
2072 firstUpdate = FALSE; // a nasty hack ... 2072 firstUpdate = FALSE; // a nasty hack ...
2073 } 2073 }
2074 2074
2075 } // cursesInit 2075 } // cursesInit
2076 2076
2089 { 2089 {
2090 colorSet = TRUE; 2090 colorSet = TRUE;
2091 nn_conn_send_msg(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); 2091 nn_conn_send_msg(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
2092 } 2092 }
2093 2093
2094 updateStatus(); 2094 nnwin_update_statusline();
2095 printEditBuf(editBuf); 2095 nnwin_update_editbuf(editBuf);
2096 updateCount = 0; 2096 updateCount = 0;
2097 } 2097 }
2098 2098
2099 } 2099 }
2100 2100
2127 2127
2128 if (cursesInit) 2128 if (cursesInit)
2129 { 2129 {
2130 if (curVis != ERR) 2130 if (curVis != ERR)
2131 curs_set(curVis); 2131 curs_set(curVis);
2132 closeWindows(); 2132 nnwin_close_windows();
2133 endwin(); 2133 endwin();
2134 THMSG(1, "NCurses deinitialized.\n"); 2134 THMSG(1, "NCurses deinitialized.\n");
2135 } 2135 }
2136 2136
2137 #ifndef __WIN32 2137 #ifndef __WIN32