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