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