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.) |
102
|
590 // If > 0, then *p_row is set to the beginning of the row |
|
591 int which_row(char **p_row) |
98
|
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; |
102
|
611 } else if (*p == ' ' && *(p-1) != ' ') { |
98
|
612 row++; |
102
|
613 *p_row = p+1; |
|
614 } |
98
|
615 } |
|
616 return row; |
|
617 } |
|
618 |
|
619 void scr_insert_text(const char *text) |
|
620 { |
|
621 char tmpLine[INPUTLINE_LENGTH+1]; |
|
622 int len = strlen(text); |
|
623 // Check the line isn't too long |
|
624 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { |
|
625 scr_LogPrint("Cannot insert text, line too long."); |
|
626 return; |
|
627 } |
|
628 |
|
629 strcpy(tmpLine, ptr_inputline); |
|
630 strcpy(ptr_inputline, text); ptr_inputline += len; |
|
631 strcpy(ptr_inputline, tmpLine); |
|
632 } |
|
633 |
|
634 void scr_handle_tab(void) |
|
635 { |
102
|
636 int nrow; |
|
637 char *row; |
|
638 const char *cchar; |
103
|
639 guint compl_categ; |
98
|
640 |
102
|
641 nrow = which_row(&row); |
98
|
642 |
103
|
643 // a) No completion if no leading slash ('cause not a command) |
|
644 // b) We can't have more than 2 parameters (we use 2 flags) |
|
645 if (nrow < 0 || nrow > 2) return; |
102
|
646 |
103
|
647 if (nrow == 0) { // Command completion |
|
648 row = &inputLine[1]; |
|
649 compl_categ = COMPL_CMD; |
|
650 } else { // Other completion, depending on the command |
|
651 cmd *com = cmd_get(inputLine); |
|
652 if (!com || !row) { |
|
653 scr_LogPrint("I cannot complete that..."); |
|
654 return; |
|
655 } |
|
656 compl_categ = com->completion_flags[nrow-1]; |
|
657 } |
|
658 |
|
659 if (!completion_started) { |
|
660 GSList *list = compl_get_category_list(compl_categ); |
|
661 if (list) { |
|
662 char *prefix = g_strndup(row, ptr_inputline-row); |
|
663 // Init completion |
|
664 new_completion(prefix, list); |
|
665 g_free(prefix); |
|
666 // Now complete |
98
|
667 cchar = complete(); |
|
668 if (cchar) |
|
669 scr_insert_text(cchar); |
103
|
670 completion_started = TRUE; |
98
|
671 } |
103
|
672 } else { // Completion already initialized |
|
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 // Now complete again |
|
681 cchar = complete(); |
|
682 if (cchar) |
|
683 scr_insert_text(cchar); |
102
|
684 } |
98
|
685 } |
|
686 |
|
687 void scr_cancel_current_completion(void) |
|
688 { |
|
689 char *c; |
|
690 guint back = cancel_completion(); |
|
691 // Remove $back chars |
|
692 ptr_inputline -= back; |
|
693 c = ptr_inputline; |
|
694 for ( ; *c ; c++) |
|
695 *c = *(c+back); |
|
696 } |
|
697 |
|
698 void scr_end_current_completion(void) |
|
699 { |
|
700 done_completion(); |
|
701 completion_started = FALSE; |
|
702 } |
|
703 |
24
|
704 // check_offset(int direction) |
|
705 // Check inputline_offset value, and make sure the cursor is inside the |
|
706 // screen. |
|
707 inline void check_offset(int direction) |
|
708 { |
|
709 // Left side |
|
710 if (inputline_offset && direction <= 0) { |
|
711 while (ptr_inputline <= (char*)&inputLine + inputline_offset) { |
|
712 if (inputline_offset) { |
|
713 inputline_offset -= 5; |
|
714 if (inputline_offset < 0) |
|
715 inputline_offset = 0; |
|
716 } |
|
717 } |
|
718 } |
|
719 // Right side |
|
720 if (direction >= 0) { |
|
721 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) |
|
722 inputline_offset += 5; |
|
723 } |
|
724 } |
|
725 |
44
|
726 // process_key(key) |
|
727 // Handle the pressed key, in the command line (bottom). |
29
|
728 int process_key(int key) |
24
|
729 { |
|
730 if (isprint(key)) { |
|
731 char tmpLine[INPUTLINE_LENGTH+1]; |
|
732 |
|
733 // Check the line isn't too long |
|
734 if (strlen(inputLine) >= INPUTLINE_LENGTH) |
|
735 return 0; |
|
736 |
|
737 // Insert char |
|
738 strcpy(tmpLine, ptr_inputline); |
|
739 *ptr_inputline++ = key; |
|
740 strcpy(ptr_inputline, tmpLine); |
|
741 check_offset(1); |
|
742 } else { |
|
743 switch(key) { |
|
744 case KEY_BACKSPACE: |
|
745 if (ptr_inputline != (char*)&inputLine) { |
42
|
746 char *c = --ptr_inputline; |
|
747 for ( ; *c ; c++) |
|
748 *c = *(c+1); |
24
|
749 check_offset(-1); |
|
750 } |
|
751 break; |
|
752 case KEY_DC: |
|
753 if (*ptr_inputline) |
|
754 strcpy(ptr_inputline, ptr_inputline+1); |
|
755 break; |
|
756 case KEY_LEFT: |
|
757 if (ptr_inputline != (char*)&inputLine) { |
|
758 ptr_inputline--; |
|
759 check_offset(-1); |
|
760 } |
|
761 break; |
|
762 case KEY_RIGHT: |
|
763 if (*ptr_inputline) |
|
764 ptr_inputline++; |
|
765 check_offset(1); |
|
766 break; |
98
|
767 case 7: // Ctrl-g |
|
768 scr_cancel_current_completion(); |
|
769 scr_end_current_completion(); |
|
770 check_offset(-1); |
|
771 break; |
24
|
772 case 9: // Tab |
98
|
773 scr_handle_tab(); |
|
774 check_offset(0); |
24
|
775 break; |
|
776 case '\n': // Enter |
|
777 chatmode = TRUE; |
|
778 if (inputLine[0] == 0) { |
|
779 scr_ShowBuddyWindow(); |
|
780 break; |
|
781 } |
29
|
782 if (process_line(inputLine)) |
24
|
783 return 255; |
|
784 ptr_inputline = inputLine; |
|
785 *ptr_inputline = 0; |
|
786 inputline_offset = 0; |
|
787 break; |
|
788 case KEY_UP: |
81
|
789 scr_RosterUp(); |
24
|
790 if (chatmode) |
|
791 scr_ShowBuddyWindow(); |
|
792 break; |
|
793 case KEY_DOWN: |
81
|
794 scr_RosterDown(); |
24
|
795 if (chatmode) |
|
796 scr_ShowBuddyWindow(); |
|
797 break; |
|
798 case KEY_PPAGE: |
|
799 scr_LogPrint("PageUp??"); |
|
800 break; |
|
801 case KEY_NPAGE: |
|
802 scr_LogPrint("PageDown??"); |
|
803 break; |
|
804 case KEY_HOME: |
|
805 case 1: |
|
806 ptr_inputline = inputLine; |
|
807 inputline_offset = 0; |
|
808 break; |
|
809 case KEY_END: |
|
810 case 5: |
|
811 for (; *ptr_inputline; ptr_inputline++) ; |
|
812 check_offset(1); |
|
813 break; |
|
814 case 21: // Ctrl-u |
|
815 strcpy(inputLine, ptr_inputline); |
|
816 ptr_inputline = inputLine; |
|
817 inputline_offset = 0; |
|
818 break; |
|
819 case KEY_EOL: |
|
820 case 11: // Ctrl-k |
|
821 *ptr_inputline = 0; |
|
822 break; |
|
823 case 16: // Ctrl-p |
|
824 scr_LogPrint("Ctrl-p not yet implemented"); |
|
825 break; |
|
826 case 14: // Ctrl-n |
|
827 scr_LogPrint("Ctrl-n not yet implemented"); |
|
828 break; |
|
829 case 27: // ESC |
|
830 currentWindow = NULL; |
|
831 chatmode = FALSE; |
|
832 top_panel(chatPanel); |
|
833 top_panel(inputPanel); |
|
834 break; |
|
835 default: |
|
836 scr_LogPrint("Unkown key=%d", key); |
|
837 } |
|
838 } |
98
|
839 if (completion_started && key != 9) |
|
840 scr_end_current_completion(); |
24
|
841 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
|
842 wclrtoeol(inputWnd); |
|
843 if (*ptr_inputline) { |
|
844 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
|
845 } |
|
846 update_panels(); |
|
847 doupdate(); |
|
848 return 0; |
|
849 } |