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