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