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