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>
|
232
|
9 #include <langinfo.h>
|
24
|
10
|
|
11 #include "screen.h"
|
81
|
12 #include "hbuf.h"
|
47
|
13 #include "commands.h"
|
95
|
14 #include "compl.h"
|
81
|
15 #include "roster.h"
|
180
|
16 #include "histolog.h"
|
24
|
17 #include "parsecfg.h"
|
81
|
18 #include "utils.h"
|
24
|
19 #include "list.h"
|
|
20
|
|
21 #define window_entry(n) list_entry(n, window_entry_t, list)
|
|
22
|
151
|
23 inline void check_offset(int);
|
|
24
|
24
|
25 LIST_HEAD(window_list);
|
|
26
|
|
27 typedef struct _window_entry_t {
|
|
28 WINDOW *win;
|
108
|
29 PANEL *panel;
|
|
30 char *name;
|
|
31 GList *hbuf;
|
181
|
32 GList *top; // If top is NULL, we'll display the last lines
|
|
33 char cleared; // For ex, user has issued a /clear command...
|
24
|
34 struct list_head list;
|
|
35 } window_entry_t;
|
|
36
|
|
37
|
|
38 static WINDOW *rosterWnd, *chatWnd, *inputWnd;
|
|
39 static WINDOW *logWnd, *logWnd_border;
|
|
40 static PANEL *rosterPanel, *chatPanel, *inputPanel;
|
|
41 static PANEL *logPanel, *logPanel_border;
|
|
42 static int maxY, maxX;
|
|
43 static window_entry_t *currentWindow;
|
|
44
|
|
45 static int chatmode;
|
30
|
46 int update_roster;
|
232
|
47 int utf8_mode = 0;
|
24
|
48
|
174
|
49 static char inputLine[INPUTLINE_LENGTH+1];
|
|
50 static char *ptr_inputline;
|
|
51 static short int inputline_offset;
|
|
52 static int completion_started;
|
173
|
53 static GList *cmdhisto;
|
|
54 static GList *cmdhisto_cur;
|
174
|
55 static char cmdhisto_backup[INPUTLINE_LENGTH+1];
|
24
|
56
|
|
57
|
99
|
58 /* Functions */
|
24
|
59
|
74
|
60 int scr_WindowWidth(WINDOW * win)
|
24
|
61 {
|
|
62 int x, y;
|
|
63 getmaxyx(win, y, x);
|
|
64 return x;
|
|
65 }
|
|
66
|
74
|
67 void scr_clear_box(WINDOW *win, int y, int x, int height, int width, int Color)
|
|
68 {
|
|
69 int i, j;
|
|
70
|
|
71 wattrset(win, COLOR_PAIR(Color));
|
|
72 for (i = 0; i < height; i++) {
|
|
73 wmove(win, y + i, x);
|
|
74 for (j = 0; j < width; j++)
|
|
75 wprintw(win, " ");
|
|
76 }
|
|
77 }
|
|
78
|
24
|
79 void scr_draw_box(WINDOW * win, int y, int x, int height, int width,
|
|
80 int Color, chtype box, chtype border)
|
|
81 {
|
|
82 int i, j;
|
|
83
|
|
84 wattrset(win, COLOR_PAIR(Color));
|
|
85 for (i = 0; i < height; i++) {
|
|
86 wmove(win, y + i, x);
|
|
87 for (j = 0; j < width; j++)
|
|
88 if (!i && !j)
|
|
89 waddch(win, border | ACS_ULCORNER);
|
|
90 else if (i == height - 1 && !j)
|
|
91 waddch(win, border | ACS_LLCORNER);
|
|
92 else if (!i && j == width - 1)
|
|
93 waddch(win, box | ACS_URCORNER);
|
|
94 else if (i == height - 1 && j == width - 1)
|
|
95 waddch(win, box | ACS_LRCORNER);
|
|
96 else if (!i)
|
|
97 waddch(win, border | ACS_HLINE);
|
|
98 else if (i == height - 1)
|
|
99 waddch(win, box | ACS_HLINE);
|
|
100 else if (!j)
|
|
101 waddch(win, border | ACS_VLINE);
|
|
102 else if (j == width - 1)
|
|
103 waddch(win, box | ACS_VLINE);
|
|
104 else
|
|
105 waddch(win, box | ' ');
|
|
106 }
|
|
107 }
|
|
108
|
|
109 int FindColor(char *name)
|
|
110 {
|
|
111 if (!strcmp(name, "default"))
|
|
112 return -1;
|
|
113 if (!strcmp(name, "black"))
|
|
114 return COLOR_BLACK;
|
|
115 if (!strcmp(name, "red"))
|
|
116 return COLOR_RED;
|
|
117 if (!strcmp(name, "green"))
|
|
118 return COLOR_GREEN;
|
|
119 if (!strcmp(name, "yellow"))
|
|
120 return COLOR_YELLOW;
|
|
121 if (!strcmp(name, "blue"))
|
|
122 return COLOR_BLUE;
|
|
123 if (!strcmp(name, "magenta"))
|
|
124 return COLOR_MAGENTA;
|
|
125 if (!strcmp(name, "cyan"))
|
|
126 return COLOR_CYAN;
|
|
127 if (!strcmp(name, "white"))
|
|
128 return COLOR_WHITE;
|
|
129
|
|
130 return -1;
|
|
131 }
|
|
132
|
|
133 void ParseColors(void)
|
|
134 {
|
|
135 char *colors[11] = {
|
|
136 "", "",
|
|
137 "borderlines",
|
|
138 "jidonline",
|
139
|
139 "newmsg",
|
24
|
140 "jidofflineselected",
|
|
141 "jidoffline",
|
|
142 "text",
|
|
143 NULL
|
|
144 };
|
|
145
|
|
146 char *tmp = malloc(1024);
|
|
147 char *color1;
|
|
148 char *background = cfg_read("color_background");
|
|
149 char *backselected = cfg_read("color_backselected");
|
|
150 int i = 0;
|
|
151
|
|
152 while (colors[i]) {
|
|
153 sprintf(tmp, "color_%s", colors[i]);
|
|
154 color1 = cfg_read(tmp);
|
|
155
|
|
156 switch (i + 1) {
|
|
157 case 1:
|
|
158 init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
|
159 break;
|
|
160 case 2:
|
|
161 init_pair(2, COLOR_WHITE, COLOR_BLACK);
|
|
162 break;
|
|
163 case 3:
|
|
164 init_pair(3, FindColor(color1), FindColor(background));
|
|
165 break;
|
|
166 case 4:
|
|
167 init_pair(4, FindColor(color1), FindColor(backselected));
|
|
168 break;
|
|
169 case 5:
|
|
170 init_pair(5, FindColor(color1), FindColor(background));
|
|
171 break;
|
|
172 case 6:
|
|
173 init_pair(6, FindColor(color1), FindColor(backselected));
|
|
174 break;
|
|
175 case 7:
|
|
176 init_pair(7, FindColor(color1), FindColor(background));
|
|
177 break;
|
|
178 case 8:
|
|
179 init_pair(8, FindColor(color1), FindColor(background));
|
|
180 break;
|
|
181 }
|
|
182 i++;
|
|
183 }
|
|
184 }
|
|
185
|
|
186
|
151
|
187 window_entry_t *scr_CreateBuddyPanel(const char *title, int dont_show)
|
24
|
188 {
|
151
|
189 int x;
|
|
190 int y;
|
|
191 int lines;
|
|
192 int cols;
|
153
|
193 window_entry_t *tmp;
|
|
194
|
|
195 do {
|
|
196 tmp = calloc(1, sizeof(window_entry_t));
|
|
197 } while (!tmp);
|
24
|
198
|
151
|
199 // Dimensions
|
|
200 x = ROSTER_WIDTH;
|
|
201 y = 0;
|
|
202 lines = CHAT_WIN_HEIGHT;
|
|
203 cols = maxX - ROSTER_WIDTH;
|
|
204
|
24
|
205 tmp->win = newwin(lines, cols, y, x);
|
154
|
206 while (!tmp->win) {
|
|
207 usleep(250);
|
|
208 tmp->win = newwin(lines, cols, y, x);
|
|
209 }
|
168
|
210 wbkgd(tmp->win, COLOR_PAIR(COLOR_GENERAL));
|
24
|
211 tmp->panel = new_panel(tmp->win);
|
153
|
212 tmp->name = (char *) calloc(1, 96);
|
|
213 strncpy(tmp->name, title, 96);
|
24
|
214
|
143
|
215 if (!dont_show) {
|
24
|
216 currentWindow = tmp;
|
|
217 } else {
|
|
218 if (currentWindow)
|
|
219 top_panel(currentWindow->panel);
|
|
220 else
|
|
221 top_panel(chatPanel);
|
|
222 }
|
143
|
223 update_panels();
|
24
|
224
|
181
|
225 // Load buddy history from file (if enabled)
|
185
|
226 hlog_read_history(title, &tmp->hbuf, maxX - ROSTER_WIDTH - PREFIX_WIDTH);
|
181
|
227
|
24
|
228 list_add_tail(&tmp->list, &window_list);
|
|
229
|
|
230 return tmp;
|
|
231 }
|
|
232
|
50
|
233 window_entry_t *scr_SearchWindow(const char *winId)
|
24
|
234 {
|
|
235 struct list_head *pos, *n;
|
|
236 window_entry_t *search_entry = NULL;
|
|
237
|
|
238 list_for_each_safe(pos, n, &window_list) {
|
|
239 search_entry = window_entry(pos);
|
|
240 if (search_entry->name) {
|
|
241 if (!strcasecmp(search_entry->name, winId)) {
|
|
242 return search_entry;
|
|
243 }
|
|
244 }
|
|
245 }
|
|
246 return NULL;
|
|
247 }
|
|
248
|
143
|
249 // scr_UpdateWindow()
|
|
250 // (Re-)Display the given chat window.
|
74
|
251 void scr_UpdateWindow(window_entry_t *win_entry)
|
|
252 {
|
|
253 int n;
|
|
254 int width;
|
184
|
255 hbb_line **lines, *line;
|
74
|
256 GList *hbuf_head;
|
184
|
257 char date[32];
|
74
|
258
|
108
|
259 width = scr_WindowWidth(win_entry->win);
|
|
260
|
|
261 // Should the window be empty?
|
|
262 if (win_entry->cleared) {
|
168
|
263 werase(win_entry->win);
|
108
|
264 return;
|
|
265 }
|
|
266
|
105
|
267 // win_entry->top is the top message of the screen. If it set to NULL, we
|
|
268 // are displaying the last messages.
|
|
269
|
74
|
270 // We will show the last CHAT_WIN_HEIGHT lines.
|
|
271 // Let's find out where it begins.
|
105
|
272 if (!win_entry->top ||
|
|
273 (g_list_position(g_list_first(win_entry->hbuf), win_entry->top) == -1)) {
|
|
274 // Move up CHAT_WIN_HEIGHT lines
|
|
275 win_entry->hbuf = g_list_last(win_entry->hbuf);
|
|
276 hbuf_head = win_entry->hbuf;
|
|
277 win_entry->top = NULL; // (Just to make sure)
|
|
278 n = 0;
|
|
279 while (hbuf_head && (n < CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head)) {
|
|
280 hbuf_head = g_list_previous(hbuf_head);
|
|
281 n++;
|
|
282 }
|
|
283 } else
|
|
284 hbuf_head = win_entry->top;
|
74
|
285
|
|
286 // Get the last CHAT_WIN_HEIGHT lines.
|
|
287 lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT);
|
|
288
|
|
289 // Display these lines
|
|
290 for (n = 0; n < CHAT_WIN_HEIGHT; n++) {
|
168
|
291 wmove(win_entry->win, n, 0);
|
184
|
292 line = *(lines+n);
|
185
|
293 // NOTE: update PREFIX_WIDTH if you change the date format!!
|
|
294 // You need to set it to the whole prefix length + 1
|
184
|
295 if (line) {
|
|
296 if (line->timestamp) {
|
185
|
297 strftime(date, 35, "%m-%d %H:%M", localtime(&line->timestamp));
|
184
|
298 } else
|
185
|
299 strcpy(date, " ");
|
197
|
300 if (line->flags & HBB_PREFIX_INFO) {
|
|
301 char dir = '*';
|
|
302 if (line->flags & HBB_PREFIX_IN)
|
|
303 dir = '<';
|
|
304 else if (line->flags & HBB_PREFIX_OUT)
|
|
305 dir = '>';
|
|
306 wprintw(win_entry->win, "%.11s *%c* ", date, dir);
|
|
307 } else if (line->flags & HBB_PREFIX_IN)
|
185
|
308 wprintw(win_entry->win, "%.11s <== ", date);
|
184
|
309 else if (line->flags & HBB_PREFIX_OUT)
|
185
|
310 wprintw(win_entry->win, "%.11s --> ", date);
|
75
|
311 else {
|
185
|
312 wprintw(win_entry->win, "%.11s ", date);
|
75
|
313 }
|
184
|
314 wprintw(win_entry->win, "%s", line->text); // line
|
168
|
315 wclrtoeol(win_entry->win);
|
184
|
316 g_free(line->text);
|
168
|
317 } else {
|
|
318 wclrtobot(win_entry->win);
|
|
319 break;
|
75
|
320 }
|
74
|
321 }
|
|
322 g_free(lines);
|
|
323 }
|
|
324
|
143
|
325 // scr_ShowWindow()
|
|
326 // Display the chat window with the given identifier.
|
50
|
327 void scr_ShowWindow(const char *winId)
|
24
|
328 {
|
74
|
329 window_entry_t *win_entry = scr_SearchWindow(winId);
|
|
330
|
181
|
331 if (!win_entry)
|
180
|
332 win_entry = scr_CreateBuddyPanel(winId, FALSE);
|
74
|
333
|
180
|
334 top_panel(win_entry->panel);
|
|
335 currentWindow = win_entry;
|
|
336 chatmode = TRUE;
|
|
337 roster_msg_setflag(winId, FALSE);
|
|
338 roster_setflags(winId, ROSTER_FLAG_LOCK, TRUE);
|
|
339 update_roster = TRUE;
|
74
|
340
|
180
|
341 // Refresh the window
|
|
342 scr_UpdateWindow(win_entry);
|
|
343
|
|
344 // Finished :)
|
|
345 update_panels();
|
142
|
346
|
|
347 top_panel(inputPanel);
|
24
|
348 }
|
|
349
|
143
|
350 // scr_ShowBuddyWindow()
|
|
351 // Display the chat window buffer for the current buddy.
|
24
|
352 void scr_ShowBuddyWindow(void)
|
|
353 {
|
105
|
354 const gchar *jid;
|
140
|
355
|
105
|
356 if (!current_buddy)
|
140
|
357 jid = NULL;
|
|
358 else
|
|
359 jid = CURRENT_JID;
|
|
360
|
|
361 if (!jid) {
|
|
362 top_panel(chatPanel);
|
143
|
363 top_panel(inputPanel);
|
140
|
364 currentWindow = NULL;
|
105
|
365 return;
|
140
|
366 }
|
|
367
|
105
|
368 scr_ShowWindow(jid);
|
24
|
369 }
|
|
370
|
|
371
|
143
|
372 // scr_WriteInWindow()
|
|
373 // Write some text in the winId window (this usually is a jid).
|
|
374 // Lines are splitted when they are too long to fit in the chat window.
|
|
375 // If this window doesn't exist, it is created.
|
184
|
376 void scr_WriteInWindow(const char *winId, const char *text, time_t timestamp,
|
|
377 unsigned int prefix_flags, int force_show)
|
24
|
378 {
|
74
|
379 window_entry_t *win_entry;
|
24
|
380 int dont_show = FALSE;
|
|
381
|
74
|
382 // Look for the window entry.
|
|
383 win_entry = scr_SearchWindow(winId);
|
|
384
|
|
385 // Do we have to really show the window?
|
24
|
386 if (!chatmode)
|
|
387 dont_show = TRUE;
|
74
|
388 else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry))))
|
24
|
389 dont_show = TRUE;
|
|
390
|
74
|
391 // If the window entry doesn't exist yet, let's create it.
|
|
392 if (win_entry == NULL) {
|
151
|
393 win_entry = scr_CreateBuddyPanel(winId, dont_show);
|
24
|
394 }
|
|
395
|
220
|
396 // The message must be displayed -> update top pointer
|
|
397 if (win_entry->cleared)
|
|
398 win_entry->top = g_list_last(win_entry->hbuf);
|
|
399
|
184
|
400 hbuf_add_line(&win_entry->hbuf, text, timestamp, prefix_flags,
|
185
|
401 maxX - ROSTER_WIDTH - PREFIX_WIDTH);
|
74
|
402
|
108
|
403 if (win_entry->cleared) {
|
220
|
404 win_entry->cleared = FALSE;
|
|
405 if (g_list_next(win_entry->top))
|
|
406 win_entry->top = g_list_next(win_entry->top);
|
|
407 }
|
|
408
|
|
409 // Make sure the last line appears in the window; update top if necessary
|
|
410 if (win_entry->top) {
|
|
411 int dist;
|
|
412 GList *first = g_list_first(win_entry->hbuf);
|
|
413 dist = g_list_position(first, g_list_last(win_entry->hbuf)) -
|
|
414 g_list_position(first, win_entry->top);
|
|
415 if (dist >= CHAT_WIN_HEIGHT)
|
|
416 win_entry->top = NULL;
|
108
|
417 }
|
|
418
|
24
|
419 if (!dont_show) {
|
74
|
420 // Show and refresh the window
|
|
421 top_panel(win_entry->panel);
|
|
422 scr_UpdateWindow(win_entry);
|
142
|
423 top_panel(inputPanel);
|
24
|
424 update_panels();
|
|
425 doupdate();
|
|
426 } else {
|
148
|
427 roster_msg_setflag(winId, TRUE);
|
30
|
428 update_roster = TRUE;
|
24
|
429 }
|
|
430 }
|
|
431
|
|
432 void scr_InitCurses(void)
|
|
433 {
|
|
434 initscr();
|
|
435 noecho();
|
|
436 raw();
|
35
|
437 halfdelay(5);
|
24
|
438 start_color();
|
|
439 use_default_colors();
|
|
440
|
|
441 ParseColors();
|
|
442
|
|
443 getmaxyx(stdscr, maxY, maxX);
|
167
|
444 if (maxY < LOG_WIN_HEIGHT+2)
|
|
445 maxY = LOG_WIN_HEIGHT+2;
|
24
|
446 inputLine[0] = 0;
|
|
447 ptr_inputline = inputLine;
|
|
448
|
35
|
449 setlocale(LC_CTYPE, "");
|
232
|
450 utf8_mode = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0);
|
24
|
451
|
|
452 return;
|
|
453 }
|
|
454
|
81
|
455 void scr_TerminateCurses(void)
|
|
456 {
|
|
457 clear();
|
|
458 refresh();
|
|
459 endwin();
|
|
460 return;
|
|
461 }
|
|
462
|
151
|
463 // scr_DrawMainWindow()
|
157
|
464 // Set fullinit to TRUE to also create panels. Set it to FALSE for a resize.
|
151
|
465 //
|
|
466 // I think it could be improved a _lot_ but I'm really not an ncurses
|
|
467 // expert... :-\ Mikael.
|
|
468 //
|
|
469 void scr_DrawMainWindow(unsigned int fullinit)
|
24
|
470 {
|
157
|
471 if (fullinit) {
|
|
472 /* Create windows */
|
|
473 rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0);
|
|
474 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH);
|
|
475 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0);
|
|
476 logWnd = newwin(LOG_WIN_HEIGHT-2, maxX-2, CHAT_WIN_HEIGHT+1, 1);
|
|
477 inputWnd = newwin(1, maxX, maxY-1, 0);
|
168
|
478 wbkgd(rosterWnd, COLOR_PAIR(COLOR_GENERAL));
|
|
479 wbkgd(chatWnd, COLOR_PAIR(COLOR_GENERAL));
|
|
480 wbkgd(logWnd_border, COLOR_PAIR(COLOR_GENERAL));
|
|
481 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL));
|
157
|
482 } else {
|
|
483 /* Resize windows */
|
|
484 wresize(rosterWnd, CHAT_WIN_HEIGHT, ROSTER_WIDTH);
|
|
485 wresize(chatWnd, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH);
|
|
486
|
|
487 wresize(logWnd_border, LOG_WIN_HEIGHT, maxX);
|
|
488 wresize(logWnd, LOG_WIN_HEIGHT-2, maxX-2);
|
|
489 mvwin(logWnd_border, CHAT_WIN_HEIGHT, 0);
|
|
490 mvwin(logWnd, CHAT_WIN_HEIGHT+1, 1);
|
|
491
|
|
492 wresize(inputWnd, 1, maxX);
|
|
493 mvwin(inputWnd, maxY-1, 0);
|
168
|
494
|
|
495 werase(chatWnd);
|
157
|
496 }
|
151
|
497
|
|
498 /* Draw/init windows */
|
|
499
|
74
|
500 mvwprintw(chatWnd, 0, 0, "This is the status window");
|
24
|
501
|
151
|
502 // - Draw/clear the log window
|
24
|
503 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0);
|
157
|
504 // Auto-scrolling in log window
|
74
|
505 scrollok(logWnd, TRUE);
|
24
|
506
|
|
507
|
151
|
508 if (fullinit) {
|
157
|
509 // Enable keypad (+ special keys)
|
|
510 keypad(inputWnd, TRUE);
|
|
511
|
151
|
512 // Create panels
|
|
513 rosterPanel = new_panel(rosterWnd);
|
|
514 chatPanel = new_panel(chatWnd);
|
|
515 logPanel_border = new_panel(logWnd_border);
|
|
516 logPanel = new_panel(logWnd);
|
|
517 inputPanel = new_panel(inputWnd);
|
232
|
518
|
|
519 if (utf8_mode)
|
|
520 scr_LogPrint("WARNING: UTF-8 not yet supported!");
|
157
|
521 } else {
|
|
522 // Update panels
|
|
523 replace_panel(rosterPanel, rosterWnd);
|
|
524 replace_panel(chatPanel, chatWnd);
|
|
525 replace_panel(logPanel, logWnd);
|
|
526 replace_panel(logPanel_border, logWnd_border);
|
|
527 replace_panel(inputPanel, inputWnd);
|
151
|
528 }
|
|
529
|
|
530 // We'll need to redraw the roster
|
149
|
531 update_roster = TRUE;
|
24
|
532 return;
|
|
533 }
|
|
534
|
151
|
535 // scr_Resize()
|
|
536 // Function called when the window is resized.
|
157
|
537 // - Resize windows
|
151
|
538 // - Rewrap lines in each buddy buffer
|
|
539 void scr_Resize()
|
|
540 {
|
|
541 struct list_head *pos, *n;
|
|
542 window_entry_t *search_entry;
|
|
543 int x, y, lines, cols;
|
|
544
|
|
545 // First, update the global variables
|
|
546 getmaxyx(stdscr, maxY, maxX);
|
167
|
547 if (maxY < LOG_WIN_HEIGHT+2)
|
|
548 maxY = LOG_WIN_HEIGHT+2;
|
151
|
549 // Make sure the cursor stays inside the window
|
|
550 check_offset(0);
|
|
551
|
157
|
552 // Resize windows and update panels
|
151
|
553 scr_DrawMainWindow(FALSE);
|
|
554
|
|
555 // Resize all buddy windows
|
|
556 x = ROSTER_WIDTH;
|
|
557 y = 0;
|
|
558 lines = CHAT_WIN_HEIGHT;
|
|
559 cols = maxX - ROSTER_WIDTH;
|
|
560
|
|
561 list_for_each_safe(pos, n, &window_list) {
|
|
562 search_entry = window_entry(pos);
|
|
563 if (search_entry->win) {
|
189
|
564 GList *rescue_top;
|
157
|
565 // Resize buddy window (no need to move it)
|
|
566 wresize(search_entry->win, lines, cols);
|
168
|
567 werase(search_entry->win);
|
151
|
568 // If a panel exists, replace the old window with the new
|
|
569 if (search_entry->panel) {
|
|
570 replace_panel(search_entry->panel, search_entry->win);
|
|
571 }
|
|
572 // Redo line wrapping
|
189
|
573 rescue_top = hbuf_previous_persistent(search_entry->top);
|
151
|
574 hbuf_rebuild(&search_entry->hbuf,
|
185
|
575 maxX - ROSTER_WIDTH - PREFIX_WIDTH);
|
189
|
576 if (g_list_position(g_list_first(search_entry->hbuf), search_entry->top) == -1)
|
|
577 search_entry->top = rescue_top;
|
151
|
578 }
|
|
579 }
|
|
580
|
|
581 // Refresh current buddy window
|
157
|
582 if (chatmode)
|
151
|
583 scr_ShowBuddyWindow();
|
|
584 }
|
|
585
|
143
|
586 // scr_DrawRoster()
|
|
587 // Actually, display the buddylist on the screen.
|
81
|
588 void scr_DrawRoster(void)
|
24
|
589 {
|
81
|
590 static guint offset = 0;
|
|
591 char name[ROSTER_WIDTH];
|
|
592 int maxx, maxy;
|
|
593 GList *buddy;
|
|
594 int i, n;
|
|
595 int rOffset;
|
164
|
596 enum imstatus currentstatus = jb_getstatus();
|
81
|
597
|
123
|
598 // We can reset update_roster
|
|
599 update_roster = FALSE;
|
|
600
|
81
|
601 getmaxyx(rosterWnd, maxy, maxx);
|
|
602 maxx --; // last char is for vertical border
|
|
603 name[ROSTER_WIDTH-7] = 0;
|
|
604
|
|
605 // cleanup of roster window
|
168
|
606 werase(rosterWnd);
|
|
607 // Redraw the vertical line (not very good...)
|
81
|
608 wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL));
|
168
|
609 for (i=0 ; i < CHAT_WIN_HEIGHT ; i++)
|
|
610 mvwaddch(rosterWnd, i, ROSTER_WIDTH-1, ACS_VLINE);
|
81
|
611
|
|
612 // Leave now if buddylist is empty
|
|
613 if (!buddylist) {
|
|
614 offset = 0;
|
123
|
615 update_panels();
|
|
616 doupdate();
|
81
|
617 return;
|
|
618 }
|
|
619
|
84
|
620 // Update offset if necessary
|
|
621 i = g_list_position(buddylist, current_buddy);
|
|
622 if (i == -1) { // This is bad
|
|
623 scr_LogPrint("Doh! Can't find current selected buddy!!");
|
|
624 return;
|
|
625 } else if (i < offset) {
|
|
626 offset = i;
|
|
627 } else if (i+1 > offset + maxy) {
|
|
628 offset = i + 1 - maxy;
|
|
629 }
|
81
|
630
|
|
631 buddy = buddylist;
|
|
632 rOffset = offset;
|
|
633
|
84
|
634 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) {
|
81
|
635
|
|
636 char status = '?';
|
|
637 char pending = ' ';
|
|
638 enum imstatus budstate;
|
149
|
639 unsigned short ismsg = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG;
|
|
640 unsigned short isgrp = buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP;
|
|
641 unsigned short ishid = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_HIDE;
|
81
|
642
|
|
643 if (rOffset > 0) {
|
|
644 rOffset--;
|
|
645 continue;
|
|
646 }
|
|
647
|
149
|
648 // Display message notice if there is a message flag, but not
|
|
649 // for unfolded groups.
|
|
650 if (ismsg && (!isgrp || ishid)) {
|
81
|
651 pending = '#';
|
|
652 }
|
|
653
|
|
654 budstate = buddy_getstatus(BUDDATA(buddy));
|
164
|
655 if (budstate >= 0 && budstate < imstatus_size && currentstatus != offline)
|
81
|
656 status = imstatus2char[budstate];
|
|
657 if (buddy == current_buddy) {
|
|
658 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL));
|
|
659 // The 3 following lines aim to color the whole line
|
|
660 wmove(rosterWnd, i, 0);
|
|
661 for (n = 0; n < maxx; n++)
|
|
662 waddch(rosterWnd, ' ');
|
|
663 } else {
|
149
|
664 if (pending == '#')
|
139
|
665 wattrset(rosterWnd, COLOR_PAIR(COLOR_NMSG));
|
|
666 else
|
|
667 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES));
|
81
|
668 }
|
|
669
|
|
670 strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7);
|
149
|
671 if (isgrp) {
|
133
|
672 char *sep;
|
149
|
673 if (ishid)
|
133
|
674 sep = "+++";
|
|
675 else
|
|
676 sep = "---";
|
|
677 mvwprintw(rosterWnd, i, 0, " %c%s %s", pending, sep, name);
|
|
678 }
|
126
|
679 else
|
|
680 mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name);
|
84
|
681
|
|
682 i++;
|
81
|
683 }
|
|
684
|
142
|
685 top_panel(inputPanel);
|
81
|
686 update_panels();
|
|
687 doupdate();
|
24
|
688 }
|
|
689
|
184
|
690 void scr_WriteMessage(const char *jid, const char *text, time_t timestamp,
|
|
691 guint prefix_flags)
|
24
|
692 {
|
184
|
693 if (!timestamp) timestamp = time(NULL);
|
|
694
|
|
695 scr_WriteInWindow(jid, text, timestamp, prefix_flags, FALSE);
|
47
|
696 }
|
|
697
|
190
|
698 // If prefix is NULL, HBB_PREFIX_IN is supposed.
|
184
|
699 void scr_WriteIncomingMessage(const char *jidfrom, const char *text,
|
190
|
700 time_t timestamp, guint prefix)
|
47
|
701 {
|
190
|
702 if (!prefix) prefix = HBB_PREFIX_IN;
|
75
|
703 // FIXME expand tabs / filter out special chars...
|
190
|
704 scr_WriteMessage(jidfrom, text, timestamp, prefix);
|
24
|
705 update_panels();
|
|
706 doupdate();
|
|
707 }
|
|
708
|
50
|
709 void scr_WriteOutgoingMessage(const char *jidto, const char *text)
|
47
|
710 {
|
184
|
711 scr_WriteMessage(jidto, text, 0, HBB_PREFIX_OUT);
|
47
|
712 scr_ShowWindow(jidto);
|
|
713 }
|
|
714
|
24
|
715 int scr_Getch(void)
|
|
716 {
|
|
717 int ch;
|
|
718 ch = wgetch(inputWnd);
|
|
719 return ch;
|
|
720 }
|
|
721
|
|
722 WINDOW *scr_GetRosterWindow(void)
|
|
723 {
|
|
724 return rosterWnd;
|
|
725 }
|
|
726
|
|
727 WINDOW *scr_GetStatusWindow(void)
|
|
728 {
|
|
729 return chatWnd;
|
|
730 }
|
|
731
|
|
732 WINDOW *scr_GetInputWindow(void)
|
|
733 {
|
|
734 return inputWnd;
|
|
735 }
|
|
736
|
143
|
737 // scr_RosterTop()
|
|
738 // Go to the first buddy in the buddylist
|
105
|
739 void scr_RosterTop(void)
|
104
|
740 {
|
143
|
741 enum imstatus prev_st = imstatus_size; // undef
|
119
|
742
|
|
743 if (current_buddy) {
|
|
744 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
745 if (chatmode)
|
|
746 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
747 }
|
104
|
748 current_buddy = buddylist;
|
119
|
749 if (chatmode && current_buddy)
|
|
750 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
751
|
|
752 // We should rebuild the buddylist but not everytime
|
143
|
753 // Here we check if we were locking a buddy who is actually offline,
|
|
754 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
120
|
755 if (current_buddy && prev_st == offline &&
|
|
756 buddylist_get_hide_offline_buddies())
|
119
|
757 buddylist_build();
|
104
|
758 if (chatmode)
|
|
759 scr_ShowBuddyWindow();
|
143
|
760 update_roster = TRUE;
|
104
|
761 }
|
|
762
|
143
|
763 // scr_RosterBottom()
|
|
764 // Go to the last buddy in the buddylist
|
105
|
765 void scr_RosterBottom(void)
|
104
|
766 {
|
143
|
767 enum imstatus prev_st = imstatus_size; // undef
|
119
|
768
|
|
769 if (current_buddy) {
|
|
770 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
771 if (chatmode)
|
|
772 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
773 }
|
104
|
774 current_buddy = g_list_last(buddylist);
|
143
|
775 // Lock the buddy in the buddylist if we're in chat mode
|
119
|
776 if (chatmode && current_buddy)
|
|
777 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
778
|
|
779 // We should rebuild the buddylist but not everytime
|
143
|
780 // Here we check if we were locking a buddy who is actually offline,
|
|
781 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
120
|
782 if (current_buddy && prev_st == offline &&
|
|
783 buddylist_get_hide_offline_buddies())
|
119
|
784 buddylist_build();
|
143
|
785
|
104
|
786 if (chatmode)
|
|
787 scr_ShowBuddyWindow();
|
143
|
788 update_roster = TRUE;
|
104
|
789 }
|
|
790
|
143
|
791 // scr_RosterUp()
|
|
792 // Go to the previous buddy in the buddylist
|
105
|
793 void scr_RosterUp(void)
|
81
|
794 {
|
143
|
795 enum imstatus prev_st = imstatus_size; // undef
|
119
|
796
|
81
|
797 if (current_buddy) {
|
|
798 if (g_list_previous(current_buddy)) {
|
120
|
799 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
119
|
800 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
81
|
801 current_buddy = g_list_previous(current_buddy);
|
143
|
802 // Lock the buddy in the buddylist if we're in chat mode
|
119
|
803 if (chatmode)
|
|
804 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
805 // We should rebuild the buddylist but not everytime
|
143
|
806 // Here we check if we were locking a buddy who is actually offline,
|
|
807 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
120
|
808 if (prev_st == offline && buddylist_get_hide_offline_buddies())
|
119
|
809 buddylist_build();
|
143
|
810 update_roster = TRUE;
|
81
|
811 }
|
|
812 }
|
104
|
813
|
|
814 if (chatmode)
|
|
815 scr_ShowBuddyWindow();
|
81
|
816 }
|
|
817
|
143
|
818 // scr_RosterDown()
|
|
819 // Go to the next buddy in the buddylist
|
105
|
820 void scr_RosterDown(void)
|
81
|
821 {
|
143
|
822 enum imstatus prev_st = imstatus_size; // undef
|
119
|
823
|
81
|
824 if (current_buddy) {
|
|
825 if (g_list_next(current_buddy)) {
|
120
|
826 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
119
|
827 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
81
|
828 current_buddy = g_list_next(current_buddy);
|
119
|
829 if (chatmode)
|
|
830 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
831 // We should rebuild the buddylist but not everytime
|
143
|
832 // Here we check if we were locking a buddy who is actually offline,
|
|
833 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
120
|
834 if (prev_st == offline && buddylist_get_hide_offline_buddies())
|
119
|
835 buddylist_build();
|
143
|
836 update_roster = TRUE;
|
81
|
837 }
|
|
838 }
|
104
|
839
|
|
840 if (chatmode)
|
|
841 scr_ShowBuddyWindow();
|
81
|
842 }
|
|
843
|
236
|
844 // scr_RosterUnreadMessage(next)
|
|
845 // Go to a new message. If next is not null, try to go to the next new
|
|
846 // message. If it is not possible or if next is NULL, go to the first new
|
|
847 // message from unread_list.
|
|
848 void scr_RosterUnreadMessage(int next)
|
|
849 {
|
|
850 enum imstatus prev_st = imstatus_size; // undef
|
|
851
|
|
852 if (current_buddy) {
|
|
853 gpointer unread_ptr;
|
|
854 gpointer refbuddata;
|
|
855 gpointer ngroup;
|
|
856 GList *nbuddy;
|
|
857
|
|
858 if (next) refbuddata = BUDDATA(current_buddy);
|
|
859 else refbuddata = NULL;
|
|
860
|
|
861 unread_ptr = unread_msg(refbuddata);
|
|
862 if (!unread_ptr) return;
|
|
863
|
|
864 // If buddy is in a folded group, we need to expand it
|
|
865 ngroup = buddy_getgroup(unread_ptr);
|
|
866 if (buddy_getflags(ngroup) & ROSTER_FLAG_HIDE) {
|
|
867 buddy_setflags(ngroup, ROSTER_FLAG_HIDE, FALSE);
|
|
868 buddylist_build();
|
|
869 }
|
|
870
|
|
871 nbuddy = g_list_find(buddylist, unread_ptr);
|
|
872 if (nbuddy) {
|
|
873 prev_st = buddy_getstatus(BUDDATA(current_buddy));
|
|
874 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
|
875 current_buddy = nbuddy;
|
|
876 if (chatmode)
|
|
877 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE);
|
|
878 // We should rebuild the buddylist but not everytime
|
|
879 // Here we check if we were locking a buddy who is actually offline,
|
|
880 // and hide_offline_buddies is TRUE. In which case we need to rebuild.
|
|
881 if (prev_st == offline && buddylist_get_hide_offline_buddies())
|
|
882 buddylist_build();
|
|
883 update_roster = TRUE;
|
|
884
|
|
885 if (chatmode) scr_ShowBuddyWindow();
|
|
886 } else scr_LogPrint("Error: nbuddy == NULL");
|
|
887 }
|
|
888 }
|
|
889
|
143
|
890 // scr_ScrollUp()
|
|
891 // Scroll up the current buddy window, half a screen.
|
105
|
892 void scr_ScrollUp(void)
|
|
893 {
|
|
894 const gchar *jid;
|
|
895 window_entry_t *win_entry;
|
|
896 int n, nblines;
|
|
897 GList *hbuf_top;
|
|
898
|
|
899 // Get win_entry
|
|
900 if (!current_buddy)
|
|
901 return;
|
|
902 jid = CURRENT_JID;
|
|
903 if (!jid)
|
|
904 return;
|
|
905 win_entry = scr_SearchWindow(jid);
|
|
906 if (!win_entry)
|
|
907 return;
|
|
908
|
|
909 // Scroll up half a screen (or less)
|
|
910 nblines = CHAT_WIN_HEIGHT/2-1;
|
|
911 hbuf_top = win_entry->top;
|
|
912 if (!hbuf_top) {
|
|
913 hbuf_top = g_list_last(win_entry->hbuf);
|
109
|
914 if (!win_entry->cleared)
|
|
915 nblines *= 3;
|
|
916 else
|
|
917 win_entry->cleared = FALSE;
|
105
|
918 }
|
|
919
|
|
920 n = 0;
|
|
921 while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) {
|
|
922 hbuf_top = g_list_previous(hbuf_top);
|
|
923 n++;
|
|
924 }
|
|
925 win_entry->top = hbuf_top;
|
|
926
|
|
927 // Refresh the window
|
|
928 scr_UpdateWindow(win_entry);
|
|
929
|
|
930 // Finished :)
|
|
931 update_panels();
|
|
932 doupdate();
|
|
933 }
|
|
934
|
143
|
935 // scr_ScrollDown()
|
|
936 // Scroll down the current buddy window, half a screen.
|
105
|
937 void scr_ScrollDown(void)
|
|
938 {
|
|
939 const gchar *jid;
|
|
940 window_entry_t *win_entry;
|
|
941 int n, nblines;
|
|
942 GList *hbuf_top;
|
|
943
|
|
944 // Get win_entry
|
|
945 if (!current_buddy)
|
|
946 return;
|
|
947 jid = CURRENT_JID;
|
|
948 if (!jid)
|
|
949 return;
|
|
950 win_entry = scr_SearchWindow(jid);
|
|
951 if (!win_entry)
|
|
952 return;
|
|
953
|
|
954 // Scroll down half a screen (or less)
|
|
955 nblines = CHAT_WIN_HEIGHT/2-1;
|
|
956 hbuf_top = win_entry->top;
|
|
957
|
|
958 for (n=0 ; hbuf_top && n < nblines ; n++)
|
|
959 hbuf_top = g_list_next(hbuf_top);
|
|
960 win_entry->top = hbuf_top;
|
|
961 // Check if we are at the bottom
|
|
962 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++)
|
|
963 hbuf_top = g_list_next(hbuf_top);
|
|
964 if (!hbuf_top)
|
|
965 win_entry->top = NULL; // End reached
|
|
966
|
|
967 // Refresh the window
|
|
968 scr_UpdateWindow(win_entry);
|
|
969
|
|
970 // Finished :)
|
|
971 update_panels();
|
|
972 doupdate();
|
|
973 }
|
|
974
|
143
|
975 // scr_Clear()
|
|
976 // Clear the current buddy window (used for the /clear command)
|
108
|
977 void scr_Clear(void)
|
|
978 {
|
|
979 const gchar *jid;
|
|
980 window_entry_t *win_entry;
|
|
981
|
|
982 // Get win_entry
|
|
983 if (!current_buddy)
|
|
984 return;
|
|
985 jid = CURRENT_JID;
|
|
986 if (!jid)
|
|
987 return;
|
|
988 win_entry = scr_SearchWindow(jid);
|
|
989 if (!win_entry)
|
|
990 return;
|
|
991
|
|
992 win_entry->cleared = TRUE;
|
109
|
993 win_entry->top = NULL;
|
108
|
994
|
|
995 // Refresh the window
|
|
996 scr_UpdateWindow(win_entry);
|
|
997
|
|
998 // Finished :)
|
|
999 update_panels();
|
|
1000 doupdate();
|
|
1001 }
|
|
1002
|
187
|
1003 // scr_BufferTop()
|
|
1004 // Jump to the head of the current buddy window
|
|
1005 void scr_BufferTop(void)
|
|
1006 {
|
|
1007 const gchar *jid;
|
|
1008 window_entry_t *win_entry;
|
|
1009
|
|
1010 // Get win_entry
|
|
1011 if (!current_buddy) return;
|
|
1012 jid = CURRENT_JID;
|
|
1013 if (!jid) return;
|
|
1014 win_entry = scr_SearchWindow(jid);
|
|
1015
|
|
1016 if (!win_entry) return;
|
|
1017
|
|
1018 win_entry->cleared = FALSE;
|
|
1019 win_entry->top = g_list_first(win_entry->hbuf);
|
|
1020
|
|
1021 // Refresh the window
|
|
1022 scr_UpdateWindow(win_entry);
|
|
1023
|
|
1024 // Finished :)
|
|
1025 update_panels();
|
|
1026 doupdate();
|
|
1027 }
|
|
1028
|
|
1029 // scr_BufferBottom()
|
|
1030 // Jump to the end of the current buddy window
|
|
1031 void scr_BufferBottom(void)
|
|
1032 {
|
|
1033 const gchar *jid;
|
|
1034 window_entry_t *win_entry;
|
|
1035
|
|
1036 // Get win_entry
|
|
1037 if (!current_buddy) return;
|
|
1038 jid = CURRENT_JID;
|
|
1039 if (!jid) return;
|
|
1040 win_entry = scr_SearchWindow(jid);
|
|
1041
|
|
1042 if (!win_entry) return;
|
|
1043
|
|
1044 win_entry->cleared = FALSE;
|
|
1045 win_entry->top = NULL;
|
|
1046
|
|
1047 // Refresh the window
|
|
1048 scr_UpdateWindow(win_entry);
|
|
1049
|
|
1050 // Finished :)
|
|
1051 update_panels();
|
|
1052 doupdate();
|
|
1053 }
|
|
1054
|
44
|
1055 // scr_LogPrint(...)
|
|
1056 // Display a message in the log window.
|
24
|
1057 void scr_LogPrint(const char *fmt, ...)
|
|
1058 {
|
|
1059 time_t timestamp;
|
|
1060 char *buffer;
|
|
1061 va_list ap;
|
|
1062
|
153
|
1063 do {
|
157
|
1064 buffer = (char *) calloc(1, 1024);
|
153
|
1065 } while (!buffer);
|
24
|
1066
|
|
1067 timestamp = time(NULL);
|
|
1068 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp));
|
|
1069 wprintw(logWnd, "\n%s", buffer);
|
|
1070
|
|
1071 va_start(ap, fmt);
|
153
|
1072 vsnprintf(buffer, 1024, fmt, ap);
|
24
|
1073 va_end(ap);
|
|
1074
|
|
1075 wprintw(logWnd, "%s", buffer);
|
|
1076 free(buffer);
|
|
1077
|
|
1078 update_panels();
|
|
1079 doupdate();
|
|
1080 }
|
|
1081
|
143
|
1082 // scr_set_chatmode()
|
|
1083 // Public fonction to (un)set chatmode...
|
129
|
1084 inline void scr_set_chatmode(int enable)
|
|
1085 {
|
|
1086 chatmode = enable;
|
|
1087 }
|
|
1088
|
173
|
1089 // scr_cmdhisto_addline()
|
|
1090 // Add a line to the inputLine history
|
|
1091 inline void scr_cmdhisto_addline(char *line)
|
|
1092 {
|
|
1093 if (!line || !*line) return;
|
|
1094
|
|
1095 cmdhisto = g_list_append(cmdhisto, g_strdup(line));
|
|
1096 }
|
|
1097
|
|
1098 // scr_cmdhisto_prev()
|
|
1099 // Look for previous line beginning w/ the given mask in the inputLine history
|
175
|
1100 // Returns NULL if none found
|
173
|
1101 const char *scr_cmdhisto_prev(char *mask, guint len)
|
|
1102 {
|
|
1103 GList *hl;
|
|
1104 if (!cmdhisto_cur) {
|
|
1105 hl = g_list_last(cmdhisto);
|
174
|
1106 if (hl) { // backup current line
|
|
1107 strncpy(cmdhisto_backup, mask, INPUTLINE_LENGTH);
|
|
1108 }
|
173
|
1109 } else {
|
|
1110 hl = g_list_previous(cmdhisto_cur);
|
|
1111 }
|
|
1112 while (hl) {
|
|
1113 if (!strncmp((char*)hl->data, mask, len)) {
|
|
1114 // Found a match
|
|
1115 cmdhisto_cur = hl;
|
|
1116 return (const char*)hl->data;
|
|
1117 }
|
|
1118 hl = g_list_previous(hl);
|
|
1119 }
|
|
1120 return NULL;
|
|
1121 }
|
|
1122
|
|
1123 // scr_cmdhisto_next()
|
|
1124 // Look for next line beginning w/ the given mask in the inputLine history
|
175
|
1125 // Returns NULL if none found
|
173
|
1126 const char *scr_cmdhisto_next(char *mask, guint len)
|
|
1127 {
|
|
1128 GList *hl;
|
|
1129 if (!cmdhisto_cur) return NULL;
|
|
1130 hl = cmdhisto_cur;
|
|
1131 while ((hl = g_list_next(hl)) != NULL)
|
|
1132 if (!strncmp((char*)hl->data, mask, len)) {
|
|
1133 // Found a match
|
|
1134 cmdhisto_cur = hl;
|
|
1135 return (const char*)hl->data;
|
|
1136 }
|
175
|
1137 // If the "backuped" line matches, we'll use it
|
|
1138 if (strncmp(cmdhisto_backup, mask, len)) return NULL; // No match
|
174
|
1139 cmdhisto_cur = NULL;
|
|
1140 return cmdhisto_backup;
|
173
|
1141 }
|
|
1142
|
195
|
1143 // readline_transpose_chars()
|
|
1144 // Drag the character before point forward over the character at
|
|
1145 // point, moving point forward as well. If point is at the end of
|
|
1146 // the line, then this transposes the two characters before point.
|
|
1147 void readline_transpose_chars()
|
|
1148 {
|
|
1149 char swp;
|
|
1150
|
|
1151 if (ptr_inputline == inputLine) return;
|
|
1152
|
|
1153 if (!*ptr_inputline) { // We're at EOL
|
|
1154 // If line is only 1 char long, nothing to do...
|
|
1155 if (ptr_inputline == inputLine+1) return;
|
|
1156 // Transpose the two previous characters
|
|
1157 swp = *(ptr_inputline-2);
|
|
1158 *(ptr_inputline-2) = *(ptr_inputline-1);
|
|
1159 *(ptr_inputline-1) = swp;
|
|
1160 } else {
|
196
|
1161 // Swap the two characters before the cursor and move right.
|
195
|
1162 swp = *(ptr_inputline-1);
|
|
1163 *(ptr_inputline-1) = *ptr_inputline;
|
|
1164 *ptr_inputline++ = swp;
|
|
1165 check_offset(1);
|
|
1166 }
|
|
1167 }
|
|
1168
|
|
1169 // readline_backward_kill_word()
|
194
|
1170 // Kill the word before the cursor, in input line
|
195
|
1171 void readline_backward_kill_word()
|
194
|
1172 {
|
|
1173 char *c, *old = ptr_inputline;
|
|
1174 int spaceallowed = 1;
|
|
1175
|
|
1176 if (ptr_inputline == inputLine) return;
|
|
1177
|
|
1178 for (c = ptr_inputline-1 ; c > inputLine ; c--)
|
|
1179 if (!isalnum(*c)) {
|
|
1180 if (*c == ' ')
|
|
1181 if (!spaceallowed) break;
|
|
1182 } else spaceallowed = 0;
|
|
1183
|
|
1184 if (c != inputLine || *c != ' ')
|
|
1185 if ((c < ptr_inputline-1) && (!isalnum(*c)))
|
|
1186 c++;
|
|
1187
|
|
1188 // Modify the line
|
|
1189 ptr_inputline = c;
|
|
1190 for (;;) {
|
|
1191 *c = *old++;
|
|
1192 if (!*c++) break;
|
|
1193 }
|
195
|
1194 check_offset(-1);
|
194
|
1195 }
|
|
1196
|
98
|
1197 // which_row()
|
|
1198 // Tells which row our cursor is in, in the command line.
|
|
1199 // -1 -> normal text
|
|
1200 // 0 -> command
|
|
1201 // 1 -> parameter 1 (etc.)
|
102
|
1202 // If > 0, then *p_row is set to the beginning of the row
|
|
1203 int which_row(char **p_row)
|
98
|
1204 {
|
|
1205 int row = -1;
|
|
1206 char *p;
|
|
1207 int quote = FALSE;
|
|
1208
|
|
1209 // Not a command?
|
|
1210 if ((ptr_inputline == inputLine) || (inputLine[0] != '/'))
|
|
1211 return -1;
|
|
1212
|
|
1213 // This is a command
|
|
1214 row = 0;
|
|
1215 for (p = inputLine ; p < ptr_inputline ; p++) {
|
|
1216 if (quote) {
|
|
1217 if (*p == '"' && *(p-1) != '\\')
|
|
1218 quote = FALSE;
|
|
1219 continue;
|
|
1220 }
|
|
1221 if (*p == '"' && *(p-1) != '\\') {
|
|
1222 quote = TRUE;
|
121
|
1223 } else if (*p == ' ') {
|
|
1224 if (*(p-1) != ' ')
|
|
1225 row++;
|
102
|
1226 *p_row = p+1;
|
|
1227 }
|
98
|
1228 }
|
|
1229 return row;
|
|
1230 }
|
|
1231
|
143
|
1232 // scr_insert_text()
|
|
1233 // Insert the given text at the current cursor position.
|
|
1234 // The cursor is moved. We don't check if the cursor still is in the screen
|
|
1235 // after, the caller should do that.
|
98
|
1236 void scr_insert_text(const char *text)
|
|
1237 {
|
|
1238 char tmpLine[INPUTLINE_LENGTH+1];
|
|
1239 int len = strlen(text);
|
|
1240 // Check the line isn't too long
|
|
1241 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) {
|
|
1242 scr_LogPrint("Cannot insert text, line too long.");
|
|
1243 return;
|
|
1244 }
|
|
1245
|
|
1246 strcpy(tmpLine, ptr_inputline);
|
|
1247 strcpy(ptr_inputline, text); ptr_inputline += len;
|
|
1248 strcpy(ptr_inputline, tmpLine);
|
|
1249 }
|
|
1250
|
143
|
1251 // scr_handle_tab()
|
|
1252 // Function called when tab is pressed.
|
|
1253 // Initiate or continue a completion...
|
98
|
1254 void scr_handle_tab(void)
|
|
1255 {
|
102
|
1256 int nrow;
|
|
1257 char *row;
|
|
1258 const char *cchar;
|
103
|
1259 guint compl_categ;
|
98
|
1260
|
102
|
1261 nrow = which_row(&row);
|
98
|
1262
|
103
|
1263 // a) No completion if no leading slash ('cause not a command)
|
|
1264 // b) We can't have more than 2 parameters (we use 2 flags)
|
|
1265 if (nrow < 0 || nrow > 2) return;
|
102
|
1266
|
103
|
1267 if (nrow == 0) { // Command completion
|
|
1268 row = &inputLine[1];
|
|
1269 compl_categ = COMPL_CMD;
|
|
1270 } else { // Other completion, depending on the command
|
|
1271 cmd *com = cmd_get(inputLine);
|
|
1272 if (!com || !row) {
|
|
1273 scr_LogPrint("I cannot complete that...");
|
|
1274 return;
|
|
1275 }
|
|
1276 compl_categ = com->completion_flags[nrow-1];
|
|
1277 }
|
|
1278
|
|
1279 if (!completion_started) {
|
|
1280 GSList *list = compl_get_category_list(compl_categ);
|
|
1281 if (list) {
|
|
1282 char *prefix = g_strndup(row, ptr_inputline-row);
|
|
1283 // Init completion
|
|
1284 new_completion(prefix, list);
|
|
1285 g_free(prefix);
|
|
1286 // Now complete
|
98
|
1287 cchar = complete();
|
|
1288 if (cchar)
|
|
1289 scr_insert_text(cchar);
|
103
|
1290 completion_started = TRUE;
|
98
|
1291 }
|
103
|
1292 } else { // Completion already initialized
|
|
1293 char *c;
|
|
1294 guint back = cancel_completion();
|
|
1295 // Remove $back chars
|
|
1296 ptr_inputline -= back;
|
|
1297 c = ptr_inputline;
|
|
1298 for ( ; *c ; c++)
|
|
1299 *c = *(c+back);
|
|
1300 // Now complete again
|
|
1301 cchar = complete();
|
|
1302 if (cchar)
|
|
1303 scr_insert_text(cchar);
|
102
|
1304 }
|
98
|
1305 }
|
|
1306
|
|
1307 void scr_cancel_current_completion(void)
|
|
1308 {
|
|
1309 char *c;
|
|
1310 guint back = cancel_completion();
|
|
1311 // Remove $back chars
|
|
1312 ptr_inputline -= back;
|
|
1313 c = ptr_inputline;
|
|
1314 for ( ; *c ; c++)
|
|
1315 *c = *(c+back);
|
|
1316 }
|
|
1317
|
|
1318 void scr_end_current_completion(void)
|
|
1319 {
|
|
1320 done_completion();
|
|
1321 completion_started = FALSE;
|
|
1322 }
|
|
1323
|
24
|
1324 // check_offset(int direction)
|
|
1325 // Check inputline_offset value, and make sure the cursor is inside the
|
|
1326 // screen.
|
|
1327 inline void check_offset(int direction)
|
|
1328 {
|
|
1329 // Left side
|
|
1330 if (inputline_offset && direction <= 0) {
|
|
1331 while (ptr_inputline <= (char*)&inputLine + inputline_offset) {
|
|
1332 if (inputline_offset) {
|
|
1333 inputline_offset -= 5;
|
|
1334 if (inputline_offset < 0)
|
|
1335 inputline_offset = 0;
|
|
1336 }
|
|
1337 }
|
|
1338 }
|
|
1339 // Right side
|
|
1340 if (direction >= 0) {
|
|
1341 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX)
|
|
1342 inputline_offset += 5;
|
|
1343 }
|
|
1344 }
|
|
1345
|
44
|
1346 // process_key(key)
|
|
1347 // Handle the pressed key, in the command line (bottom).
|
29
|
1348 int process_key(int key)
|
24
|
1349 {
|
|
1350 if (isprint(key)) {
|
|
1351 char tmpLine[INPUTLINE_LENGTH+1];
|
|
1352
|
|
1353 // Check the line isn't too long
|
|
1354 if (strlen(inputLine) >= INPUTLINE_LENGTH)
|
|
1355 return 0;
|
|
1356
|
|
1357 // Insert char
|
|
1358 strcpy(tmpLine, ptr_inputline);
|
|
1359 *ptr_inputline++ = key;
|
|
1360 strcpy(ptr_inputline, tmpLine);
|
|
1361 check_offset(1);
|
|
1362 } else {
|
|
1363 switch(key) {
|
232
|
1364 case 8: // Ctrl-h
|
|
1365 case 127: // Backspace too
|
24
|
1366 case KEY_BACKSPACE:
|
|
1367 if (ptr_inputline != (char*)&inputLine) {
|
42
|
1368 char *c = --ptr_inputline;
|
|
1369 for ( ; *c ; c++)
|
|
1370 *c = *(c+1);
|
24
|
1371 check_offset(-1);
|
|
1372 }
|
|
1373 break;
|
|
1374 case KEY_DC:
|
|
1375 if (*ptr_inputline)
|
|
1376 strcpy(ptr_inputline, ptr_inputline+1);
|
|
1377 break;
|
|
1378 case KEY_LEFT:
|
|
1379 if (ptr_inputline != (char*)&inputLine) {
|
|
1380 ptr_inputline--;
|
|
1381 check_offset(-1);
|
|
1382 }
|
|
1383 break;
|
|
1384 case KEY_RIGHT:
|
|
1385 if (*ptr_inputline)
|
|
1386 ptr_inputline++;
|
|
1387 check_offset(1);
|
|
1388 break;
|
98
|
1389 case 7: // Ctrl-g
|
|
1390 scr_cancel_current_completion();
|
|
1391 scr_end_current_completion();
|
|
1392 check_offset(-1);
|
|
1393 break;
|
24
|
1394 case 9: // Tab
|
98
|
1395 scr_handle_tab();
|
|
1396 check_offset(0);
|
24
|
1397 break;
|
|
1398 case '\n': // Enter
|
29
|
1399 if (process_line(inputLine))
|
24
|
1400 return 255;
|
173
|
1401 // Add line to history
|
|
1402 scr_cmdhisto_addline(inputLine);
|
|
1403 cmdhisto_cur = NULL;
|
|
1404 // Reset the line
|
24
|
1405 ptr_inputline = inputLine;
|
|
1406 *ptr_inputline = 0;
|
|
1407 inputline_offset = 0;
|
|
1408 break;
|
|
1409 case KEY_UP:
|
175
|
1410 {
|
|
1411 const char *l = scr_cmdhisto_prev(inputLine,
|
|
1412 ptr_inputline-inputLine);
|
|
1413 if (l) {
|
|
1414 strcpy(inputLine, l);
|
|
1415 }
|
|
1416 }
|
24
|
1417 break;
|
|
1418 case KEY_DOWN:
|
175
|
1419 {
|
|
1420 const char *l = scr_cmdhisto_next(inputLine,
|
|
1421 ptr_inputline-inputLine);
|
|
1422 if (l) {
|
|
1423 strcpy(inputLine, l);
|
|
1424 }
|
|
1425 }
|
24
|
1426 break;
|
|
1427 case KEY_PPAGE:
|
175
|
1428 scr_RosterUp();
|
24
|
1429 break;
|
|
1430 case KEY_NPAGE:
|
175
|
1431 scr_RosterDown();
|
24
|
1432 break;
|
|
1433 case KEY_HOME:
|
|
1434 case 1:
|
|
1435 ptr_inputline = inputLine;
|
|
1436 inputline_offset = 0;
|
|
1437 break;
|
|
1438 case KEY_END:
|
|
1439 case 5:
|
|
1440 for (; *ptr_inputline; ptr_inputline++) ;
|
|
1441 check_offset(1);
|
|
1442 break;
|
|
1443 case 21: // Ctrl-u
|
|
1444 strcpy(inputLine, ptr_inputline);
|
|
1445 ptr_inputline = inputLine;
|
|
1446 inputline_offset = 0;
|
|
1447 break;
|
|
1448 case KEY_EOL:
|
|
1449 case 11: // Ctrl-k
|
|
1450 *ptr_inputline = 0;
|
|
1451 break;
|
|
1452 case 16: // Ctrl-p
|
175
|
1453 scr_ScrollUp();
|
24
|
1454 break;
|
|
1455 case 14: // Ctrl-n
|
175
|
1456 scr_ScrollDown();
|
24
|
1457 break;
|
196
|
1458 case 17: // Ctrl-q
|
236
|
1459 scr_RosterUnreadMessage(1); // next unread message
|
196
|
1460 break;
|
195
|
1461 case 20: // Ctrl-t
|
|
1462 readline_transpose_chars();
|
|
1463 break;
|
194
|
1464 case 23: // Ctrl-w
|
195
|
1465 readline_backward_kill_word();
|
194
|
1466 break;
|
24
|
1467 case 27: // ESC
|
|
1468 currentWindow = NULL;
|
|
1469 chatmode = FALSE;
|
119
|
1470 if (current_buddy)
|
|
1471 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE);
|
24
|
1472 top_panel(chatPanel);
|
|
1473 top_panel(inputPanel);
|
157
|
1474 update_panels();
|
24
|
1475 break;
|
151
|
1476 case 12: // Ctrl-l
|
|
1477 case KEY_RESIZE:
|
|
1478 scr_Resize();
|
|
1479 break;
|
24
|
1480 default:
|
|
1481 scr_LogPrint("Unkown key=%d", key);
|
232
|
1482 if (utf8_mode)
|
|
1483 scr_LogPrint("WARNING: UTF-8 not yet supported!");
|
24
|
1484 }
|
|
1485 }
|
157
|
1486 if (completion_started && key != 9 && key != KEY_RESIZE)
|
98
|
1487 scr_end_current_completion();
|
24
|
1488 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset);
|
|
1489 wclrtoeol(inputWnd);
|
|
1490 if (*ptr_inputline) {
|
|
1491 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset);
|
|
1492 }
|
157
|
1493 if (!update_roster) {
|
|
1494 //update_panels();
|
|
1495 doupdate();
|
|
1496 }
|
24
|
1497 return 0;
|
|
1498 }
|