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