24
|
1 |
#include <stdio.h> |
|
2 |
#include <stdlib.h> |
|
3 |
#include <string.h> |
|
4 |
#include <ncurses.h> |
|
5 |
#include <panel.h> |
|
6 |
#include <time.h> |
|
7 |
#include <ctype.h> |
|
8 |
#include <locale.h> |
|
9 |
|
|
10 |
#include "screen.h" |
81
|
11 |
#include "hbuf.h" |
47
|
12 |
#include "commands.h" |
95
|
13 |
#include "compl.h" |
81
|
14 |
#include "roster.h" |
24
|
15 |
#include "parsecfg.h" |
81
|
16 |
#include "utils.h" |
24
|
17 |
#include "list.h" |
|
18 |
|
|
19 |
#define window_entry(n) list_entry(n, window_entry_t, list) |
|
20 |
|
|
21 |
LIST_HEAD(window_list); |
|
22 |
|
|
23 |
typedef struct _window_entry_t { |
|
24 |
WINDOW *win; |
108
|
25 |
PANEL *panel; |
|
26 |
char *name; |
|
27 |
GList *hbuf; |
|
28 |
GList *top; // If top is not specified (NULL), we'll display the last lines |
|
29 |
char cleared; // For ex, user has issued a /clear command... |
24
|
30 |
struct list_head list; |
|
31 |
} window_entry_t; |
|
32 |
|
|
33 |
|
|
34 |
static WINDOW *rosterWnd, *chatWnd, *inputWnd; |
|
35 |
static WINDOW *logWnd, *logWnd_border; |
|
36 |
static PANEL *rosterPanel, *chatPanel, *inputPanel; |
|
37 |
static PANEL *logPanel, *logPanel_border; |
|
38 |
static int maxY, maxX; |
|
39 |
static window_entry_t *currentWindow; |
|
40 |
|
|
41 |
static int chatmode; |
30
|
42 |
int update_roster; |
24
|
43 |
|
|
44 |
static char inputLine[INPUTLINE_LENGTH+1]; |
|
45 |
static char *ptr_inputline; |
|
46 |
static short int inputline_offset; |
98
|
47 |
static int completion_started; |
24
|
48 |
|
|
49 |
|
99
|
50 |
/* Functions */ |
24
|
51 |
|
74
|
52 |
int scr_WindowWidth(WINDOW * win) |
24
|
53 |
{ |
|
54 |
int x, y; |
|
55 |
getmaxyx(win, y, x); |
|
56 |
return x; |
|
57 |
} |
|
58 |
|
74
|
59 |
void scr_clear_box(WINDOW *win, int y, int x, int height, int width, int Color) |
|
60 |
{ |
|
61 |
int i, j; |
|
62 |
|
|
63 |
wattrset(win, COLOR_PAIR(Color)); |
|
64 |
for (i = 0; i < height; i++) { |
|
65 |
wmove(win, y + i, x); |
|
66 |
for (j = 0; j < width; j++) |
|
67 |
wprintw(win, " "); |
|
68 |
} |
|
69 |
} |
|
70 |
|
24
|
71 |
void scr_draw_box(WINDOW * win, int y, int x, int height, int width, |
|
72 |
int Color, chtype box, chtype border) |
|
73 |
{ |
|
74 |
int i, j; |
|
75 |
|
|
76 |
wattrset(win, COLOR_PAIR(Color)); |
|
77 |
for (i = 0; i < height; i++) { |
|
78 |
wmove(win, y + i, x); |
|
79 |
for (j = 0; j < width; j++) |
|
80 |
if (!i && !j) |
|
81 |
waddch(win, border | ACS_ULCORNER); |
|
82 |
else if (i == height - 1 && !j) |
|
83 |
waddch(win, border | ACS_LLCORNER); |
|
84 |
else if (!i && j == width - 1) |
|
85 |
waddch(win, box | ACS_URCORNER); |
|
86 |
else if (i == height - 1 && j == width - 1) |
|
87 |
waddch(win, box | ACS_LRCORNER); |
|
88 |
else if (!i) |
|
89 |
waddch(win, border | ACS_HLINE); |
|
90 |
else if (i == height - 1) |
|
91 |
waddch(win, box | ACS_HLINE); |
|
92 |
else if (!j) |
|
93 |
waddch(win, border | ACS_VLINE); |
|
94 |
else if (j == width - 1) |
|
95 |
waddch(win, box | ACS_VLINE); |
|
96 |
else |
|
97 |
waddch(win, box | ' '); |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
int FindColor(char *name) |
|
102 |
{ |
|
103 |
if (!strcmp(name, "default")) |
|
104 |
return -1; |
|
105 |
if (!strcmp(name, "black")) |
|
106 |
return COLOR_BLACK; |
|
107 |
if (!strcmp(name, "red")) |
|
108 |
return COLOR_RED; |
|
109 |
if (!strcmp(name, "green")) |
|
110 |
return COLOR_GREEN; |
|
111 |
if (!strcmp(name, "yellow")) |
|
112 |
return COLOR_YELLOW; |
|
113 |
if (!strcmp(name, "blue")) |
|
114 |
return COLOR_BLUE; |
|
115 |
if (!strcmp(name, "magenta")) |
|
116 |
return COLOR_MAGENTA; |
|
117 |
if (!strcmp(name, "cyan")) |
|
118 |
return COLOR_CYAN; |
|
119 |
if (!strcmp(name, "white")) |
|
120 |
return COLOR_WHITE; |
|
121 |
|
|
122 |
return -1; |
|
123 |
} |
|
124 |
|
|
125 |
void ParseColors(void) |
|
126 |
{ |
|
127 |
char *colors[11] = { |
|
128 |
"", "", |
|
129 |
"borderlines", |
|
130 |
"jidonline", |
139
|
131 |
"newmsg", |
24
|
132 |
"jidofflineselected", |
|
133 |
"jidoffline", |
|
134 |
"text", |
|
135 |
NULL |
|
136 |
}; |
|
137 |
|
|
138 |
char *tmp = malloc(1024); |
|
139 |
char *color1; |
|
140 |
char *background = cfg_read("color_background"); |
|
141 |
char *backselected = cfg_read("color_backselected"); |
|
142 |
int i = 0; |
|
143 |
|
|
144 |
while (colors[i]) { |
|
145 |
sprintf(tmp, "color_%s", colors[i]); |
|
146 |
color1 = cfg_read(tmp); |
|
147 |
|
|
148 |
switch (i + 1) { |
|
149 |
case 1: |
|
150 |
init_pair(1, COLOR_BLACK, COLOR_WHITE); |
|
151 |
break; |
|
152 |
case 2: |
|
153 |
init_pair(2, COLOR_WHITE, COLOR_BLACK); |
|
154 |
break; |
|
155 |
case 3: |
|
156 |
init_pair(3, FindColor(color1), FindColor(background)); |
|
157 |
break; |
|
158 |
case 4: |
|
159 |
init_pair(4, FindColor(color1), FindColor(backselected)); |
|
160 |
break; |
|
161 |
case 5: |
|
162 |
init_pair(5, FindColor(color1), FindColor(background)); |
|
163 |
break; |
|
164 |
case 6: |
|
165 |
init_pair(6, FindColor(color1), FindColor(backselected)); |
|
166 |
break; |
|
167 |
case 7: |
|
168 |
init_pair(7, FindColor(color1), FindColor(background)); |
|
169 |
break; |
|
170 |
case 8: |
|
171 |
init_pair(8, FindColor(color1), FindColor(background)); |
|
172 |
break; |
|
173 |
} |
|
174 |
i++; |
|
175 |
} |
|
176 |
} |
|
177 |
|
|
178 |
|
50
|
179 |
window_entry_t *scr_CreatePanel(const char *title, int x, int y, |
|
180 |
int lines, int cols, int dont_show) |
24
|
181 |
{ |
|
182 |
window_entry_t *tmp = calloc(1, sizeof(window_entry_t)); |
|
183 |
|
|
184 |
tmp->win = newwin(lines, cols, y, x); |
|
185 |
tmp->panel = new_panel(tmp->win); |
|
186 |
tmp->name = (char *) calloc(1, 1024); |
|
187 |
strncpy(tmp->name, title, 1024); |
76
|
188 |
scr_clear_box(tmp->win, 0, 0, lines, cols, COLOR_GENERAL); |
24
|
189 |
|
|
190 |
if ((!dont_show)) { |
|
191 |
currentWindow = tmp; |
|
192 |
} else { |
|
193 |
if (currentWindow) |
|
194 |
top_panel(currentWindow->panel); |
|
195 |
else |
|
196 |
top_panel(chatPanel); |
|
197 |
} |
|
198 |
|
|
199 |
list_add_tail(&tmp->list, &window_list); |
|
200 |
update_panels(); |
|
201 |
|
|
202 |
return tmp; |
|
203 |
} |
|
204 |
|
50
|
205 |
window_entry_t *scr_SearchWindow(const char *winId) |
24
|
206 |
{ |
|
207 |
struct list_head *pos, *n; |
|
208 |
window_entry_t *search_entry = NULL; |
|
209 |
|
|
210 |
list_for_each_safe(pos, n, &window_list) { |
|
211 |
search_entry = window_entry(pos); |
|
212 |
if (search_entry->name) { |
|
213 |
if (!strcasecmp(search_entry->name, winId)) { |
|
214 |
return search_entry; |
|
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
return NULL; |
|
219 |
} |
|
220 |
|
74
|
221 |
void scr_UpdateWindow(window_entry_t *win_entry) |
|
222 |
{ |
|
223 |
int n; |
|
224 |
int width; |
|
225 |
char **lines; |
|
226 |
GList *hbuf_head; |
|
227 |
|
108
|
228 |
width = scr_WindowWidth(win_entry->win); |
|
229 |
|
|
230 |
// Should the window be empty? |
|
231 |
if (win_entry->cleared) { |
|
232 |
scr_clear_box(win_entry->win, 0, 0, CHAT_WIN_HEIGHT, width, COLOR_GENERAL); |
|
233 |
return; |
|
234 |
} |
|
235 |
|
105
|
236 |
// win_entry->top is the top message of the screen. If it set to NULL, we |
|
237 |
// are displaying the last messages. |
|
238 |
|
74
|
239 |
// We will show the last CHAT_WIN_HEIGHT lines. |
|
240 |
// Let's find out where it begins. |
105
|
241 |
if (!win_entry->top || |
|
242 |
(g_list_position(g_list_first(win_entry->hbuf), win_entry->top) == -1)) { |
|
243 |
// Move up CHAT_WIN_HEIGHT lines |
|
244 |
win_entry->hbuf = g_list_last(win_entry->hbuf); |
|
245 |
hbuf_head = win_entry->hbuf; |
|
246 |
win_entry->top = NULL; // (Just to make sure) |
|
247 |
n = 0; |
|
248 |
while (hbuf_head && (n < CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head)) { |
|
249 |
hbuf_head = g_list_previous(hbuf_head); |
|
250 |
n++; |
|
251 |
} |
|
252 |
} else |
|
253 |
hbuf_head = win_entry->top; |
74
|
254 |
|
|
255 |
// Get the last CHAT_WIN_HEIGHT lines. |
|
256 |
lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT); |
|
257 |
|
|
258 |
// Display these lines |
|
259 |
wmove(win_entry->win, 0, 0); |
|
260 |
for (n = 0; n < CHAT_WIN_HEIGHT; n++) { |
|
261 |
int r = width; |
75
|
262 |
if (*(lines+2*n)) { |
|
263 |
if (**(lines+2*n)) |
|
264 |
wprintw(win_entry->win, "%s", *(lines+2*n)); // prefix |
|
265 |
else { |
|
266 |
wprintw(win_entry->win, " "); |
|
267 |
r -= 12; |
|
268 |
} |
|
269 |
wprintw(win_entry->win, "%s", *(lines+2*n+1)); // line |
|
270 |
// Calculate the number of blank characters to empty the line |
|
271 |
r -= strlen(*(lines+2*n)) + strlen(*(lines+2*n+1)); |
|
272 |
} |
74
|
273 |
for ( ; r>0 ; r--) { |
|
274 |
wprintw(win_entry->win, " "); |
|
275 |
} |
|
276 |
//// wclrtoeol(win_entry->win); does not work :( |
|
277 |
} |
|
278 |
g_free(lines); |
|
279 |
} |
|
280 |
|
50
|
281 |
void scr_ShowWindow(const char *winId) |
24
|
282 |
{ |
74
|
283 |
window_entry_t *win_entry = scr_SearchWindow(winId); |
|
284 |
|
|
285 |
if (win_entry != NULL) { |
|
286 |
top_panel(win_entry->panel); |
|
287 |
currentWindow = win_entry; |
24
|
288 |
chatmode = TRUE; |
81
|
289 |
roster_setflags(winId, ROSTER_FLAG_MSG, FALSE); |
119
|
290 |
roster_setflags(winId, ROSTER_FLAG_LOCK, TRUE); |
30
|
291 |
update_roster = TRUE; |
74
|
292 |
|
105
|
293 |
// Refresh the window |
74
|
294 |
scr_UpdateWindow(win_entry); |
|
295 |
|
|
296 |
// Finished :) |
24
|
297 |
update_panels(); |
|
298 |
doupdate(); |
|
299 |
} else { |
|
300 |
top_panel(chatPanel); |
74
|
301 |
currentWindow = win_entry; // == NULL (current window empty) |
24
|
302 |
} |
|
303 |
} |
|
304 |
|
|
305 |
void scr_ShowBuddyWindow(void) |
|
306 |
{ |
105
|
307 |
const gchar *jid; |
140
|
308 |
|
105
|
309 |
if (!current_buddy) |
140
|
310 |
jid = NULL; |
|
311 |
else |
|
312 |
jid = CURRENT_JID; |
|
313 |
|
|
314 |
if (!jid) { |
|
315 |
top_panel(chatPanel); |
|
316 |
currentWindow = NULL; |
105
|
317 |
return; |
140
|
318 |
} |
|
319 |
|
105
|
320 |
scr_ShowWindow(jid); |
24
|
321 |
top_panel(inputPanel); |
|
322 |
} |
|
323 |
|
|
324 |
|
75
|
325 |
void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp, |
|
326 |
const char *prefix, int force_show) |
24
|
327 |
{ |
75
|
328 |
char *fullprefix = NULL; |
74
|
329 |
window_entry_t *win_entry; |
24
|
330 |
int dont_show = FALSE; |
|
331 |
|
75
|
332 |
// Prepare the prefix |
|
333 |
if (prefix || TimeStamp) { |
|
334 |
if (!prefix) prefix = ""; |
|
335 |
fullprefix = calloc(1, strlen(prefix)+16); |
|
336 |
if (TimeStamp) { |
|
337 |
time_t now = time(NULL); |
|
338 |
strftime(fullprefix, 12, "[%H:%M] ", localtime(&now)); |
|
339 |
} else { |
|
340 |
strcpy(fullprefix, " "); |
|
341 |
} |
|
342 |
strcat(fullprefix, prefix); |
74
|
343 |
} |
|
344 |
|
|
345 |
// Look for the window entry. |
|
346 |
win_entry = scr_SearchWindow(winId); |
|
347 |
|
|
348 |
// Do we have to really show the window? |
24
|
349 |
if (!chatmode) |
|
350 |
dont_show = TRUE; |
74
|
351 |
else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry)))) |
24
|
352 |
dont_show = TRUE; |
|
353 |
|
74
|
354 |
// If the window entry doesn't exist yet, let's create it. |
|
355 |
if (win_entry == NULL) { |
|
356 |
win_entry = scr_CreatePanel(winId, ROSTER_WIDTH, 0, CHAT_WIN_HEIGHT, |
|
357 |
maxX - ROSTER_WIDTH, dont_show); |
24
|
358 |
} |
|
359 |
|
75
|
360 |
hbuf_add_line(&win_entry->hbuf, text, fullprefix, |
74
|
361 |
maxX - scr_WindowWidth(rosterWnd) - 14); |
75
|
362 |
free(fullprefix); |
74
|
363 |
|
108
|
364 |
if (win_entry->cleared) { |
|
365 |
win_entry->cleared = 0; // The message must be displayed |
|
366 |
win_entry->top = g_list_last(win_entry->hbuf); |
|
367 |
} |
|
368 |
|
24
|
369 |
if (!dont_show) { |
74
|
370 |
// Show and refresh the window |
|
371 |
top_panel(win_entry->panel); |
|
372 |
scr_UpdateWindow(win_entry); |
24
|
373 |
update_panels(); |
|
374 |
doupdate(); |
|
375 |
} else { |
81
|
376 |
roster_setflags(winId, ROSTER_FLAG_MSG, TRUE); |
30
|
377 |
update_roster = TRUE; |
24
|
378 |
} |
|
379 |
} |
|
380 |
|
|
381 |
void scr_InitCurses(void) |
|
382 |
{ |
|
383 |
initscr(); |
|
384 |
noecho(); |
|
385 |
raw(); |
35
|
386 |
halfdelay(5); |
24
|
387 |
start_color(); |
|
388 |
use_default_colors(); |
|
389 |
|
|
390 |
ParseColors(); |
|
391 |
|
|
392 |
getmaxyx(stdscr, maxY, maxX); |
|
393 |
inputLine[0] = 0; |
|
394 |
ptr_inputline = inputLine; |
|
395 |
|
35
|
396 |
setlocale(LC_CTYPE, ""); |
24
|
397 |
|
|
398 |
return; |
|
399 |
} |
|
400 |
|
81
|
401 |
void scr_TerminateCurses(void) |
|
402 |
{ |
|
403 |
clear(); |
|
404 |
refresh(); |
|
405 |
endwin(); |
|
406 |
return; |
|
407 |
} |
|
408 |
|
24
|
409 |
void scr_DrawMainWindow(void) |
|
410 |
{ |
74
|
411 |
int l; |
|
412 |
|
24
|
413 |
/* Draw main panels */ |
74
|
414 |
rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0); |
24
|
415 |
rosterPanel = new_panel(rosterWnd); |
74
|
416 |
scr_clear_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, ROSTER_WIDTH, |
|
417 |
COLOR_GENERAL); |
|
418 |
for (l=0 ; l < CHAT_WIN_HEIGHT ; l++) |
|
419 |
mvwaddch(rosterWnd, l, ROSTER_WIDTH-1, ACS_VLINE); |
24
|
420 |
|
74
|
421 |
chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH); |
24
|
422 |
chatPanel = new_panel(chatWnd); |
74
|
423 |
scr_clear_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, |
|
424 |
COLOR_GENERAL); |
|
425 |
scrollok(chatWnd, TRUE); |
|
426 |
mvwprintw(chatWnd, 0, 0, "This is the status window"); |
24
|
427 |
|
|
428 |
logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0); |
|
429 |
logPanel_border = new_panel(logWnd_border); |
|
430 |
scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0); |
|
431 |
logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1); |
|
432 |
logPanel = new_panel(logWnd); |
|
433 |
wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
434 |
|
74
|
435 |
scrollok(logWnd, TRUE); |
24
|
436 |
|
|
437 |
inputWnd = newwin(1, maxX, maxY-1, 0); |
|
438 |
inputPanel = new_panel(inputWnd); |
|
439 |
|
81
|
440 |
scr_DrawRoster(); |
24
|
441 |
update_panels(); |
|
442 |
doupdate(); |
|
443 |
return; |
|
444 |
} |
|
445 |
|
81
|
446 |
void scr_DrawRoster(void) |
24
|
447 |
{ |
81
|
448 |
static guint offset = 0; |
|
449 |
char name[ROSTER_WIDTH]; |
|
450 |
int maxx, maxy; |
|
451 |
GList *buddy; |
|
452 |
int i, n; |
|
453 |
int rOffset; |
|
454 |
|
123
|
455 |
// We can reset update_roster |
|
456 |
update_roster = FALSE; |
|
457 |
|
81
|
458 |
getmaxyx(rosterWnd, maxy, maxx); |
|
459 |
maxx --; // last char is for vertical border |
|
460 |
name[ROSTER_WIDTH-7] = 0; |
|
461 |
|
|
462 |
// cleanup of roster window |
|
463 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
464 |
for (i = 0; i < maxy; i++) { |
|
465 |
mvwprintw(rosterWnd, i, 0, ""); |
|
466 |
for (n = 0; n < maxx; n++) |
|
467 |
waddch(rosterWnd, ' '); |
|
468 |
} |
|
469 |
|
|
470 |
// Leave now if buddylist is empty |
|
471 |
if (!buddylist) { |
|
472 |
offset = 0; |
123
|
473 |
update_panels(); |
|
474 |
doupdate(); |
81
|
475 |
return; |
|
476 |
} |
|
477 |
|
84
|
478 |
// Update offset if necessary |
|
479 |
i = g_list_position(buddylist, current_buddy); |
|
480 |
if (i == -1) { // This is bad |
|
481 |
scr_LogPrint("Doh! Can't find current selected buddy!!"); |
123
|
482 |
update_panels(); |
|
483 |
doupdate(); |
84
|
484 |
return; |
|
485 |
} else if (i < offset) { |
|
486 |
offset = i; |
|
487 |
} else if (i+1 > offset + maxy) { |
|
488 |
offset = i + 1 - maxy; |
|
489 |
} |
81
|
490 |
|
|
491 |
buddy = buddylist; |
|
492 |
rOffset = offset; |
|
493 |
|
84
|
494 |
for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) { |
81
|
495 |
|
|
496 |
char status = '?'; |
|
497 |
char pending = ' '; |
|
498 |
enum imstatus budstate; |
|
499 |
|
|
500 |
if (rOffset > 0) { |
|
501 |
rOffset--; |
|
502 |
continue; |
|
503 |
} |
|
504 |
|
|
505 |
if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) { |
|
506 |
pending = '#'; |
|
507 |
} |
|
508 |
|
|
509 |
budstate = buddy_getstatus(BUDDATA(buddy)); |
|
510 |
if (budstate >= 0 && budstate < imstatus_size) |
|
511 |
status = imstatus2char[budstate]; |
|
512 |
if (buddy == current_buddy) { |
|
513 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL)); |
|
514 |
// The 3 following lines aim to color the whole line |
|
515 |
wmove(rosterWnd, i, 0); |
|
516 |
for (n = 0; n < maxx; n++) |
|
517 |
waddch(rosterWnd, ' '); |
|
518 |
} else { |
139
|
519 |
if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) |
|
520 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_NMSG)); |
|
521 |
else |
|
522 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES)); |
81
|
523 |
} |
|
524 |
|
|
525 |
strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7); |
133
|
526 |
if (buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP) { |
|
527 |
char *sep; |
|
528 |
if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_HIDE) |
|
529 |
sep = "+++"; |
|
530 |
else |
|
531 |
sep = "---"; |
|
532 |
mvwprintw(rosterWnd, i, 0, " %c%s %s", pending, sep, name); |
|
533 |
} |
126
|
534 |
else |
|
535 |
mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name); |
84
|
536 |
|
|
537 |
i++; |
81
|
538 |
} |
|
539 |
|
|
540 |
update_panels(); |
|
541 |
doupdate(); |
24
|
542 |
} |
|
543 |
|
50
|
544 |
void scr_WriteMessage(const char *jid, const char *text, char *prefix) |
24
|
545 |
{ |
75
|
546 |
scr_WriteInWindow(jid, text, TRUE, prefix, FALSE); |
47
|
547 |
} |
|
548 |
|
50
|
549 |
void scr_WriteIncomingMessage(const char *jidfrom, const char *text) |
47
|
550 |
{ |
75
|
551 |
// FIXME expand tabs / filter out special chars... |
115
|
552 |
scr_WriteMessage(jidfrom, text, "<== "); |
65
|
553 |
top_panel(inputPanel); |
24
|
554 |
update_panels(); |
|
555 |
doupdate(); |
|
556 |
} |
|
557 |
|
50
|
558 |
void scr_WriteOutgoingMessage(const char *jidto, const char *text) |
47
|
559 |
{ |
65
|
560 |
scr_WriteMessage(jidto, text, "--> "); |
47
|
561 |
scr_ShowWindow(jidto); |
65
|
562 |
top_panel(inputPanel); |
47
|
563 |
} |
|
564 |
|
24
|
565 |
int scr_Getch(void) |
|
566 |
{ |
|
567 |
int ch; |
|
568 |
// keypad(inputWnd, TRUE); |
|
569 |
ch = wgetch(inputWnd); |
|
570 |
return ch; |
|
571 |
} |
|
572 |
|
|
573 |
WINDOW *scr_GetRosterWindow(void) |
|
574 |
{ |
|
575 |
return rosterWnd; |
|
576 |
} |
|
577 |
|
|
578 |
WINDOW *scr_GetStatusWindow(void) |
|
579 |
{ |
|
580 |
return chatWnd; |
|
581 |
} |
|
582 |
|
|
583 |
WINDOW *scr_GetInputWindow(void) |
|
584 |
{ |
|
585 |
return inputWnd; |
|
586 |
} |
|
587 |
|
105
|
588 |
void scr_RosterTop(void) |
104
|
589 |
{ |
119
|
590 |
enum imstatus prev_st; |
|
591 |
|
|
592 |
if (current_buddy) { |
|
593 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
594 |
if (chatmode) |
|
595 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
596 |
} |
104
|
597 |
current_buddy = buddylist; |
119
|
598 |
if (chatmode && current_buddy) |
|
599 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
600 |
|
|
601 |
// We should rebuild the buddylist but not everytime |
120
|
602 |
if (current_buddy && prev_st == offline && |
|
603 |
buddylist_get_hide_offline_buddies()) |
119
|
604 |
buddylist_build(); |
104
|
605 |
if (chatmode) |
|
606 |
scr_ShowBuddyWindow(); |
|
607 |
} |
|
608 |
|
105
|
609 |
void scr_RosterBottom(void) |
104
|
610 |
{ |
119
|
611 |
enum imstatus prev_st; |
|
612 |
|
|
613 |
if (current_buddy) { |
|
614 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
|
615 |
if (chatmode) |
|
616 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
|
617 |
} |
104
|
618 |
current_buddy = g_list_last(buddylist); |
119
|
619 |
if (chatmode && current_buddy) |
|
620 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
621 |
|
|
622 |
// We should rebuild the buddylist but not everytime |
120
|
623 |
if (current_buddy && prev_st == offline && |
|
624 |
buddylist_get_hide_offline_buddies()) |
119
|
625 |
buddylist_build(); |
104
|
626 |
if (chatmode) |
|
627 |
scr_ShowBuddyWindow(); |
|
628 |
} |
|
629 |
|
105
|
630 |
void scr_RosterUp(void) |
81
|
631 |
{ |
119
|
632 |
enum imstatus prev_st; |
|
633 |
|
81
|
634 |
if (current_buddy) { |
|
635 |
if (g_list_previous(current_buddy)) { |
120
|
636 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119
|
637 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81
|
638 |
current_buddy = g_list_previous(current_buddy); |
119
|
639 |
if (chatmode) |
|
640 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
641 |
// We should rebuild the buddylist but not everytime |
120
|
642 |
if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119
|
643 |
buddylist_build(); |
81
|
644 |
scr_DrawRoster(); |
|
645 |
} |
|
646 |
} |
104
|
647 |
|
|
648 |
if (chatmode) |
|
649 |
scr_ShowBuddyWindow(); |
81
|
650 |
} |
|
651 |
|
105
|
652 |
void scr_RosterDown(void) |
81
|
653 |
{ |
119
|
654 |
enum imstatus prev_st; |
|
655 |
|
81
|
656 |
if (current_buddy) { |
|
657 |
if (g_list_next(current_buddy)) { |
120
|
658 |
prev_st = buddy_getstatus(BUDDATA(current_buddy)); |
119
|
659 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
81
|
660 |
current_buddy = g_list_next(current_buddy); |
119
|
661 |
if (chatmode) |
|
662 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, TRUE); |
|
663 |
// We should rebuild the buddylist but not everytime |
120
|
664 |
if (prev_st == offline && buddylist_get_hide_offline_buddies()) |
119
|
665 |
buddylist_build(); |
81
|
666 |
scr_DrawRoster(); |
|
667 |
} |
|
668 |
} |
104
|
669 |
|
|
670 |
if (chatmode) |
|
671 |
scr_ShowBuddyWindow(); |
81
|
672 |
} |
|
673 |
|
105
|
674 |
void scr_ScrollUp(void) |
|
675 |
{ |
|
676 |
const gchar *jid; |
|
677 |
window_entry_t *win_entry; |
|
678 |
int n, nblines; |
|
679 |
GList *hbuf_top; |
|
680 |
|
|
681 |
// Get win_entry |
|
682 |
if (!current_buddy) |
|
683 |
return; |
|
684 |
jid = CURRENT_JID; |
|
685 |
if (!jid) |
|
686 |
return; |
|
687 |
win_entry = scr_SearchWindow(jid); |
|
688 |
if (!win_entry) |
|
689 |
return; |
|
690 |
|
|
691 |
// Scroll up half a screen (or less) |
|
692 |
nblines = CHAT_WIN_HEIGHT/2-1; |
|
693 |
hbuf_top = win_entry->top; |
|
694 |
if (!hbuf_top) { |
|
695 |
hbuf_top = g_list_last(win_entry->hbuf); |
109
|
696 |
if (!win_entry->cleared) |
|
697 |
nblines *= 3; |
|
698 |
else |
|
699 |
win_entry->cleared = FALSE; |
105
|
700 |
} |
|
701 |
|
|
702 |
n = 0; |
|
703 |
while (hbuf_top && n < nblines && g_list_previous(hbuf_top)) { |
|
704 |
hbuf_top = g_list_previous(hbuf_top); |
|
705 |
n++; |
|
706 |
} |
|
707 |
win_entry->top = hbuf_top; |
|
708 |
|
|
709 |
// Refresh the window |
|
710 |
scr_UpdateWindow(win_entry); |
|
711 |
|
|
712 |
// Finished :) |
|
713 |
update_panels(); |
|
714 |
doupdate(); |
|
715 |
} |
|
716 |
|
|
717 |
void scr_ScrollDown(void) |
|
718 |
{ |
|
719 |
const gchar *jid; |
|
720 |
window_entry_t *win_entry; |
|
721 |
int n, nblines; |
|
722 |
GList *hbuf_top; |
|
723 |
|
|
724 |
// Get win_entry |
|
725 |
if (!current_buddy) |
|
726 |
return; |
|
727 |
jid = CURRENT_JID; |
|
728 |
if (!jid) |
|
729 |
return; |
|
730 |
win_entry = scr_SearchWindow(jid); |
|
731 |
if (!win_entry) |
|
732 |
return; |
|
733 |
|
|
734 |
// Scroll down half a screen (or less) |
|
735 |
nblines = CHAT_WIN_HEIGHT/2-1; |
|
736 |
hbuf_top = win_entry->top; |
|
737 |
|
|
738 |
for (n=0 ; hbuf_top && n < nblines ; n++) |
|
739 |
hbuf_top = g_list_next(hbuf_top); |
|
740 |
win_entry->top = hbuf_top; |
|
741 |
// Check if we are at the bottom |
|
742 |
for (n=0 ; hbuf_top && n < CHAT_WIN_HEIGHT-1 ; n++) |
|
743 |
hbuf_top = g_list_next(hbuf_top); |
|
744 |
if (!hbuf_top) |
|
745 |
win_entry->top = NULL; // End reached |
|
746 |
|
|
747 |
// Refresh the window |
|
748 |
scr_UpdateWindow(win_entry); |
|
749 |
|
|
750 |
// Finished :) |
|
751 |
update_panels(); |
|
752 |
doupdate(); |
|
753 |
} |
|
754 |
|
108
|
755 |
void scr_Clear(void) |
|
756 |
{ |
|
757 |
const gchar *jid; |
|
758 |
window_entry_t *win_entry; |
|
759 |
|
|
760 |
// Get win_entry |
|
761 |
if (!current_buddy) |
|
762 |
return; |
|
763 |
jid = CURRENT_JID; |
|
764 |
if (!jid) |
|
765 |
return; |
|
766 |
win_entry = scr_SearchWindow(jid); |
|
767 |
if (!win_entry) |
|
768 |
return; |
|
769 |
|
|
770 |
win_entry->cleared = TRUE; |
109
|
771 |
win_entry->top = NULL; |
108
|
772 |
|
|
773 |
// Refresh the window |
|
774 |
scr_UpdateWindow(win_entry); |
|
775 |
|
|
776 |
// Finished :) |
|
777 |
update_panels(); |
|
778 |
doupdate(); |
|
779 |
} |
|
780 |
|
44
|
781 |
// scr_LogPrint(...) |
|
782 |
// Display a message in the log window. |
24
|
783 |
void scr_LogPrint(const char *fmt, ...) |
|
784 |
{ |
|
785 |
time_t timestamp; |
|
786 |
char *buffer; |
|
787 |
va_list ap; |
|
788 |
|
|
789 |
buffer = (char *) calloc(1, 4096); |
|
790 |
|
|
791 |
timestamp = time(NULL); |
|
792 |
strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
793 |
wprintw(logWnd, "\n%s", buffer); |
|
794 |
|
|
795 |
va_start(ap, fmt); |
|
796 |
vsnprintf(buffer, 4096, fmt, ap); |
|
797 |
va_end(ap); |
|
798 |
|
|
799 |
wprintw(logWnd, "%s", buffer); |
|
800 |
free(buffer); |
|
801 |
|
|
802 |
update_panels(); |
|
803 |
doupdate(); |
|
804 |
} |
|
805 |
|
129
|
806 |
inline void scr_set_chatmode(int enable) |
|
807 |
{ |
|
808 |
chatmode = enable; |
|
809 |
} |
|
810 |
|
98
|
811 |
// which_row() |
|
812 |
// Tells which row our cursor is in, in the command line. |
|
813 |
// -1 -> normal text |
|
814 |
// 0 -> command |
|
815 |
// 1 -> parameter 1 (etc.) |
102
|
816 |
// If > 0, then *p_row is set to the beginning of the row |
|
817 |
int which_row(char **p_row) |
98
|
818 |
{ |
|
819 |
int row = -1; |
|
820 |
char *p; |
|
821 |
int quote = FALSE; |
|
822 |
|
|
823 |
// Not a command? |
|
824 |
if ((ptr_inputline == inputLine) || (inputLine[0] != '/')) |
|
825 |
return -1; |
|
826 |
|
|
827 |
// This is a command |
|
828 |
row = 0; |
|
829 |
for (p = inputLine ; p < ptr_inputline ; p++) { |
|
830 |
if (quote) { |
|
831 |
if (*p == '"' && *(p-1) != '\\') |
|
832 |
quote = FALSE; |
|
833 |
continue; |
|
834 |
} |
|
835 |
if (*p == '"' && *(p-1) != '\\') { |
|
836 |
quote = TRUE; |
121
|
837 |
} else if (*p == ' ') { |
|
838 |
if (*(p-1) != ' ') |
|
839 |
row++; |
102
|
840 |
*p_row = p+1; |
|
841 |
} |
98
|
842 |
} |
|
843 |
return row; |
|
844 |
} |
|
845 |
|
|
846 |
void scr_insert_text(const char *text) |
|
847 |
{ |
|
848 |
char tmpLine[INPUTLINE_LENGTH+1]; |
|
849 |
int len = strlen(text); |
|
850 |
// Check the line isn't too long |
|
851 |
if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { |
|
852 |
scr_LogPrint("Cannot insert text, line too long."); |
|
853 |
return; |
|
854 |
} |
|
855 |
|
|
856 |
strcpy(tmpLine, ptr_inputline); |
|
857 |
strcpy(ptr_inputline, text); ptr_inputline += len; |
|
858 |
strcpy(ptr_inputline, tmpLine); |
|
859 |
} |
|
860 |
|
|
861 |
void scr_handle_tab(void) |
|
862 |
{ |
102
|
863 |
int nrow; |
|
864 |
char *row; |
|
865 |
const char *cchar; |
103
|
866 |
guint compl_categ; |
98
|
867 |
|
102
|
868 |
nrow = which_row(&row); |
98
|
869 |
|
103
|
870 |
// a) No completion if no leading slash ('cause not a command) |
|
871 |
// b) We can't have more than 2 parameters (we use 2 flags) |
|
872 |
if (nrow < 0 || nrow > 2) return; |
102
|
873 |
|
103
|
874 |
if (nrow == 0) { // Command completion |
|
875 |
row = &inputLine[1]; |
|
876 |
compl_categ = COMPL_CMD; |
|
877 |
} else { // Other completion, depending on the command |
|
878 |
cmd *com = cmd_get(inputLine); |
|
879 |
if (!com || !row) { |
|
880 |
scr_LogPrint("I cannot complete that..."); |
|
881 |
return; |
|
882 |
} |
|
883 |
compl_categ = com->completion_flags[nrow-1]; |
|
884 |
} |
|
885 |
|
|
886 |
if (!completion_started) { |
|
887 |
GSList *list = compl_get_category_list(compl_categ); |
|
888 |
if (list) { |
|
889 |
char *prefix = g_strndup(row, ptr_inputline-row); |
|
890 |
// Init completion |
|
891 |
new_completion(prefix, list); |
|
892 |
g_free(prefix); |
|
893 |
// Now complete |
98
|
894 |
cchar = complete(); |
|
895 |
if (cchar) |
|
896 |
scr_insert_text(cchar); |
103
|
897 |
completion_started = TRUE; |
98
|
898 |
} |
103
|
899 |
} else { // Completion already initialized |
|
900 |
char *c; |
|
901 |
guint back = cancel_completion(); |
|
902 |
// Remove $back chars |
|
903 |
ptr_inputline -= back; |
|
904 |
c = ptr_inputline; |
|
905 |
for ( ; *c ; c++) |
|
906 |
*c = *(c+back); |
|
907 |
// Now complete again |
|
908 |
cchar = complete(); |
|
909 |
if (cchar) |
|
910 |
scr_insert_text(cchar); |
102
|
911 |
} |
98
|
912 |
} |
|
913 |
|
|
914 |
void scr_cancel_current_completion(void) |
|
915 |
{ |
|
916 |
char *c; |
|
917 |
guint back = cancel_completion(); |
|
918 |
// Remove $back chars |
|
919 |
ptr_inputline -= back; |
|
920 |
c = ptr_inputline; |
|
921 |
for ( ; *c ; c++) |
|
922 |
*c = *(c+back); |
|
923 |
} |
|
924 |
|
|
925 |
void scr_end_current_completion(void) |
|
926 |
{ |
|
927 |
done_completion(); |
|
928 |
completion_started = FALSE; |
|
929 |
} |
|
930 |
|
24
|
931 |
// check_offset(int direction) |
|
932 |
// Check inputline_offset value, and make sure the cursor is inside the |
|
933 |
// screen. |
|
934 |
inline void check_offset(int direction) |
|
935 |
{ |
|
936 |
// Left side |
|
937 |
if (inputline_offset && direction <= 0) { |
|
938 |
while (ptr_inputline <= (char*)&inputLine + inputline_offset) { |
|
939 |
if (inputline_offset) { |
|
940 |
inputline_offset -= 5; |
|
941 |
if (inputline_offset < 0) |
|
942 |
inputline_offset = 0; |
|
943 |
} |
|
944 |
} |
|
945 |
} |
|
946 |
// Right side |
|
947 |
if (direction >= 0) { |
|
948 |
while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) |
|
949 |
inputline_offset += 5; |
|
950 |
} |
|
951 |
} |
|
952 |
|
44
|
953 |
// process_key(key) |
|
954 |
// Handle the pressed key, in the command line (bottom). |
29
|
955 |
int process_key(int key) |
24
|
956 |
{ |
|
957 |
if (isprint(key)) { |
|
958 |
char tmpLine[INPUTLINE_LENGTH+1]; |
|
959 |
|
|
960 |
// Check the line isn't too long |
|
961 |
if (strlen(inputLine) >= INPUTLINE_LENGTH) |
|
962 |
return 0; |
|
963 |
|
|
964 |
// Insert char |
|
965 |
strcpy(tmpLine, ptr_inputline); |
|
966 |
*ptr_inputline++ = key; |
|
967 |
strcpy(ptr_inputline, tmpLine); |
|
968 |
check_offset(1); |
|
969 |
} else { |
|
970 |
switch(key) { |
|
971 |
case KEY_BACKSPACE: |
|
972 |
if (ptr_inputline != (char*)&inputLine) { |
42
|
973 |
char *c = --ptr_inputline; |
|
974 |
for ( ; *c ; c++) |
|
975 |
*c = *(c+1); |
24
|
976 |
check_offset(-1); |
|
977 |
} |
|
978 |
break; |
|
979 |
case KEY_DC: |
|
980 |
if (*ptr_inputline) |
|
981 |
strcpy(ptr_inputline, ptr_inputline+1); |
|
982 |
break; |
|
983 |
case KEY_LEFT: |
|
984 |
if (ptr_inputline != (char*)&inputLine) { |
|
985 |
ptr_inputline--; |
|
986 |
check_offset(-1); |
|
987 |
} |
|
988 |
break; |
|
989 |
case KEY_RIGHT: |
|
990 |
if (*ptr_inputline) |
|
991 |
ptr_inputline++; |
|
992 |
check_offset(1); |
|
993 |
break; |
98
|
994 |
case 7: // Ctrl-g |
|
995 |
scr_cancel_current_completion(); |
|
996 |
scr_end_current_completion(); |
|
997 |
check_offset(-1); |
|
998 |
break; |
24
|
999 |
case 9: // Tab |
98
|
1000 |
scr_handle_tab(); |
|
1001 |
check_offset(0); |
24
|
1002 |
break; |
|
1003 |
case '\n': // Enter |
29
|
1004 |
if (process_line(inputLine)) |
24
|
1005 |
return 255; |
|
1006 |
ptr_inputline = inputLine; |
|
1007 |
*ptr_inputline = 0; |
|
1008 |
inputline_offset = 0; |
|
1009 |
break; |
|
1010 |
case KEY_UP: |
81
|
1011 |
scr_RosterUp(); |
24
|
1012 |
break; |
|
1013 |
case KEY_DOWN: |
81
|
1014 |
scr_RosterDown(); |
24
|
1015 |
break; |
|
1016 |
case KEY_PPAGE: |
105
|
1017 |
scr_ScrollUp(); |
24
|
1018 |
break; |
|
1019 |
case KEY_NPAGE: |
105
|
1020 |
scr_ScrollDown(); |
24
|
1021 |
break; |
|
1022 |
case KEY_HOME: |
|
1023 |
case 1: |
|
1024 |
ptr_inputline = inputLine; |
|
1025 |
inputline_offset = 0; |
|
1026 |
break; |
|
1027 |
case KEY_END: |
|
1028 |
case 5: |
|
1029 |
for (; *ptr_inputline; ptr_inputline++) ; |
|
1030 |
check_offset(1); |
|
1031 |
break; |
|
1032 |
case 21: // Ctrl-u |
|
1033 |
strcpy(inputLine, ptr_inputline); |
|
1034 |
ptr_inputline = inputLine; |
|
1035 |
inputline_offset = 0; |
|
1036 |
break; |
|
1037 |
case KEY_EOL: |
|
1038 |
case 11: // Ctrl-k |
|
1039 |
*ptr_inputline = 0; |
|
1040 |
break; |
|
1041 |
case 16: // Ctrl-p |
|
1042 |
scr_LogPrint("Ctrl-p not yet implemented"); |
|
1043 |
break; |
|
1044 |
case 14: // Ctrl-n |
|
1045 |
scr_LogPrint("Ctrl-n not yet implemented"); |
|
1046 |
break; |
|
1047 |
case 27: // ESC |
|
1048 |
currentWindow = NULL; |
|
1049 |
chatmode = FALSE; |
119
|
1050 |
if (current_buddy) |
|
1051 |
buddy_setflags(BUDDATA(current_buddy), ROSTER_FLAG_LOCK, FALSE); |
24
|
1052 |
top_panel(chatPanel); |
|
1053 |
top_panel(inputPanel); |
|
1054 |
break; |
|
1055 |
default: |
|
1056 |
scr_LogPrint("Unkown key=%d", key); |
|
1057 |
} |
|
1058 |
} |
98
|
1059 |
if (completion_started && key != 9) |
|
1060 |
scr_end_current_completion(); |
24
|
1061 |
mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
|
1062 |
wclrtoeol(inputWnd); |
|
1063 |
if (*ptr_inputline) { |
|
1064 |
wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
|
1065 |
} |
|
1066 |
update_panels(); |
|
1067 |
doupdate(); |
|
1068 |
return 0; |
|
1069 |
} |