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