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