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 "jidonline", |
139
|
131 "newmsg", |
24
|
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 } |
142
|
303 |
|
304 top_panel(inputPanel); |
24
|
305 } |
|
306 |
|
307 void scr_ShowBuddyWindow(void) |
|
308 { |
105
|
309 const gchar *jid; |
140
|
310 |
105
|
311 if (!current_buddy) |
140
|
312 jid = NULL; |
|
313 else |
|
314 jid = CURRENT_JID; |
|
315 |
|
316 if (!jid) { |
|
317 top_panel(chatPanel); |
|
318 currentWindow = NULL; |
105
|
319 return; |
140
|
320 } |
|
321 |
105
|
322 scr_ShowWindow(jid); |
24
|
323 } |
|
324 |
|
325 |
75
|
326 void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp, |
|
327 const char *prefix, int force_show) |
24
|
328 { |
75
|
329 char *fullprefix = NULL; |
74
|
330 window_entry_t *win_entry; |
24
|
331 int dont_show = FALSE; |
|
332 |
75
|
333 // Prepare the prefix |
|
334 if (prefix || TimeStamp) { |
|
335 if (!prefix) prefix = ""; |
|
336 fullprefix = calloc(1, strlen(prefix)+16); |
|
337 if (TimeStamp) { |
|
338 time_t now = time(NULL); |
|
339 strftime(fullprefix, 12, "[%H:%M] ", localtime(&now)); |
|
340 } else { |
|
341 strcpy(fullprefix, " "); |
|
342 } |
|
343 strcat(fullprefix, prefix); |
74
|
344 } |
|
345 |
|
346 // Look for the window entry. |
|
347 win_entry = scr_SearchWindow(winId); |
|
348 |
|
349 // Do we have to really show the window? |
24
|
350 if (!chatmode) |
|
351 dont_show = TRUE; |
74
|
352 else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry)))) |
24
|
353 dont_show = TRUE; |
|
354 |
74
|
355 // If the window entry doesn't exist yet, let's create it. |
|
356 if (win_entry == NULL) { |
|
357 win_entry = scr_CreatePanel(winId, ROSTER_WIDTH, 0, CHAT_WIN_HEIGHT, |
|
358 maxX - ROSTER_WIDTH, dont_show); |
24
|
359 } |
|
360 |
75
|
361 hbuf_add_line(&win_entry->hbuf, text, fullprefix, |
74
|
362 maxX - scr_WindowWidth(rosterWnd) - 14); |
75
|
363 free(fullprefix); |
74
|
364 |
108
|
365 if (win_entry->cleared) { |
|
366 win_entry->cleared = 0; // The message must be displayed |
|
367 win_entry->top = g_list_last(win_entry->hbuf); |
|
368 } |
|
369 |
24
|
370 if (!dont_show) { |
74
|
371 // Show and refresh the window |
|
372 top_panel(win_entry->panel); |
|
373 scr_UpdateWindow(win_entry); |
142
|
374 top_panel(inputPanel); |
24
|
375 update_panels(); |
|
376 doupdate(); |
|
377 } else { |
81
|
378 roster_setflags(winId, ROSTER_FLAG_MSG, TRUE); |
30
|
379 update_roster = TRUE; |
24
|
380 } |
|
381 } |
|
382 |
|
383 void scr_InitCurses(void) |
|
384 { |
|
385 initscr(); |
|
386 noecho(); |
|
387 raw(); |
35
|
388 halfdelay(5); |
24
|
389 start_color(); |
|
390 use_default_colors(); |
|
391 |
|
392 ParseColors(); |
|
393 |
|
394 getmaxyx(stdscr, maxY, maxX); |
|
395 inputLine[0] = 0; |
|
396 ptr_inputline = inputLine; |
|
397 |
35
|
398 setlocale(LC_CTYPE, ""); |
24
|
399 |
|
400 return; |
|
401 } |
|
402 |
81
|
403 void scr_TerminateCurses(void) |
|
404 { |
|
405 clear(); |
|
406 refresh(); |
|
407 endwin(); |
|
408 return; |
|
409 } |
|
410 |
24
|
411 void scr_DrawMainWindow(void) |
|
412 { |
74
|
413 int l; |
|
414 |
24
|
415 /* Draw main panels */ |
74
|
416 rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0); |
24
|
417 rosterPanel = new_panel(rosterWnd); |
74
|
418 scr_clear_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, ROSTER_WIDTH, |
|
419 COLOR_GENERAL); |
|
420 for (l=0 ; l < CHAT_WIN_HEIGHT ; l++) |
|
421 mvwaddch(rosterWnd, l, ROSTER_WIDTH-1, ACS_VLINE); |
24
|
422 |
74
|
423 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH); |
24
|
424 chatPanel = new_panel(chatWnd); |
74
|
425 scr_clear_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, |
|
426 COLOR_GENERAL); |
|
427 scrollok(chatWnd, TRUE); |
|
428 mvwprintw(chatWnd, 0, 0, "This is the status window"); |
24
|
429 |
|
430 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0); |
|
431 logPanel_border = new_panel(logWnd_border); |
|
432 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0); |
|
433 logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1); |
|
434 logPanel = new_panel(logWnd); |
|
435 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
436 |
74
|
437 scrollok(logWnd, TRUE); |
24
|
438 |
|
439 inputWnd = newwin(1, maxX, maxY-1, 0); |
|
440 inputPanel = new_panel(inputWnd); |
|
441 |
81
|
442 scr_DrawRoster(); |
24
|
443 return; |
|
444 } |
|
445 |
81
|
446 void scr_DrawRoster(void) |
24
|
447 { |
81
|
448 static guint offset = 0; |
|
449 char name[ROSTER_WIDTH]; |
|
450 int maxx, maxy; |
|
451 GList *buddy; |
|
452 int i, n; |
|
453 int rOffset; |
|
454 |
123
|
455 // We can reset update_roster |
|
456 update_roster = FALSE; |
|
457 |
81
|
458 getmaxyx(rosterWnd, maxy, maxx); |
|
459 maxx --; // last char is for vertical border |
|
460 name[ROSTER_WIDTH-7] = 0; |
|
461 |
|
462 // cleanup of roster window |
|
463 wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
464 for (i = 0; i < maxy; i++) { |
|
465 mvwprintw(rosterWnd, i, 0, ""); |
|
466 for (n = 0; n < maxx; n++) |
|
467 waddch(rosterWnd, ' '); |
|
468 } |
|
469 |
|
470 // Leave now if buddylist is empty |
|
471 if (!buddylist) { |
|
472 offset = 0; |
123
|
473 update_panels(); |
|
474 doupdate(); |
81
|
475 return; |
|
476 } |
|
477 |
84
|
478 // Update offset if necessary |
|
479 i = g_list_position(buddylist, current_buddy); |
|
480 if (i == -1) { // This is bad |
|
481 scr_LogPrint("Doh! Can't find current selected buddy!!"); |
123
|
482 update_panels(); |
|
483 doupdate(); |
84
|
484 return; |
|
485 } else if (i < offset) { |
|
486 offset = i; |
|
487 } else if (i+1 > offset + maxy) { |
|
488 offset = i + 1 - maxy; |
|
489 } |
81
|
490 |
|
491 buddy = buddylist; |
|
492 rOffset = offset; |
|
493 |
84
|
494 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) { |
81
|
495 |
|
496 char status = '?'; |
|
497 char pending = ' '; |
|
498 enum imstatus budstate; |
|
499 |
|
500 if (rOffset > 0) { |
|
501 rOffset--; |
|
502 continue; |
|
503 } |
|
504 |
|
505 if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) { |
|
506 pending = '#'; |
|
507 } |
|
508 |
|
509 budstate = buddy_getstatus(BUDDATA(buddy)); |
|
510 if (budstate >= 0 && budstate < imstatus_size) |
|
511 status = imstatus2char[budstate]; |
|
512 if (buddy == current_buddy) { |
|
513 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL)); |
|
514 // The 3 following lines aim to color the whole line |
|
515 wmove(rosterWnd, i, 0); |
|
516 for (n = 0; n < maxx; n++) |
|
517 waddch(rosterWnd, ' '); |
|
518 } else { |
139
|
519 if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) |
|
520 wattrset(rosterWnd, COLOR_PAIR(COLOR_NMSG)); |
|
521 else |
|
522 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES)); |
81
|
523 } |
|
524 |
|
525 strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7); |
133
|
526 if (buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP) { |
|
527 char *sep; |
|
528 if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_HIDE) |
|
529 sep = "+++"; |
|
530 else |
|
531 sep = "---"; |
|
532 mvwprintw(rosterWnd, i, 0, " %c%s %s", pending, sep, name); |
|
533 } |
126
|
534 else |
|
535 mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name); |
84
|
536 |
|
537 i++; |
81
|
538 } |
|
539 |
142
|
540 top_panel(inputPanel); |
81
|
541 update_panels(); |
|
542 doupdate(); |
24
|
543 } |
|
544 |
50
|
545 void scr_WriteMessage(const char *jid, const char *text, char *prefix) |
24
|
546 { |
75
|
547 scr_WriteInWindow(jid, text, TRUE, prefix, FALSE); |
47
|
548 } |
|
549 |
50
|
550 void scr_WriteIncomingMessage(const char *jidfrom, const char *text) |
47
|
551 { |
75
|
552 // FIXME expand tabs / filter out special chars... |
115
|
553 scr_WriteMessage(jidfrom, text, "<== "); |
24
|
554 update_panels(); |
|
555 doupdate(); |
|
556 } |
|
557 |
50
|
558 void scr_WriteOutgoingMessage(const char *jidto, const char *text) |
47
|
559 { |
65
|
560 scr_WriteMessage(jidto, text, "--> "); |
47
|
561 scr_ShowWindow(jidto); |
|
562 } |
|
563 |
24
|
564 int scr_Getch(void) |
|
565 { |
|
566 int ch; |
|
567 // keypad(inputWnd, TRUE); |
|
568 ch = wgetch(inputWnd); |
|
569 return ch; |
|
570 } |
|
571 |
|
572 WINDOW *scr_GetRosterWindow(void) |
|
573 { |
|
574 return rosterWnd; |
|
575 } |
|
576 |
|
577 WINDOW *scr_GetStatusWindow(void) |
|
578 { |
|
579 return chatWnd; |
|
580 } |
|
581 |
|
582 WINDOW *scr_GetInputWindow(void) |
|
583 { |
|
584 return inputWnd; |
|
585 } |
|
586 |
105
|
587 void scr_RosterTop(void) |
104
|
588 { |
119
|
589 enum imstatus prev_st; |
|
590 |
|
591 if (current_buddy) { |
|
592 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
593 if (chatmode) |
|
594 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
595 } |
104
|
596 current_buddy = buddylist; |
119
|
597 if (chatmode && current_buddy) |
|
598 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
599 |
|
600 // We should rebuild the buddylist but not everytime |
120
|
601 if (current_buddy && prev_st == offline && |
|
602 buddylist_get_hide_offline_buddies()) |
119
|
603 buddylist_build(); |
104
|
604 if (chatmode) |
|
605 scr_ShowBuddyWindow(); |
|
606 } |
|
607 |
105
|
608 void scr_RosterBottom(void) |
104
|
609 { |
119
|
610 enum imstatus prev_st; |
|
611 |
|
612 if (current_buddy) { |
|
613 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
614 if (chatmode) |
|
615 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
616 } |
104
|
617 current_buddy = g_list_last(buddylist); |
119
|
618 if (chatmode && current_buddy) |
|
619 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
620 |
|
621 // We should rebuild the buddylist but not everytime |
120
|
622 if (current_buddy && prev_st == offline && |
|
623 buddylist_get_hide_offline_buddies()) |
119
|
624 buddylist_build(); |
104
|
625 if (chatmode) |
|
626 scr_ShowBuddyWindow(); |
|
627 } |
|
628 |
105
|
629 void scr_RosterUp(void) |
81
|
630 { |
119
|
631 enum imstatus prev_st; |
|
632 |
81
|
633 if (current_buddy) { |
|
634 if (g_list_previous(current_buddy)) { |
120
|
635 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119
|
636 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81
|
637 current_buddy = g_list_previous(current_buddy); |
119
|
638 if (chatmode) |
|
639 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
640 // We should rebuild the buddylist but not everytime |
120
|
641 if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119
|
642 buddylist_build(); |
81
|
643 scr_DrawRoster(); |
|
644 } |
|
645 } |
104
|
646 |
|
647 if (chatmode) |
|
648 scr_ShowBuddyWindow(); |
81
|
649 } |
|
650 |
105
|
651 void scr_RosterDown(void) |
81
|
652 { |
119
|
653 enum imstatus prev_st; |
|
654 |
81
|
655 if (current_buddy) { |
|
656 if (g_list_next(current_buddy)) { |
120
|
657 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119
|
658 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81
|
659 current_buddy = g_list_next(current_buddy); |
119
|
660 if (chatmode) |
|
661 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
662 // We should rebuild the buddylist but not everytime |
120
|
663 if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119
|
664 buddylist_build(); |
81
|
665 scr_DrawRoster(); |
|
666 } |
|
667 } |
104
|
668 |
|
669 if (chatmode) |
|
670 scr_ShowBuddyWindow(); |
81
|
671 } |
|
672 |
105
|
673 void scr_ScrollUp(void) |
|
674 { |
|
675 const gchar *jid; |
|
676 window_entry_t *win_entry; |
|
677 int n, nblines; |
|
678 GList *hbuf_top; |
|
679 |
|
680 // Get win_entry |
|
681 if (!current_buddy) |
|
682 return; |
|
683 jid = CURRENT_JID; |
|
684 if (!jid) |
|
685 return; |
|
686 win_entry = scr_SearchWindow(jid); |
|
687 if (!win_entry) |
|
688 return; |
|
689 |
|
690 // Scroll up half a screen (or less) |
|
691 nblines = CHAT_WIN_HEIGHT/2-1; |
|
692 hbuf_top = win_entry->top; |
|
693 if (!hbuf_top) { |
|
694 hbuf_top = g_list_last(win_entry->hbuf); |
109
|
695 if (!win_entry->cleared) |
|
696 nblines *= 3; |
|
697 else |
|
698 win_entry->cleared = FALSE; |
105
|
699 } |
|
700 |
|
701 n = 0; |
|
702 while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) { |
|
703 hbuf_top = g_list_previous(hbuf_top); |
|
704 n++; |
|
705 } |
|
706 win_entry->top = hbuf_top; |
|
707 |
|
708 // Refresh the window |
|
709 scr_UpdateWindow(win_entry); |
|
710 |
|
711 // Finished :) |
|
712 update_panels(); |
|
713 doupdate(); |
|
714 } |
|
715 |
|
716 void scr_ScrollDown(void) |
|
717 { |
|
718 const gchar *jid; |
|
719 window_entry_t *win_entry; |
|
720 int n, nblines; |
|
721 GList *hbuf_top; |
|
722 |
|
723 // Get win_entry |
|
724 if (!current_buddy) |
|
725 return; |
|
726 jid = CURRENT_JID; |
|
727 if (!jid) |
|
728 return; |
|
729 win_entry = scr_SearchWindow(jid); |
|
730 if (!win_entry) |
|
731 return; |
|
732 |
|
733 // Scroll down half a screen (or less) |
|
734 nblines = CHAT_WIN_HEIGHT/2-1; |
|
735 hbuf_top = win_entry->top; |
|
736 |
|
737 for (n=0 ; hbuf_top && n < nblines ; n++) |
|
738 hbuf_top = g_list_next(hbuf_top); |
|
739 win_entry->top = hbuf_top; |
|
740 // Check if we are at the bottom |
|
741 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++) |
|
742 hbuf_top = g_list_next(hbuf_top); |
|
743 if (!hbuf_top) |
|
744 win_entry->top = NULL; // End reached |
|
745 |
|
746 // Refresh the window |
|
747 scr_UpdateWindow(win_entry); |
|
748 |
|
749 // Finished :) |
|
750 update_panels(); |
|
751 doupdate(); |
|
752 } |
|
753 |
108
|
754 void scr_Clear(void) |
|
755 { |
|
756 const gchar *jid; |
|
757 window_entry_t *win_entry; |
|
758 |
|
759 // Get win_entry |
|
760 if (!current_buddy) |
|
761 return; |
|
762 jid = CURRENT_JID; |
|
763 if (!jid) |
|
764 return; |
|
765 win_entry = scr_SearchWindow(jid); |
|
766 if (!win_entry) |
|
767 return; |
|
768 |
|
769 win_entry->cleared = TRUE; |
109
|
770 win_entry->top = NULL; |
108
|
771 |
|
772 // Refresh the window |
|
773 scr_UpdateWindow(win_entry); |
|
774 |
|
775 // Finished :) |
|
776 update_panels(); |
|
777 doupdate(); |
|
778 } |
|
779 |
44
|
780 // scr_LogPrint(...) |
|
781 // Display a message in the log window. |
24
|
782 void scr_LogPrint(const char *fmt, ...) |
|
783 { |
|
784 time_t timestamp; |
|
785 char *buffer; |
|
786 va_list ap; |
|
787 |
|
788 buffer = (char *) calloc(1, 4096); |
|
789 |
|
790 timestamp = time(NULL); |
|
791 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
792 wprintw(logWnd, "\n%s", buffer); |
|
793 |
|
794 va_start(ap, fmt); |
|
795 vsnprintf(buffer, 4096, fmt, ap); |
|
796 va_end(ap); |
|
797 |
|
798 wprintw(logWnd, "%s", buffer); |
|
799 free(buffer); |
|
800 |
|
801 update_panels(); |
|
802 doupdate(); |
|
803 } |
|
804 |
129
|
805 inline void scr_set_chatmode(int enable) |
|
806 { |
|
807 chatmode = enable; |
|
808 } |
|
809 |
98
|
810 // which_row() |
|
811 // Tells which row our cursor is in, in the command line. |
|
812 // -1 -> normal text |
|
813 // 0 -> command |
|
814 // 1 -> parameter 1 (etc.) |
102
|
815 // If > 0, then *p_row is set to the beginning of the row |
|
816 int which_row(char **p_row) |
98
|
817 { |
|
818 int row = -1; |
|
819 char *p; |
|
820 int quote = FALSE; |
|
821 |
|
822 // Not a command? |
|
823 if ((ptr_inputline == inputLine) || (inputLine[0] != '/')) |
|
824 return -1; |
|
825 |
|
826 // This is a command |
|
827 row = 0; |
|
828 for (p = inputLine ; p < ptr_inputline ; p++) { |
|
829 if (quote) { |
|
830 if (*p == '"' && *(p-1) != '\\') |
|
831 quote = FALSE; |
|
832 continue; |
|
833 } |
|
834 if (*p == '"' && *(p-1) != '\\') { |
|
835 quote = TRUE; |
121
|
836 } else if (*p == ' ') { |
|
837 if (*(p-1) != ' ') |
|
838 row++; |
102
|
839 *p_row = p+1; |
|
840 } |
98
|
841 } |
|
842 return row; |
|
843 } |
|
844 |
|
845 void scr_insert_text(const char *text) |
|
846 { |
|
847 char tmpLine[INPUTLINE_LENGTH+1]; |
|
848 int len = strlen(text); |
|
849 // Check the line isn't too long |
|
850 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { |
|
851 scr_LogPrint("Cannot insert text, line too long."); |
|
852 return; |
|
853 } |
|
854 |
|
855 strcpy(tmpLine, ptr_inputline); |
|
856 strcpy(ptr_inputline, text); ptr_inputline += len; |
|
857 strcpy(ptr_inputline, tmpLine); |
|
858 } |
|
859 |
|
860 void scr_handle_tab(void) |
|
861 { |
102
|
862 int nrow; |
|
863 char *row; |
|
864 const char *cchar; |
103
|
865 guint compl_categ; |
98
|
866 |
102
|
867 nrow = which_row(&row); |
98
|
868 |
103
|
869 // a) No completion if no leading slash ('cause not a command) |
|
870 // b) We can't have more than 2 parameters (we use 2 flags) |
|
871 if (nrow < 0 || nrow > 2) return; |
102
|
872 |
103
|
873 if (nrow == 0) { // Command completion |
|
874 row = &inputLine[1]; |
|
875 compl_categ = COMPL_CMD; |
|
876 } else { // Other completion, depending on the command |
|
877 cmd *com = cmd_get(inputLine); |
|
878 if (!com || !row) { |
|
879 scr_LogPrint("I cannot complete that..."); |
|
880 return; |
|
881 } |
|
882 compl_categ = com->completion_flags[nrow-1]; |
|
883 } |
|
884 |
|
885 if (!completion_started) { |
|
886 GSList *list = compl_get_category_list(compl_categ); |
|
887 if (list) { |
|
888 char *prefix = g_strndup(row, ptr_inputline-row); |
|
889 // Init completion |
|
890 new_completion(prefix, list); |
|
891 g_free(prefix); |
|
892 // Now complete |
98
|
893 cchar = complete(); |
|
894 if (cchar) |
|
895 scr_insert_text(cchar); |
103
|
896 completion_started = TRUE; |
98
|
897 } |
103
|
898 } else { // Completion already initialized |
|
899 char *c; |
|
900 guint back = cancel_completion(); |
|
901 // Remove $back chars |
|
902 ptr_inputline -= back; |
|
903 c = ptr_inputline; |
|
904 for ( ; *c ; c++) |
|
905 *c = *(c+back); |
|
906 // Now complete again |
|
907 cchar = complete(); |
|
908 if (cchar) |
|
909 scr_insert_text(cchar); |
102
|
910 } |
98
|
911 } |
|
912 |
|
913 void scr_cancel_current_completion(void) |
|
914 { |
|
915 char *c; |
|
916 guint back = cancel_completion(); |
|
917 // Remove $back chars |
|
918 ptr_inputline -= back; |
|
919 c = ptr_inputline; |
|
920 for ( ; *c ; c++) |
|
921 *c = *(c+back); |
|
922 } |
|
923 |
|
924 void scr_end_current_completion(void) |
|
925 { |
|
926 done_completion(); |
|
927 completion_started = FALSE; |
|
928 } |
|
929 |
24
|
930 // check_offset(int direction) |
|
931 // Check inputline_offset value, and make sure the cursor is inside the |
|
932 // screen. |
|
933 inline void check_offset(int direction) |
|
934 { |
|
935 // Left side |
|
936 if (inputline_offset && direction <= 0) { |
|
937 while (ptr_inputline <= (char*)&inputLine + inputline_offset) { |
|
938 if (inputline_offset) { |
|
939 inputline_offset -= 5; |
|
940 if (inputline_offset < 0) |
|
941 inputline_offset = 0; |
|
942 } |
|
943 } |
|
944 } |
|
945 // Right side |
|
946 if (direction >= 0) { |
|
947 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) |
|
948 inputline_offset += 5; |
|
949 } |
|
950 } |
|
951 |
44
|
952 // process_key(key) |
|
953 // Handle the pressed key, in the command line (bottom). |
29
|
954 int process_key(int key) |
24
|
955 { |
|
956 if (isprint(key)) { |
|
957 char tmpLine[INPUTLINE_LENGTH+1]; |
|
958 |
|
959 // Check the line isn't too long |
|
960 if (strlen(inputLine) >= INPUTLINE_LENGTH) |
|
961 return 0; |
|
962 |
|
963 // Insert char |
|
964 strcpy(tmpLine, ptr_inputline); |
|
965 *ptr_inputline++ = key; |
|
966 strcpy(ptr_inputline, tmpLine); |
|
967 check_offset(1); |
|
968 } else { |
|
969 switch(key) { |
|
970 case KEY_BACKSPACE: |
|
971 if (ptr_inputline != (char*)&inputLine) { |
42
|
972 char *c = --ptr_inputline; |
|
973 for ( ; *c ; c++) |
|
974 *c = *(c+1); |
24
|
975 check_offset(-1); |
|
976 } |
|
977 break; |
|
978 case KEY_DC: |
|
979 if (*ptr_inputline) |
|
980 strcpy(ptr_inputline, ptr_inputline+1); |
|
981 break; |
|
982 case KEY_LEFT: |
|
983 if (ptr_inputline != (char*)&inputLine) { |
|
984 ptr_inputline--; |
|
985 check_offset(-1); |
|
986 } |
|
987 break; |
|
988 case KEY_RIGHT: |
|
989 if (*ptr_inputline) |
|
990 ptr_inputline++; |
|
991 check_offset(1); |
|
992 break; |
98
|
993 case 7: // Ctrl-g |
|
994 scr_cancel_current_completion(); |
|
995 scr_end_current_completion(); |
|
996 check_offset(-1); |
|
997 break; |
24
|
998 case 9: // Tab |
98
|
999 scr_handle_tab(); |
|
1000 check_offset(0); |
24
|
1001 break; |
|
1002 case '\n': // Enter |
29
|
1003 if (process_line(inputLine)) |
24
|
1004 return 255; |
|
1005 ptr_inputline = inputLine; |
|
1006 *ptr_inputline = 0; |
|
1007 inputline_offset = 0; |
|
1008 break; |
|
1009 case KEY_UP: |
81
|
1010 scr_RosterUp(); |
24
|
1011 break; |
|
1012 case KEY_DOWN: |
81
|
1013 scr_RosterDown(); |
24
|
1014 break; |
|
1015 case KEY_PPAGE: |
105
|
1016 scr_ScrollUp(); |
24
|
1017 break; |
|
1018 case KEY_NPAGE: |
105
|
1019 scr_ScrollDown(); |
24
|
1020 break; |
|
1021 case KEY_HOME: |
|
1022 case 1: |
|
1023 ptr_inputline = inputLine; |
|
1024 inputline_offset = 0; |
|
1025 break; |
|
1026 case KEY_END: |
|
1027 case 5: |
|
1028 for (; *ptr_inputline; ptr_inputline++) ; |
|
1029 check_offset(1); |
|
1030 break; |
|
1031 case 21: // Ctrl-u |
|
1032 strcpy(inputLine, ptr_inputline); |
|
1033 ptr_inputline = inputLine; |
|
1034 inputline_offset = 0; |
|
1035 break; |
|
1036 case KEY_EOL: |
|
1037 case 11: // Ctrl-k |
|
1038 *ptr_inputline = 0; |
|
1039 break; |
|
1040 case 16: // Ctrl-p |
|
1041 scr_LogPrint("Ctrl-p not yet implemented"); |
|
1042 break; |
|
1043 case 14: // Ctrl-n |
|
1044 scr_LogPrint("Ctrl-n not yet implemented"); |
|
1045 break; |
|
1046 case 27: // ESC |
|
1047 currentWindow = NULL; |
|
1048 chatmode = FALSE; |
119
|
1049 if (current_buddy) |
|
1050 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
24
|
1051 top_panel(chatPanel); |
|
1052 top_panel(inputPanel); |
|
1053 break; |
|
1054 default: |
|
1055 scr_LogPrint("Unkown key=%d", key); |
|
1056 } |
|
1057 } |
98
|
1058 if (completion_started && key != 9) |
|
1059 scr_end_current_completion(); |
24
|
1060 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
|
1061 wclrtoeol(inputWnd); |
|
1062 if (*ptr_inputline) { |
|
1063 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
|
1064 } |
|
1065 update_panels(); |
|
1066 doupdate(); |
|
1067 return 0; |
|
1068 } |