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