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
|
143
|
190 if (!dont_show) {
|
24
|
191 currentWindow = tmp;
|
|
192 } else {
|
|
193 if (currentWindow)
|
|
194 top_panel(currentWindow->panel);
|
|
195 else
|
|
196 top_panel(chatPanel);
|
|
197 }
|
143
|
198 update_panels();
|
24
|
199
|
|
200 list_add_tail(&tmp->list, &window_list);
|
|
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
|
143
|
221 // scr_UpdateWindow()
|
|
222 // (Re-)Display the given chat window.
|
74
|
223 void scr_UpdateWindow(window_entry_t *win_entry)
|
|
224 {
|
|
225 int n;
|
|
226 int width;
|
|
227 char **lines;
|
|
228 GList *hbuf_head;
|
|
229
|
108
|
230 width = scr_WindowWidth(win_entry->win);
|
|
231
|
|
232 // Should the window be empty?
|
|
233 if (win_entry->cleared) {
|
|
234 scr_clear_box(win_entry->win, 0, 0, CHAT_WIN_HEIGHT, width, COLOR_GENERAL);
|
|
235 return;
|
|
236 }
|
|
237
|
105
|
238 // win_entry->top is the top message of the screen. If it set to NULL, we
|
|
239 // are displaying the last messages.
|
|
240
|
74
|
241 // We will show the last CHAT_WIN_HEIGHT lines.
|
|
242 // Let's find out where it begins.
|
105
|
243 if (!win_entry->top ||
|
|
244 (g_list_position(g_list_first(win_entry->hbuf), win_entry->top) == -1)) {
|
|
245 // Move up CHAT_WIN_HEIGHT lines
|
|
246 win_entry->hbuf = g_list_last(win_entry->hbuf);
|
|
247 hbuf_head = win_entry->hbuf;
|
|
248 win_entry->top = NULL; // (Just to make sure)
|
|
249 n = 0;
|
|
250 while (hbuf_head && (n < CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head)) {
|
|
251 hbuf_head = g_list_previous(hbuf_head);
|
|
252 n++;
|
|
253 }
|
|
254 } else
|
|
255 hbuf_head = win_entry->top;
|
74
|
256
|
|
257 // Get the last CHAT_WIN_HEIGHT lines.
|
|
258 lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT);
|
|
259
|
|
260 // Display these lines
|
|
261 wmove(win_entry->win, 0, 0);
|
|
262 for (n = 0; n < CHAT_WIN_HEIGHT; n++) {
|
|
263 int r = width;
|
75
|
264 if (*(lines+2*n)) {
|
|
265 if (**(lines+2*n))
|
|
266 wprintw(win_entry->win, "%s", *(lines+2*n)); // prefix
|
|
267 else {
|
|
268 wprintw(win_entry->win, " ");
|
|
269 r -= 12;
|
|
270 }
|
|
271 wprintw(win_entry->win, "%s", *(lines+2*n+1)); // line
|
|
272 // Calculate the number of blank characters to empty the line
|
|
273 r -= strlen(*(lines+2*n)) + strlen(*(lines+2*n+1));
|
|
274 }
|
74
|
275 for ( ; r>0 ; r--) {
|
|
276 wprintw(win_entry->win, " ");
|
|
277 }
|
|
278 //// wclrtoeol(win_entry->win); does not work :(
|
|
279 }
|
|
280 g_free(lines);
|
|
281 }
|
|
282
|
143
|
283 // scr_ShowWindow()
|
|
284 // Display the chat window with the given identifier.
|
50
|
285 void scr_ShowWindow(const char *winId)
|
24
|
286 {
|
74
|
287 window_entry_t *win_entry = scr_SearchWindow(winId);
|
|
288
|
|
289 if (win_entry != NULL) {
|
|
290 top_panel(win_entry->panel);
|
|
291 currentWindow = win_entry;
|
24
|
292 chatmode = TRUE;
|
148
|
293 roster_msg_setflag(winId, FALSE);
|
119
|
294 roster_setflags(winId, ROSTER_FLAG_LOCK, TRUE);
|
30
|
295 update_roster = TRUE;
|
74
|
296
|
105
|
297 // Refresh the window
|
74
|
298 scr_UpdateWindow(win_entry);
|
|
299
|
|
300 // Finished :)
|
24
|
301 update_panels();
|
|
302 doupdate();
|
|
303 } else {
|
|
304 top_panel(chatPanel);
|
74
|
305 currentWindow = win_entry; // == NULL (current window empty)
|
24
|
306 }
|
142
|
307
|
|
308 top_panel(inputPanel);
|
24
|
309 }
|
|
310
|
143
|
311 // scr_ShowBuddyWindow()
|
|
312 // Display the chat window buffer for the current buddy.
|
24
|
313 void scr_ShowBuddyWindow(void)
|
|
314 {
|
105
|
315 const gchar *jid;
|
140
|
316
|
105
|
317 if (!current_buddy)
|
140
|
318 jid = NULL;
|
|
319 else
|
|
320 jid = CURRENT_JID;
|
|
321
|
|
322 if (!jid) {
|
|
323 top_panel(chatPanel);
|
143
|
324 top_panel(inputPanel);
|
140
|
325 currentWindow = NULL;
|
105
|
326 return;
|
140
|
327 }
|
|
328
|
105
|
329 scr_ShowWindow(jid);
|
24
|
330 }
|
|
331
|
|
332
|
143
|
333 // scr_WriteInWindow()
|
|
334 // Write some text in the winId window (this usually is a jid).
|
|
335 // Lines are splitted when they are too long to fit in the chat window.
|
|
336 // If this window doesn't exist, it is created.
|
75
|
337 void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp,
|
|
338 const char *prefix, int force_show)
|
24
|
339 {
|
75
|
340 char *fullprefix = NULL;
|
74
|
341 window_entry_t *win_entry;
|
24
|
342 int dont_show = FALSE;
|
|
343
|
75
|
344 // Prepare the prefix
|
|
345 if (prefix || TimeStamp) {
|
|
346 if (!prefix) prefix = "";
|
|
347 fullprefix = calloc(1, strlen(prefix)+16);
|
|
348 if (TimeStamp) {
|
|
349 time_t now = time(NULL);
|
|
350 strftime(fullprefix, 12, "[%H:%M] ", localtime(&now));
|
|
351 } else {
|
|
352 strcpy(fullprefix, " ");
|
|
353 }
|
|
354 strcat(fullprefix, prefix);
|
74
|
355 }
|
|
356
|
|
357 // Look for the window entry.
|
|
358 win_entry = scr_SearchWindow(winId);
|
|
359
|
|
360 // Do we have to really show the window?
|
24
|
361 if (!chatmode)
|
|
362 dont_show = TRUE;
|
74
|
363 else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry))))
|
24
|
364 dont_show = TRUE;
|
|
365
|
74
|
366 // If the window entry doesn't exist yet, let's create it.
|
|
367 if (win_entry == NULL) {
|
|
368 win_entry = scr_CreatePanel(winId, ROSTER_WIDTH, 0, CHAT_WIN_HEIGHT,
|
|
369 maxX - ROSTER_WIDTH, dont_show);
|
24
|
370 }
|
|
371
|
75
|
372 hbuf_add_line(&win_entry->hbuf, text, fullprefix,
|
74
|
373 maxX - scr_WindowWidth(rosterWnd) - 14);
|
75
|
374 free(fullprefix);
|
74
|
375
|
108
|
376 if (win_entry->cleared) {
|
|
377 win_entry->cleared = 0; // The message must be displayed
|
|
378 win_entry->top = g_list_last(win_entry->hbuf);
|
|
379 }
|
|
380
|
24
|
381 if (!dont_show) {
|
74
|
382 // Show and refresh the window
|
|
383 top_panel(win_entry->panel);
|
|
384 scr_UpdateWindow(win_entry);
|
142
|
385 top_panel(inputPanel);
|
24
|
386 update_panels();
|
|
387 doupdate();
|
|
388 } else {
|
148
|
389 roster_msg_setflag(winId, TRUE);
|
30
|
390 update_roster = TRUE;
|
24
|
391 }
|
|
392 }
|
|
393
|
|
394 void scr_InitCurses(void)
|
|
395 {
|
|
396 initscr();
|
|
397 noecho();
|
|
398 raw();
|
35
|
399 halfdelay(5);
|
24
|
400 start_color();
|
|
401 use_default_colors();
|
|
402
|
|
403 ParseColors();
|
|
404
|
|
405 getmaxyx(stdscr, maxY, maxX);
|
|
406 inputLine[0] = 0;
|
|
407 ptr_inputline = inputLine;
|
|
408
|
35
|
409 setlocale(LC_CTYPE, "");
|
24
|
410
|
|
411 return;
|
|
412 }
|
|
413
|
81
|
414 void scr_TerminateCurses(void)
|
|
415 {
|
|
416 clear();
|
|
417 refresh();
|
|
418 endwin();
|
|
419 return;
|
|
420 }
|
|
421
|
24
|
422 void scr_DrawMainWindow(void)
|
|
423 {
|
74
|
424 int l;
|
|
425
|
24
|
426 /* Draw main panels */
|
74
|
427 rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0);
|
24
|
428 rosterPanel = new_panel(rosterWnd);
|
74
|
429 scr_clear_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, ROSTER_WIDTH,
|
|
430 COLOR_GENERAL);
|
|
431 for (l=0 ; l < CHAT_WIN_HEIGHT ; l++)
|
|
432 mvwaddch(rosterWnd, l, ROSTER_WIDTH-1, ACS_VLINE);
|
24
|
433
|
74
|
434 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH);
|
24
|
435 chatPanel = new_panel(chatWnd);
|
74
|
436 scr_clear_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH,
|
|
437 COLOR_GENERAL);
|
|
438 scrollok(chatWnd, TRUE);
|
|
439 mvwprintw(chatWnd, 0, 0, "This is the status window");
|
24
|
440
|
|
441 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0);
|
|
442 logPanel_border = new_panel(logWnd_border);
|
|
443 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0);
|
|
444 logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1);
|
|
445 logPanel = new_panel(logWnd);
|
|
446 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL));
|
|
447
|
74
|
448 scrollok(logWnd, TRUE);
|
24
|
449
|
|
450 inputWnd = newwin(1, maxX, maxY-1, 0);
|
|
451 inputPanel = new_panel(inputWnd);
|
|
452
|
149
|
453 update_roster = TRUE;
|
24
|
454 return;
|
|
455 }
|
|
456
|
143
|
457 // scr_DrawRoster()
|
|
458 // Actually, display the buddylist on the screen.
|
81
|
459 void scr_DrawRoster(void)
|
24
|
460 {
|
81
|
461 static guint offset = 0;
|
|
462 char name[ROSTER_WIDTH];
|
|
463 int maxx, maxy;
|
|
464 GList *buddy;
|
|
465 int i, n;
|
|
466 int rOffset;
|
|
467
|
123
|
468 // We can reset update_roster
|
|
469 update_roster = FALSE;
|
|
470
|
81
|
471 getmaxyx(rosterWnd, maxy, maxx);
|
|
472 maxx --; // last char is for vertical border
|
|
473 name[ROSTER_WIDTH-7] = 0;
|
|
474
|
|
475 // cleanup of roster window
|
|
476 wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL));
|
|
477 for (i = 0; i < maxy; i++) {
|
|
478 mvwprintw(rosterWnd, i, 0, "");
|
|
479 for (n = 0; n < maxx; n++)
|
|
480 waddch(rosterWnd, ' ');
|
|
481 }
|
|
482
|
|
483 // Leave now if buddylist is empty
|
|
484 if (!buddylist) {
|
|
485 offset = 0;
|
123
|
486 update_panels();
|
|
487 doupdate();
|
81
|
488 return;
|
|
489 }
|
|
490
|
84
|
491 // Update offset if necessary
|
|
492 i = g_list_position(buddylist, current_buddy);
|
|
493 if (i == -1) { // This is bad
|
|
494 scr_LogPrint("Doh! Can't find current selected buddy!!");
|
123
|
495 update_panels();
|
|
496 doupdate();
|
84
|
497 return;
|
|
498 } else if (i < offset) {
|
|
499 offset = i;
|
|
500 } else if (i+1 > offset + maxy) {
|
|
501 offset = i + 1 - maxy;
|
|
502 }
|
81
|
503
|
|
504 buddy = buddylist;
|
|
505 rOffset = offset;
|
|
506
|
84
|
507 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) {
|
81
|
508
|
|
509 char status = '?';
|
|
510 char pending = ' ';
|
|
511 enum imstatus budstate;
|
149
|
512 unsigned short ismsg = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG;
|
|
513 unsigned short isgrp = buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP;
|
|
514 unsigned short ishid = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_HIDE;
|
81
|
515
|
|
516 if (rOffset > 0) {
|
|
517 rOffset--;
|
|
518 continue;
|
|
519 }
|
|
520
|
149
|
521 // Display message notice if there is a message flag, but not
|
|
522 // for unfolded groups.
|
|
523 if (ismsg && (!isgrp || ishid)) {
|
81
|
524 pending = '#';
|
|
525 }
|
|
526
|
|
527 budstate = buddy_getstatus(BUDDATA(buddy));
|
|
528 if (budstate >= 0 && budstate < imstatus_size)
|
|
529 status = imstatus2char[budstate];
|
|
530 if (buddy == current_buddy) {
|
|
531 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL));
|
|
532 // The 3 following lines aim to color the whole line
|
|
533 wmove(rosterWnd, i, 0);
|
|
534 for (n = 0; n < maxx; n++)
|
|
535 waddch(rosterWnd, ' ');
|
|
536 } else {
|
149
|
537 if (pending == '#')
|
139
|
538 wattrset(rosterWnd, COLOR_PAIR(COLOR_NMSG));
|
|
539 else
|
|
540 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES));
|
81
|
541 }
|
|
542
|
|
543 strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7);
|
149
|
544 if (isgrp) {
|
133
|
545 char *sep;
|
149
|
546 if (ishid)
|
133
|
547 sep = "+++";
|
|
548 else
|
|
549 sep = "---";
|
|
550 mvwprintw(rosterWnd, i, 0, " %c%s %s", pending, sep, name);
|
|
551 }
|
126
|
552 else
|
|
553 mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name);
|
84
|
554
|
|
555 i++;
|
81
|
556 }
|
|
557
|
142
|
558 top_panel(inputPanel);
|
81
|
559 update_panels();
|
|
560 doupdate();
|
24
|
561 }
|
|
562
|
50
|
563 void scr_WriteMessage(const char *jid, const char *text, char *prefix)
|
24
|
564 {
|
75
|
565 scr_WriteInWindow(jid, text, TRUE, prefix, FALSE);
|
47
|
566 }
|
|
567
|
50
|
568 void scr_WriteIncomingMessage(const char *jidfrom, const char *text)
|
47
|
569 {
|
75
|
570 // FIXME expand tabs / filter out special chars...
|
115
|
571 scr_WriteMessage(jidfrom, text, "<== ");
|
24
|
572 update_panels();
|
|
573 doupdate();
|
|
574 }
|
|
575
|
50
|
576 void scr_WriteOutgoingMessage(const char *jidto, const char *text)
|
47
|
577 {
|
65
|
578 scr_WriteMessage(jidto, text, "--> ");
|
47
|
579 scr_ShowWindow(jidto);
|
|
580 }
|
|
581
|
24
|
582 int scr_Getch(void)
|
|
583 {
|
|
584 int ch;
|
|
585 ch = wgetch(inputWnd);
|
|
586 return ch;
|
|
587 }
|
|
588
|
|
589 WINDOW *scr_GetRosterWindow(void)
|
|
590 {
|
|
591 return rosterWnd;
|
|
592 }
|
|
593
|
|
594 WINDOW *scr_GetStatusWindow(void)
|
|
595 {
|
|
596 return chatWnd;
|
|
597 }
|
|
598
|
|
599 WINDOW *scr_GetInputWindow(void)
|
|
600 {
|
|
601 return inputWnd;
|
|
602 }
|
|
603
|
143
|
604 // scr_RosterTop()
|
|
605 // Go to the first buddy in the buddylist
|
105
|
606 void scr_RosterTop(void)
|
104
|
607 {
|
143
|
608 enum imstatus prev_st = imstatus_size; // undef
|
119
|
609
|
|
610 if (current_buddy) {
|
|
611 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
612 if (chatmode)
|
|
613 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
614 }
|
104
|
615 current_buddy = buddylist;
|
119
|
616 if (chatmode && current_buddy)
|
|
617 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
618
|
|
619 // We should rebuild the buddylist but not everytime
|
143
|
620 // Here we check if we were locking a buddy who is actually offline,
|
|
621 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
120
|
622 if (current_buddy && prev_st == offline &&
|
|
623 buddylist_get_hide_offline_buddies())
|
119
|
624 buddylist_build();
|
104
|
625 if (chatmode)
|
|
626 scr_ShowBuddyWindow();
|
143
|
627 update_roster = TRUE;
|
104
|
628 }
|
|
629
|
143
|
630 // scr_RosterBottom()
|
|
631 // Go to the last buddy in the buddylist
|
105
|
632 void scr_RosterBottom(void)
|
104
|
633 {
|
143
|
634 enum imstatus prev_st = imstatus_size; // undef
|
119
|
635
|
|
636 if (current_buddy) {
|
|
637 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
638 if (chatmode)
|
|
639 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
640 }
|
104
|
641 current_buddy = g_list_last(buddylist);
|
143
|
642 // Lock the buddy in the buddylist if we're in chat mode
|
119
|
643 if (chatmode && current_buddy)
|
|
644 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
645
|
|
646 // We should rebuild the buddylist but not everytime
|
143
|
647 // Here we check if we were locking a buddy who is actually offline,
|
|
648 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
120
|
649 if (current_buddy && prev_st == offline &&
|
|
650 buddylist_get_hide_offline_buddies())
|
119
|
651 buddylist_build();
|
143
|
652
|
104
|
653 if (chatmode)
|
|
654 scr_ShowBuddyWindow();
|
143
|
655 update_roster = TRUE;
|
104
|
656 }
|
|
657
|
143
|
658 // scr_RosterUp()
|
|
659 // Go to the previous buddy in the buddylist
|
105
|
660 void scr_RosterUp(void)
|
81
|
661 {
|
143
|
662 enum imstatus prev_st = imstatus_size; // undef
|
119
|
663
|
81
|
664 if (current_buddy) {
|
|
665 if (g_list_previous(current_buddy)) {
|
120
|
666 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
119
|
667 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
81
|
668 current_buddy = g_list_previous(current_buddy);
|
143
|
669 // Lock the buddy in the buddylist if we're in chat mode
|
119
|
670 if (chatmode)
|
|
671 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
672 // We should rebuild the buddylist but not everytime
|
143
|
673 // Here we check if we were locking a buddy who is actually offline,
|
|
674 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
120
|
675 if (prev_st == offline && buddylist_get_hide_offline_buddies())
|
119
|
676 buddylist_build();
|
143
|
677 update_roster = TRUE;
|
81
|
678 }
|
|
679 }
|
104
|
680
|
|
681 if (chatmode)
|
|
682 scr_ShowBuddyWindow();
|
81
|
683 }
|
|
684
|
143
|
685 // scr_RosterDown()
|
|
686 // Go to the next buddy in the buddylist
|
105
|
687 void scr_RosterDown(void)
|
81
|
688 {
|
143
|
689 enum imstatus prev_st = imstatus_size; // undef
|
119
|
690
|
81
|
691 if (current_buddy) {
|
|
692 if (g_list_next(current_buddy)) {
|
120
|
693 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
119
|
694 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
81
|
695 current_buddy = g_list_next(current_buddy);
|
119
|
696 if (chatmode)
|
|
697 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
698 // We should rebuild the buddylist but not everytime
|
143
|
699 // Here we check if we were locking a buddy who is actually offline,
|
|
700 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
120
|
701 if (prev_st == offline && buddylist_get_hide_offline_buddies())
|
119
|
702 buddylist_build();
|
143
|
703 update_roster = TRUE;
|
81
|
704 }
|
|
705 }
|
104
|
706
|
|
707 if (chatmode)
|
|
708 scr_ShowBuddyWindow();
|
81
|
709 }
|
|
710
|
143
|
711 // scr_ScrollUp()
|
|
712 // Scroll up the current buddy window, half a screen.
|
105
|
713 void scr_ScrollUp(void)
|
|
714 {
|
|
715 const gchar *jid;
|
|
716 window_entry_t *win_entry;
|
|
717 int n, nblines;
|
|
718 GList *hbuf_top;
|
|
719
|
|
720 // Get win_entry
|
|
721 if (!current_buddy)
|
|
722 return;
|
|
723 jid = CURRENT_JID;
|
|
724 if (!jid)
|
|
725 return;
|
|
726 win_entry = scr_SearchWindow(jid);
|
|
727 if (!win_entry)
|
|
728 return;
|
|
729
|
|
730 // Scroll up half a screen (or less)
|
|
731 nblines = CHAT_WIN_HEIGHT/2-1;
|
|
732 hbuf_top = win_entry->top;
|
|
733 if (!hbuf_top) {
|
|
734 hbuf_top = g_list_last(win_entry->hbuf);
|
109
|
735 if (!win_entry->cleared)
|
|
736 nblines *= 3;
|
|
737 else
|
|
738 win_entry->cleared = FALSE;
|
105
|
739 }
|
|
740
|
|
741 n = 0;
|
|
742 while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) {
|
|
743 hbuf_top = g_list_previous(hbuf_top);
|
|
744 n++;
|
|
745 }
|
|
746 win_entry->top = hbuf_top;
|
|
747
|
|
748 // Refresh the window
|
|
749 scr_UpdateWindow(win_entry);
|
|
750
|
|
751 // Finished :)
|
|
752 update_panels();
|
|
753 doupdate();
|
|
754 }
|
|
755
|
143
|
756 // scr_ScrollDown()
|
|
757 // Scroll down the current buddy window, half a screen.
|
105
|
758 void scr_ScrollDown(void)
|
|
759 {
|
|
760 const gchar *jid;
|
|
761 window_entry_t *win_entry;
|
|
762 int n, nblines;
|
|
763 GList *hbuf_top;
|
|
764
|
|
765 // Get win_entry
|
|
766 if (!current_buddy)
|
|
767 return;
|
|
768 jid = CURRENT_JID;
|
|
769 if (!jid)
|
|
770 return;
|
|
771 win_entry = scr_SearchWindow(jid);
|
|
772 if (!win_entry)
|
|
773 return;
|
|
774
|
|
775 // Scroll down half a screen (or less)
|
|
776 nblines = CHAT_WIN_HEIGHT/2-1;
|
|
777 hbuf_top = win_entry->top;
|
|
778
|
|
779 for (n=0 ; hbuf_top && n < nblines ; n++)
|
|
780 hbuf_top = g_list_next(hbuf_top);
|
|
781 win_entry->top = hbuf_top;
|
|
782 // Check if we are at the bottom
|
|
783 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++)
|
|
784 hbuf_top = g_list_next(hbuf_top);
|
|
785 if (!hbuf_top)
|
|
786 win_entry->top = NULL; // End reached
|
|
787
|
|
788 // Refresh the window
|
|
789 scr_UpdateWindow(win_entry);
|
|
790
|
|
791 // Finished :)
|
|
792 update_panels();
|
|
793 doupdate();
|
|
794 }
|
|
795
|
143
|
796 // scr_Clear()
|
|
797 // Clear the current buddy window (used for the /clear command)
|
108
|
798 void scr_Clear(void)
|
|
799 {
|
|
800 const gchar *jid;
|
|
801 window_entry_t *win_entry;
|
|
802
|
|
803 // Get win_entry
|
|
804 if (!current_buddy)
|
|
805 return;
|
|
806 jid = CURRENT_JID;
|
|
807 if (!jid)
|
|
808 return;
|
|
809 win_entry = scr_SearchWindow(jid);
|
|
810 if (!win_entry)
|
|
811 return;
|
|
812
|
|
813 win_entry->cleared = TRUE;
|
109
|
814 win_entry->top = NULL;
|
108
|
815
|
|
816 // Refresh the window
|
|
817 scr_UpdateWindow(win_entry);
|
|
818
|
|
819 // Finished :)
|
|
820 update_panels();
|
|
821 doupdate();
|
|
822 }
|
|
823
|
44
|
824 // scr_LogPrint(...)
|
|
825 // Display a message in the log window.
|
24
|
826 void scr_LogPrint(const char *fmt, ...)
|
|
827 {
|
|
828 time_t timestamp;
|
|
829 char *buffer;
|
|
830 va_list ap;
|
|
831
|
|
832 buffer = (char *) calloc(1, 4096);
|
|
833
|
|
834 timestamp = time(NULL);
|
|
835 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp));
|
|
836 wprintw(logWnd, "\n%s", buffer);
|
|
837
|
|
838 va_start(ap, fmt);
|
|
839 vsnprintf(buffer, 4096, fmt, ap);
|
|
840 va_end(ap);
|
|
841
|
|
842 wprintw(logWnd, "%s", buffer);
|
|
843 free(buffer);
|
|
844
|
|
845 update_panels();
|
|
846 doupdate();
|
|
847 }
|
|
848
|
143
|
849 // scr_set_chatmode()
|
|
850 // Public fonction to (un)set chatmode...
|
129
|
851 inline void scr_set_chatmode(int enable)
|
|
852 {
|
|
853 chatmode = enable;
|
|
854 }
|
|
855
|
98
|
856 // which_row()
|
|
857 // Tells which row our cursor is in, in the command line.
|
|
858 // -1 -> normal text
|
|
859 // 0 -> command
|
|
860 // 1 -> parameter 1 (etc.)
|
102
|
861 // If > 0, then *p_row is set to the beginning of the row
|
|
862 int which_row(char **p_row)
|
98
|
863 {
|
|
864 int row = -1;
|
|
865 char *p;
|
|
866 int quote = FALSE;
|
|
867
|
|
868 // Not a command?
|
|
869 if ((ptr_inputline == inputLine) || (inputLine[0] != '/'))
|
|
870 return -1;
|
|
871
|
|
872 // This is a command
|
|
873 row = 0;
|
|
874 for (p = inputLine ; p < ptr_inputline ; p++) {
|
|
875 if (quote) {
|
|
876 if (*p == '"' && *(p-1) != '\\')
|
|
877 quote = FALSE;
|
|
878 continue;
|
|
879 }
|
|
880 if (*p == '"' && *(p-1) != '\\') {
|
|
881 quote = TRUE;
|
121
|
882 } else if (*p == ' ') {
|
|
883 if (*(p-1) != ' ')
|
|
884 row++;
|
102
|
885 *p_row = p+1;
|
|
886 }
|
98
|
887 }
|
|
888 return row;
|
|
889 }
|
|
890
|
143
|
891 // scr_insert_text()
|
|
892 // Insert the given text at the current cursor position.
|
|
893 // The cursor is moved. We don't check if the cursor still is in the screen
|
|
894 // after, the caller should do that.
|
98
|
895 void scr_insert_text(const char *text)
|
|
896 {
|
|
897 char tmpLine[INPUTLINE_LENGTH+1];
|
|
898 int len = strlen(text);
|
|
899 // Check the line isn't too long
|
|
900 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) {
|
|
901 scr_LogPrint("Cannot insert text, line too long.");
|
|
902 return;
|
|
903 }
|
|
904
|
|
905 strcpy(tmpLine, ptr_inputline);
|
|
906 strcpy(ptr_inputline, text); ptr_inputline += len;
|
|
907 strcpy(ptr_inputline, tmpLine);
|
|
908 }
|
|
909
|
143
|
910 // scr_handle_tab()
|
|
911 // Function called when tab is pressed.
|
|
912 // Initiate or continue a completion...
|
98
|
913 void scr_handle_tab(void)
|
|
914 {
|
102
|
915 int nrow;
|
|
916 char *row;
|
|
917 const char *cchar;
|
103
|
918 guint compl_categ;
|
98
|
919
|
102
|
920 nrow = which_row(&row);
|
98
|
921
|
103
|
922 // a) No completion if no leading slash ('cause not a command)
|
|
923 // b) We can't have more than 2 parameters (we use 2 flags)
|
|
924 if (nrow < 0 || nrow > 2) return;
|
102
|
925
|
103
|
926 if (nrow == 0) { // Command completion
|
|
927 row = &inputLine[1];
|
|
928 compl_categ = COMPL_CMD;
|
|
929 } else { // Other completion, depending on the command
|
|
930 cmd *com = cmd_get(inputLine);
|
|
931 if (!com || !row) {
|
|
932 scr_LogPrint("I cannot complete that...");
|
|
933 return;
|
|
934 }
|
|
935 compl_categ = com->completion_flags[nrow-1];
|
|
936 }
|
|
937
|
|
938 if (!completion_started) {
|
|
939 GSList *list = compl_get_category_list(compl_categ);
|
|
940 if (list) {
|
|
941 char *prefix = g_strndup(row, ptr_inputline-row);
|
|
942 // Init completion
|
|
943 new_completion(prefix, list);
|
|
944 g_free(prefix);
|
|
945 // Now complete
|
98
|
946 cchar = complete();
|
|
947 if (cchar)
|
|
948 scr_insert_text(cchar);
|
103
|
949 completion_started = TRUE;
|
98
|
950 }
|
103
|
951 } else { // Completion already initialized
|
|
952 char *c;
|
|
953 guint back = cancel_completion();
|
|
954 // Remove $back chars
|
|
955 ptr_inputline -= back;
|
|
956 c = ptr_inputline;
|
|
957 for ( ; *c ; c++)
|
|
958 *c = *(c+back);
|
|
959 // Now complete again
|
|
960 cchar = complete();
|
|
961 if (cchar)
|
|
962 scr_insert_text(cchar);
|
102
|
963 }
|
98
|
964 }
|
|
965
|
|
966 void scr_cancel_current_completion(void)
|
|
967 {
|
|
968 char *c;
|
|
969 guint back = cancel_completion();
|
|
970 // Remove $back chars
|
|
971 ptr_inputline -= back;
|
|
972 c = ptr_inputline;
|
|
973 for ( ; *c ; c++)
|
|
974 *c = *(c+back);
|
|
975 }
|
|
976
|
|
977 void scr_end_current_completion(void)
|
|
978 {
|
|
979 done_completion();
|
|
980 completion_started = FALSE;
|
|
981 }
|
|
982
|
24
|
983 // check_offset(int direction)
|
|
984 // Check inputline_offset value, and make sure the cursor is inside the
|
|
985 // screen.
|
|
986 inline void check_offset(int direction)
|
|
987 {
|
|
988 // Left side
|
|
989 if (inputline_offset && direction <= 0) {
|
|
990 while (ptr_inputline <= (char*)&inputLine + inputline_offset) {
|
|
991 if (inputline_offset) {
|
|
992 inputline_offset -= 5;
|
|
993 if (inputline_offset < 0)
|
|
994 inputline_offset = 0;
|
|
995 }
|
|
996 }
|
|
997 }
|
|
998 // Right side
|
|
999 if (direction >= 0) {
|
|
1000 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX)
|
|
1001 inputline_offset += 5;
|
|
1002 }
|
|
1003 }
|
|
1004
|
44
|
1005 // process_key(key)
|
|
1006 // Handle the pressed key, in the command line (bottom).
|
29
|
1007 int process_key(int key)
|
24
|
1008 {
|
|
1009 if (isprint(key)) {
|
|
1010 char tmpLine[INPUTLINE_LENGTH+1];
|
|
1011
|
|
1012 // Check the line isn't too long
|
|
1013 if (strlen(inputLine) >= INPUTLINE_LENGTH)
|
|
1014 return 0;
|
|
1015
|
|
1016 // Insert char
|
|
1017 strcpy(tmpLine, ptr_inputline);
|
|
1018 *ptr_inputline++ = key;
|
|
1019 strcpy(ptr_inputline, tmpLine);
|
|
1020 check_offset(1);
|
|
1021 } else {
|
|
1022 switch(key) {
|
|
1023 case KEY_BACKSPACE:
|
|
1024 if (ptr_inputline != (char*)&inputLine) {
|
42
|
1025 char *c = --ptr_inputline;
|
|
1026 for ( ; *c ; c++)
|
|
1027 *c = *(c+1);
|
24
|
1028 check_offset(-1);
|
|
1029 }
|
|
1030 break;
|
|
1031 case KEY_DC:
|
|
1032 if (*ptr_inputline)
|
|
1033 strcpy(ptr_inputline, ptr_inputline+1);
|
|
1034 break;
|
|
1035 case KEY_LEFT:
|
|
1036 if (ptr_inputline != (char*)&inputLine) {
|
|
1037 ptr_inputline--;
|
|
1038 check_offset(-1);
|
|
1039 }
|
|
1040 break;
|
|
1041 case KEY_RIGHT:
|
|
1042 if (*ptr_inputline)
|
|
1043 ptr_inputline++;
|
|
1044 check_offset(1);
|
|
1045 break;
|
98
|
1046 case 7: // Ctrl-g
|
|
1047 scr_cancel_current_completion();
|
|
1048 scr_end_current_completion();
|
|
1049 check_offset(-1);
|
|
1050 break;
|
24
|
1051 case 9: // Tab
|
98
|
1052 scr_handle_tab();
|
|
1053 check_offset(0);
|
24
|
1054 break;
|
|
1055 case '\n': // Enter
|
29
|
1056 if (process_line(inputLine))
|
24
|
1057 return 255;
|
|
1058 ptr_inputline = inputLine;
|
|
1059 *ptr_inputline = 0;
|
|
1060 inputline_offset = 0;
|
|
1061 break;
|
|
1062 case KEY_UP:
|
81
|
1063 scr_RosterUp();
|
24
|
1064 break;
|
|
1065 case KEY_DOWN:
|
81
|
1066 scr_RosterDown();
|
24
|
1067 break;
|
|
1068 case KEY_PPAGE:
|
105
|
1069 scr_ScrollUp();
|
24
|
1070 break;
|
|
1071 case KEY_NPAGE:
|
105
|
1072 scr_ScrollDown();
|
24
|
1073 break;
|
|
1074 case KEY_HOME:
|
|
1075 case 1:
|
|
1076 ptr_inputline = inputLine;
|
|
1077 inputline_offset = 0;
|
|
1078 break;
|
|
1079 case KEY_END:
|
|
1080 case 5:
|
|
1081 for (; *ptr_inputline; ptr_inputline++) ;
|
|
1082 check_offset(1);
|
|
1083 break;
|
|
1084 case 21: // Ctrl-u
|
|
1085 strcpy(inputLine, ptr_inputline);
|
|
1086 ptr_inputline = inputLine;
|
|
1087 inputline_offset = 0;
|
|
1088 break;
|
|
1089 case KEY_EOL:
|
|
1090 case 11: // Ctrl-k
|
|
1091 *ptr_inputline = 0;
|
|
1092 break;
|
|
1093 case 16: // Ctrl-p
|
|
1094 scr_LogPrint("Ctrl-p not yet implemented");
|
|
1095 break;
|
|
1096 case 14: // Ctrl-n
|
|
1097 scr_LogPrint("Ctrl-n not yet implemented");
|
|
1098 break;
|
|
1099 case 27: // ESC
|
|
1100 currentWindow = NULL;
|
|
1101 chatmode = FALSE;
|
119
|
1102 if (current_buddy)
|
|
1103 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
24
|
1104 top_panel(chatPanel);
|
|
1105 top_panel(inputPanel);
|
|
1106 break;
|
|
1107 default:
|
|
1108 scr_LogPrint("Unkown key=%d", key);
|
|
1109 }
|
|
1110 }
|
98
|
1111 if (completion_started && key != 9)
|
|
1112 scr_end_current_completion();
|
24
|
1113 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset);
|
|
1114 wclrtoeol(inputWnd);
|
|
1115 if (*ptr_inputline) {
|
|
1116 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset);
|
|
1117 }
|
|
1118 update_panels();
|
|
1119 doupdate();
|
|
1120 return 0;
|
|
1121 }
|