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