24
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
|
4 #include <ncurses.h>
|
|
5 #include <panel.h>
|
|
6 #include <time.h>
|
|
7 #include <ctype.h>
|
|
8 #include <locale.h>
|
|
9
|
|
10 #include "screen.h"
|
81
|
11 #include "hbuf.h"
|
47
|
12 #include "commands.h"
|
95
|
13 #include "compl.h"
|
81
|
14 #include "roster.h"
|
24
|
15 #include "parsecfg.h"
|
|
16 #include "lang.h"
|
81
|
17 #include "utils.h"
|
24
|
18 #include "list.h"
|
|
19
|
|
20 #define window_entry(n) list_entry(n, window_entry_t, list)
|
|
21
|
|
22 LIST_HEAD(window_list);
|
|
23
|
|
24 typedef struct _window_entry_t {
|
|
25 WINDOW *win;
|
108
|
26 PANEL *panel;
|
|
27 char *name;
|
|
28 GList *hbuf;
|
|
29 GList *top; // If top is not specified (NULL), we'll display the last lines
|
|
30 char cleared; // For ex, user has issued a /clear command...
|
24
|
31 struct list_head list;
|
|
32 } window_entry_t;
|
|
33
|
|
34
|
|
35 static WINDOW *rosterWnd, *chatWnd, *inputWnd;
|
|
36 static WINDOW *logWnd, *logWnd_border;
|
|
37 static PANEL *rosterPanel, *chatPanel, *inputPanel;
|
|
38 static PANEL *logPanel, *logPanel_border;
|
|
39 static int maxY, maxX;
|
|
40 static window_entry_t *currentWindow;
|
|
41
|
|
42 static int chatmode;
|
30
|
43 int update_roster;
|
24
|
44
|
|
45 static char inputLine[INPUTLINE_LENGTH+1];
|
|
46 static char *ptr_inputline;
|
|
47 static short int inputline_offset;
|
98
|
48 static int completion_started;
|
24
|
49
|
|
50
|
99
|
51 /* Functions */
|
24
|
52
|
74
|
53 int scr_WindowWidth(WINDOW * win)
|
24
|
54 {
|
|
55 int x, y;
|
|
56 getmaxyx(win, y, x);
|
|
57 return x;
|
|
58 }
|
|
59
|
74
|
60 void scr_clear_box(WINDOW *win, int y, int x, int height, int width, int Color)
|
|
61 {
|
|
62 int i, j;
|
|
63
|
|
64 wattrset(win, COLOR_PAIR(Color));
|
|
65 for (i = 0; i < height; i++) {
|
|
66 wmove(win, y + i, x);
|
|
67 for (j = 0; j < width; j++)
|
|
68 wprintw(win, " ");
|
|
69 }
|
|
70 }
|
|
71
|
24
|
72 void scr_draw_box(WINDOW * win, int y, int x, int height, int width,
|
|
73 int Color, chtype box, chtype border)
|
|
74 {
|
|
75 int i, j;
|
|
76
|
|
77 wattrset(win, COLOR_PAIR(Color));
|
|
78 for (i = 0; i < height; i++) {
|
|
79 wmove(win, y + i, x);
|
|
80 for (j = 0; j < width; j++)
|
|
81 if (!i && !j)
|
|
82 waddch(win, border | ACS_ULCORNER);
|
|
83 else if (i == height - 1 && !j)
|
|
84 waddch(win, border | ACS_LLCORNER);
|
|
85 else if (!i && j == width - 1)
|
|
86 waddch(win, box | ACS_URCORNER);
|
|
87 else if (i == height - 1 && j == width - 1)
|
|
88 waddch(win, box | ACS_LRCORNER);
|
|
89 else if (!i)
|
|
90 waddch(win, border | ACS_HLINE);
|
|
91 else if (i == height - 1)
|
|
92 waddch(win, box | ACS_HLINE);
|
|
93 else if (!j)
|
|
94 waddch(win, border | ACS_VLINE);
|
|
95 else if (j == width - 1)
|
|
96 waddch(win, box | ACS_VLINE);
|
|
97 else
|
|
98 waddch(win, box | ' ');
|
|
99 }
|
|
100 }
|
|
101
|
|
102 int FindColor(char *name)
|
|
103 {
|
|
104 if (!strcmp(name, "default"))
|
|
105 return -1;
|
|
106 if (!strcmp(name, "black"))
|
|
107 return COLOR_BLACK;
|
|
108 if (!strcmp(name, "red"))
|
|
109 return COLOR_RED;
|
|
110 if (!strcmp(name, "green"))
|
|
111 return COLOR_GREEN;
|
|
112 if (!strcmp(name, "yellow"))
|
|
113 return COLOR_YELLOW;
|
|
114 if (!strcmp(name, "blue"))
|
|
115 return COLOR_BLUE;
|
|
116 if (!strcmp(name, "magenta"))
|
|
117 return COLOR_MAGENTA;
|
|
118 if (!strcmp(name, "cyan"))
|
|
119 return COLOR_CYAN;
|
|
120 if (!strcmp(name, "white"))
|
|
121 return COLOR_WHITE;
|
|
122
|
|
123 return -1;
|
|
124 }
|
|
125
|
|
126 void ParseColors(void)
|
|
127 {
|
|
128 char *colors[11] = {
|
|
129 "", "",
|
|
130 "borderlines",
|
|
131 "jidonlineselected",
|
|
132 "jidonline",
|
|
133 "jidofflineselected",
|
|
134 "jidoffline",
|
|
135 "text",
|
|
136 NULL
|
|
137 };
|
|
138
|
|
139 char *tmp = malloc(1024);
|
|
140 char *color1;
|
|
141 char *background = cfg_read("color_background");
|
|
142 char *backselected = cfg_read("color_backselected");
|
|
143 int i = 0;
|
|
144
|
|
145 while (colors[i]) {
|
|
146 sprintf(tmp, "color_%s", colors[i]);
|
|
147 color1 = cfg_read(tmp);
|
|
148
|
|
149 switch (i + 1) {
|
|
150 case 1:
|
|
151 init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
|
152 break;
|
|
153 case 2:
|
|
154 init_pair(2, COLOR_WHITE, COLOR_BLACK);
|
|
155 break;
|
|
156 case 3:
|
|
157 init_pair(3, FindColor(color1), FindColor(background));
|
|
158 break;
|
|
159 case 4:
|
|
160 init_pair(4, FindColor(color1), FindColor(backselected));
|
|
161 break;
|
|
162 case 5:
|
|
163 init_pair(5, FindColor(color1), FindColor(background));
|
|
164 break;
|
|
165 case 6:
|
|
166 init_pair(6, FindColor(color1), FindColor(backselected));
|
|
167 break;
|
|
168 case 7:
|
|
169 init_pair(7, FindColor(color1), FindColor(background));
|
|
170 break;
|
|
171 case 8:
|
|
172 init_pair(8, FindColor(color1), FindColor(background));
|
|
173 break;
|
|
174 }
|
|
175 i++;
|
|
176 }
|
|
177 }
|
|
178
|
|
179
|
50
|
180 window_entry_t *scr_CreatePanel(const char *title, int x, int y,
|
|
181 int lines, int cols, int dont_show)
|
24
|
182 {
|
|
183 window_entry_t *tmp = calloc(1, sizeof(window_entry_t));
|
|
184
|
|
185 tmp->win = newwin(lines, cols, y, x);
|
|
186 tmp->panel = new_panel(tmp->win);
|
|
187 tmp->name = (char *) calloc(1, 1024);
|
|
188 strncpy(tmp->name, title, 1024);
|
76
|
189 scr_clear_box(tmp->win, 0, 0, lines, cols, COLOR_GENERAL);
|
24
|
190
|
|
191 if ((!dont_show)) {
|
|
192 currentWindow = tmp;
|
|
193 } else {
|
|
194 if (currentWindow)
|
|
195 top_panel(currentWindow->panel);
|
|
196 else
|
|
197 top_panel(chatPanel);
|
|
198 }
|
|
199
|
|
200 list_add_tail(&tmp->list, &window_list);
|
|
201 update_panels();
|
|
202
|
|
203 return tmp;
|
|
204 }
|
|
205
|
50
|
206 window_entry_t *scr_SearchWindow(const char *winId)
|
24
|
207 {
|
|
208 struct list_head *pos, *n;
|
|
209 window_entry_t *search_entry = NULL;
|
|
210
|
|
211 list_for_each_safe(pos, n, &window_list) {
|
|
212 search_entry = window_entry(pos);
|
|
213 if (search_entry->name) {
|
|
214 if (!strcasecmp(search_entry->name, winId)) {
|
|
215 return search_entry;
|
|
216 }
|
|
217 }
|
|
218 }
|
|
219 return NULL;
|
|
220 }
|
|
221
|
74
|
222 void scr_UpdateWindow(window_entry_t *win_entry)
|
|
223 {
|
|
224 int n;
|
|
225 int width;
|
|
226 char **lines;
|
|
227 GList *hbuf_head;
|
|
228
|
108
|
229 width = scr_WindowWidth(win_entry->win);
|
|
230
|
|
231 // Should the window be empty?
|
|
232 if (win_entry->cleared) {
|
|
233 scr_clear_box(win_entry->win, 0, 0, CHAT_WIN_HEIGHT, width, COLOR_GENERAL);
|
|
234 return;
|
|
235 }
|
|
236
|
105
|
237 // win_entry->top is the top message of the screen. If it set to NULL, we
|
|
238 // are displaying the last messages.
|
|
239
|
74
|
240 // We will show the last CHAT_WIN_HEIGHT lines.
|
|
241 // Let's find out where it begins.
|
105
|
242 if (!win_entry->top ||
|
|
243 (g_list_position(g_list_first(win_entry->hbuf), win_entry->top) == -1)) {
|
|
244 // Move up CHAT_WIN_HEIGHT lines
|
|
245 win_entry->hbuf = g_list_last(win_entry->hbuf);
|
|
246 hbuf_head = win_entry->hbuf;
|
|
247 win_entry->top = NULL; // (Just to make sure)
|
|
248 n = 0;
|
|
249 while (hbuf_head && (n < CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head)) {
|
|
250 hbuf_head = g_list_previous(hbuf_head);
|
|
251 n++;
|
|
252 }
|
|
253 } else
|
|
254 hbuf_head = win_entry->top;
|
74
|
255
|
|
256 // Get the last CHAT_WIN_HEIGHT lines.
|
|
257 lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT);
|
|
258
|
|
259 // Display these lines
|
|
260 wmove(win_entry->win, 0, 0);
|
|
261 for (n = 0; n < CHAT_WIN_HEIGHT; n++) {
|
|
262 int r = width;
|
75
|
263 if (*(lines+2*n)) {
|
|
264 if (**(lines+2*n))
|
|
265 wprintw(win_entry->win, "%s", *(lines+2*n)); // prefix
|
|
266 else {
|
|
267 wprintw(win_entry->win, " ");
|
|
268 r -= 12;
|
|
269 }
|
|
270 wprintw(win_entry->win, "%s", *(lines+2*n+1)); // line
|
|
271 // Calculate the number of blank characters to empty the line
|
|
272 r -= strlen(*(lines+2*n)) + strlen(*(lines+2*n+1));
|
|
273 }
|
74
|
274 for ( ; r>0 ; r--) {
|
|
275 wprintw(win_entry->win, " ");
|
|
276 }
|
|
277 //// wclrtoeol(win_entry->win); does not work :(
|
|
278 }
|
|
279 g_free(lines);
|
|
280 }
|
|
281
|
50
|
282 void scr_ShowWindow(const char *winId)
|
24
|
283 {
|
74
|
284 window_entry_t *win_entry = scr_SearchWindow(winId);
|
|
285
|
|
286 if (win_entry != NULL) {
|
|
287 top_panel(win_entry->panel);
|
|
288 currentWindow = win_entry;
|
24
|
289 chatmode = TRUE;
|
81
|
290 roster_setflags(winId, ROSTER_FLAG_MSG, FALSE);
|
119
|
291 roster_setflags(winId, ROSTER_FLAG_LOCK, TRUE);
|
30
|
292 update_roster = TRUE;
|
74
|
293
|
105
|
294 // Refresh the window
|
74
|
295 scr_UpdateWindow(win_entry);
|
|
296
|
|
297 // Finished :)
|
24
|
298 update_panels();
|
|
299 doupdate();
|
|
300 } else {
|
|
301 top_panel(chatPanel);
|
74
|
302 currentWindow = win_entry; // == NULL (current window empty)
|
24
|
303 }
|
|
304 }
|
|
305
|
|
306 void scr_ShowBuddyWindow(void)
|
|
307 {
|
105
|
308 const gchar *jid;
|
|
309 if (!current_buddy)
|
|
310 return;
|
|
311 jid = CURRENT_JID;
|
|
312 if (!jid)
|
|
313 return;
|
|
314 scr_ShowWindow(jid);
|
24
|
315 top_panel(inputPanel);
|
|
316 }
|
|
317
|
|
318
|
75
|
319 void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp,
|
|
320 const char *prefix, int force_show)
|
24
|
321 {
|
75
|
322 char *fullprefix = NULL;
|
74
|
323 window_entry_t *win_entry;
|
24
|
324 int dont_show = FALSE;
|
|
325
|
75
|
326 // Prepare the prefix
|
|
327 if (prefix || TimeStamp) {
|
|
328 if (!prefix) prefix = "";
|
|
329 fullprefix = calloc(1, strlen(prefix)+16);
|
|
330 if (TimeStamp) {
|
|
331 time_t now = time(NULL);
|
|
332 strftime(fullprefix, 12, "[%H:%M] ", localtime(&now));
|
|
333 } else {
|
|
334 strcpy(fullprefix, " ");
|
|
335 }
|
|
336 strcat(fullprefix, prefix);
|
74
|
337 }
|
|
338
|
|
339 // Look for the window entry.
|
|
340 win_entry = scr_SearchWindow(winId);
|
|
341
|
|
342 // Do we have to really show the window?
|
24
|
343 if (!chatmode)
|
|
344 dont_show = TRUE;
|
74
|
345 else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry))))
|
24
|
346 dont_show = TRUE;
|
|
347
|
74
|
348 // If the window entry doesn't exist yet, let's create it.
|
|
349 if (win_entry == NULL) {
|
|
350 win_entry = scr_CreatePanel(winId, ROSTER_WIDTH, 0, CHAT_WIN_HEIGHT,
|
|
351 maxX - ROSTER_WIDTH, dont_show);
|
24
|
352 }
|
|
353
|
75
|
354 hbuf_add_line(&win_entry->hbuf, text, fullprefix,
|
74
|
355 maxX - scr_WindowWidth(rosterWnd) - 14);
|
75
|
356 free(fullprefix);
|
74
|
357
|
108
|
358 if (win_entry->cleared) {
|
|
359 win_entry->cleared = 0; // The message must be displayed
|
|
360 win_entry->top = g_list_last(win_entry->hbuf);
|
|
361 }
|
|
362
|
24
|
363 if (!dont_show) {
|
74
|
364 // Show and refresh the window
|
|
365 top_panel(win_entry->panel);
|
|
366 scr_UpdateWindow(win_entry);
|
24
|
367 update_panels();
|
|
368 doupdate();
|
|
369 } else {
|
81
|
370 roster_setflags(winId, ROSTER_FLAG_MSG, TRUE);
|
30
|
371 update_roster = TRUE;
|
24
|
372 }
|
|
373 }
|
|
374
|
|
375 void scr_InitCurses(void)
|
|
376 {
|
|
377 initscr();
|
|
378 noecho();
|
|
379 raw();
|
35
|
380 halfdelay(5);
|
24
|
381 start_color();
|
|
382 use_default_colors();
|
|
383
|
|
384 ParseColors();
|
|
385
|
|
386 getmaxyx(stdscr, maxY, maxX);
|
|
387 inputLine[0] = 0;
|
|
388 ptr_inputline = inputLine;
|
|
389
|
35
|
390 setlocale(LC_CTYPE, "");
|
24
|
391
|
|
392 return;
|
|
393 }
|
|
394
|
81
|
395 void scr_TerminateCurses(void)
|
|
396 {
|
|
397 clear();
|
|
398 refresh();
|
|
399 endwin();
|
|
400 return;
|
|
401 }
|
|
402
|
24
|
403 void scr_DrawMainWindow(void)
|
|
404 {
|
74
|
405 int l;
|
|
406
|
24
|
407 /* Draw main panels */
|
74
|
408 rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0);
|
24
|
409 rosterPanel = new_panel(rosterWnd);
|
74
|
410 scr_clear_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, ROSTER_WIDTH,
|
|
411 COLOR_GENERAL);
|
|
412 for (l=0 ; l < CHAT_WIN_HEIGHT ; l++)
|
|
413 mvwaddch(rosterWnd, l, ROSTER_WIDTH-1, ACS_VLINE);
|
24
|
414
|
74
|
415 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH);
|
24
|
416 chatPanel = new_panel(chatWnd);
|
74
|
417 scr_clear_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH,
|
|
418 COLOR_GENERAL);
|
|
419 scrollok(chatWnd, TRUE);
|
|
420 mvwprintw(chatWnd, 0, 0, "This is the status window");
|
24
|
421
|
|
422 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0);
|
|
423 logPanel_border = new_panel(logWnd_border);
|
|
424 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0);
|
|
425 logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1);
|
|
426 logPanel = new_panel(logWnd);
|
|
427 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL));
|
|
428
|
74
|
429 scrollok(logWnd, TRUE);
|
24
|
430
|
|
431 inputWnd = newwin(1, maxX, maxY-1, 0);
|
|
432 inputPanel = new_panel(inputWnd);
|
|
433
|
81
|
434 scr_DrawRoster();
|
24
|
435 update_panels();
|
|
436 doupdate();
|
|
437 return;
|
|
438 }
|
|
439
|
81
|
440 void scr_DrawRoster(void)
|
24
|
441 {
|
81
|
442 static guint offset = 0;
|
|
443 char name[ROSTER_WIDTH];
|
|
444 int maxx, maxy;
|
|
445 GList *buddy;
|
|
446 int i, n;
|
|
447 int rOffset;
|
|
448
|
|
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 {
|
75
|
529 // FIXME expand tabs / filter out special chars...
|
115
|
530 scr_WriteMessage(jidfrom, text, "<== ");
|
65
|
531 top_panel(inputPanel);
|
24
|
532 update_panels();
|
|
533 doupdate();
|
|
534 }
|
|
535
|
50
|
536 void scr_WriteOutgoingMessage(const char *jidto, const char *text)
|
47
|
537 {
|
65
|
538 scr_WriteMessage(jidto, text, "--> ");
|
47
|
539 scr_ShowWindow(jidto);
|
65
|
540 top_panel(inputPanel);
|
47
|
541 }
|
|
542
|
24
|
543 int scr_Getch(void)
|
|
544 {
|
|
545 int ch;
|
|
546 // keypad(inputWnd, TRUE);
|
|
547 ch = wgetch(inputWnd);
|
|
548 return ch;
|
|
549 }
|
|
550
|
|
551 WINDOW *scr_GetRosterWindow(void)
|
|
552 {
|
|
553 return rosterWnd;
|
|
554 }
|
|
555
|
|
556 WINDOW *scr_GetStatusWindow(void)
|
|
557 {
|
|
558 return chatWnd;
|
|
559 }
|
|
560
|
|
561 WINDOW *scr_GetInputWindow(void)
|
|
562 {
|
|
563 return inputWnd;
|
|
564 }
|
|
565
|
105
|
566 void scr_RosterTop(void)
|
104
|
567 {
|
119
|
568 enum imstatus prev_st;
|
|
569
|
|
570 if (current_buddy) {
|
|
571 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
572 if (chatmode)
|
|
573 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
574 }
|
104
|
575 current_buddy = buddylist;
|
119
|
576 if (chatmode && current_buddy)
|
|
577 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
578
|
|
579 // We should rebuild the buddylist but not everytime
|
|
580 if (current_buddy && prev_st == offline)
|
|
581 buddylist_build();
|
104
|
582 if (chatmode)
|
|
583 scr_ShowBuddyWindow();
|
|
584 }
|
|
585
|
105
|
586 void scr_RosterBottom(void)
|
104
|
587 {
|
119
|
588 enum imstatus prev_st;
|
|
589
|
|
590 if (current_buddy) {
|
|
591 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
592 if (chatmode)
|
|
593 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
594 }
|
104
|
595 current_buddy = g_list_last(buddylist);
|
119
|
596 if (chatmode && current_buddy)
|
|
597 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
598
|
|
599 // We should rebuild the buddylist but not everytime
|
|
600 if (current_buddy && prev_st == offline)
|
|
601 buddylist_build();
|
104
|
602 if (chatmode)
|
|
603 scr_ShowBuddyWindow();
|
|
604 }
|
|
605
|
105
|
606 void scr_RosterUp(void)
|
81
|
607 {
|
119
|
608 enum imstatus prev_st;
|
|
609
|
81
|
610 if (current_buddy) {
|
119
|
611 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
81
|
612 if (g_list_previous(current_buddy)) {
|
119
|
613 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
81
|
614 current_buddy = g_list_previous(current_buddy);
|
119
|
615 if (chatmode)
|
|
616 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
617 // We should rebuild the buddylist but not everytime
|
|
618 if (prev_st == offline)
|
|
619 buddylist_build();
|
81
|
620 scr_DrawRoster();
|
|
621 }
|
|
622 }
|
104
|
623
|
|
624 if (chatmode)
|
|
625 scr_ShowBuddyWindow();
|
81
|
626 }
|
|
627
|
105
|
628 void scr_RosterDown(void)
|
81
|
629 {
|
119
|
630 enum imstatus prev_st;
|
|
631
|
81
|
632 if (current_buddy) {
|
119
|
633 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
81
|
634 if (g_list_next(current_buddy)) {
|
119
|
635 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
81
|
636 current_buddy = g_list_next(current_buddy);
|
119
|
637 if (chatmode)
|
|
638 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
639 // We should rebuild the buddylist but not everytime
|
|
640 if (prev_st == offline)
|
|
641 buddylist_build();
|
81
|
642 scr_DrawRoster();
|
|
643 }
|
|
644 }
|
104
|
645
|
|
646 if (chatmode)
|
|
647 scr_ShowBuddyWindow();
|
81
|
648 }
|
|
649
|
105
|
650 void scr_ScrollUp(void)
|
|
651 {
|
|
652 const gchar *jid;
|
|
653 window_entry_t *win_entry;
|
|
654 int n, nblines;
|
|
655 GList *hbuf_top;
|
|
656
|
|
657 // Get win_entry
|
|
658 if (!current_buddy)
|
|
659 return;
|
|
660 jid = CURRENT_JID;
|
|
661 if (!jid)
|
|
662 return;
|
|
663 win_entry = scr_SearchWindow(jid);
|
|
664 if (!win_entry)
|
|
665 return;
|
|
666
|
|
667 // Scroll up half a screen (or less)
|
|
668 nblines = CHAT_WIN_HEIGHT/2-1;
|
|
669 hbuf_top = win_entry->top;
|
|
670 if (!hbuf_top) {
|
|
671 hbuf_top = g_list_last(win_entry->hbuf);
|
109
|
672 if (!win_entry->cleared)
|
|
673 nblines *= 3;
|
|
674 else
|
|
675 win_entry->cleared = FALSE;
|
105
|
676 }
|
|
677
|
|
678 n = 0;
|
|
679 while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) {
|
|
680 hbuf_top = g_list_previous(hbuf_top);
|
|
681 n++;
|
|
682 }
|
|
683 win_entry->top = hbuf_top;
|
|
684
|
|
685 // Refresh the window
|
|
686 scr_UpdateWindow(win_entry);
|
|
687
|
|
688 // Finished :)
|
|
689 update_panels();
|
|
690 doupdate();
|
|
691 }
|
|
692
|
|
693 void scr_ScrollDown(void)
|
|
694 {
|
|
695 const gchar *jid;
|
|
696 window_entry_t *win_entry;
|
|
697 int n, nblines;
|
|
698 GList *hbuf_top;
|
|
699
|
|
700 // Get win_entry
|
|
701 if (!current_buddy)
|
|
702 return;
|
|
703 jid = CURRENT_JID;
|
|
704 if (!jid)
|
|
705 return;
|
|
706 win_entry = scr_SearchWindow(jid);
|
|
707 if (!win_entry)
|
|
708 return;
|
|
709
|
|
710 // Scroll down half a screen (or less)
|
|
711 nblines = CHAT_WIN_HEIGHT/2-1;
|
|
712 hbuf_top = win_entry->top;
|
|
713
|
|
714 for (n=0 ; hbuf_top && n < nblines ; n++)
|
|
715 hbuf_top = g_list_next(hbuf_top);
|
|
716 win_entry->top = hbuf_top;
|
|
717 // Check if we are at the bottom
|
|
718 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++)
|
|
719 hbuf_top = g_list_next(hbuf_top);
|
|
720 if (!hbuf_top)
|
|
721 win_entry->top = NULL; // End reached
|
|
722
|
|
723 // Refresh the window
|
|
724 scr_UpdateWindow(win_entry);
|
|
725
|
|
726 // Finished :)
|
|
727 update_panels();
|
|
728 doupdate();
|
|
729 }
|
|
730
|
108
|
731 void scr_Clear(void)
|
|
732 {
|
|
733 const gchar *jid;
|
|
734 window_entry_t *win_entry;
|
|
735
|
|
736 // Get win_entry
|
|
737 if (!current_buddy)
|
|
738 return;
|
|
739 jid = CURRENT_JID;
|
|
740 if (!jid)
|
|
741 return;
|
|
742 win_entry = scr_SearchWindow(jid);
|
|
743 if (!win_entry)
|
|
744 return;
|
|
745
|
|
746 win_entry->cleared = TRUE;
|
109
|
747 win_entry->top = NULL;
|
108
|
748
|
|
749 // Refresh the window
|
|
750 scr_UpdateWindow(win_entry);
|
|
751
|
|
752 // Finished :)
|
|
753 update_panels();
|
|
754 doupdate();
|
|
755 }
|
|
756
|
44
|
757 // scr_LogPrint(...)
|
|
758 // Display a message in the log window.
|
24
|
759 void scr_LogPrint(const char *fmt, ...)
|
|
760 {
|
|
761 time_t timestamp;
|
|
762 char *buffer;
|
|
763 va_list ap;
|
|
764
|
|
765 buffer = (char *) calloc(1, 4096);
|
|
766
|
|
767 timestamp = time(NULL);
|
|
768 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp));
|
|
769 wprintw(logWnd, "\n%s", buffer);
|
|
770
|
|
771 va_start(ap, fmt);
|
|
772 vsnprintf(buffer, 4096, fmt, ap);
|
|
773 va_end(ap);
|
|
774
|
|
775 wprintw(logWnd, "%s", buffer);
|
|
776 free(buffer);
|
|
777
|
|
778 update_panels();
|
|
779 doupdate();
|
|
780 }
|
|
781
|
98
|
782 // which_row()
|
|
783 // Tells which row our cursor is in, in the command line.
|
|
784 // -1 -> normal text
|
|
785 // 0 -> command
|
|
786 // 1 -> parameter 1 (etc.)
|
102
|
787 // If > 0, then *p_row is set to the beginning of the row
|
|
788 int which_row(char **p_row)
|
98
|
789 {
|
|
790 int row = -1;
|
|
791 char *p;
|
|
792 int quote = FALSE;
|
|
793
|
|
794 // Not a command?
|
|
795 if ((ptr_inputline == inputLine) || (inputLine[0] != '/'))
|
|
796 return -1;
|
|
797
|
|
798 // This is a command
|
|
799 row = 0;
|
|
800 for (p = inputLine ; p < ptr_inputline ; p++) {
|
|
801 if (quote) {
|
|
802 if (*p == '"' && *(p-1) != '\\')
|
|
803 quote = FALSE;
|
|
804 continue;
|
|
805 }
|
|
806 if (*p == '"' && *(p-1) != '\\') {
|
|
807 quote = TRUE;
|
102
|
808 } else if (*p == ' ' && *(p-1) != ' ') {
|
98
|
809 row++;
|
102
|
810 *p_row = p+1;
|
|
811 }
|
98
|
812 }
|
|
813 return row;
|
|
814 }
|
|
815
|
|
816 void scr_insert_text(const char *text)
|
|
817 {
|
|
818 char tmpLine[INPUTLINE_LENGTH+1];
|
|
819 int len = strlen(text);
|
|
820 // Check the line isn't too long
|
|
821 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) {
|
|
822 scr_LogPrint("Cannot insert text, line too long.");
|
|
823 return;
|
|
824 }
|
|
825
|
|
826 strcpy(tmpLine, ptr_inputline);
|
|
827 strcpy(ptr_inputline, text); ptr_inputline += len;
|
|
828 strcpy(ptr_inputline, tmpLine);
|
|
829 }
|
|
830
|
|
831 void scr_handle_tab(void)
|
|
832 {
|
102
|
833 int nrow;
|
|
834 char *row;
|
|
835 const char *cchar;
|
103
|
836 guint compl_categ;
|
98
|
837
|
102
|
838 nrow = which_row(&row);
|
98
|
839
|
103
|
840 // a) No completion if no leading slash ('cause not a command)
|
|
841 // b) We can't have more than 2 parameters (we use 2 flags)
|
|
842 if (nrow < 0 || nrow > 2) return;
|
102
|
843
|
103
|
844 if (nrow == 0) { // Command completion
|
|
845 row = &inputLine[1];
|
|
846 compl_categ = COMPL_CMD;
|
|
847 } else { // Other completion, depending on the command
|
|
848 cmd *com = cmd_get(inputLine);
|
|
849 if (!com || !row) {
|
|
850 scr_LogPrint("I cannot complete that...");
|
|
851 return;
|
|
852 }
|
|
853 compl_categ = com->completion_flags[nrow-1];
|
|
854 }
|
|
855
|
|
856 if (!completion_started) {
|
|
857 GSList *list = compl_get_category_list(compl_categ);
|
|
858 if (list) {
|
|
859 char *prefix = g_strndup(row, ptr_inputline-row);
|
|
860 // Init completion
|
|
861 new_completion(prefix, list);
|
|
862 g_free(prefix);
|
|
863 // Now complete
|
98
|
864 cchar = complete();
|
|
865 if (cchar)
|
|
866 scr_insert_text(cchar);
|
103
|
867 completion_started = TRUE;
|
98
|
868 }
|
103
|
869 } else { // Completion already initialized
|
|
870 char *c;
|
|
871 guint back = cancel_completion();
|
|
872 // Remove $back chars
|
|
873 ptr_inputline -= back;
|
|
874 c = ptr_inputline;
|
|
875 for ( ; *c ; c++)
|
|
876 *c = *(c+back);
|
|
877 // Now complete again
|
|
878 cchar = complete();
|
|
879 if (cchar)
|
|
880 scr_insert_text(cchar);
|
102
|
881 }
|
98
|
882 }
|
|
883
|
|
884 void scr_cancel_current_completion(void)
|
|
885 {
|
|
886 char *c;
|
|
887 guint back = cancel_completion();
|
|
888 // Remove $back chars
|
|
889 ptr_inputline -= back;
|
|
890 c = ptr_inputline;
|
|
891 for ( ; *c ; c++)
|
|
892 *c = *(c+back);
|
|
893 }
|
|
894
|
|
895 void scr_end_current_completion(void)
|
|
896 {
|
|
897 done_completion();
|
|
898 completion_started = FALSE;
|
|
899 }
|
|
900
|
24
|
901 // check_offset(int direction)
|
|
902 // Check inputline_offset value, and make sure the cursor is inside the
|
|
903 // screen.
|
|
904 inline void check_offset(int direction)
|
|
905 {
|
|
906 // Left side
|
|
907 if (inputline_offset && direction <= 0) {
|
|
908 while (ptr_inputline <= (char*)&inputLine + inputline_offset) {
|
|
909 if (inputline_offset) {
|
|
910 inputline_offset -= 5;
|
|
911 if (inputline_offset < 0)
|
|
912 inputline_offset = 0;
|
|
913 }
|
|
914 }
|
|
915 }
|
|
916 // Right side
|
|
917 if (direction >= 0) {
|
|
918 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX)
|
|
919 inputline_offset += 5;
|
|
920 }
|
|
921 }
|
|
922
|
44
|
923 // process_key(key)
|
|
924 // Handle the pressed key, in the command line (bottom).
|
29
|
925 int process_key(int key)
|
24
|
926 {
|
|
927 if (isprint(key)) {
|
|
928 char tmpLine[INPUTLINE_LENGTH+1];
|
|
929
|
|
930 // Check the line isn't too long
|
|
931 if (strlen(inputLine) >= INPUTLINE_LENGTH)
|
|
932 return 0;
|
|
933
|
|
934 // Insert char
|
|
935 strcpy(tmpLine, ptr_inputline);
|
|
936 *ptr_inputline++ = key;
|
|
937 strcpy(ptr_inputline, tmpLine);
|
|
938 check_offset(1);
|
|
939 } else {
|
|
940 switch(key) {
|
|
941 case KEY_BACKSPACE:
|
|
942 if (ptr_inputline != (char*)&inputLine) {
|
42
|
943 char *c = --ptr_inputline;
|
|
944 for ( ; *c ; c++)
|
|
945 *c = *(c+1);
|
24
|
946 check_offset(-1);
|
|
947 }
|
|
948 break;
|
|
949 case KEY_DC:
|
|
950 if (*ptr_inputline)
|
|
951 strcpy(ptr_inputline, ptr_inputline+1);
|
|
952 break;
|
|
953 case KEY_LEFT:
|
|
954 if (ptr_inputline != (char*)&inputLine) {
|
|
955 ptr_inputline--;
|
|
956 check_offset(-1);
|
|
957 }
|
|
958 break;
|
|
959 case KEY_RIGHT:
|
|
960 if (*ptr_inputline)
|
|
961 ptr_inputline++;
|
|
962 check_offset(1);
|
|
963 break;
|
98
|
964 case 7: // Ctrl-g
|
|
965 scr_cancel_current_completion();
|
|
966 scr_end_current_completion();
|
|
967 check_offset(-1);
|
|
968 break;
|
24
|
969 case 9: // Tab
|
98
|
970 scr_handle_tab();
|
|
971 check_offset(0);
|
24
|
972 break;
|
|
973 case '\n': // Enter
|
|
974 chatmode = TRUE;
|
119
|
975 if (current_buddy)
|
|
976 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
24
|
977 if (inputLine[0] == 0) {
|
|
978 scr_ShowBuddyWindow();
|
|
979 break;
|
|
980 }
|
29
|
981 if (process_line(inputLine))
|
24
|
982 return 255;
|
|
983 ptr_inputline = inputLine;
|
|
984 *ptr_inputline = 0;
|
|
985 inputline_offset = 0;
|
|
986 break;
|
|
987 case KEY_UP:
|
81
|
988 scr_RosterUp();
|
24
|
989 break;
|
|
990 case KEY_DOWN:
|
81
|
991 scr_RosterDown();
|
24
|
992 break;
|
|
993 case KEY_PPAGE:
|
105
|
994 scr_ScrollUp();
|
24
|
995 break;
|
|
996 case KEY_NPAGE:
|
105
|
997 scr_ScrollDown();
|
24
|
998 break;
|
|
999 case KEY_HOME:
|
|
1000 case 1:
|
|
1001 ptr_inputline = inputLine;
|
|
1002 inputline_offset = 0;
|
|
1003 break;
|
|
1004 case KEY_END:
|
|
1005 case 5:
|
|
1006 for (; *ptr_inputline; ptr_inputline++) ;
|
|
1007 check_offset(1);
|
|
1008 break;
|
|
1009 case 21: // Ctrl-u
|
|
1010 strcpy(inputLine, ptr_inputline);
|
|
1011 ptr_inputline = inputLine;
|
|
1012 inputline_offset = 0;
|
|
1013 break;
|
|
1014 case KEY_EOL:
|
|
1015 case 11: // Ctrl-k
|
|
1016 *ptr_inputline = 0;
|
|
1017 break;
|
|
1018 case 16: // Ctrl-p
|
|
1019 scr_LogPrint("Ctrl-p not yet implemented");
|
|
1020 break;
|
|
1021 case 14: // Ctrl-n
|
|
1022 scr_LogPrint("Ctrl-n not yet implemented");
|
|
1023 break;
|
|
1024 case 27: // ESC
|
|
1025 currentWindow = NULL;
|
|
1026 chatmode = FALSE;
|
119
|
1027 if (current_buddy)
|
|
1028 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
24
|
1029 top_panel(chatPanel);
|
|
1030 top_panel(inputPanel);
|
|
1031 break;
|
|
1032 default:
|
|
1033 scr_LogPrint("Unkown key=%d", key);
|
|
1034 }
|
|
1035 }
|
98
|
1036 if (completion_started && key != 9)
|
|
1037 scr_end_current_completion();
|
24
|
1038 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset);
|
|
1039 wclrtoeol(inputWnd);
|
|
1040 if (*ptr_inputline) {
|
|
1041 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset);
|
|
1042 }
|
|
1043 update_panels();
|
|
1044 doupdate();
|
|
1045 return 0;
|
|
1046 }
|