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
|
123
|
449 // We can reset update_roster
|
|
450 update_roster = FALSE;
|
|
451
|
81
|
452 getmaxyx(rosterWnd, maxy, maxx);
|
|
453 maxx --; // last char is for vertical border
|
|
454 name[ROSTER_WIDTH-7] = 0;
|
|
455
|
|
456 // cleanup of roster window
|
|
457 wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL));
|
|
458 for (i = 0; i < maxy; i++) {
|
|
459 mvwprintw(rosterWnd, i, 0, "");
|
|
460 for (n = 0; n < maxx; n++)
|
|
461 waddch(rosterWnd, ' ');
|
|
462 }
|
|
463
|
|
464 // Leave now if buddylist is empty
|
|
465 if (!buddylist) {
|
|
466 offset = 0;
|
123
|
467 update_panels();
|
|
468 doupdate();
|
81
|
469 return;
|
|
470 }
|
|
471
|
84
|
472 // Update offset if necessary
|
|
473 i = g_list_position(buddylist, current_buddy);
|
|
474 if (i == -1) { // This is bad
|
|
475 scr_LogPrint("Doh! Can't find current selected buddy!!");
|
123
|
476 update_panels();
|
|
477 doupdate();
|
84
|
478 return;
|
|
479 } else if (i < offset) {
|
|
480 offset = i;
|
|
481 } else if (i+1 > offset + maxy) {
|
|
482 offset = i + 1 - maxy;
|
|
483 }
|
81
|
484
|
|
485 buddy = buddylist;
|
|
486 rOffset = offset;
|
|
487
|
84
|
488 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) {
|
81
|
489
|
|
490 char status = '?';
|
|
491 char pending = ' ';
|
|
492 enum imstatus budstate;
|
|
493
|
|
494 if (rOffset > 0) {
|
|
495 rOffset--;
|
|
496 continue;
|
|
497 }
|
|
498
|
|
499 if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) {
|
|
500 pending = '#';
|
|
501 }
|
|
502
|
|
503 budstate = buddy_getstatus(BUDDATA(buddy));
|
|
504 if (budstate >= 0 && budstate < imstatus_size)
|
|
505 status = imstatus2char[budstate];
|
|
506 if (buddy == current_buddy) {
|
|
507 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL));
|
|
508 // The 3 following lines aim to color the whole line
|
|
509 wmove(rosterWnd, i, 0);
|
|
510 for (n = 0; n < maxx; n++)
|
|
511 waddch(rosterWnd, ' ');
|
|
512 } else {
|
|
513 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES));
|
|
514 }
|
|
515
|
|
516 strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7);
|
|
517 // TODO: status is meaningless for groups:
|
|
518 if (buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP) status='G';
|
|
519 mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name);
|
84
|
520
|
|
521 i++;
|
81
|
522 }
|
|
523
|
|
524 update_panels();
|
|
525 doupdate();
|
24
|
526 }
|
|
527
|
50
|
528 void scr_WriteMessage(const char *jid, const char *text, char *prefix)
|
24
|
529 {
|
75
|
530 scr_WriteInWindow(jid, text, TRUE, prefix, FALSE);
|
47
|
531 }
|
|
532
|
50
|
533 void scr_WriteIncomingMessage(const char *jidfrom, const char *text)
|
47
|
534 {
|
75
|
535 // FIXME expand tabs / filter out special chars...
|
115
|
536 scr_WriteMessage(jidfrom, text, "<== ");
|
65
|
537 top_panel(inputPanel);
|
24
|
538 update_panels();
|
|
539 doupdate();
|
|
540 }
|
|
541
|
50
|
542 void scr_WriteOutgoingMessage(const char *jidto, const char *text)
|
47
|
543 {
|
65
|
544 scr_WriteMessage(jidto, text, "--> ");
|
47
|
545 scr_ShowWindow(jidto);
|
65
|
546 top_panel(inputPanel);
|
47
|
547 }
|
|
548
|
24
|
549 int scr_Getch(void)
|
|
550 {
|
|
551 int ch;
|
|
552 // keypad(inputWnd, TRUE);
|
|
553 ch = wgetch(inputWnd);
|
|
554 return ch;
|
|
555 }
|
|
556
|
|
557 WINDOW *scr_GetRosterWindow(void)
|
|
558 {
|
|
559 return rosterWnd;
|
|
560 }
|
|
561
|
|
562 WINDOW *scr_GetStatusWindow(void)
|
|
563 {
|
|
564 return chatWnd;
|
|
565 }
|
|
566
|
|
567 WINDOW *scr_GetInputWindow(void)
|
|
568 {
|
|
569 return inputWnd;
|
|
570 }
|
|
571
|
105
|
572 void scr_RosterTop(void)
|
104
|
573 {
|
119
|
574 enum imstatus prev_st;
|
|
575
|
|
576 if (current_buddy) {
|
|
577 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
578 if (chatmode)
|
|
579 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
580 }
|
104
|
581 current_buddy = buddylist;
|
119
|
582 if (chatmode && current_buddy)
|
|
583 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
584
|
|
585 // We should rebuild the buddylist but not everytime
|
120
|
586 if (current_buddy && prev_st == offline &&
|
|
587 buddylist_get_hide_offline_buddies())
|
119
|
588 buddylist_build();
|
104
|
589 if (chatmode)
|
|
590 scr_ShowBuddyWindow();
|
|
591 }
|
|
592
|
105
|
593 void scr_RosterBottom(void)
|
104
|
594 {
|
119
|
595 enum imstatus prev_st;
|
|
596
|
|
597 if (current_buddy) {
|
|
598 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
599 if (chatmode)
|
|
600 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
601 }
|
104
|
602 current_buddy = g_list_last(buddylist);
|
119
|
603 if (chatmode && current_buddy)
|
|
604 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
605
|
|
606 // We should rebuild the buddylist but not everytime
|
120
|
607 if (current_buddy && prev_st == offline &&
|
|
608 buddylist_get_hide_offline_buddies())
|
119
|
609 buddylist_build();
|
104
|
610 if (chatmode)
|
|
611 scr_ShowBuddyWindow();
|
|
612 }
|
|
613
|
105
|
614 void scr_RosterUp(void)
|
81
|
615 {
|
119
|
616 enum imstatus prev_st;
|
|
617
|
81
|
618 if (current_buddy) {
|
|
619 if (g_list_previous(current_buddy)) {
|
120
|
620 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
119
|
621 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
81
|
622 current_buddy = g_list_previous(current_buddy);
|
119
|
623 if (chatmode)
|
|
624 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
625 // We should rebuild the buddylist but not everytime
|
120
|
626 if (prev_st == offline && buddylist_get_hide_offline_buddies())
|
119
|
627 buddylist_build();
|
81
|
628 scr_DrawRoster();
|
|
629 }
|
|
630 }
|
104
|
631
|
|
632 if (chatmode)
|
|
633 scr_ShowBuddyWindow();
|
81
|
634 }
|
|
635
|
105
|
636 void scr_RosterDown(void)
|
81
|
637 {
|
119
|
638 enum imstatus prev_st;
|
|
639
|
81
|
640 if (current_buddy) {
|
|
641 if (g_list_next(current_buddy)) {
|
120
|
642 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
119
|
643 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
81
|
644 current_buddy = g_list_next(current_buddy);
|
119
|
645 if (chatmode)
|
|
646 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
647 // We should rebuild the buddylist but not everytime
|
120
|
648 if (prev_st == offline && buddylist_get_hide_offline_buddies())
|
119
|
649 buddylist_build();
|
81
|
650 scr_DrawRoster();
|
|
651 }
|
|
652 }
|
104
|
653
|
|
654 if (chatmode)
|
|
655 scr_ShowBuddyWindow();
|
81
|
656 }
|
|
657
|
105
|
658 void scr_ScrollUp(void)
|
|
659 {
|
|
660 const gchar *jid;
|
|
661 window_entry_t *win_entry;
|
|
662 int n, nblines;
|
|
663 GList *hbuf_top;
|
|
664
|
|
665 // Get win_entry
|
|
666 if (!current_buddy)
|
|
667 return;
|
|
668 jid = CURRENT_JID;
|
|
669 if (!jid)
|
|
670 return;
|
|
671 win_entry = scr_SearchWindow(jid);
|
|
672 if (!win_entry)
|
|
673 return;
|
|
674
|
|
675 // Scroll up half a screen (or less)
|
|
676 nblines = CHAT_WIN_HEIGHT/2-1;
|
|
677 hbuf_top = win_entry->top;
|
|
678 if (!hbuf_top) {
|
|
679 hbuf_top = g_list_last(win_entry->hbuf);
|
109
|
680 if (!win_entry->cleared)
|
|
681 nblines *= 3;
|
|
682 else
|
|
683 win_entry->cleared = FALSE;
|
105
|
684 }
|
|
685
|
|
686 n = 0;
|
|
687 while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) {
|
|
688 hbuf_top = g_list_previous(hbuf_top);
|
|
689 n++;
|
|
690 }
|
|
691 win_entry->top = hbuf_top;
|
|
692
|
|
693 // Refresh the window
|
|
694 scr_UpdateWindow(win_entry);
|
|
695
|
|
696 // Finished :)
|
|
697 update_panels();
|
|
698 doupdate();
|
|
699 }
|
|
700
|
|
701 void scr_ScrollDown(void)
|
|
702 {
|
|
703 const gchar *jid;
|
|
704 window_entry_t *win_entry;
|
|
705 int n, nblines;
|
|
706 GList *hbuf_top;
|
|
707
|
|
708 // Get win_entry
|
|
709 if (!current_buddy)
|
|
710 return;
|
|
711 jid = CURRENT_JID;
|
|
712 if (!jid)
|
|
713 return;
|
|
714 win_entry = scr_SearchWindow(jid);
|
|
715 if (!win_entry)
|
|
716 return;
|
|
717
|
|
718 // Scroll down half a screen (or less)
|
|
719 nblines = CHAT_WIN_HEIGHT/2-1;
|
|
720 hbuf_top = win_entry->top;
|
|
721
|
|
722 for (n=0 ; hbuf_top && n < nblines ; n++)
|
|
723 hbuf_top = g_list_next(hbuf_top);
|
|
724 win_entry->top = hbuf_top;
|
|
725 // Check if we are at the bottom
|
|
726 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++)
|
|
727 hbuf_top = g_list_next(hbuf_top);
|
|
728 if (!hbuf_top)
|
|
729 win_entry->top = NULL; // End reached
|
|
730
|
|
731 // Refresh the window
|
|
732 scr_UpdateWindow(win_entry);
|
|
733
|
|
734 // Finished :)
|
|
735 update_panels();
|
|
736 doupdate();
|
|
737 }
|
|
738
|
108
|
739 void scr_Clear(void)
|
|
740 {
|
|
741 const gchar *jid;
|
|
742 window_entry_t *win_entry;
|
|
743
|
|
744 // Get win_entry
|
|
745 if (!current_buddy)
|
|
746 return;
|
|
747 jid = CURRENT_JID;
|
|
748 if (!jid)
|
|
749 return;
|
|
750 win_entry = scr_SearchWindow(jid);
|
|
751 if (!win_entry)
|
|
752 return;
|
|
753
|
|
754 win_entry->cleared = TRUE;
|
109
|
755 win_entry->top = NULL;
|
108
|
756
|
|
757 // Refresh the window
|
|
758 scr_UpdateWindow(win_entry);
|
|
759
|
|
760 // Finished :)
|
|
761 update_panels();
|
|
762 doupdate();
|
|
763 }
|
|
764
|
44
|
765 // scr_LogPrint(...)
|
|
766 // Display a message in the log window.
|
24
|
767 void scr_LogPrint(const char *fmt, ...)
|
|
768 {
|
|
769 time_t timestamp;
|
|
770 char *buffer;
|
|
771 va_list ap;
|
|
772
|
|
773 buffer = (char *) calloc(1, 4096);
|
|
774
|
|
775 timestamp = time(NULL);
|
|
776 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp));
|
|
777 wprintw(logWnd, "\n%s", buffer);
|
|
778
|
|
779 va_start(ap, fmt);
|
|
780 vsnprintf(buffer, 4096, fmt, ap);
|
|
781 va_end(ap);
|
|
782
|
|
783 wprintw(logWnd, "%s", buffer);
|
|
784 free(buffer);
|
|
785
|
|
786 update_panels();
|
|
787 doupdate();
|
|
788 }
|
|
789
|
98
|
790 // which_row()
|
|
791 // Tells which row our cursor is in, in the command line.
|
|
792 // -1 -> normal text
|
|
793 // 0 -> command
|
|
794 // 1 -> parameter 1 (etc.)
|
102
|
795 // If > 0, then *p_row is set to the beginning of the row
|
|
796 int which_row(char **p_row)
|
98
|
797 {
|
|
798 int row = -1;
|
|
799 char *p;
|
|
800 int quote = FALSE;
|
|
801
|
|
802 // Not a command?
|
|
803 if ((ptr_inputline == inputLine) || (inputLine[0] != '/'))
|
|
804 return -1;
|
|
805
|
|
806 // This is a command
|
|
807 row = 0;
|
|
808 for (p = inputLine ; p < ptr_inputline ; p++) {
|
|
809 if (quote) {
|
|
810 if (*p == '"' && *(p-1) != '\\')
|
|
811 quote = FALSE;
|
|
812 continue;
|
|
813 }
|
|
814 if (*p == '"' && *(p-1) != '\\') {
|
|
815 quote = TRUE;
|
121
|
816 } else if (*p == ' ') {
|
|
817 if (*(p-1) != ' ')
|
|
818 row++;
|
102
|
819 *p_row = p+1;
|
|
820 }
|
98
|
821 }
|
|
822 return row;
|
|
823 }
|
|
824
|
|
825 void scr_insert_text(const char *text)
|
|
826 {
|
|
827 char tmpLine[INPUTLINE_LENGTH+1];
|
|
828 int len = strlen(text);
|
|
829 // Check the line isn't too long
|
|
830 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) {
|
|
831 scr_LogPrint("Cannot insert text, line too long.");
|
|
832 return;
|
|
833 }
|
|
834
|
|
835 strcpy(tmpLine, ptr_inputline);
|
|
836 strcpy(ptr_inputline, text); ptr_inputline += len;
|
|
837 strcpy(ptr_inputline, tmpLine);
|
|
838 }
|
|
839
|
|
840 void scr_handle_tab(void)
|
|
841 {
|
102
|
842 int nrow;
|
|
843 char *row;
|
|
844 const char *cchar;
|
103
|
845 guint compl_categ;
|
98
|
846
|
102
|
847 nrow = which_row(&row);
|
98
|
848
|
103
|
849 // a) No completion if no leading slash ('cause not a command)
|
|
850 // b) We can't have more than 2 parameters (we use 2 flags)
|
|
851 if (nrow < 0 || nrow > 2) return;
|
102
|
852
|
103
|
853 if (nrow == 0) { // Command completion
|
|
854 row = &inputLine[1];
|
|
855 compl_categ = COMPL_CMD;
|
|
856 } else { // Other completion, depending on the command
|
|
857 cmd *com = cmd_get(inputLine);
|
|
858 if (!com || !row) {
|
|
859 scr_LogPrint("I cannot complete that...");
|
|
860 return;
|
|
861 }
|
|
862 compl_categ = com->completion_flags[nrow-1];
|
|
863 }
|
|
864
|
|
865 if (!completion_started) {
|
|
866 GSList *list = compl_get_category_list(compl_categ);
|
|
867 if (list) {
|
|
868 char *prefix = g_strndup(row, ptr_inputline-row);
|
|
869 // Init completion
|
|
870 new_completion(prefix, list);
|
|
871 g_free(prefix);
|
|
872 // Now complete
|
98
|
873 cchar = complete();
|
|
874 if (cchar)
|
|
875 scr_insert_text(cchar);
|
103
|
876 completion_started = TRUE;
|
98
|
877 }
|
103
|
878 } else { // Completion already initialized
|
|
879 char *c;
|
|
880 guint back = cancel_completion();
|
|
881 // Remove $back chars
|
|
882 ptr_inputline -= back;
|
|
883 c = ptr_inputline;
|
|
884 for ( ; *c ; c++)
|
|
885 *c = *(c+back);
|
|
886 // Now complete again
|
|
887 cchar = complete();
|
|
888 if (cchar)
|
|
889 scr_insert_text(cchar);
|
102
|
890 }
|
98
|
891 }
|
|
892
|
|
893 void scr_cancel_current_completion(void)
|
|
894 {
|
|
895 char *c;
|
|
896 guint back = cancel_completion();
|
|
897 // Remove $back chars
|
|
898 ptr_inputline -= back;
|
|
899 c = ptr_inputline;
|
|
900 for ( ; *c ; c++)
|
|
901 *c = *(c+back);
|
|
902 }
|
|
903
|
|
904 void scr_end_current_completion(void)
|
|
905 {
|
|
906 done_completion();
|
|
907 completion_started = FALSE;
|
|
908 }
|
|
909
|
24
|
910 // check_offset(int direction)
|
|
911 // Check inputline_offset value, and make sure the cursor is inside the
|
|
912 // screen.
|
|
913 inline void check_offset(int direction)
|
|
914 {
|
|
915 // Left side
|
|
916 if (inputline_offset && direction <= 0) {
|
|
917 while (ptr_inputline <= (char*)&inputLine + inputline_offset) {
|
|
918 if (inputline_offset) {
|
|
919 inputline_offset -= 5;
|
|
920 if (inputline_offset < 0)
|
|
921 inputline_offset = 0;
|
|
922 }
|
|
923 }
|
|
924 }
|
|
925 // Right side
|
|
926 if (direction >= 0) {
|
|
927 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX)
|
|
928 inputline_offset += 5;
|
|
929 }
|
|
930 }
|
|
931
|
44
|
932 // process_key(key)
|
|
933 // Handle the pressed key, in the command line (bottom).
|
29
|
934 int process_key(int key)
|
24
|
935 {
|
|
936 if (isprint(key)) {
|
|
937 char tmpLine[INPUTLINE_LENGTH+1];
|
|
938
|
|
939 // Check the line isn't too long
|
|
940 if (strlen(inputLine) >= INPUTLINE_LENGTH)
|
|
941 return 0;
|
|
942
|
|
943 // Insert char
|
|
944 strcpy(tmpLine, ptr_inputline);
|
|
945 *ptr_inputline++ = key;
|
|
946 strcpy(ptr_inputline, tmpLine);
|
|
947 check_offset(1);
|
|
948 } else {
|
|
949 switch(key) {
|
|
950 case KEY_BACKSPACE:
|
|
951 if (ptr_inputline != (char*)&inputLine) {
|
42
|
952 char *c = --ptr_inputline;
|
|
953 for ( ; *c ; c++)
|
|
954 *c = *(c+1);
|
24
|
955 check_offset(-1);
|
|
956 }
|
|
957 break;
|
|
958 case KEY_DC:
|
|
959 if (*ptr_inputline)
|
|
960 strcpy(ptr_inputline, ptr_inputline+1);
|
|
961 break;
|
|
962 case KEY_LEFT:
|
|
963 if (ptr_inputline != (char*)&inputLine) {
|
|
964 ptr_inputline--;
|
|
965 check_offset(-1);
|
|
966 }
|
|
967 break;
|
|
968 case KEY_RIGHT:
|
|
969 if (*ptr_inputline)
|
|
970 ptr_inputline++;
|
|
971 check_offset(1);
|
|
972 break;
|
98
|
973 case 7: // Ctrl-g
|
|
974 scr_cancel_current_completion();
|
|
975 scr_end_current_completion();
|
|
976 check_offset(-1);
|
|
977 break;
|
24
|
978 case 9: // Tab
|
98
|
979 scr_handle_tab();
|
|
980 check_offset(0);
|
24
|
981 break;
|
|
982 case '\n': // Enter
|
|
983 chatmode = TRUE;
|
119
|
984 if (current_buddy)
|
|
985 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
24
|
986 if (inputLine[0] == 0) {
|
|
987 scr_ShowBuddyWindow();
|
|
988 break;
|
|
989 }
|
29
|
990 if (process_line(inputLine))
|
24
|
991 return 255;
|
|
992 ptr_inputline = inputLine;
|
|
993 *ptr_inputline = 0;
|
|
994 inputline_offset = 0;
|
|
995 break;
|
|
996 case KEY_UP:
|
81
|
997 scr_RosterUp();
|
24
|
998 break;
|
|
999 case KEY_DOWN:
|
81
|
1000 scr_RosterDown();
|
24
|
1001 break;
|
|
1002 case KEY_PPAGE:
|
105
|
1003 scr_ScrollUp();
|
24
|
1004 break;
|
|
1005 case KEY_NPAGE:
|
105
|
1006 scr_ScrollDown();
|
24
|
1007 break;
|
|
1008 case KEY_HOME:
|
|
1009 case 1:
|
|
1010 ptr_inputline = inputLine;
|
|
1011 inputline_offset = 0;
|
|
1012 break;
|
|
1013 case KEY_END:
|
|
1014 case 5:
|
|
1015 for (; *ptr_inputline; ptr_inputline++) ;
|
|
1016 check_offset(1);
|
|
1017 break;
|
|
1018 case 21: // Ctrl-u
|
|
1019 strcpy(inputLine, ptr_inputline);
|
|
1020 ptr_inputline = inputLine;
|
|
1021 inputline_offset = 0;
|
|
1022 break;
|
|
1023 case KEY_EOL:
|
|
1024 case 11: // Ctrl-k
|
|
1025 *ptr_inputline = 0;
|
|
1026 break;
|
|
1027 case 16: // Ctrl-p
|
|
1028 scr_LogPrint("Ctrl-p not yet implemented");
|
|
1029 break;
|
|
1030 case 14: // Ctrl-n
|
|
1031 scr_LogPrint("Ctrl-n not yet implemented");
|
|
1032 break;
|
|
1033 case 27: // ESC
|
|
1034 currentWindow = NULL;
|
|
1035 chatmode = FALSE;
|
119
|
1036 if (current_buddy)
|
|
1037 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
24
|
1038 top_panel(chatPanel);
|
|
1039 top_panel(inputPanel);
|
|
1040 break;
|
|
1041 default:
|
|
1042 scr_LogPrint("Unkown key=%d", key);
|
|
1043 }
|
|
1044 }
|
98
|
1045 if (completion_started && key != 9)
|
|
1046 scr_end_current_completion();
|
24
|
1047 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset);
|
|
1048 wclrtoeol(inputWnd);
|
|
1049 if (*ptr_inputline) {
|
|
1050 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset);
|
|
1051 }
|
|
1052 update_panels();
|
|
1053 doupdate();
|
|
1054 return 0;
|
|
1055 }
|