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