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