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