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