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) { |
189
|
536 GList *rescue_top; |
157
|
537 // Resize buddy window (no need to move it) |
|
538 wresize(search_entry->win, lines, cols); |
168
|
539 werase(search_entry->win); |
151
|
540 // If a panel exists, replace the old window with the new |
|
541 if (search_entry->panel) { |
|
542 replace_panel(search_entry->panel, search_entry->win); |
|
543 } |
|
544 // Redo line wrapping |
189
|
545 rescue_top = hbuf_previous_persistent(search_entry->top); |
151
|
546 hbuf_rebuild(&search_entry->hbuf, |
185
|
547 maxX - ROSTER_WIDTH - PREFIX_WIDTH); |
189
|
548 if (g_list_position(g_list_first(search_entry->hbuf), search_entry->top) == -1) |
|
549 search_entry->top = rescue_top; |
151
|
550 } |
|
551 } |
|
552 |
|
553 // Refresh current buddy window |
157
|
554 if (chatmode) |
151
|
555 scr_ShowBuddyWindow(); |
|
556 } |
|
557 |
143
|
558 // scr_DrawRoster() |
|
559 // Actually, display the buddylist on the screen. |
81
|
560 void scr_DrawRoster(void) |
24
|
561 { |
81
|
562 static guint offset = 0; |
|
563 char name[ROSTER_WIDTH]; |
|
564 int maxx, maxy; |
|
565 GList *buddy; |
|
566 int i, n; |
|
567 int rOffset; |
164
|
568 enum imstatus currentstatus = jb_getstatus(); |
81
|
569 |
123
|
570 // We can reset update_roster |
|
571 update_roster = FALSE; |
|
572 |
81
|
573 getmaxyx(rosterWnd, maxy, maxx); |
|
574 maxx --; // last char is for vertical border |
|
575 name[ROSTER_WIDTH-7] = 0; |
|
576 |
|
577 // cleanup of roster window |
168
|
578 werase(rosterWnd); |
|
579 // Redraw the vertical line (not very good...) |
81
|
580 wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
168
|
581 for (i=0 ; i < CHAT_WIN_HEIGHT ; i++) |
|
582 mvwaddch(rosterWnd, i, ROSTER_WIDTH-1, ACS_VLINE); |
81
|
583 |
|
584 // Leave now if buddylist is empty |
|
585 if (!buddylist) { |
|
586 offset = 0; |
123
|
587 update_panels(); |
|
588 doupdate(); |
81
|
589 return; |
|
590 } |
|
591 |
84
|
592 // Update offset if necessary |
|
593 i = g_list_position(buddylist, current_buddy); |
|
594 if (i == -1) { // This is bad |
|
595 scr_LogPrint("Doh! Can't find current selected buddy!!"); |
123
|
596 update_panels(); |
|
597 doupdate(); |
84
|
598 return; |
|
599 } else if (i < offset) { |
|
600 offset = i; |
|
601 } else if (i+1 > offset + maxy) { |
|
602 offset = i + 1 - maxy; |
|
603 } |
81
|
604 |
|
605 buddy = buddylist; |
|
606 rOffset = offset; |
|
607 |
84
|
608 for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) { |
81
|
609 |
|
610 char status = '?'; |
|
611 char pending = ' '; |
|
612 enum imstatus budstate; |
149
|
613 unsigned short ismsg = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG; |
|
614 unsigned short isgrp = buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP; |
|
615 unsigned short ishid = buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_HIDE; |
81
|
616 |
|
617 if (rOffset > 0) { |
|
618 rOffset--; |
|
619 continue; |
|
620 } |
|
621 |
149
|
622 // Display message notice if there is a message flag, but not |
|
623 // for unfolded groups. |
|
624 if (ismsg && (!isgrp || ishid)) { |
81
|
625 pending = '#'; |
|
626 } |
|
627 |
|
628 budstate = buddy_getstatus(BUDDATA(buddy)); |
164
|
629 if (budstate >= 0 && budstate < imstatus_size && currentstatus != offline) |
81
|
630 status = imstatus2char[budstate]; |
|
631 if (buddy == current_buddy) { |
|
632 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL)); |
|
633 // The 3 following lines aim to color the whole line |
|
634 wmove(rosterWnd, i, 0); |
|
635 for (n = 0; n < maxx; n++) |
|
636 waddch(rosterWnd, ' '); |
|
637 } else { |
149
|
638 if (pending == '#') |
139
|
639 wattrset(rosterWnd, COLOR_PAIR(COLOR_NMSG)); |
|
640 else |
|
641 wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES)); |
81
|
642 } |
|
643 |
|
644 strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7); |
149
|
645 if (isgrp) { |
133
|
646 char *sep; |
149
|
647 if (ishid) |
133
|
648 sep = "+++"; |
|
649 else |
|
650 sep = "---"; |
|
651 mvwprintw(rosterWnd, i, 0, " %c%s %s", pending, sep, name); |
|
652 } |
126
|
653 else |
|
654 mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name); |
84
|
655 |
|
656 i++; |
81
|
657 } |
|
658 |
142
|
659 top_panel(inputPanel); |
81
|
660 update_panels(); |
|
661 doupdate(); |
24
|
662 } |
|
663 |
184
|
664 void scr_WriteMessage(const char *jid, const char *text, time_t timestamp, |
|
665 guint prefix_flags) |
24
|
666 { |
184
|
667 if (!timestamp) timestamp = time(NULL); |
|
668 |
|
669 scr_WriteInWindow(jid, text, timestamp, prefix_flags, FALSE); |
47
|
670 } |
|
671 |
184
|
672 void scr_WriteIncomingMessage(const char *jidfrom, const char *text, |
|
673 time_t timestamp) |
47
|
674 { |
75
|
675 // FIXME expand tabs / filter out special chars... |
184
|
676 scr_WriteMessage(jidfrom, text, timestamp, HBB_PREFIX_IN); |
24
|
677 update_panels(); |
|
678 doupdate(); |
|
679 } |
|
680 |
50
|
681 void scr_WriteOutgoingMessage(const char *jidto, const char *text) |
47
|
682 { |
184
|
683 scr_WriteMessage(jidto, text, 0, HBB_PREFIX_OUT); |
47
|
684 scr_ShowWindow(jidto); |
|
685 } |
|
686 |
24
|
687 int scr_Getch(void) |
|
688 { |
|
689 int ch; |
|
690 ch = wgetch(inputWnd); |
|
691 return ch; |
|
692 } |
|
693 |
|
694 WINDOW *scr_GetRosterWindow(void) |
|
695 { |
|
696 return rosterWnd; |
|
697 } |
|
698 |
|
699 WINDOW *scr_GetStatusWindow(void) |
|
700 { |
|
701 return chatWnd; |
|
702 } |
|
703 |
|
704 WINDOW *scr_GetInputWindow(void) |
|
705 { |
|
706 return inputWnd; |
|
707 } |
|
708 |
143
|
709 // scr_RosterTop() |
|
710 // Go to the first buddy in the buddylist |
105
|
711 void scr_RosterTop(void) |
104
|
712 { |
143
|
713 enum imstatus prev_st = imstatus_size; // undef |
119
|
714 |
|
715 if (current_buddy) { |
|
716 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
717 if (chatmode) |
|
718 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
719 } |
104
|
720 current_buddy = buddylist; |
119
|
721 if (chatmode && current_buddy) |
|
722 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
723 |
|
724 // We should rebuild the buddylist but not everytime |
143
|
725 // Here we check if we were locking a buddy who is actually offline, |
|
726 // and hide_offline_buddies is TRUE. In which case we need to rebuild. |
120
|
727 if (current_buddy && prev_st == offline && |
|
728 buddylist_get_hide_offline_buddies()) |
119
|
729 buddylist_build(); |
104
|
730 if (chatmode) |
|
731 scr_ShowBuddyWindow(); |
143
|
732 update_roster = TRUE; |
104
|
733 } |
|
734 |
143
|
735 // scr_RosterBottom() |
|
736 // Go to the last buddy in the buddylist |
105
|
737 void scr_RosterBottom(void) |
104
|
738 { |
143
|
739 enum imstatus prev_st = imstatus_size; // undef |
119
|
740 |
|
741 if (current_buddy) { |
|
742 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
743 if (chatmode) |
|
744 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
745 } |
104
|
746 current_buddy = g_list_last(buddylist); |
143
|
747 // Lock the buddy in the buddylist if we're in chat mode |
119
|
748 if (chatmode && current_buddy) |
|
749 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
750 |
|
751 // We should rebuild the buddylist but not everytime |
143
|
752 // Here we check if we were locking a buddy who is actually offline, |
|
753 // and hide_offline_buddies is TRUE. In which case we need to rebuild. |
120
|
754 if (current_buddy && prev_st == offline && |
|
755 buddylist_get_hide_offline_buddies()) |
119
|
756 buddylist_build(); |
143
|
757 |
104
|
758 if (chatmode) |
|
759 scr_ShowBuddyWindow(); |
143
|
760 update_roster = TRUE; |
104
|
761 } |
|
762 |
143
|
763 // scr_RosterUp() |
|
764 // Go to the previous buddy in the buddylist |
105
|
765 void scr_RosterUp(void) |
81
|
766 { |
143
|
767 enum imstatus prev_st = imstatus_size; // undef |
119
|
768 |
81
|
769 if (current_buddy) { |
|
770 if (g_list_previous(current_buddy)) { |
120
|
771 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119
|
772 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81
|
773 current_buddy = g_list_previous(current_buddy); |
143
|
774 // Lock the buddy in the buddylist if we're in chat mode |
119
|
775 if (chatmode) |
|
776 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
777 // We should rebuild the buddylist but not everytime |
143
|
778 // Here we check if we were locking a buddy who is actually offline, |
|
779 // and hide_offline_buddies is TRUE. In which case we need to rebuild. |
120
|
780 if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119
|
781 buddylist_build(); |
143
|
782 update_roster = TRUE; |
81
|
783 } |
|
784 } |
104
|
785 |
|
786 if (chatmode) |
|
787 scr_ShowBuddyWindow(); |
81
|
788 } |
|
789 |
143
|
790 // scr_RosterDown() |
|
791 // Go to the next buddy in the buddylist |
105
|
792 void scr_RosterDown(void) |
81
|
793 { |
143
|
794 enum imstatus prev_st = imstatus_size; // undef |
119
|
795 |
81
|
796 if (current_buddy) { |
|
797 if (g_list_next(current_buddy)) { |
120
|
798 prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119
|
799 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81
|
800 current_buddy = g_list_next(current_buddy); |
119
|
801 if (chatmode) |
|
802 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
803 // We should rebuild the buddylist but not everytime |
143
|
804 // Here we check if we were locking a buddy who is actually offline, |
|
805 // and hide_offline_buddies is TRUE. In which case we need to rebuild. |
120
|
806 if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119
|
807 buddylist_build(); |
143
|
808 update_roster = TRUE; |
81
|
809 } |
|
810 } |
104
|
811 |
|
812 if (chatmode) |
|
813 scr_ShowBuddyWindow(); |
81
|
814 } |
|
815 |
143
|
816 // scr_ScrollUp() |
|
817 // Scroll up the current buddy window, half a screen. |
105
|
818 void scr_ScrollUp(void) |
|
819 { |
|
820 const gchar *jid; |
|
821 window_entry_t *win_entry; |
|
822 int n, nblines; |
|
823 GList *hbuf_top; |
|
824 |
|
825 // Get win_entry |
|
826 if (!current_buddy) |
|
827 return; |
|
828 jid = CURRENT_JID; |
|
829 if (!jid) |
|
830 return; |
|
831 win_entry = scr_SearchWindow(jid); |
|
832 if (!win_entry) |
|
833 return; |
|
834 |
|
835 // Scroll up half a screen (or less) |
|
836 nblines = CHAT_WIN_HEIGHT/2-1; |
|
837 hbuf_top = win_entry->top; |
|
838 if (!hbuf_top) { |
|
839 hbuf_top = g_list_last(win_entry->hbuf); |
109
|
840 if (!win_entry->cleared) |
|
841 nblines *= 3; |
|
842 else |
|
843 win_entry->cleared = FALSE; |
105
|
844 } |
|
845 |
|
846 n = 0; |
|
847 while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) { |
|
848 hbuf_top = g_list_previous(hbuf_top); |
|
849 n++; |
|
850 } |
|
851 win_entry->top = hbuf_top; |
|
852 |
|
853 // Refresh the window |
|
854 scr_UpdateWindow(win_entry); |
|
855 |
|
856 // Finished :) |
|
857 update_panels(); |
|
858 doupdate(); |
|
859 } |
|
860 |
143
|
861 // scr_ScrollDown() |
|
862 // Scroll down the current buddy window, half a screen. |
105
|
863 void scr_ScrollDown(void) |
|
864 { |
|
865 const gchar *jid; |
|
866 window_entry_t *win_entry; |
|
867 int n, nblines; |
|
868 GList *hbuf_top; |
|
869 |
|
870 // Get win_entry |
|
871 if (!current_buddy) |
|
872 return; |
|
873 jid = CURRENT_JID; |
|
874 if (!jid) |
|
875 return; |
|
876 win_entry = scr_SearchWindow(jid); |
|
877 if (!win_entry) |
|
878 return; |
|
879 |
|
880 // Scroll down half a screen (or less) |
|
881 nblines = CHAT_WIN_HEIGHT/2-1; |
|
882 hbuf_top = win_entry->top; |
|
883 |
|
884 for (n=0 ; hbuf_top && n < nblines ; n++) |
|
885 hbuf_top = g_list_next(hbuf_top); |
|
886 win_entry->top = hbuf_top; |
|
887 // Check if we are at the bottom |
|
888 for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++) |
|
889 hbuf_top = g_list_next(hbuf_top); |
|
890 if (!hbuf_top) |
|
891 win_entry->top = NULL; // End reached |
|
892 |
|
893 // Refresh the window |
|
894 scr_UpdateWindow(win_entry); |
|
895 |
|
896 // Finished :) |
|
897 update_panels(); |
|
898 doupdate(); |
|
899 } |
|
900 |
143
|
901 // scr_Clear() |
|
902 // Clear the current buddy window (used for the /clear command) |
108
|
903 void scr_Clear(void) |
|
904 { |
|
905 const gchar *jid; |
|
906 window_entry_t *win_entry; |
|
907 |
|
908 // Get win_entry |
|
909 if (!current_buddy) |
|
910 return; |
|
911 jid = CURRENT_JID; |
|
912 if (!jid) |
|
913 return; |
|
914 win_entry = scr_SearchWindow(jid); |
|
915 if (!win_entry) |
|
916 return; |
|
917 |
|
918 win_entry->cleared = TRUE; |
109
|
919 win_entry->top = NULL; |
108
|
920 |
|
921 // Refresh the window |
|
922 scr_UpdateWindow(win_entry); |
|
923 |
|
924 // Finished :) |
|
925 update_panels(); |
|
926 doupdate(); |
|
927 } |
|
928 |
187
|
929 // scr_BufferTop() |
|
930 // Jump to the head of the current buddy window |
|
931 void scr_BufferTop(void) |
|
932 { |
|
933 const gchar *jid; |
|
934 window_entry_t *win_entry; |
|
935 |
|
936 // Get win_entry |
|
937 if (!current_buddy) return; |
|
938 jid = CURRENT_JID; |
|
939 if (!jid) return; |
|
940 win_entry = scr_SearchWindow(jid); |
|
941 |
|
942 if (!win_entry) return; |
|
943 |
|
944 win_entry->cleared = FALSE; |
|
945 win_entry->top = g_list_first(win_entry->hbuf); |
|
946 |
|
947 // Refresh the window |
|
948 scr_UpdateWindow(win_entry); |
|
949 |
|
950 // Finished :) |
|
951 update_panels(); |
|
952 doupdate(); |
|
953 } |
|
954 |
|
955 // scr_BufferBottom() |
|
956 // Jump to the end of the current buddy window |
|
957 void scr_BufferBottom(void) |
|
958 { |
|
959 const gchar *jid; |
|
960 window_entry_t *win_entry; |
|
961 |
|
962 // Get win_entry |
|
963 if (!current_buddy) return; |
|
964 jid = CURRENT_JID; |
|
965 if (!jid) return; |
|
966 win_entry = scr_SearchWindow(jid); |
|
967 |
|
968 if (!win_entry) return; |
|
969 |
|
970 win_entry->cleared = FALSE; |
|
971 win_entry->top = NULL; |
|
972 |
|
973 // Refresh the window |
|
974 scr_UpdateWindow(win_entry); |
|
975 |
|
976 // Finished :) |
|
977 update_panels(); |
|
978 doupdate(); |
|
979 } |
|
980 |
44
|
981 // scr_LogPrint(...) |
|
982 // Display a message in the log window. |
24
|
983 void scr_LogPrint(const char *fmt, ...) |
|
984 { |
|
985 time_t timestamp; |
|
986 char *buffer; |
|
987 va_list ap; |
|
988 |
153
|
989 do { |
157
|
990 buffer = (char *) calloc(1, 1024); |
153
|
991 } while (!buffer); |
24
|
992 |
|
993 timestamp = time(NULL); |
|
994 strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
995 wprintw(logWnd, "\n%s", buffer); |
|
996 |
|
997 va_start(ap, fmt); |
153
|
998 vsnprintf(buffer, 1024, fmt, ap); |
24
|
999 va_end(ap); |
|
1000 |
|
1001 wprintw(logWnd, "%s", buffer); |
|
1002 free(buffer); |
|
1003 |
|
1004 update_panels(); |
|
1005 doupdate(); |
|
1006 } |
|
1007 |
143
|
1008 // scr_set_chatmode() |
|
1009 // Public fonction to (un)set chatmode... |
129
|
1010 inline void scr_set_chatmode(int enable) |
|
1011 { |
|
1012 chatmode = enable; |
|
1013 } |
|
1014 |
173
|
1015 // scr_cmdhisto_addline() |
|
1016 // Add a line to the inputLine history |
|
1017 inline void scr_cmdhisto_addline(char *line) |
|
1018 { |
|
1019 if (!line || !*line) return; |
|
1020 |
|
1021 cmdhisto = g_list_append(cmdhisto, g_strdup(line)); |
|
1022 } |
|
1023 |
|
1024 // scr_cmdhisto_prev() |
|
1025 // Look for previous line beginning w/ the given mask in the inputLine history |
175
|
1026 // Returns NULL if none found |
173
|
1027 const char *scr_cmdhisto_prev(char *mask, guint len) |
|
1028 { |
|
1029 GList *hl; |
|
1030 if (!cmdhisto_cur) { |
|
1031 hl = g_list_last(cmdhisto); |
174
|
1032 if (hl) { // backup current line |
|
1033 strncpy(cmdhisto_backup, mask, INPUTLINE_LENGTH); |
|
1034 } |
173
|
1035 } else { |
|
1036 hl = g_list_previous(cmdhisto_cur); |
|
1037 } |
|
1038 while (hl) { |
|
1039 if (!strncmp((char*)hl->data, mask, len)) { |
|
1040 // Found a match |
|
1041 cmdhisto_cur = hl; |
|
1042 return (const char*)hl->data; |
|
1043 } |
|
1044 hl = g_list_previous(hl); |
|
1045 } |
|
1046 return NULL; |
|
1047 } |
|
1048 |
|
1049 // scr_cmdhisto_next() |
|
1050 // Look for next line beginning w/ the given mask in the inputLine history |
175
|
1051 // Returns NULL if none found |
173
|
1052 const char *scr_cmdhisto_next(char *mask, guint len) |
|
1053 { |
|
1054 GList *hl; |
|
1055 if (!cmdhisto_cur) return NULL; |
|
1056 hl = cmdhisto_cur; |
|
1057 while ((hl = g_list_next(hl)) != NULL) |
|
1058 if (!strncmp((char*)hl->data, mask, len)) { |
|
1059 // Found a match |
|
1060 cmdhisto_cur = hl; |
|
1061 return (const char*)hl->data; |
|
1062 } |
175
|
1063 // If the "backuped" line matches, we'll use it |
|
1064 if (strncmp(cmdhisto_backup, mask, len)) return NULL; // No match |
174
|
1065 cmdhisto_cur = NULL; |
|
1066 return cmdhisto_backup; |
173
|
1067 } |
|
1068 |
98
|
1069 // which_row() |
|
1070 // Tells which row our cursor is in, in the command line. |
|
1071 // -1 -> normal text |
|
1072 // 0 -> command |
|
1073 // 1 -> parameter 1 (etc.) |
102
|
1074 // If > 0, then *p_row is set to the beginning of the row |
|
1075 int which_row(char **p_row) |
98
|
1076 { |
|
1077 int row = -1; |
|
1078 char *p; |
|
1079 int quote = FALSE; |
|
1080 |
|
1081 // Not a command? |
|
1082 if ((ptr_inputline == inputLine) || (inputLine[0] != '/')) |
|
1083 return -1; |
|
1084 |
|
1085 // This is a command |
|
1086 row = 0; |
|
1087 for (p = inputLine ; p < ptr_inputline ; p++) { |
|
1088 if (quote) { |
|
1089 if (*p == '"' && *(p-1) != '\\') |
|
1090 quote = FALSE; |
|
1091 continue; |
|
1092 } |
|
1093 if (*p == '"' && *(p-1) != '\\') { |
|
1094 quote = TRUE; |
121
|
1095 } else if (*p == ' ') { |
|
1096 if (*(p-1) != ' ') |
|
1097 row++; |
102
|
1098 *p_row = p+1; |
|
1099 } |
98
|
1100 } |
|
1101 return row; |
|
1102 } |
|
1103 |
143
|
1104 // scr_insert_text() |
|
1105 // Insert the given text at the current cursor position. |
|
1106 // The cursor is moved. We don't check if the cursor still is in the screen |
|
1107 // after, the caller should do that. |
98
|
1108 void scr_insert_text(const char *text) |
|
1109 { |
|
1110 char tmpLine[INPUTLINE_LENGTH+1]; |
|
1111 int len = strlen(text); |
|
1112 // Check the line isn't too long |
|
1113 if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { |
|
1114 scr_LogPrint("Cannot insert text, line too long."); |
|
1115 return; |
|
1116 } |
|
1117 |
|
1118 strcpy(tmpLine, ptr_inputline); |
|
1119 strcpy(ptr_inputline, text); ptr_inputline += len; |
|
1120 strcpy(ptr_inputline, tmpLine); |
|
1121 } |
|
1122 |
143
|
1123 // scr_handle_tab() |
|
1124 // Function called when tab is pressed. |
|
1125 // Initiate or continue a completion... |
98
|
1126 void scr_handle_tab(void) |
|
1127 { |
102
|
1128 int nrow; |
|
1129 char *row; |
|
1130 const char *cchar; |
103
|
1131 guint compl_categ; |
98
|
1132 |
102
|
1133 nrow = which_row(&row); |
98
|
1134 |
103
|
1135 // a) No completion if no leading slash ('cause not a command) |
|
1136 // b) We can't have more than 2 parameters (we use 2 flags) |
|
1137 if (nrow < 0 || nrow > 2) return; |
102
|
1138 |
103
|
1139 if (nrow == 0) { // Command completion |
|
1140 row = &inputLine[1]; |
|
1141 compl_categ = COMPL_CMD; |
|
1142 } else { // Other completion, depending on the command |
|
1143 cmd *com = cmd_get(inputLine); |
|
1144 if (!com || !row) { |
|
1145 scr_LogPrint("I cannot complete that..."); |
|
1146 return; |
|
1147 } |
|
1148 compl_categ = com->completion_flags[nrow-1]; |
|
1149 } |
|
1150 |
|
1151 if (!completion_started) { |
|
1152 GSList *list = compl_get_category_list(compl_categ); |
|
1153 if (list) { |
|
1154 char *prefix = g_strndup(row, ptr_inputline-row); |
|
1155 // Init completion |
|
1156 new_completion(prefix, list); |
|
1157 g_free(prefix); |
|
1158 // Now complete |
98
|
1159 cchar = complete(); |
|
1160 if (cchar) |
|
1161 scr_insert_text(cchar); |
103
|
1162 completion_started = TRUE; |
98
|
1163 } |
103
|
1164 } else { // Completion already initialized |
|
1165 char *c; |
|
1166 guint back = cancel_completion(); |
|
1167 // Remove $back chars |
|
1168 ptr_inputline -= back; |
|
1169 c = ptr_inputline; |
|
1170 for ( ; *c ; c++) |
|
1171 *c = *(c+back); |
|
1172 // Now complete again |
|
1173 cchar = complete(); |
|
1174 if (cchar) |
|
1175 scr_insert_text(cchar); |
102
|
1176 } |
98
|
1177 } |
|
1178 |
|
1179 void scr_cancel_current_completion(void) |
|
1180 { |
|
1181 char *c; |
|
1182 guint back = cancel_completion(); |
|
1183 // Remove $back chars |
|
1184 ptr_inputline -= back; |
|
1185 c = ptr_inputline; |
|
1186 for ( ; *c ; c++) |
|
1187 *c = *(c+back); |
|
1188 } |
|
1189 |
|
1190 void scr_end_current_completion(void) |
|
1191 { |
|
1192 done_completion(); |
|
1193 completion_started = FALSE; |
|
1194 } |
|
1195 |
24
|
1196 // check_offset(int direction) |
|
1197 // Check inputline_offset value, and make sure the cursor is inside the |
|
1198 // screen. |
|
1199 inline void check_offset(int direction) |
|
1200 { |
|
1201 // Left side |
|
1202 if (inputline_offset && direction <= 0) { |
|
1203 while (ptr_inputline <= (char*)&inputLine + inputline_offset) { |
|
1204 if (inputline_offset) { |
|
1205 inputline_offset -= 5; |
|
1206 if (inputline_offset < 0) |
|
1207 inputline_offset = 0; |
|
1208 } |
|
1209 } |
|
1210 } |
|
1211 // Right side |
|
1212 if (direction >= 0) { |
|
1213 while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) |
|
1214 inputline_offset += 5; |
|
1215 } |
|
1216 } |
|
1217 |
44
|
1218 // process_key(key) |
|
1219 // Handle the pressed key, in the command line (bottom). |
29
|
1220 int process_key(int key) |
24
|
1221 { |
|
1222 if (isprint(key)) { |
|
1223 char tmpLine[INPUTLINE_LENGTH+1]; |
|
1224 |
|
1225 // Check the line isn't too long |
|
1226 if (strlen(inputLine) >= INPUTLINE_LENGTH) |
|
1227 return 0; |
|
1228 |
|
1229 // Insert char |
|
1230 strcpy(tmpLine, ptr_inputline); |
|
1231 *ptr_inputline++ = key; |
|
1232 strcpy(ptr_inputline, tmpLine); |
|
1233 check_offset(1); |
|
1234 } else { |
|
1235 switch(key) { |
|
1236 case KEY_BACKSPACE: |
|
1237 if (ptr_inputline != (char*)&inputLine) { |
42
|
1238 char *c = --ptr_inputline; |
|
1239 for ( ; *c ; c++) |
|
1240 *c = *(c+1); |
24
|
1241 check_offset(-1); |
|
1242 } |
|
1243 break; |
|
1244 case KEY_DC: |
|
1245 if (*ptr_inputline) |
|
1246 strcpy(ptr_inputline, ptr_inputline+1); |
|
1247 break; |
|
1248 case KEY_LEFT: |
|
1249 if (ptr_inputline != (char*)&inputLine) { |
|
1250 ptr_inputline--; |
|
1251 check_offset(-1); |
|
1252 } |
|
1253 break; |
|
1254 case KEY_RIGHT: |
|
1255 if (*ptr_inputline) |
|
1256 ptr_inputline++; |
|
1257 check_offset(1); |
|
1258 break; |
98
|
1259 case 7: // Ctrl-g |
|
1260 scr_cancel_current_completion(); |
|
1261 scr_end_current_completion(); |
|
1262 check_offset(-1); |
|
1263 break; |
24
|
1264 case 9: // Tab |
98
|
1265 scr_handle_tab(); |
|
1266 check_offset(0); |
24
|
1267 break; |
|
1268 case '\n': // Enter |
29
|
1269 if (process_line(inputLine)) |
24
|
1270 return 255; |
173
|
1271 // Add line to history |
|
1272 scr_cmdhisto_addline(inputLine); |
|
1273 cmdhisto_cur = NULL; |
|
1274 // Reset the line |
24
|
1275 ptr_inputline = inputLine; |
|
1276 *ptr_inputline = 0; |
|
1277 inputline_offset = 0; |
|
1278 break; |
|
1279 case KEY_UP: |
175
|
1280 { |
|
1281 const char *l = scr_cmdhisto_prev(inputLine, |
|
1282 ptr_inputline-inputLine); |
|
1283 if (l) { |
|
1284 strcpy(inputLine, l); |
|
1285 } |
|
1286 } |
24
|
1287 break; |
|
1288 case KEY_DOWN: |
175
|
1289 { |
|
1290 const char *l = scr_cmdhisto_next(inputLine, |
|
1291 ptr_inputline-inputLine); |
|
1292 if (l) { |
|
1293 strcpy(inputLine, l); |
|
1294 } |
|
1295 } |
24
|
1296 break; |
|
1297 case KEY_PPAGE: |
175
|
1298 scr_RosterUp(); |
24
|
1299 break; |
|
1300 case KEY_NPAGE: |
175
|
1301 scr_RosterDown(); |
24
|
1302 break; |
|
1303 case KEY_HOME: |
|
1304 case 1: |
|
1305 ptr_inputline = inputLine; |
|
1306 inputline_offset = 0; |
|
1307 break; |
|
1308 case KEY_END: |
|
1309 case 5: |
|
1310 for (; *ptr_inputline; ptr_inputline++) ; |
|
1311 check_offset(1); |
|
1312 break; |
|
1313 case 21: // Ctrl-u |
|
1314 strcpy(inputLine, ptr_inputline); |
|
1315 ptr_inputline = inputLine; |
|
1316 inputline_offset = 0; |
|
1317 break; |
|
1318 case KEY_EOL: |
|
1319 case 11: // Ctrl-k |
|
1320 *ptr_inputline = 0; |
|
1321 break; |
|
1322 case 16: // Ctrl-p |
175
|
1323 scr_ScrollUp(); |
24
|
1324 break; |
|
1325 case 14: // Ctrl-n |
175
|
1326 scr_ScrollDown(); |
24
|
1327 break; |
|
1328 case 27: // ESC |
|
1329 currentWindow = NULL; |
|
1330 chatmode = FALSE; |
119
|
1331 if (current_buddy) |
|
1332 buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
24
|
1333 top_panel(chatPanel); |
|
1334 top_panel(inputPanel); |
157
|
1335 update_panels(); |
24
|
1336 break; |
151
|
1337 case 12: // Ctrl-l |
|
1338 case KEY_RESIZE: |
|
1339 scr_Resize(); |
|
1340 break; |
24
|
1341 default: |
|
1342 scr_LogPrint("Unkown key=%d", key); |
|
1343 } |
|
1344 } |
157
|
1345 if (completion_started && key != 9 && key != KEY_RESIZE) |
98
|
1346 scr_end_current_completion(); |
24
|
1347 mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
|
1348 wclrtoeol(inputWnd); |
|
1349 if (*ptr_inputline) { |
|
1350 wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
|
1351 } |
157
|
1352 if (!update_roster) { |
|
1353 //update_panels(); |
|
1354 doupdate(); |
|
1355 } |
24
|
1356 return 0; |
|
1357 } |