24
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
|
3 #include <string.h>
|
|
4 #include <ncurses.h>
|
|
5 #include <panel.h>
|
|
6 #include <time.h>
|
|
7 #include <ctype.h>
|
|
8 #include <locale.h>
|
|
9
|
|
10 #include "screen.h"
|
81
|
11 #include "hbuf.h"
|
47
|
12 #include "commands.h"
|
95
|
13 #include "compl.h"
|
81
|
14 #include "roster.h"
|
24
|
15 #include "parsecfg.h"
|
|
16 #include "lang.h"
|
81
|
17 #include "utf8.h"
|
|
18 #include "utils.h"
|
24
|
19 #include "list.h"
|
|
20
|
|
21 #define window_entry(n) list_entry(n, window_entry_t, list)
|
|
22
|
|
23 LIST_HEAD(window_list);
|
|
24
|
|
25 typedef struct _window_entry_t {
|
|
26 WINDOW *win;
|
|
27 PANEL *panel;
|
|
28 char *name;
|
74
|
29 GList *hbuf;
|
24
|
30 struct list_head list;
|
|
31 } window_entry_t;
|
|
32
|
|
33
|
|
34 static WINDOW *rosterWnd, *chatWnd, *inputWnd;
|
|
35 static WINDOW *logWnd, *logWnd_border;
|
|
36 static PANEL *rosterPanel, *chatPanel, *inputPanel;
|
|
37 static PANEL *logPanel, *logPanel_border;
|
|
38 static int maxY, maxX;
|
|
39 static window_entry_t *currentWindow;
|
|
40
|
|
41 static int chatmode;
|
30
|
42 int update_roster;
|
24
|
43
|
|
44 static char inputLine[INPUTLINE_LENGTH+1];
|
|
45 static char *ptr_inputline;
|
|
46 static short int inputline_offset;
|
98
|
47 static int completion_started;
|
24
|
48
|
|
49
|
99
|
50 /* Functions */
|
24
|
51
|
74
|
52 int scr_WindowWidth(WINDOW * win)
|
24
|
53 {
|
|
54 int x, y;
|
|
55 getmaxyx(win, y, x);
|
|
56 return x;
|
|
57 }
|
|
58
|
74
|
59 void scr_clear_box(WINDOW *win, int y, int x, int height, int width, int Color)
|
|
60 {
|
|
61 int i, j;
|
|
62
|
|
63 wattrset(win, COLOR_PAIR(Color));
|
|
64 for (i = 0; i < height; i++) {
|
|
65 wmove(win, y + i, x);
|
|
66 for (j = 0; j < width; j++)
|
|
67 wprintw(win, " ");
|
|
68 }
|
|
69 }
|
|
70
|
24
|
71 void scr_draw_box(WINDOW * win, int y, int x, int height, int width,
|
|
72 int Color, chtype box, chtype border)
|
|
73 {
|
|
74 int i, j;
|
|
75
|
|
76 wattrset(win, COLOR_PAIR(Color));
|
|
77 for (i = 0; i < height; i++) {
|
|
78 wmove(win, y + i, x);
|
|
79 for (j = 0; j < width; j++)
|
|
80 if (!i && !j)
|
|
81 waddch(win, border | ACS_ULCORNER);
|
|
82 else if (i == height - 1 && !j)
|
|
83 waddch(win, border | ACS_LLCORNER);
|
|
84 else if (!i && j == width - 1)
|
|
85 waddch(win, box | ACS_URCORNER);
|
|
86 else if (i == height - 1 && j == width - 1)
|
|
87 waddch(win, box | ACS_LRCORNER);
|
|
88 else if (!i)
|
|
89 waddch(win, border | ACS_HLINE);
|
|
90 else if (i == height - 1)
|
|
91 waddch(win, box | ACS_HLINE);
|
|
92 else if (!j)
|
|
93 waddch(win, border | ACS_VLINE);
|
|
94 else if (j == width - 1)
|
|
95 waddch(win, box | ACS_VLINE);
|
|
96 else
|
|
97 waddch(win, box | ' ');
|
|
98 }
|
|
99 }
|
|
100
|
|
101 int FindColor(char *name)
|
|
102 {
|
|
103 if (!strcmp(name, "default"))
|
|
104 return -1;
|
|
105 if (!strcmp(name, "black"))
|
|
106 return COLOR_BLACK;
|
|
107 if (!strcmp(name, "red"))
|
|
108 return COLOR_RED;
|
|
109 if (!strcmp(name, "green"))
|
|
110 return COLOR_GREEN;
|
|
111 if (!strcmp(name, "yellow"))
|
|
112 return COLOR_YELLOW;
|
|
113 if (!strcmp(name, "blue"))
|
|
114 return COLOR_BLUE;
|
|
115 if (!strcmp(name, "magenta"))
|
|
116 return COLOR_MAGENTA;
|
|
117 if (!strcmp(name, "cyan"))
|
|
118 return COLOR_CYAN;
|
|
119 if (!strcmp(name, "white"))
|
|
120 return COLOR_WHITE;
|
|
121
|
|
122 return -1;
|
|
123 }
|
|
124
|
|
125 void ParseColors(void)
|
|
126 {
|
|
127 char *colors[11] = {
|
|
128 "", "",
|
|
129 "borderlines",
|
|
130 "jidonlineselected",
|
|
131 "jidonline",
|
|
132 "jidofflineselected",
|
|
133 "jidoffline",
|
|
134 "text",
|
|
135 NULL
|
|
136 };
|
|
137
|
|
138 char *tmp = malloc(1024);
|
|
139 char *color1;
|
|
140 char *background = cfg_read("color_background");
|
|
141 char *backselected = cfg_read("color_backselected");
|
|
142 int i = 0;
|
|
143
|
|
144 while (colors[i]) {
|
|
145 sprintf(tmp, "color_%s", colors[i]);
|
|
146 color1 = cfg_read(tmp);
|
|
147
|
|
148 switch (i + 1) {
|
|
149 case 1:
|
|
150 init_pair(1, COLOR_BLACK, COLOR_WHITE);
|
|
151 break;
|
|
152 case 2:
|
|
153 init_pair(2, COLOR_WHITE, COLOR_BLACK);
|
|
154 break;
|
|
155 case 3:
|
|
156 init_pair(3, FindColor(color1), FindColor(background));
|
|
157 break;
|
|
158 case 4:
|
|
159 init_pair(4, FindColor(color1), FindColor(backselected));
|
|
160 break;
|
|
161 case 5:
|
|
162 init_pair(5, FindColor(color1), FindColor(background));
|
|
163 break;
|
|
164 case 6:
|
|
165 init_pair(6, FindColor(color1), FindColor(backselected));
|
|
166 break;
|
|
167 case 7:
|
|
168 init_pair(7, FindColor(color1), FindColor(background));
|
|
169 break;
|
|
170 case 8:
|
|
171 init_pair(8, FindColor(color1), FindColor(background));
|
|
172 break;
|
|
173 }
|
|
174 i++;
|
|
175 }
|
|
176 }
|
|
177
|
|
178
|
50
|
179 window_entry_t *scr_CreatePanel(const char *title, int x, int y,
|
|
180 int lines, int cols, int dont_show)
|
24
|
181 {
|
|
182 window_entry_t *tmp = calloc(1, sizeof(window_entry_t));
|
|
183
|
|
184 tmp->win = newwin(lines, cols, y, x);
|
|
185 tmp->panel = new_panel(tmp->win);
|
|
186 tmp->name = (char *) calloc(1, 1024);
|
|
187 strncpy(tmp->name, title, 1024);
|
76
|
188 scr_clear_box(tmp->win, 0, 0, lines, cols, COLOR_GENERAL);
|
24
|
189
|
|
190 if ((!dont_show)) {
|
|
191 currentWindow = tmp;
|
|
192 } else {
|
|
193 if (currentWindow)
|
|
194 top_panel(currentWindow->panel);
|
|
195 else
|
|
196 top_panel(chatPanel);
|
|
197 }
|
|
198
|
|
199 list_add_tail(&tmp->list, &window_list);
|
|
200 update_panels();
|
|
201
|
|
202 return tmp;
|
|
203 }
|
|
204
|
50
|
205 window_entry_t *scr_SearchWindow(const char *winId)
|
24
|
206 {
|
|
207 struct list_head *pos, *n;
|
|
208 window_entry_t *search_entry = NULL;
|
|
209
|
|
210 list_for_each_safe(pos, n, &window_list) {
|
|
211 search_entry = window_entry(pos);
|
|
212 if (search_entry->name) {
|
|
213 if (!strcasecmp(search_entry->name, winId)) {
|
|
214 return search_entry;
|
|
215 }
|
|
216 }
|
|
217 }
|
|
218 return NULL;
|
|
219 }
|
|
220
|
74
|
221 void scr_UpdateWindow(window_entry_t *win_entry)
|
|
222 {
|
|
223 int n;
|
|
224 int width;
|
|
225 char **lines;
|
|
226 GList *hbuf_head;
|
|
227
|
|
228 // We will show the last CHAT_WIN_HEIGHT lines.
|
|
229 // Let's find out where it begins.
|
|
230 win_entry->hbuf = g_list_last(win_entry->hbuf);
|
|
231 hbuf_head = win_entry->hbuf;
|
|
232 for (n=0; hbuf_head && n<(CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head); n++)
|
|
233 hbuf_head = g_list_previous(hbuf_head);
|
|
234
|
|
235 // Get the last CHAT_WIN_HEIGHT lines.
|
|
236 lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT);
|
|
237
|
|
238 // Display these lines
|
|
239 width = scr_WindowWidth(win_entry->win);
|
|
240 wmove(win_entry->win, 0, 0);
|
|
241 for (n = 0; n < CHAT_WIN_HEIGHT; n++) {
|
|
242 int r = width;
|
75
|
243 if (*(lines+2*n)) {
|
|
244 if (**(lines+2*n))
|
|
245 wprintw(win_entry->win, "%s", *(lines+2*n)); // prefix
|
|
246 else {
|
|
247 wprintw(win_entry->win, " ");
|
|
248 r -= 12;
|
|
249 }
|
|
250 wprintw(win_entry->win, "%s", *(lines+2*n+1)); // line
|
|
251 // Calculate the number of blank characters to empty the line
|
|
252 r -= strlen(*(lines+2*n)) + strlen(*(lines+2*n+1));
|
|
253 }
|
74
|
254 for ( ; r>0 ; r--) {
|
|
255 wprintw(win_entry->win, " ");
|
|
256 }
|
|
257 //// wclrtoeol(win_entry->win); does not work :(
|
|
258 }
|
|
259 g_free(lines);
|
|
260 }
|
|
261
|
50
|
262 void scr_ShowWindow(const char *winId)
|
24
|
263 {
|
74
|
264 window_entry_t *win_entry = scr_SearchWindow(winId);
|
|
265
|
|
266 if (win_entry != NULL) {
|
|
267 top_panel(win_entry->panel);
|
|
268 currentWindow = win_entry;
|
24
|
269 chatmode = TRUE;
|
81
|
270 roster_setflags(winId, ROSTER_FLAG_MSG, FALSE);
|
30
|
271 update_roster = TRUE;
|
74
|
272
|
|
273 // Refresh the window entry
|
|
274 scr_UpdateWindow(win_entry);
|
|
275
|
|
276 // Finished :)
|
24
|
277 update_panels();
|
|
278 doupdate();
|
|
279 } else {
|
|
280 top_panel(chatPanel);
|
74
|
281 currentWindow = win_entry; // == NULL (current window empty)
|
24
|
282 }
|
|
283 }
|
|
284
|
|
285 void scr_ShowBuddyWindow(void)
|
|
286 {
|
81
|
287 const gchar *jid = CURRENT_JID;
|
|
288 if (jid != NULL)
|
|
289 scr_ShowWindow(jid);
|
24
|
290 top_panel(inputPanel);
|
|
291 }
|
|
292
|
|
293
|
75
|
294 void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp,
|
|
295 const char *prefix, int force_show)
|
24
|
296 {
|
75
|
297 char *fullprefix = NULL;
|
74
|
298 window_entry_t *win_entry;
|
24
|
299 int dont_show = FALSE;
|
|
300
|
75
|
301 // Prepare the prefix
|
|
302 if (prefix || TimeStamp) {
|
|
303 if (!prefix) prefix = "";
|
|
304 fullprefix = calloc(1, strlen(prefix)+16);
|
|
305 if (TimeStamp) {
|
|
306 time_t now = time(NULL);
|
|
307 strftime(fullprefix, 12, "[%H:%M] ", localtime(&now));
|
|
308 } else {
|
|
309 strcpy(fullprefix, " ");
|
|
310 }
|
|
311 strcat(fullprefix, prefix);
|
74
|
312 }
|
|
313
|
|
314 // Look for the window entry.
|
|
315 win_entry = scr_SearchWindow(winId);
|
|
316
|
|
317 // Do we have to really show the window?
|
24
|
318 if (!chatmode)
|
|
319 dont_show = TRUE;
|
74
|
320 else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry))))
|
24
|
321 dont_show = TRUE;
|
|
322
|
74
|
323 // If the window entry doesn't exist yet, let's create it.
|
|
324 if (win_entry == NULL) {
|
|
325 win_entry = scr_CreatePanel(winId, ROSTER_WIDTH, 0, CHAT_WIN_HEIGHT,
|
|
326 maxX - ROSTER_WIDTH, dont_show);
|
24
|
327 }
|
|
328
|
75
|
329 hbuf_add_line(&win_entry->hbuf, text, fullprefix,
|
74
|
330 maxX - scr_WindowWidth(rosterWnd) - 14);
|
75
|
331 free(fullprefix);
|
74
|
332
|
24
|
333 if (!dont_show) {
|
74
|
334 // Show and refresh the window
|
|
335 top_panel(win_entry->panel);
|
|
336 scr_UpdateWindow(win_entry);
|
24
|
337 update_panels();
|
|
338 doupdate();
|
|
339 } else {
|
81
|
340 roster_setflags(winId, ROSTER_FLAG_MSG, TRUE);
|
30
|
341 update_roster = TRUE;
|
24
|
342 }
|
|
343 }
|
|
344
|
|
345 void scr_InitCurses(void)
|
|
346 {
|
|
347 initscr();
|
|
348 noecho();
|
|
349 raw();
|
35
|
350 halfdelay(5);
|
24
|
351 start_color();
|
|
352 use_default_colors();
|
|
353
|
|
354 ParseColors();
|
|
355
|
|
356 getmaxyx(stdscr, maxY, maxX);
|
|
357 inputLine[0] = 0;
|
|
358 ptr_inputline = inputLine;
|
|
359
|
35
|
360 setlocale(LC_CTYPE, "");
|
24
|
361
|
|
362 return;
|
|
363 }
|
|
364
|
81
|
365 void scr_TerminateCurses(void)
|
|
366 {
|
|
367 clear();
|
|
368 refresh();
|
|
369 endwin();
|
|
370 return;
|
|
371 }
|
|
372
|
24
|
373 void scr_DrawMainWindow(void)
|
|
374 {
|
74
|
375 int l;
|
|
376
|
24
|
377 /* Draw main panels */
|
74
|
378 rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0);
|
24
|
379 rosterPanel = new_panel(rosterWnd);
|
74
|
380 scr_clear_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, ROSTER_WIDTH,
|
|
381 COLOR_GENERAL);
|
|
382 for (l=0 ; l < CHAT_WIN_HEIGHT ; l++)
|
|
383 mvwaddch(rosterWnd, l, ROSTER_WIDTH-1, ACS_VLINE);
|
24
|
384
|
74
|
385 chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH);
|
24
|
386 chatPanel = new_panel(chatWnd);
|
74
|
387 scr_clear_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH,
|
|
388 COLOR_GENERAL);
|
|
389 scrollok(chatWnd, TRUE);
|
|
390 mvwprintw(chatWnd, 0, 0, "This is the status window");
|
24
|
391
|
|
392 logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0);
|
|
393 logPanel_border = new_panel(logWnd_border);
|
|
394 scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0);
|
|
395 logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1);
|
|
396 logPanel = new_panel(logWnd);
|
|
397 wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL));
|
|
398
|
74
|
399 scrollok(logWnd, TRUE);
|
24
|
400
|
|
401 inputWnd = newwin(1, maxX, maxY-1, 0);
|
|
402 inputPanel = new_panel(inputWnd);
|
|
403
|
81
|
404 scr_DrawRoster();
|
24
|
405 update_panels();
|
|
406 doupdate();
|
|
407 return;
|
|
408 }
|
|
409
|
81
|
410 void scr_DrawRoster(void)
|
24
|
411 {
|
81
|
412 static guint offset = 0;
|
|
413 char name[ROSTER_WIDTH];
|
|
414 int maxx, maxy;
|
|
415 GList *buddy;
|
|
416 int i, n;
|
|
417 int rOffset;
|
|
418
|
|
419 getmaxyx(rosterWnd, maxy, maxx);
|
|
420 maxx --; // last char is for vertical border
|
|
421 name[ROSTER_WIDTH-7] = 0;
|
|
422
|
|
423 // cleanup of roster window
|
|
424 wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL));
|
|
425 for (i = 0; i < maxy; i++) {
|
|
426 mvwprintw(rosterWnd, i, 0, "");
|
|
427 for (n = 0; n < maxx; n++)
|
|
428 waddch(rosterWnd, ' ');
|
|
429 }
|
|
430
|
|
431 // Leave now if buddylist is empty
|
|
432 if (!buddylist) {
|
|
433 offset = 0;
|
|
434 return;
|
|
435 }
|
|
436
|
84
|
437 // Update offset if necessary
|
|
438 i = g_list_position(buddylist, current_buddy);
|
|
439 if (i == -1) { // This is bad
|
|
440 scr_LogPrint("Doh! Can't find current selected buddy!!");
|
|
441 return;
|
|
442 } else if (i < offset) {
|
|
443 offset = i;
|
|
444 } else if (i+1 > offset + maxy) {
|
|
445 offset = i + 1 - maxy;
|
|
446 }
|
81
|
447
|
|
448 buddy = buddylist;
|
|
449 rOffset = offset;
|
|
450
|
84
|
451 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) {
|
81
|
452
|
|
453 char status = '?';
|
|
454 char pending = ' ';
|
|
455 enum imstatus budstate;
|
|
456
|
|
457 if (rOffset > 0) {
|
|
458 rOffset--;
|
|
459 continue;
|
|
460 }
|
|
461
|
|
462 if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) {
|
|
463 pending = '#';
|
|
464 }
|
|
465
|
|
466 budstate = buddy_getstatus(BUDDATA(buddy));
|
|
467 if (budstate >= 0 && budstate < imstatus_size)
|
|
468 status = imstatus2char[budstate];
|
|
469 if (buddy == current_buddy) {
|
|
470 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL));
|
|
471 // The 3 following lines aim to color the whole line
|
|
472 wmove(rosterWnd, i, 0);
|
|
473 for (n = 0; n < maxx; n++)
|
|
474 waddch(rosterWnd, ' ');
|
|
475 } else {
|
|
476 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES));
|
|
477 }
|
|
478
|
|
479 strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7);
|
|
480 // TODO: status is meaningless for groups:
|
|
481 if (buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP) status='G';
|
|
482 mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name);
|
84
|
483
|
|
484 i++;
|
81
|
485 }
|
|
486
|
|
487 update_panels();
|
|
488 doupdate();
|
|
489 update_roster = FALSE;
|
24
|
490 }
|
|
491
|
50
|
492 void scr_WriteMessage(const char *jid, const char *text, char *prefix)
|
24
|
493 {
|
75
|
494 scr_WriteInWindow(jid, text, TRUE, prefix, FALSE);
|
47
|
495 }
|
|
496
|
50
|
497 void scr_WriteIncomingMessage(const char *jidfrom, const char *text)
|
47
|
498 {
|
|
499 char *buffer = utf8_decode(text);
|
75
|
500 // FIXME expand tabs / filter out special chars...
|
47
|
501 scr_WriteMessage(jidfrom, buffer, "<== ");
|
|
502 free(buffer);
|
65
|
503 top_panel(inputPanel);
|
24
|
504 update_panels();
|
|
505 doupdate();
|
|
506 }
|
|
507
|
50
|
508 void scr_WriteOutgoingMessage(const char *jidto, const char *text)
|
47
|
509 {
|
65
|
510 scr_WriteMessage(jidto, text, "--> ");
|
47
|
511 scr_ShowWindow(jidto);
|
65
|
512 top_panel(inputPanel);
|
47
|
513 }
|
|
514
|
24
|
515 int scr_Getch(void)
|
|
516 {
|
|
517 int ch;
|
|
518 // keypad(inputWnd, TRUE);
|
|
519 ch = wgetch(inputWnd);
|
|
520 return ch;
|
|
521 }
|
|
522
|
|
523 WINDOW *scr_GetRosterWindow(void)
|
|
524 {
|
|
525 return rosterWnd;
|
|
526 }
|
|
527
|
|
528 WINDOW *scr_GetStatusWindow(void)
|
|
529 {
|
|
530 return chatWnd;
|
|
531 }
|
|
532
|
|
533 WINDOW *scr_GetInputWindow(void)
|
|
534 {
|
|
535 return inputWnd;
|
|
536 }
|
|
537
|
81
|
538 void scr_RosterUp()
|
|
539 {
|
|
540 if (current_buddy) {
|
|
541 if (g_list_previous(current_buddy)) {
|
|
542 current_buddy = g_list_previous(current_buddy);
|
|
543 scr_DrawRoster();
|
|
544 }
|
|
545 }
|
|
546 // XXX We should rebuild the buddylist but perhaps not everytime?
|
|
547 }
|
|
548
|
|
549 void scr_RosterDown()
|
|
550 {
|
|
551 if (current_buddy) {
|
|
552 if (g_list_next(current_buddy)) {
|
|
553 current_buddy = g_list_next(current_buddy);
|
|
554 scr_DrawRoster();
|
|
555 }
|
|
556 }
|
|
557 // XXX We should rebuild the buddylist but perhaps not everytime?
|
|
558 }
|
|
559
|
44
|
560 // scr_LogPrint(...)
|
|
561 // Display a message in the log window.
|
24
|
562 void scr_LogPrint(const char *fmt, ...)
|
|
563 {
|
|
564 time_t timestamp;
|
|
565 char *buffer;
|
|
566 va_list ap;
|
|
567
|
|
568 buffer = (char *) calloc(1, 4096);
|
|
569
|
|
570 timestamp = time(NULL);
|
|
571 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp));
|
|
572 wprintw(logWnd, "\n%s", buffer);
|
|
573
|
|
574 va_start(ap, fmt);
|
|
575 vsnprintf(buffer, 4096, fmt, ap);
|
|
576 va_end(ap);
|
|
577
|
|
578 wprintw(logWnd, "%s", buffer);
|
|
579 free(buffer);
|
|
580
|
|
581 update_panels();
|
|
582 doupdate();
|
|
583 }
|
|
584
|
98
|
585 // which_row()
|
|
586 // Tells which row our cursor is in, in the command line.
|
|
587 // -1 -> normal text
|
|
588 // 0 -> command
|
|
589 // 1 -> parameter 1 (etc.)
|
|
590 int which_row(void)
|
|
591 {
|
|
592 int row = -1;
|
|
593 char *p;
|
|
594 int quote = FALSE;
|
|
595
|
|
596 // Not a command?
|
|
597 if ((ptr_inputline == inputLine) || (inputLine[0] != '/'))
|
|
598 return -1;
|
|
599
|
|
600 // This is a command
|
|
601 row = 0;
|
|
602 for (p = inputLine ; p < ptr_inputline ; p++) {
|
|
603 if (quote) {
|
|
604 if (*p == '"' && *(p-1) != '\\')
|
|
605 quote = FALSE;
|
|
606 continue;
|
|
607 }
|
|
608 if (*p == '"' && *(p-1) != '\\') {
|
|
609 quote = TRUE;
|
|
610 } else if (*p == ' ' && *(p-1) != ' ')
|
|
611 row++;
|
|
612 }
|
|
613 return row;
|
|
614 }
|
|
615
|
|
616 void scr_insert_text(const char *text)
|
|
617 {
|
|
618 char tmpLine[INPUTLINE_LENGTH+1];
|
|
619 int len = strlen(text);
|
|
620 // Check the line isn't too long
|
|
621 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) {
|
|
622 scr_LogPrint("Cannot insert text, line too long.");
|
|
623 return;
|
|
624 }
|
|
625
|
|
626 strcpy(tmpLine, ptr_inputline);
|
|
627 strcpy(ptr_inputline, text); ptr_inputline += len;
|
|
628 strcpy(ptr_inputline, tmpLine);
|
|
629 }
|
|
630
|
|
631 void scr_handle_tab(void)
|
|
632 {
|
|
633 int row;
|
|
634 row = which_row();
|
|
635
|
|
636 if (row == -1) return; // No completion if no leading slash
|
|
637
|
|
638 if (row == 0) { // Command completion
|
|
639 if (!completion_started) {
|
100
|
640 const char *cchar;
|
98
|
641 GSList *list = compl_get_category_list(COMPL_CMD);
|
|
642 if (list) {
|
99
|
643 char *prefix = g_strndup(&inputLine[1], ptr_inputline-inputLine-1);
|
100
|
644 // Init completion
|
98
|
645 new_completion(prefix, list);
|
100
|
646 g_free(prefix);
|
|
647 // Now complete
|
98
|
648 cchar = complete();
|
|
649 if (cchar)
|
|
650 scr_insert_text(cchar);
|
|
651 completion_started = TRUE;
|
|
652 }
|
|
653 } else {
|
|
654 char *c;
|
|
655 guint back = cancel_completion();
|
|
656 // Remove $back chars
|
|
657 ptr_inputline -= back;
|
|
658 c = ptr_inputline;
|
|
659 for ( ; *c ; c++)
|
|
660 *c = *(c+back);
|
|
661 // Now complete again
|
|
662 cchar = complete();
|
|
663 if (cchar)
|
|
664 scr_insert_text(cchar);
|
|
665 }
|
|
666 return;
|
|
667 }
|
100
|
668
|
|
669 // Other completion, depending on the command
|
98
|
670 scr_LogPrint("I'm unable to complete that yet");
|
|
671 }
|
|
672
|
|
673 void scr_cancel_current_completion(void)
|
|
674 {
|
|
675 char *c;
|
|
676 guint back = cancel_completion();
|
|
677 // Remove $back chars
|
|
678 ptr_inputline -= back;
|
|
679 c = ptr_inputline;
|
|
680 for ( ; *c ; c++)
|
|
681 *c = *(c+back);
|
|
682 }
|
|
683
|
|
684 void scr_end_current_completion(void)
|
|
685 {
|
|
686 done_completion();
|
|
687 completion_started = FALSE;
|
|
688 }
|
|
689
|
24
|
690 // check_offset(int direction)
|
|
691 // Check inputline_offset value, and make sure the cursor is inside the
|
|
692 // screen.
|
|
693 inline void check_offset(int direction)
|
|
694 {
|
|
695 // Left side
|
|
696 if (inputline_offset && direction <= 0) {
|
|
697 while (ptr_inputline <= (char*)&inputLine + inputline_offset) {
|
|
698 if (inputline_offset) {
|
|
699 inputline_offset -= 5;
|
|
700 if (inputline_offset < 0)
|
|
701 inputline_offset = 0;
|
|
702 }
|
|
703 }
|
|
704 }
|
|
705 // Right side
|
|
706 if (direction >= 0) {
|
|
707 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX)
|
|
708 inputline_offset += 5;
|
|
709 }
|
|
710 }
|
|
711
|
44
|
712 // process_key(key)
|
|
713 // Handle the pressed key, in the command line (bottom).
|
29
|
714 int process_key(int key)
|
24
|
715 {
|
|
716 if (isprint(key)) {
|
|
717 char tmpLine[INPUTLINE_LENGTH+1];
|
|
718
|
|
719 // Check the line isn't too long
|
|
720 if (strlen(inputLine) >= INPUTLINE_LENGTH)
|
|
721 return 0;
|
|
722
|
|
723 // Insert char
|
|
724 strcpy(tmpLine, ptr_inputline);
|
|
725 *ptr_inputline++ = key;
|
|
726 strcpy(ptr_inputline, tmpLine);
|
|
727 check_offset(1);
|
|
728 } else {
|
|
729 switch(key) {
|
|
730 case KEY_BACKSPACE:
|
|
731 if (ptr_inputline != (char*)&inputLine) {
|
42
|
732 char *c = --ptr_inputline;
|
|
733 for ( ; *c ; c++)
|
|
734 *c = *(c+1);
|
24
|
735 check_offset(-1);
|
|
736 }
|
|
737 break;
|
|
738 case KEY_DC:
|
|
739 if (*ptr_inputline)
|
|
740 strcpy(ptr_inputline, ptr_inputline+1);
|
|
741 break;
|
|
742 case KEY_LEFT:
|
|
743 if (ptr_inputline != (char*)&inputLine) {
|
|
744 ptr_inputline--;
|
|
745 check_offset(-1);
|
|
746 }
|
|
747 break;
|
|
748 case KEY_RIGHT:
|
|
749 if (*ptr_inputline)
|
|
750 ptr_inputline++;
|
|
751 check_offset(1);
|
|
752 break;
|
98
|
753 case 7: // Ctrl-g
|
|
754 scr_cancel_current_completion();
|
|
755 scr_end_current_completion();
|
|
756 check_offset(-1);
|
|
757 break;
|
24
|
758 case 9: // Tab
|
98
|
759 scr_handle_tab();
|
|
760 check_offset(0);
|
24
|
761 break;
|
|
762 case '\n': // Enter
|
|
763 chatmode = TRUE;
|
|
764 if (inputLine[0] == 0) {
|
|
765 scr_ShowBuddyWindow();
|
|
766 break;
|
|
767 }
|
29
|
768 if (process_line(inputLine))
|
24
|
769 return 255;
|
|
770 ptr_inputline = inputLine;
|
|
771 *ptr_inputline = 0;
|
|
772 inputline_offset = 0;
|
|
773 break;
|
|
774 case KEY_UP:
|
81
|
775 scr_RosterUp();
|
24
|
776 if (chatmode)
|
|
777 scr_ShowBuddyWindow();
|
|
778 break;
|
|
779 case KEY_DOWN:
|
81
|
780 scr_RosterDown();
|
24
|
781 if (chatmode)
|
|
782 scr_ShowBuddyWindow();
|
|
783 break;
|
|
784 case KEY_PPAGE:
|
|
785 scr_LogPrint("PageUp??");
|
|
786 break;
|
|
787 case KEY_NPAGE:
|
|
788 scr_LogPrint("PageDown??");
|
|
789 break;
|
|
790 case KEY_HOME:
|
|
791 case 1:
|
|
792 ptr_inputline = inputLine;
|
|
793 inputline_offset = 0;
|
|
794 break;
|
|
795 case KEY_END:
|
|
796 case 5:
|
|
797 for (; *ptr_inputline; ptr_inputline++) ;
|
|
798 check_offset(1);
|
|
799 break;
|
|
800 case 21: // Ctrl-u
|
|
801 strcpy(inputLine, ptr_inputline);
|
|
802 ptr_inputline = inputLine;
|
|
803 inputline_offset = 0;
|
|
804 break;
|
|
805 case KEY_EOL:
|
|
806 case 11: // Ctrl-k
|
|
807 *ptr_inputline = 0;
|
|
808 break;
|
|
809 case 16: // Ctrl-p
|
|
810 scr_LogPrint("Ctrl-p not yet implemented");
|
|
811 break;
|
|
812 case 14: // Ctrl-n
|
|
813 scr_LogPrint("Ctrl-n not yet implemented");
|
|
814 break;
|
|
815 case 27: // ESC
|
|
816 currentWindow = NULL;
|
|
817 chatmode = FALSE;
|
|
818 top_panel(chatPanel);
|
|
819 top_panel(inputPanel);
|
|
820 break;
|
|
821 default:
|
|
822 scr_LogPrint("Unkown key=%d", key);
|
|
823 }
|
|
824 }
|
98
|
825 if (completion_started && key != 9)
|
|
826 scr_end_current_completion();
|
24
|
827 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset);
|
|
828 wclrtoeol(inputWnd);
|
|
829 if (*ptr_inputline) {
|
|
830 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset);
|
|
831 }
|
|
832 update_panels();
|
|
833 doupdate();
|
|
834 return 0;
|
|
835 }
|