24
|
1 #include <stdio.h> |
|
2 #include <stdlib.h> |
|
3 #include <string.h> |
|
4 #include <ncurses.h> |
|
5 #include <panel.h> |
|
6 #include <time.h> |
|
7 #include <ctype.h> |
|
8 #include <locale.h> |
|
9 |
|
10 #include "screen.h" |
81
|
11 #include "hbuf.h" |
47
|
12 #include "commands.h" |
95
|
13 #include "compl.h" |
81
|
14 #include "roster.h" |
24
|
15 #include "parsecfg.h" |
|
16 #include "lang.h" |
81
|
17 #include "utf8.h" |
|
18 #include "utils.h" |
24
|
19 #include "list.h" |
|
20 |
|
21 #define window_entry(n) list_entry(n, window_entry_t, list) |
|
22 |
|
23 LIST_HEAD(window_list); |
|
24 |
|
25 typedef struct _window_entry_t { |
|
26 WINDOW *win; |
108
|
27 PANEL *panel; |
|
28 char *name; |
|
29 GList *hbuf; |
|
30 GList *top; // If top is not specified (NULL), we'll display the last lines |
|
31 char cleared; // For ex, user has issued a /clear command... |
24
|
32 struct list_head list; |
|
33 } window_entry_t; |
|
34 |
|
35 |
|
36 static WINDOW *rosterWnd, *chatWnd, *inputWnd; |
|
37 static WINDOW *logWnd, *logWnd_border; |
|
38 static PANEL *rosterPanel, *chatPanel, *inputPanel; |
|
39 static PANEL *logPanel, *logPanel_border; |
|
40 static int maxY, maxX; |
|
41 static window_entry_t *currentWindow; |
|
42 |
|
43 static int chatmode; |
30
|
44 int update_roster; |
24
|
45 |
|
46 static char inputLine[INPUTLINE_LENGTH+1]; |
|
47 static char *ptr_inputline; |
|
48 static short int inputline_offset; |
98
|
49 static int completion_started; |
24
|
50 |
|
51 |
99
|
52 /* Functions */ |
24
|
53 |
74
|
54 int scr_WindowWidth(WINDOW * win) |
24
|
55 { |
|
56 int x, y; |
|
57 getmaxyx(win, y, x); |
|
58 return x; |
|
59 } |
|
60 |
74
|
61 void scr_clear_box(WINDOW *win, int y, int x, int height, int width, int Color) |
|
62 { |
|
63 int i, j; |
|
64 |
|
65 wattrset(win, COLOR_PAIR(Color)); |
|
66 for (i = 0; i < height; i++) { |
|
67 wmove(win, y + i, x); |
|
68 for (j = 0; j < width; j++) |
|
69 wprintw(win, " "); |
|
70 } |
|
71 } |
|
72 |
24
|
73 void scr_draw_box(WINDOW * win, int y, int x, int height, int width, |
|
74 int Color, chtype box, chtype border) |
|
75 { |
|
76 int i, j; |
|
77 |
|
78 wattrset(win, COLOR_PAIR(Color)); |
|
79 for (i = 0; i < height; i++) { |
|
80 wmove(win, y + i, x); |
|
81 for (j = 0; j < width; j++) |
|
82 if (!i && !j) |
|
83 waddch(win, border | ACS_ULCORNER); |
|
84 else if (i == height - 1 && !j) |
|
85 waddch(win, border | ACS_LLCORNER); |
|
86 else if (!i && j == width - 1) |
|
87 waddch(win, box | ACS_URCORNER); |
|
88 else if (i == height - 1 && j == width - 1) |
|
89 waddch(win, box | ACS_LRCORNER); |
|
90 else if (!i) |
|
91 waddch(win, border | ACS_HLINE); |
|
92 else if (i == height - 1) |
|
93 waddch(win, box | ACS_HLINE); |
|
94 else if (!j) |
|
95 waddch(win, border | ACS_VLINE); |
|
96 else if (j == width - 1) |
|
97 waddch(win, box | ACS_VLINE); |
|
98 else |
|
99 waddch(win, box | ' '); |
|
100 } |
|
101 } |
|
102 |
|
103 int FindColor(char *name) |
|
104 { |
|
105 if (!strcmp(name, "default")) |
|
106 return -1; |
|
107 if (!strcmp(name, "black")) |
|
108 return COLOR_BLACK; |
|
109 if (!strcmp(name, "red")) |
|
110 return COLOR_RED; |
|
111 if (!strcmp(name, "green")) |
|
112 return COLOR_GREEN; |
|
113 if (!strcmp(name, "yellow")) |
|
114 return COLOR_YELLOW; |
|
115 if (!strcmp(name, "blue")) |
|
116 return COLOR_BLUE; |
|
117 if (!strcmp(name, "magenta")) |
|
118 return COLOR_MAGENTA; |
|
119 if (!strcmp(name, "cyan")) |
|
120 return COLOR_CYAN; |
|
121 if (!strcmp(name, "white")) |
|
122 return COLOR_WHITE; |
|
123 |
|
124 return -1; |
|
125 } |
|
126 |
|
127 void ParseColors(void) |
|
128 { |
|
129 char *colors[11] = { |
|
130 "", "", |
|
131 "borderlines", |
|
132 "jidonlineselected", |
|
133 "jidonline", |
|
134 "jidofflineselected", |
|
135 "jidoffline", |
|
136 "text", |
|
137 NULL |
|
138 }; |
|
139 |
|
140 char *tmp = malloc(1024); |
|
141 char *color1; |
|
142 char *background = cfg_read("color_background"); |
|
143 char *backselected = cfg_read("color_backselected"); |
|
144 int i = 0; |
|
145 |
|
146 while (colors[i]) { |
|
147 sprintf(tmp, "color_%s", colors[i]); |
|
148 color1 = cfg_read(tmp); |
|
149 |
|
150 switch (i + 1) { |
|
151 case 1: |
|
152 init_pair(1, COLOR_BLACK, COLOR_WHITE); |
|
153 break; |
|
154 case 2: |
|
155 init_pair(2, COLOR_WHITE, COLOR_BLACK); |
|
156 break; |
|
157 case 3: |
|
158 init_pair(3, FindColor(color1), FindColor(background)); |
|
159 break; |
|
160 case 4: |
|
161 init_pair(4, FindColor(color1), FindColor(backselected)); |
|
162 break; |
|
163 case 5: |
|
164 init_pair(5, FindColor(color1), FindColor(background)); |
|
165 break; |
|
166 case 6: |
|
167 init_pair(6, FindColor(color1), FindColor(backselected)); |
|
168 break; |
|
169 case 7: |
|
170 init_pair(7, FindColor(color1), FindColor(background)); |
|
171 break; |
|
172 case 8: |
|
173 init_pair(8, FindColor(color1), FindColor(background)); |
|
174 break; |
|
175 } |
|
176 i++; |
|
177 } |
|
178 } |
|
179 |
|
180 |
50
|
181 window_entry_t *scr_CreatePanel(const char *title, int x, int y, |
|
182 int lines, int cols, int dont_show) |
24
|
183 { |
|
184 window_entry_t *tmp = calloc(1, sizeof(window_entry_t)); |
|
185 |
|
186 tmp->win = newwin(lines, cols, y, x); |
|
187 tmp->panel = new_panel(tmp->win); |
|
188 tmp->name = (char *) calloc(1, 1024); |
|
189 strncpy(tmp->name, title, 1024); |
76
|
190 scr_clear_box(tmp->win, 0, 0, lines, cols, COLOR_GENERAL); |
24
|
191 |
|
192 if ((!dont_show)) { |
|
193 currentWindow = tmp; |
|
194 } else { |
|
195 if (currentWindow) |
|
196 top_panel(currentWindow->panel); |
|
197 else |
|
198 top_panel(chatPanel); |
|
199 } |
|
200 |
|
201 list_add_tail(&tmp->list, &window_list); |
|
202 update_panels(); |
|
203 |
|
204 return tmp; |
|
205 } |
|
206 |
50
|
207 window_entry_t *scr_SearchWindow(const char *winId) |
24
|
208 { |
|
209 struct list_head *pos, *n; |
|
210 window_entry_t *search_entry = NULL; |
|
211 |
|
212 list_for_each_safe(pos, n, &window_list) { |
|
213 search_entry = window_entry(pos); |
|
214 if (search_entry->name) { |
|
215 if (!strcasecmp(search_entry->name, winId)) { |
|
216 return search_entry; |
|
217 } |
|
218 } |
|
219 } |
|
220 return NULL; |
|
221 } |
|
222 |
74
|
223 void scr_UpdateWindow(window_entry_t *win_entry) |
|
224 { |
|
225 int n; |
|
226 int width; |
|
227 char **lines; |
|
228 GList *hbuf_head; |
|
229 |
108
|
230 width = scr_WindowWidth(win_entry->win); |
|
231 |
|
232 // Should the window be empty? |
|
233 if (win_entry->cleared) { |
|
234 scr_clear_box(win_entry->win, 0, 0, CHAT_WIN_HEIGHT, width, COLOR_GENERAL); |
|
235 return; |
|
236 } |
|
237 |
105
|
238 // win_entry->top is the top message of the screen. If it set to NULL, we |
|
239 // are displaying the last messages. |
|
240 |
74
|
241 // We will show the last CHAT_WIN_HEIGHT lines. |
|
242 // Let's find out where it begins. |
105
|
243 if (!win_entry->top || |
|
244 (g_list_position(g_list_first(win_entry->hbuf), win_entry->top) == -1)) { |
|
245 // Move up CHAT_WIN_HEIGHT lines |
|
246 win_entry->hbuf = g_list_last(win_entry->hbuf); |
|
247 hbuf_head = win_entry->hbuf; |
|
248 win_entry->top = NULL; // (Just to make sure) |
|
249 n = 0; |
|
250 while (hbuf_head && (n < CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head)) { |
|
251 hbuf_head = g_list_previous(hbuf_head); |
|
252 n++; |
|
253 } |
|
254 } else |
|
255 hbuf_head = win_entry->top; |
74
|
256 |
|
257 // Get the last CHAT_WIN_HEIGHT lines. |
|
258 lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT); |
|
259 |
|
260 // Display these lines |
|
261 wmove(win_entry->win, 0, 0); |
|
262 for (n = 0; n < CHAT_WIN_HEIGHT; n++) { |
|
263 int r = width; |
75
|
264 if (*(lines+2*n)) { |
|
265 if (**(lines+2*n)) |
|
266 wprintw(win_entry->win, "%s", *(lines+2*n)); // prefix |
|
267 else { |
|
268 wprintw(win_entry->win, " "); |
|
269 r -= 12; |
|
270 } |
|
271 wprintw(win_entry->win, "%s", *(lines+2*n+1)); // line |
|
272 // Calculate the number of blank characters to empty the line |
|
273 r -= strlen(*(lines+2*n)) + strlen(*(lines+2*n+1)); |
|
274 } |
74
|
275 for ( ; r>0 ; r--) { |
|
276 wprintw(win_entry->win, " "); |
|
277 } |
|
278 //// wclrtoeol(win_entry->win); does not work :( |
|
279 } |
|
280 g_free(lines); |
|
281 } |
|
282 |
50
|
283 void scr_ShowWindow(const char *winId) |
24
|
284 { |
74
|
285 window_entry_t *win_entry = scr_SearchWindow(winId); |
|
286 |
|
287 if (win_entry != NULL) { |
|
288 top_panel(win_entry->panel); |
|
289 currentWindow = win_entry; |
24
|
290 chatmode = TRUE; |
81
|
291 roster_setflags(winId, ROSTER_FLAG_MSG, FALSE); |
30
|
292 update_roster = TRUE; |
74
|
293 |
105
|
294 // Refresh the window |
74
|
295 scr_UpdateWindow(win_entry); |
|
296 |
|
297 // Finished :) |
24
|
298 update_panels(); |
|
299 doupdate(); |
|
300 } else { |
|
301 top_panel(chatPanel); |
74
|
302 currentWindow = win_entry; // == NULL (current window empty) |
24
|
303 } |
|
304 } |
|
305 |
|
306 void scr_ShowBuddyWindow(void) |
|
307 { |
105
|
308 const gchar *jid; |
|
309 if (!current_buddy) |
|
310 return; |
|
311 jid = CURRENT_JID; |
|
312 if (!jid) |
|
313 return; |
|
314 scr_ShowWindow(jid); |
24
|
315 top_panel(inputPanel); |
|
316 } |
|
317 |
|
318 |
75
|
319 void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp, |
|
320 const char *prefix, int force_show) |
24
|
321 { |
75
|
322 char *fullprefix = NULL; |
74
|
323 window_entry_t *win_entry; |
24
|
324 int dont_show = FALSE; |
|
325 |
75
|
326 // Prepare the prefix |
|
327 if (prefix || TimeStamp) { |
|
328 if (!prefix) prefix = ""; |
|
329 fullprefix = calloc(1, strlen(prefix)+16); |
|
330 if (TimeStamp) { |
|
331 time_t now = time(NULL); |
|
332 strftime(fullprefix, 12, "[%H:%M] ", localtime(&now)); |
|
333 } else { |
|
334 strcpy(fullprefix, " "); |
|
335 } |
|
336 strcat(fullprefix, prefix); |
74
|
337 } |
|
338 |
|
339 // Look for the window entry. |
|
340 win_entry = scr_SearchWindow(winId); |
|
341 |
|
342 // Do we have to really show the window? |
24
|
343 if (!chatmode) |
|
344 dont_show = TRUE; |
74
|
345 else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry)))) |
24
|
346 dont_show = TRUE; |
|
347 |
74
|
348 // If the window entry doesn't exist yet, let's create it. |
|
349 if (win_entry == NULL) { |
|
350 win_entry = scr_CreatePanel(winId, ROSTER_WIDTH, 0, CHAT_WIN_HEIGHT, |
|
351 maxX - ROSTER_WIDTH, dont_show); |
24
|
352 } |
|
353 |
75
|
354 hbuf_add_line(&win_entry->hbuf, text, fullprefix, |
74
|
355 maxX - scr_WindowWidth(rosterWnd) - 14); |
75
|
356 free(fullprefix); |
74
|
357 |
108
|
358 if (win_entry->cleared) { |
|
359 win_entry->cleared = 0; // The message must be displayed |
|
360 win_entry->top = g_list_last(win_entry->hbuf); |
|
361 } |
|
362 |
24
|
363 if (!dont_show) { |
74
|
364 // Show and refresh the window |
|
365 top_panel(win_entry->panel); |
|
366 scr_UpdateWindow(win_entry); |
24
|
367 update_panels(); |
|
368 doupdate(); |
|
369 } else { |
81
|
370 roster_setflags(winId, ROSTER_FLAG_MSG, TRUE); |
30
|
371 update_roster = TRUE; |
24
|
372 } |
|
373 } |
|
374 |
|
375 void scr_InitCurses(void) |
|
376 { |
|
377 initscr(); |
|
378 noecho(); |
|
379 raw(); |
35
|
380 halfdelay(5); |
24
|
381 start_color(); |
|
382 use_default_colors(); |
|
383 |
|
384 ParseColors(); |
|
385 |
|
386 getmaxyx(stdscr, maxY, maxX); |
|
387 inputLine[0] = 0; |
|
388 ptr_inputline = inputLine; |
|
389 |
35
|
390 setlocale(LC_CTYPE, ""); |
24
|
391 |
|
392 return; |
|
393 } |
|
394 |
81
|
395 void scr_TerminateCurses(void) |
|
396 { |
|
397 clear(); |
|
398 refresh(); |
|
399 endwin(); |
|
400 return; |
|
401 } |
|
402 |
24
|
403 void scr_DrawMainWindow(void) |
|
404 { |
74
|
405 int l; |
|
406 |
24
|
407 /* Draw main panels */ |
74
|
408 rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0); |
24
|
409 rosterPanel = new_panel(rosterWnd); |
74
|
410 scr_clear_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, ROSTER_WIDTH, |
|
411 COLOR_GENERAL); |
|
412 for (l=0 ; l < CHAT_WIN_HEIGHT ; l++) |
|
413 mvwaddch(rosterWnd, l, ROSTER_WIDTH-1, ACS_VLINE); |
24
|
414 |
74
|
415 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH); |
24
|
416 chatPanel = new_panel(chatWnd); |
74
|
417 scr_clear_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, |
|
418 COLOR_GENERAL); |
|
419 scrollok(chatWnd, TRUE); |
|
420 mvwprintw(chatWnd, 0, 0, "This is the status window"); |
24
|
421 |
|
422 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0); |
|
423 logPanel_border = new_panel(logWnd_border); |
|
424 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0); |
|
425 logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1); |
|
426 logPanel = new_panel(logWnd); |
|
427 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
428 |
74
|
429 scrollok(logWnd, TRUE); |
24
|
430 |
|
431 inputWnd = newwin(1, maxX, maxY-1, 0); |
|
432 inputPanel = new_panel(inputWnd); |
|
433 |
81
|
434 scr_DrawRoster(); |
24
|
435 update_panels(); |
|
436 doupdate(); |
|
437 return; |
|
438 } |
|
439 |
81
|
440 void scr_DrawRoster(void) |
24
|
441 { |
81
|
442 static guint offset = 0; |
|
443 char name[ROSTER_WIDTH]; |
|
444 int maxx, maxy; |
|
445 GList *buddy; |
|
446 int i, n; |
|
447 int rOffset; |
|
448 |
|
449 getmaxyx(rosterWnd, maxy, maxx); |
|
450 maxx --; // last char is for vertical border |
|
451 name[ROSTER_WIDTH-7] = 0; |
|
452 |
|
453 // cleanup of roster window |
|
454 wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
455 for (i = 0; i < maxy; i++) { |
|
456 mvwprintw(rosterWnd, i, 0, ""); |
|
457 for (n = 0; n < maxx; n++) |
|
458 waddch(rosterWnd, ' '); |
|
459 } |
|
460 |
|
461 // Leave now if buddylist is empty |
|
462 if (!buddylist) { |
|
463 offset = 0; |
|
464 return; |
|
465 } |
|
466 |
84
|
467 // Update offset if necessary |
|
468 i = g_list_position(buddylist, current_buddy); |
|
469 if (i == -1) { // This is bad |
|
470 scr_LogPrint("Doh! Can't find current selected buddy!!"); |
|
471 return; |
|
472 } else if (i < offset) { |
|
473 offset = i; |
|
474 } else if (i+1 > offset + maxy) { |
|
475 offset = i + 1 - maxy; |
|
476 } |
81
|
477 |
|
478 buddy = buddylist; |
|
479 rOffset = offset; |
|
480 |
84
|
481 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) { |
81
|
482 |
|
483 char status = '?'; |
|
484 char pending = ' '; |
|
485 enum imstatus budstate; |
|
486 |
|
487 if (rOffset > 0) { |
|
488 rOffset--; |
|
489 continue; |
|
490 } |
|
491 |
|
492 if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) { |
|
493 pending = '#'; |
|
494 } |
|
495 |
|
496 budstate = buddy_getstatus(BUDDATA(buddy)); |
|
497 if (budstate >= 0 && budstate < imstatus_size) |
|
498 status = imstatus2char[budstate]; |
|
499 if (buddy == current_buddy) { |
|
500 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL)); |
|
501 // The 3 following lines aim to color the whole line |
|
502 wmove(rosterWnd, i, 0); |
|
503 for (n = 0; n < maxx; n++) |
|
504 waddch(rosterWnd, ' '); |
|
505 } else { |
|
506 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES)); |
|
507 } |
|
508 |
|
509 strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7); |
|
510 // TODO: status is meaningless for groups: |
|
511 if (buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP) status='G'; |
|
512 mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name); |
84
|
513 |
|
514 i++; |
81
|
515 } |
|
516 |
|
517 update_panels(); |
|
518 doupdate(); |
|
519 update_roster = FALSE; |
24
|
520 } |
|
521 |
50
|
522 void scr_WriteMessage(const char *jid, const char *text, char *prefix) |
24
|
523 { |
75
|
524 scr_WriteInWindow(jid, text, TRUE, prefix, FALSE); |
47
|
525 } |
|
526 |
50
|
527 void scr_WriteIncomingMessage(const char *jidfrom, const char *text) |
47
|
528 { |
|
529 char *buffer = utf8_decode(text); |
75
|
530 // FIXME expand tabs / filter out special chars... |
47
|
531 scr_WriteMessage(jidfrom, buffer, "<== "); |
|
532 free(buffer); |
65
|
533 top_panel(inputPanel); |
24
|
534 update_panels(); |
|
535 doupdate(); |
|
536 } |
|
537 |
50
|
538 void scr_WriteOutgoingMessage(const char *jidto, const char *text) |
47
|
539 { |
65
|
540 scr_WriteMessage(jidto, text, "--> "); |
47
|
541 scr_ShowWindow(jidto); |
65
|
542 top_panel(inputPanel); |
47
|
543 } |
|
544 |
24
|
545 int scr_Getch(void) |
|
546 { |
|
547 int ch; |
|
548 // keypad(inputWnd, TRUE); |
|
549 ch = wgetch(inputWnd); |
|
550 return ch; |
|
551 } |
|
552 |
|
553 WINDOW *scr_GetRosterWindow(void) |
|
554 { |
|
555 return rosterWnd; |
|
556 } |
|
557 |
|
558 WINDOW *scr_GetStatusWindow(void) |
|
559 { |
|
560 return chatWnd; |
|
561 } |
|
562 |
|
563 WINDOW *scr_GetInputWindow(void) |
|
564 { |
|
565 return inputWnd; |
|
566 } |
|
567 |
105
|
568 void scr_RosterTop(void) |
104
|
569 { |
|
570 current_buddy = buddylist; |
|
571 // XXX We should rebuild the buddylist but perhaps not everytime? |
|
572 if (chatmode) |
|
573 scr_ShowBuddyWindow(); |
|
574 } |
|
575 |
105
|
576 void scr_RosterBottom(void) |
104
|
577 { |
|
578 current_buddy = g_list_last(buddylist); |
|
579 // XXX We should rebuild the buddylist but perhaps not everytime? |
|
580 if (chatmode) |
|
581 scr_ShowBuddyWindow(); |
|
582 } |
|
583 |
105
|
584 void scr_RosterUp(void) |
81
|
585 { |
|
586 if (current_buddy) { |
|
587 if (g_list_previous(current_buddy)) { |
|
588 current_buddy = g_list_previous(current_buddy); |
|
589 scr_DrawRoster(); |
|
590 } |
|
591 } |
|
592 // XXX We should rebuild the buddylist but perhaps not everytime? |
104
|
593 |
|
594 if (chatmode) |
|
595 scr_ShowBuddyWindow(); |
81
|
596 } |
|
597 |
105
|
598 void scr_RosterDown(void) |
81
|
599 { |
|
600 if (current_buddy) { |
|
601 if (g_list_next(current_buddy)) { |
|
602 current_buddy = g_list_next(current_buddy); |
|
603 scr_DrawRoster(); |
|
604 } |
|
605 } |
|
606 // XXX We should rebuild the buddylist but perhaps not everytime? |
104
|
607 |
|
608 if (chatmode) |
|
609 scr_ShowBuddyWindow(); |
81
|
610 } |
|
611 |
105
|
612 void scr_ScrollUp(void) |
|
613 { |
|
614 const gchar *jid; |
|
615 window_entry_t *win_entry; |
|
616 int n, nblines; |
|
617 GList *hbuf_top; |
|
618 |
|
619 // Get win_entry |
|
620 if (!current_buddy) |
|
621 return; |
|
622 jid = CURRENT_JID; |
|
623 if (!jid) |
|
624 return; |
|
625 win_entry = scr_SearchWindow(jid); |
|
626 if (!win_entry) |
|
627 return; |
|
628 |
|
629 // Scroll up half a screen (or less) |
|
630 nblines = CHAT_WIN_HEIGHT/2-1; |
|
631 hbuf_top = win_entry->top; |
|
632 if (!hbuf_top) { |
|
633 hbuf_top = g_list_last(win_entry->hbuf); |
|
634 nblines *= 3; |
|
635 } |
|
636 |
|
637 n = 0; |
|
638 while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) { |
|
639 hbuf_top = g_list_previous(hbuf_top); |
|
640 n++; |
|
641 } |
|
642 win_entry->top = hbuf_top; |
|
643 |
|
644 // Refresh the window |
|
645 scr_UpdateWindow(win_entry); |
|
646 |
|
647 // Finished :) |
|
648 update_panels(); |
|
649 doupdate(); |
|
650 } |
|
651 |
|
652 void scr_ScrollDown(void) |
|
653 { |
|
654 const gchar *jid; |
|
655 window_entry_t *win_entry; |
|
656 int n, nblines; |
|
657 GList *hbuf_top; |
|
658 |
|
659 // Get win_entry |
|
660 if (!current_buddy) |
|
661 return; |
|
662 jid = CURRENT_JID; |
|
663 if (!jid) |
|
664 return; |
|
665 win_entry = scr_SearchWindow(jid); |
|
666 if (!win_entry) |
|
667 return; |
|
668 |
|
669 // Scroll down half a screen (or less) |
|
670 nblines = CHAT_WIN_HEIGHT/2-1; |
|
671 hbuf_top = win_entry->top; |
|
672 |
|
673 for (n=0 ; hbuf_top && n < nblines ; n++) |
|
674 hbuf_top = g_list_next(hbuf_top); |
|
675 win_entry->top = hbuf_top; |
|
676 // Check if we are at the bottom |
|
677 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++) |
|
678 hbuf_top = g_list_next(hbuf_top); |
|
679 if (!hbuf_top) |
|
680 win_entry->top = NULL; // End reached |
|
681 |
|
682 // Refresh the window |
|
683 scr_UpdateWindow(win_entry); |
|
684 |
|
685 // Finished :) |
|
686 update_panels(); |
|
687 doupdate(); |
|
688 } |
|
689 |
108
|
690 void scr_Clear(void) |
|
691 { |
|
692 const gchar *jid; |
|
693 window_entry_t *win_entry; |
|
694 |
|
695 // Get win_entry |
|
696 if (!current_buddy) |
|
697 return; |
|
698 jid = CURRENT_JID; |
|
699 if (!jid) |
|
700 return; |
|
701 win_entry = scr_SearchWindow(jid); |
|
702 if (!win_entry) |
|
703 return; |
|
704 |
|
705 win_entry->cleared = TRUE; |
|
706 |
|
707 // Refresh the window |
|
708 scr_UpdateWindow(win_entry); |
|
709 |
|
710 // Finished :) |
|
711 update_panels(); |
|
712 doupdate(); |
|
713 } |
|
714 |
44
|
715 // scr_LogPrint(...) |
|
716 // Display a message in the log window. |
24
|
717 void scr_LogPrint(const char *fmt, ...) |
|
718 { |
|
719 time_t timestamp; |
|
720 char *buffer; |
|
721 va_list ap; |
|
722 |
|
723 buffer = (char *) calloc(1, 4096); |
|
724 |
|
725 timestamp = time(NULL); |
|
726 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
727 wprintw(logWnd, "\n%s", buffer); |
|
728 |
|
729 va_start(ap, fmt); |
|
730 vsnprintf(buffer, 4096, fmt, ap); |
|
731 va_end(ap); |
|
732 |
|
733 wprintw(logWnd, "%s", buffer); |
|
734 free(buffer); |
|
735 |
|
736 update_panels(); |
|
737 doupdate(); |
|
738 } |
|
739 |
98
|
740 // which_row() |
|
741 // Tells which row our cursor is in, in the command line. |
|
742 // -1 -> normal text |
|
743 // 0 -> command |
|
744 // 1 -> parameter 1 (etc.) |
102
|
745 // If > 0, then *p_row is set to the beginning of the row |
|
746 int which_row(char **p_row) |
98
|
747 { |
|
748 int row = -1; |
|
749 char *p; |
|
750 int quote = FALSE; |
|
751 |
|
752 // Not a command? |
|
753 if ((ptr_inputline == inputLine) || (inputLine[0] != '/')) |
|
754 return -1; |
|
755 |
|
756 // This is a command |
|
757 row = 0; |
|
758 for (p = inputLine ; p < ptr_inputline ; p++) { |
|
759 if (quote) { |
|
760 if (*p == '"' && *(p-1) != '\\') |
|
761 quote = FALSE; |
|
762 continue; |
|
763 } |
|
764 if (*p == '"' && *(p-1) != '\\') { |
|
765 quote = TRUE; |
102
|
766 } else if (*p == ' ' && *(p-1) != ' ') { |
98
|
767 row++; |
102
|
768 *p_row = p+1; |
|
769 } |
98
|
770 } |
|
771 return row; |
|
772 } |
|
773 |
|
774 void scr_insert_text(const char *text) |
|
775 { |
|
776 char tmpLine[INPUTLINE_LENGTH+1]; |
|
777 int len = strlen(text); |
|
778 // Check the line isn't too long |
|
779 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { |
|
780 scr_LogPrint("Cannot insert text, line too long."); |
|
781 return; |
|
782 } |
|
783 |
|
784 strcpy(tmpLine, ptr_inputline); |
|
785 strcpy(ptr_inputline, text); ptr_inputline += len; |
|
786 strcpy(ptr_inputline, tmpLine); |
|
787 } |
|
788 |
|
789 void scr_handle_tab(void) |
|
790 { |
102
|
791 int nrow; |
|
792 char *row; |
|
793 const char *cchar; |
103
|
794 guint compl_categ; |
98
|
795 |
102
|
796 nrow = which_row(&row); |
98
|
797 |
103
|
798 // a) No completion if no leading slash ('cause not a command) |
|
799 // b) We can't have more than 2 parameters (we use 2 flags) |
|
800 if (nrow < 0 || nrow > 2) return; |
102
|
801 |
103
|
802 if (nrow == 0) { // Command completion |
|
803 row = &inputLine[1]; |
|
804 compl_categ = COMPL_CMD; |
|
805 } else { // Other completion, depending on the command |
|
806 cmd *com = cmd_get(inputLine); |
|
807 if (!com || !row) { |
|
808 scr_LogPrint("I cannot complete that..."); |
|
809 return; |
|
810 } |
|
811 compl_categ = com->completion_flags[nrow-1]; |
|
812 } |
|
813 |
|
814 if (!completion_started) { |
|
815 GSList *list = compl_get_category_list(compl_categ); |
|
816 if (list) { |
|
817 char *prefix = g_strndup(row, ptr_inputline-row); |
|
818 // Init completion |
|
819 new_completion(prefix, list); |
|
820 g_free(prefix); |
|
821 // Now complete |
98
|
822 cchar = complete(); |
|
823 if (cchar) |
|
824 scr_insert_text(cchar); |
103
|
825 completion_started = TRUE; |
98
|
826 } |
103
|
827 } else { // Completion already initialized |
|
828 char *c; |
|
829 guint back = cancel_completion(); |
|
830 // Remove $back chars |
|
831 ptr_inputline -= back; |
|
832 c = ptr_inputline; |
|
833 for ( ; *c ; c++) |
|
834 *c = *(c+back); |
|
835 // Now complete again |
|
836 cchar = complete(); |
|
837 if (cchar) |
|
838 scr_insert_text(cchar); |
102
|
839 } |
98
|
840 } |
|
841 |
|
842 void scr_cancel_current_completion(void) |
|
843 { |
|
844 char *c; |
|
845 guint back = cancel_completion(); |
|
846 // Remove $back chars |
|
847 ptr_inputline -= back; |
|
848 c = ptr_inputline; |
|
849 for ( ; *c ; c++) |
|
850 *c = *(c+back); |
|
851 } |
|
852 |
|
853 void scr_end_current_completion(void) |
|
854 { |
|
855 done_completion(); |
|
856 completion_started = FALSE; |
|
857 } |
|
858 |
24
|
859 // check_offset(int direction) |
|
860 // Check inputline_offset value, and make sure the cursor is inside the |
|
861 // screen. |
|
862 inline void check_offset(int direction) |
|
863 { |
|
864 // Left side |
|
865 if (inputline_offset && direction <= 0) { |
|
866 while (ptr_inputline <= (char*)&inputLine + inputline_offset) { |
|
867 if (inputline_offset) { |
|
868 inputline_offset -= 5; |
|
869 if (inputline_offset < 0) |
|
870 inputline_offset = 0; |
|
871 } |
|
872 } |
|
873 } |
|
874 // Right side |
|
875 if (direction >= 0) { |
|
876 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) |
|
877 inputline_offset += 5; |
|
878 } |
|
879 } |
|
880 |
44
|
881 // process_key(key) |
|
882 // Handle the pressed key, in the command line (bottom). |
29
|
883 int process_key(int key) |
24
|
884 { |
|
885 if (isprint(key)) { |
|
886 char tmpLine[INPUTLINE_LENGTH+1]; |
|
887 |
|
888 // Check the line isn't too long |
|
889 if (strlen(inputLine) >= INPUTLINE_LENGTH) |
|
890 return 0; |
|
891 |
|
892 // Insert char |
|
893 strcpy(tmpLine, ptr_inputline); |
|
894 *ptr_inputline++ = key; |
|
895 strcpy(ptr_inputline, tmpLine); |
|
896 check_offset(1); |
|
897 } else { |
|
898 switch(key) { |
|
899 case KEY_BACKSPACE: |
|
900 if (ptr_inputline != (char*)&inputLine) { |
42
|
901 char *c = --ptr_inputline; |
|
902 for ( ; *c ; c++) |
|
903 *c = *(c+1); |
24
|
904 check_offset(-1); |
|
905 } |
|
906 break; |
|
907 case KEY_DC: |
|
908 if (*ptr_inputline) |
|
909 strcpy(ptr_inputline, ptr_inputline+1); |
|
910 break; |
|
911 case KEY_LEFT: |
|
912 if (ptr_inputline != (char*)&inputLine) { |
|
913 ptr_inputline--; |
|
914 check_offset(-1); |
|
915 } |
|
916 break; |
|
917 case KEY_RIGHT: |
|
918 if (*ptr_inputline) |
|
919 ptr_inputline++; |
|
920 check_offset(1); |
|
921 break; |
98
|
922 case 7: // Ctrl-g |
|
923 scr_cancel_current_completion(); |
|
924 scr_end_current_completion(); |
|
925 check_offset(-1); |
|
926 break; |
24
|
927 case 9: // Tab |
98
|
928 scr_handle_tab(); |
|
929 check_offset(0); |
24
|
930 break; |
|
931 case '\n': // Enter |
|
932 chatmode = TRUE; |
|
933 if (inputLine[0] == 0) { |
|
934 scr_ShowBuddyWindow(); |
|
935 break; |
|
936 } |
29
|
937 if (process_line(inputLine)) |
24
|
938 return 255; |
|
939 ptr_inputline = inputLine; |
|
940 *ptr_inputline = 0; |
|
941 inputline_offset = 0; |
|
942 break; |
|
943 case KEY_UP: |
81
|
944 scr_RosterUp(); |
24
|
945 break; |
|
946 case KEY_DOWN: |
81
|
947 scr_RosterDown(); |
24
|
948 break; |
|
949 case KEY_PPAGE: |
105
|
950 scr_ScrollUp(); |
24
|
951 break; |
|
952 case KEY_NPAGE: |
105
|
953 scr_ScrollDown(); |
24
|
954 break; |
|
955 case KEY_HOME: |
|
956 case 1: |
|
957 ptr_inputline = inputLine; |
|
958 inputline_offset = 0; |
|
959 break; |
|
960 case KEY_END: |
|
961 case 5: |
|
962 for (; *ptr_inputline; ptr_inputline++) ; |
|
963 check_offset(1); |
|
964 break; |
|
965 case 21: // Ctrl-u |
|
966 strcpy(inputLine, ptr_inputline); |
|
967 ptr_inputline = inputLine; |
|
968 inputline_offset = 0; |
|
969 break; |
|
970 case KEY_EOL: |
|
971 case 11: // Ctrl-k |
|
972 *ptr_inputline = 0; |
|
973 break; |
|
974 case 16: // Ctrl-p |
|
975 scr_LogPrint("Ctrl-p not yet implemented"); |
|
976 break; |
|
977 case 14: // Ctrl-n |
|
978 scr_LogPrint("Ctrl-n not yet implemented"); |
|
979 break; |
|
980 case 27: // ESC |
|
981 currentWindow = NULL; |
|
982 chatmode = FALSE; |
|
983 top_panel(chatPanel); |
|
984 top_panel(inputPanel); |
|
985 break; |
|
986 default: |
|
987 scr_LogPrint("Unkown key=%d", key); |
|
988 } |
|
989 } |
98
|
990 if (completion_started && key != 9) |
|
991 scr_end_current_completion(); |
24
|
992 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
|
993 wclrtoeol(inputWnd); |
|
994 if (*ptr_inputline) { |
|
995 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
|
996 } |
|
997 update_panels(); |
|
998 doupdate(); |
|
999 return 0; |
|
1000 } |