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