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