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" |
81
|
13 |
#include "roster.h" |
24
|
14 |
#include "parsecfg.h" |
|
15 |
#include "lang.h" |
81
|
16 |
#include "utf8.h" |
|
17 |
#include "utils.h" |
24
|
18 |
#include "list.h" |
|
19 |
|
|
20 |
#define window_entry(n) list_entry(n, window_entry_t, list) |
|
21 |
|
|
22 |
LIST_HEAD(window_list); |
|
23 |
|
|
24 |
typedef struct _window_entry_t { |
|
25 |
WINDOW *win; |
|
26 |
PANEL *panel; |
|
27 |
char *name; |
74
|
28 |
GList *hbuf; |
24
|
29 |
struct list_head list; |
|
30 |
} window_entry_t; |
|
31 |
|
|
32 |
|
|
33 |
/* Variables globales a SCREEN.C */ |
|
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; |
|
47 |
|
|
48 |
|
|
49 |
/* Funciones */ |
|
50 |
|
74
|
51 |
int scr_WindowWidth(WINDOW * win) |
24
|
52 |
{ |
|
53 |
int x, y; |
|
54 |
getmaxyx(win, y, x); |
|
55 |
return x; |
|
56 |
} |
|
57 |
|
74
|
58 |
void scr_clear_box(WINDOW *win, int y, int x, int height, int width, int Color) |
|
59 |
{ |
|
60 |
int i, j; |
|
61 |
|
|
62 |
wattrset(win, COLOR_PAIR(Color)); |
|
63 |
for (i = 0; i < height; i++) { |
|
64 |
wmove(win, y + i, x); |
|
65 |
for (j = 0; j < width; j++) |
|
66 |
wprintw(win, " "); |
|
67 |
} |
|
68 |
} |
|
69 |
|
24
|
70 |
void scr_draw_box(WINDOW * win, int y, int x, int height, int width, |
|
71 |
int Color, chtype box, chtype border) |
|
72 |
{ |
|
73 |
int i, j; |
|
74 |
|
|
75 |
wattrset(win, COLOR_PAIR(Color)); |
|
76 |
for (i = 0; i < height; i++) { |
|
77 |
wmove(win, y + i, x); |
|
78 |
for (j = 0; j < width; j++) |
|
79 |
if (!i && !j) |
|
80 |
waddch(win, border | ACS_ULCORNER); |
|
81 |
else if (i == height - 1 && !j) |
|
82 |
waddch(win, border | ACS_LLCORNER); |
|
83 |
else if (!i && j == width - 1) |
|
84 |
waddch(win, box | ACS_URCORNER); |
|
85 |
else if (i == height - 1 && j == width - 1) |
|
86 |
waddch(win, box | ACS_LRCORNER); |
|
87 |
else if (!i) |
|
88 |
waddch(win, border | ACS_HLINE); |
|
89 |
else if (i == height - 1) |
|
90 |
waddch(win, box | ACS_HLINE); |
|
91 |
else if (!j) |
|
92 |
waddch(win, border | ACS_VLINE); |
|
93 |
else if (j == width - 1) |
|
94 |
waddch(win, box | ACS_VLINE); |
|
95 |
else |
|
96 |
waddch(win, box | ' '); |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
int FindColor(char *name) |
|
101 |
{ |
|
102 |
if (!strcmp(name, "default")) |
|
103 |
return -1; |
|
104 |
if (!strcmp(name, "black")) |
|
105 |
return COLOR_BLACK; |
|
106 |
if (!strcmp(name, "red")) |
|
107 |
return COLOR_RED; |
|
108 |
if (!strcmp(name, "green")) |
|
109 |
return COLOR_GREEN; |
|
110 |
if (!strcmp(name, "yellow")) |
|
111 |
return COLOR_YELLOW; |
|
112 |
if (!strcmp(name, "blue")) |
|
113 |
return COLOR_BLUE; |
|
114 |
if (!strcmp(name, "magenta")) |
|
115 |
return COLOR_MAGENTA; |
|
116 |
if (!strcmp(name, "cyan")) |
|
117 |
return COLOR_CYAN; |
|
118 |
if (!strcmp(name, "white")) |
|
119 |
return COLOR_WHITE; |
|
120 |
|
|
121 |
return -1; |
|
122 |
} |
|
123 |
|
|
124 |
void ParseColors(void) |
|
125 |
{ |
|
126 |
char *colors[11] = { |
|
127 |
"", "", |
|
128 |
"borderlines", |
|
129 |
"jidonlineselected", |
|
130 |
"jidonline", |
|
131 |
"jidofflineselected", |
|
132 |
"jidoffline", |
|
133 |
"text", |
|
134 |
NULL |
|
135 |
}; |
|
136 |
|
|
137 |
char *tmp = malloc(1024); |
|
138 |
char *color1; |
|
139 |
char *background = cfg_read("color_background"); |
|
140 |
char *backselected = cfg_read("color_backselected"); |
|
141 |
int i = 0; |
|
142 |
|
|
143 |
while (colors[i]) { |
|
144 |
sprintf(tmp, "color_%s", colors[i]); |
|
145 |
color1 = cfg_read(tmp); |
|
146 |
|
|
147 |
switch (i + 1) { |
|
148 |
case 1: |
|
149 |
init_pair(1, COLOR_BLACK, COLOR_WHITE); |
|
150 |
break; |
|
151 |
case 2: |
|
152 |
init_pair(2, COLOR_WHITE, COLOR_BLACK); |
|
153 |
break; |
|
154 |
case 3: |
|
155 |
init_pair(3, FindColor(color1), FindColor(background)); |
|
156 |
break; |
|
157 |
case 4: |
|
158 |
init_pair(4, FindColor(color1), FindColor(backselected)); |
|
159 |
break; |
|
160 |
case 5: |
|
161 |
init_pair(5, FindColor(color1), FindColor(background)); |
|
162 |
break; |
|
163 |
case 6: |
|
164 |
init_pair(6, FindColor(color1), FindColor(backselected)); |
|
165 |
break; |
|
166 |
case 7: |
|
167 |
init_pair(7, FindColor(color1), FindColor(background)); |
|
168 |
break; |
|
169 |
case 8: |
|
170 |
init_pair(8, FindColor(color1), FindColor(background)); |
|
171 |
break; |
|
172 |
} |
|
173 |
i++; |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
|
50
|
178 |
window_entry_t *scr_CreatePanel(const char *title, int x, int y, |
|
179 |
int lines, int cols, int dont_show) |
24
|
180 |
{ |
|
181 |
window_entry_t *tmp = calloc(1, sizeof(window_entry_t)); |
|
182 |
|
|
183 |
tmp->win = newwin(lines, cols, y, x); |
|
184 |
tmp->panel = new_panel(tmp->win); |
|
185 |
tmp->name = (char *) calloc(1, 1024); |
|
186 |
strncpy(tmp->name, title, 1024); |
76
|
187 |
scr_clear_box(tmp->win, 0, 0, lines, cols, COLOR_GENERAL); |
24
|
188 |
|
|
189 |
if ((!dont_show)) { |
|
190 |
currentWindow = tmp; |
|
191 |
} else { |
|
192 |
if (currentWindow) |
|
193 |
top_panel(currentWindow->panel); |
|
194 |
else |
|
195 |
top_panel(chatPanel); |
|
196 |
} |
|
197 |
|
|
198 |
list_add_tail(&tmp->list, &window_list); |
|
199 |
update_panels(); |
|
200 |
|
|
201 |
return tmp; |
|
202 |
} |
|
203 |
|
50
|
204 |
window_entry_t *scr_SearchWindow(const char *winId) |
24
|
205 |
{ |
|
206 |
struct list_head *pos, *n; |
|
207 |
window_entry_t *search_entry = NULL; |
|
208 |
|
|
209 |
list_for_each_safe(pos, n, &window_list) { |
|
210 |
search_entry = window_entry(pos); |
|
211 |
if (search_entry->name) { |
|
212 |
if (!strcasecmp(search_entry->name, winId)) { |
|
213 |
return search_entry; |
|
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |
return NULL; |
|
218 |
} |
|
219 |
|
74
|
220 |
void scr_UpdateWindow(window_entry_t *win_entry) |
|
221 |
{ |
|
222 |
int n; |
|
223 |
int width; |
|
224 |
char **lines; |
|
225 |
GList *hbuf_head; |
|
226 |
|
|
227 |
// We will show the last CHAT_WIN_HEIGHT lines. |
|
228 |
// Let's find out where it begins. |
|
229 |
win_entry->hbuf = g_list_last(win_entry->hbuf); |
|
230 |
hbuf_head = win_entry->hbuf; |
|
231 |
for (n=0; hbuf_head && n<(CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head); n++) |
|
232 |
hbuf_head = g_list_previous(hbuf_head); |
|
233 |
|
|
234 |
// Get the last CHAT_WIN_HEIGHT lines. |
|
235 |
lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT); |
|
236 |
|
|
237 |
// Display these lines |
|
238 |
width = scr_WindowWidth(win_entry->win); |
|
239 |
wmove(win_entry->win, 0, 0); |
|
240 |
for (n = 0; n < CHAT_WIN_HEIGHT; n++) { |
|
241 |
int r = width; |
75
|
242 |
if (*(lines+2*n)) { |
|
243 |
if (**(lines+2*n)) |
|
244 |
wprintw(win_entry->win, "%s", *(lines+2*n)); // prefix |
|
245 |
else { |
|
246 |
wprintw(win_entry->win, " "); |
|
247 |
r -= 12; |
|
248 |
} |
|
249 |
wprintw(win_entry->win, "%s", *(lines+2*n+1)); // line |
|
250 |
// Calculate the number of blank characters to empty the line |
|
251 |
r -= strlen(*(lines+2*n)) + strlen(*(lines+2*n+1)); |
|
252 |
} |
74
|
253 |
for ( ; r>0 ; r--) { |
|
254 |
wprintw(win_entry->win, " "); |
|
255 |
} |
|
256 |
//// wclrtoeol(win_entry->win); does not work :( |
|
257 |
} |
|
258 |
g_free(lines); |
|
259 |
} |
|
260 |
|
50
|
261 |
void scr_ShowWindow(const char *winId) |
24
|
262 |
{ |
74
|
263 |
window_entry_t *win_entry = scr_SearchWindow(winId); |
|
264 |
|
|
265 |
if (win_entry != NULL) { |
|
266 |
top_panel(win_entry->panel); |
|
267 |
currentWindow = win_entry; |
24
|
268 |
chatmode = TRUE; |
81
|
269 |
roster_setflags(winId, ROSTER_FLAG_MSG, FALSE); |
30
|
270 |
update_roster = TRUE; |
74
|
271 |
|
|
272 |
// Refresh the window entry |
|
273 |
scr_UpdateWindow(win_entry); |
|
274 |
|
|
275 |
// Finished :) |
24
|
276 |
update_panels(); |
|
277 |
doupdate(); |
|
278 |
} else { |
|
279 |
top_panel(chatPanel); |
74
|
280 |
currentWindow = win_entry; // == NULL (current window empty) |
24
|
281 |
} |
|
282 |
} |
|
283 |
|
|
284 |
void scr_ShowBuddyWindow(void) |
|
285 |
{ |
81
|
286 |
const gchar *jid = CURRENT_JID; |
|
287 |
if (jid != NULL) |
|
288 |
scr_ShowWindow(jid); |
24
|
289 |
top_panel(inputPanel); |
|
290 |
} |
|
291 |
|
|
292 |
|
75
|
293 |
void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp, |
|
294 |
const char *prefix, int force_show) |
24
|
295 |
{ |
75
|
296 |
char *fullprefix = NULL; |
74
|
297 |
window_entry_t *win_entry; |
24
|
298 |
int dont_show = FALSE; |
|
299 |
|
75
|
300 |
// Prepare the prefix |
|
301 |
if (prefix || TimeStamp) { |
|
302 |
if (!prefix) prefix = ""; |
|
303 |
fullprefix = calloc(1, strlen(prefix)+16); |
|
304 |
if (TimeStamp) { |
|
305 |
time_t now = time(NULL); |
|
306 |
strftime(fullprefix, 12, "[%H:%M] ", localtime(&now)); |
|
307 |
} else { |
|
308 |
strcpy(fullprefix, " "); |
|
309 |
} |
|
310 |
strcat(fullprefix, prefix); |
74
|
311 |
} |
|
312 |
|
|
313 |
// Look for the window entry. |
|
314 |
win_entry = scr_SearchWindow(winId); |
|
315 |
|
|
316 |
// Do we have to really show the window? |
24
|
317 |
if (!chatmode) |
|
318 |
dont_show = TRUE; |
74
|
319 |
else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry)))) |
24
|
320 |
dont_show = TRUE; |
|
321 |
|
74
|
322 |
// If the window entry doesn't exist yet, let's create it. |
|
323 |
if (win_entry == NULL) { |
|
324 |
win_entry = scr_CreatePanel(winId, ROSTER_WIDTH, 0, CHAT_WIN_HEIGHT, |
|
325 |
maxX - ROSTER_WIDTH, dont_show); |
24
|
326 |
} |
|
327 |
|
75
|
328 |
hbuf_add_line(&win_entry->hbuf, text, fullprefix, |
74
|
329 |
maxX - scr_WindowWidth(rosterWnd) - 14); |
75
|
330 |
free(fullprefix); |
74
|
331 |
|
24
|
332 |
if (!dont_show) { |
74
|
333 |
// Show and refresh the window |
|
334 |
top_panel(win_entry->panel); |
|
335 |
scr_UpdateWindow(win_entry); |
24
|
336 |
update_panels(); |
|
337 |
doupdate(); |
|
338 |
} else { |
81
|
339 |
roster_setflags(winId, ROSTER_FLAG_MSG, TRUE); |
30
|
340 |
update_roster = TRUE; |
24
|
341 |
} |
|
342 |
} |
|
343 |
|
|
344 |
void scr_InitCurses(void) |
|
345 |
{ |
|
346 |
initscr(); |
|
347 |
noecho(); |
|
348 |
raw(); |
35
|
349 |
halfdelay(5); |
24
|
350 |
start_color(); |
|
351 |
use_default_colors(); |
|
352 |
|
|
353 |
ParseColors(); |
|
354 |
|
|
355 |
getmaxyx(stdscr, maxY, maxX); |
|
356 |
inputLine[0] = 0; |
|
357 |
ptr_inputline = inputLine; |
|
358 |
|
35
|
359 |
setlocale(LC_CTYPE, ""); |
24
|
360 |
|
|
361 |
return; |
|
362 |
} |
|
363 |
|
81
|
364 |
void scr_TerminateCurses(void) |
|
365 |
{ |
|
366 |
clear(); |
|
367 |
refresh(); |
|
368 |
endwin(); |
|
369 |
return; |
|
370 |
} |
|
371 |
|
24
|
372 |
void scr_DrawMainWindow(void) |
|
373 |
{ |
74
|
374 |
int l; |
|
375 |
|
24
|
376 |
/* Draw main panels */ |
74
|
377 |
rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0); |
24
|
378 |
rosterPanel = new_panel(rosterWnd); |
74
|
379 |
scr_clear_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, ROSTER_WIDTH, |
|
380 |
COLOR_GENERAL); |
|
381 |
for (l=0 ; l < CHAT_WIN_HEIGHT ; l++) |
|
382 |
mvwaddch(rosterWnd, l, ROSTER_WIDTH-1, ACS_VLINE); |
24
|
383 |
|
74
|
384 |
chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH); |
24
|
385 |
chatPanel = new_panel(chatWnd); |
74
|
386 |
scr_clear_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, |
|
387 |
COLOR_GENERAL); |
|
388 |
scrollok(chatWnd, TRUE); |
|
389 |
mvwprintw(chatWnd, 0, 0, "This is the status window"); |
24
|
390 |
|
|
391 |
logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0); |
|
392 |
logPanel_border = new_panel(logWnd_border); |
|
393 |
scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0); |
|
394 |
logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1); |
|
395 |
logPanel = new_panel(logWnd); |
|
396 |
wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
397 |
|
74
|
398 |
scrollok(logWnd, TRUE); |
24
|
399 |
|
|
400 |
inputWnd = newwin(1, maxX, maxY-1, 0); |
|
401 |
inputPanel = new_panel(inputWnd); |
|
402 |
|
81
|
403 |
scr_DrawRoster(); |
24
|
404 |
update_panels(); |
|
405 |
doupdate(); |
|
406 |
return; |
|
407 |
} |
|
408 |
|
81
|
409 |
void scr_DrawRoster(void) |
24
|
410 |
{ |
81
|
411 |
static guint offset = 0; |
|
412 |
char name[ROSTER_WIDTH]; |
|
413 |
int maxx, maxy; |
|
414 |
GList *buddy; |
|
415 |
int i, n; |
|
416 |
int rOffset; |
|
417 |
|
|
418 |
getmaxyx(rosterWnd, maxy, maxx); |
|
419 |
maxx --; // last char is for vertical border |
|
420 |
name[ROSTER_WIDTH-7] = 0; |
|
421 |
|
|
422 |
// cleanup of roster window |
|
423 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
424 |
for (i = 0; i < maxy; i++) { |
|
425 |
mvwprintw(rosterWnd, i, 0, ""); |
|
426 |
for (n = 0; n < maxx; n++) |
|
427 |
waddch(rosterWnd, ' '); |
|
428 |
} |
|
429 |
|
|
430 |
// Leave now if buddylist is empty |
|
431 |
if (!buddylist) { |
|
432 |
offset = 0; |
|
433 |
return; |
|
434 |
} |
|
435 |
|
|
436 |
// TODO: update offset if necessary |
|
437 |
|
|
438 |
buddy = buddylist; |
|
439 |
rOffset = offset; |
|
440 |
|
|
441 |
for (i=0; i<maxy && buddy; i++, buddy = g_list_next(buddy)) { |
|
442 |
|
|
443 |
char status = '?'; |
|
444 |
char pending = ' '; |
|
445 |
enum imstatus budstate; |
|
446 |
|
|
447 |
if (rOffset > 0) { |
|
448 |
rOffset--; |
|
449 |
continue; |
|
450 |
} |
|
451 |
|
|
452 |
if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) { |
|
453 |
pending = '#'; |
|
454 |
} |
|
455 |
|
|
456 |
budstate = buddy_getstatus(BUDDATA(buddy)); |
|
457 |
if (budstate >= 0 && budstate < imstatus_size) |
|
458 |
status = imstatus2char[budstate]; |
|
459 |
if (buddy == current_buddy) { |
|
460 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL)); |
|
461 |
// The 3 following lines aim to color the whole line |
|
462 |
wmove(rosterWnd, i, 0); |
|
463 |
for (n = 0; n < maxx; n++) |
|
464 |
waddch(rosterWnd, ' '); |
|
465 |
} else { |
|
466 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES)); |
|
467 |
} |
|
468 |
|
|
469 |
strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7); |
|
470 |
// TODO: status is meaningless for groups: |
|
471 |
if (buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP) status='G'; |
|
472 |
mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name); |
|
473 |
} |
|
474 |
|
|
475 |
update_panels(); |
|
476 |
doupdate(); |
|
477 |
update_roster = FALSE; |
24
|
478 |
} |
|
479 |
|
50
|
480 |
void scr_WriteMessage(const char *jid, const char *text, char *prefix) |
24
|
481 |
{ |
75
|
482 |
scr_WriteInWindow(jid, text, TRUE, prefix, FALSE); |
47
|
483 |
} |
|
484 |
|
50
|
485 |
void scr_WriteIncomingMessage(const char *jidfrom, const char *text) |
47
|
486 |
{ |
|
487 |
char *buffer = utf8_decode(text); |
75
|
488 |
// FIXME expand tabs / filter out special chars... |
47
|
489 |
scr_WriteMessage(jidfrom, buffer, "<== "); |
|
490 |
free(buffer); |
65
|
491 |
top_panel(inputPanel); |
24
|
492 |
update_panels(); |
|
493 |
doupdate(); |
|
494 |
} |
|
495 |
|
50
|
496 |
void scr_WriteOutgoingMessage(const char *jidto, const char *text) |
47
|
497 |
{ |
65
|
498 |
scr_WriteMessage(jidto, text, "--> "); |
47
|
499 |
scr_ShowWindow(jidto); |
65
|
500 |
top_panel(inputPanel); |
47
|
501 |
} |
|
502 |
|
24
|
503 |
int scr_Getch(void) |
|
504 |
{ |
|
505 |
int ch; |
|
506 |
// keypad(inputWnd, TRUE); |
|
507 |
ch = wgetch(inputWnd); |
|
508 |
return ch; |
|
509 |
} |
|
510 |
|
|
511 |
WINDOW *scr_GetRosterWindow(void) |
|
512 |
{ |
|
513 |
return rosterWnd; |
|
514 |
} |
|
515 |
|
|
516 |
WINDOW *scr_GetStatusWindow(void) |
|
517 |
{ |
|
518 |
return chatWnd; |
|
519 |
} |
|
520 |
|
|
521 |
WINDOW *scr_GetInputWindow(void) |
|
522 |
{ |
|
523 |
return inputWnd; |
|
524 |
} |
|
525 |
|
81
|
526 |
void scr_RosterUp() |
|
527 |
{ |
|
528 |
if (current_buddy) { |
|
529 |
if (g_list_previous(current_buddy)) { |
|
530 |
current_buddy = g_list_previous(current_buddy); |
|
531 |
scr_DrawRoster(); |
|
532 |
} |
|
533 |
} |
|
534 |
// XXX We should rebuild the buddylist but perhaps not everytime? |
|
535 |
} |
|
536 |
|
|
537 |
void scr_RosterDown() |
|
538 |
{ |
|
539 |
if (current_buddy) { |
|
540 |
if (g_list_next(current_buddy)) { |
|
541 |
current_buddy = g_list_next(current_buddy); |
|
542 |
scr_DrawRoster(); |
|
543 |
} |
|
544 |
} |
|
545 |
// XXX We should rebuild the buddylist but perhaps not everytime? |
|
546 |
} |
|
547 |
|
44
|
548 |
// scr_LogPrint(...) |
|
549 |
// Display a message in the log window. |
24
|
550 |
void scr_LogPrint(const char *fmt, ...) |
|
551 |
{ |
|
552 |
time_t timestamp; |
|
553 |
char *buffer; |
|
554 |
va_list ap; |
|
555 |
|
|
556 |
buffer = (char *) calloc(1, 4096); |
|
557 |
|
|
558 |
timestamp = time(NULL); |
|
559 |
strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
560 |
wprintw(logWnd, "\n%s", buffer); |
|
561 |
|
|
562 |
va_start(ap, fmt); |
|
563 |
vsnprintf(buffer, 4096, fmt, ap); |
|
564 |
va_end(ap); |
|
565 |
|
|
566 |
wprintw(logWnd, "%s", buffer); |
|
567 |
free(buffer); |
|
568 |
|
|
569 |
update_panels(); |
|
570 |
doupdate(); |
|
571 |
} |
|
572 |
|
|
573 |
// check_offset(int direction) |
|
574 |
// Check inputline_offset value, and make sure the cursor is inside the |
|
575 |
// screen. |
|
576 |
inline void check_offset(int direction) |
|
577 |
{ |
|
578 |
// Left side |
|
579 |
if (inputline_offset && direction <= 0) { |
|
580 |
while (ptr_inputline <= (char*)&inputLine + inputline_offset) { |
|
581 |
if (inputline_offset) { |
|
582 |
inputline_offset -= 5; |
|
583 |
if (inputline_offset < 0) |
|
584 |
inputline_offset = 0; |
|
585 |
} |
|
586 |
} |
|
587 |
} |
|
588 |
// Right side |
|
589 |
if (direction >= 0) { |
|
590 |
while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) |
|
591 |
inputline_offset += 5; |
|
592 |
} |
|
593 |
} |
|
594 |
|
44
|
595 |
// process_key(key) |
|
596 |
// Handle the pressed key, in the command line (bottom). |
29
|
597 |
int process_key(int key) |
24
|
598 |
{ |
|
599 |
if (isprint(key)) { |
|
600 |
char tmpLine[INPUTLINE_LENGTH+1]; |
|
601 |
|
|
602 |
// Check the line isn't too long |
|
603 |
if (strlen(inputLine) >= INPUTLINE_LENGTH) |
|
604 |
return 0; |
|
605 |
|
|
606 |
// Insert char |
|
607 |
strcpy(tmpLine, ptr_inputline); |
|
608 |
*ptr_inputline++ = key; |
|
609 |
strcpy(ptr_inputline, tmpLine); |
|
610 |
check_offset(1); |
|
611 |
} else { |
|
612 |
switch(key) { |
|
613 |
case KEY_BACKSPACE: |
|
614 |
if (ptr_inputline != (char*)&inputLine) { |
42
|
615 |
char *c = --ptr_inputline; |
|
616 |
for ( ; *c ; c++) |
|
617 |
*c = *(c+1); |
24
|
618 |
check_offset(-1); |
|
619 |
} |
|
620 |
break; |
|
621 |
case KEY_DC: |
|
622 |
if (*ptr_inputline) |
|
623 |
strcpy(ptr_inputline, ptr_inputline+1); |
|
624 |
break; |
|
625 |
case KEY_LEFT: |
|
626 |
if (ptr_inputline != (char*)&inputLine) { |
|
627 |
ptr_inputline--; |
|
628 |
check_offset(-1); |
|
629 |
} |
|
630 |
break; |
|
631 |
case KEY_RIGHT: |
|
632 |
if (*ptr_inputline) |
|
633 |
ptr_inputline++; |
|
634 |
check_offset(1); |
|
635 |
break; |
|
636 |
case 9: // Tab |
|
637 |
scr_LogPrint("I'm unable to complete yet"); |
|
638 |
break; |
|
639 |
case '\n': // Enter |
|
640 |
chatmode = TRUE; |
|
641 |
if (inputLine[0] == 0) { |
|
642 |
scr_ShowBuddyWindow(); |
|
643 |
break; |
|
644 |
} |
29
|
645 |
if (process_line(inputLine)) |
24
|
646 |
return 255; |
|
647 |
ptr_inputline = inputLine; |
|
648 |
*ptr_inputline = 0; |
|
649 |
inputline_offset = 0; |
|
650 |
break; |
|
651 |
case KEY_UP: |
81
|
652 |
scr_RosterUp(); |
24
|
653 |
if (chatmode) |
|
654 |
scr_ShowBuddyWindow(); |
|
655 |
break; |
|
656 |
case KEY_DOWN: |
81
|
657 |
scr_RosterDown(); |
24
|
658 |
if (chatmode) |
|
659 |
scr_ShowBuddyWindow(); |
|
660 |
break; |
|
661 |
case KEY_PPAGE: |
|
662 |
scr_LogPrint("PageUp??"); |
|
663 |
break; |
|
664 |
case KEY_NPAGE: |
|
665 |
scr_LogPrint("PageDown??"); |
|
666 |
break; |
|
667 |
case KEY_HOME: |
|
668 |
case 1: |
|
669 |
ptr_inputline = inputLine; |
|
670 |
inputline_offset = 0; |
|
671 |
break; |
|
672 |
case KEY_END: |
|
673 |
case 5: |
|
674 |
for (; *ptr_inputline; ptr_inputline++) ; |
|
675 |
check_offset(1); |
|
676 |
break; |
|
677 |
case 21: // Ctrl-u |
|
678 |
strcpy(inputLine, ptr_inputline); |
|
679 |
ptr_inputline = inputLine; |
|
680 |
inputline_offset = 0; |
|
681 |
break; |
|
682 |
case KEY_EOL: |
|
683 |
case 11: // Ctrl-k |
|
684 |
*ptr_inputline = 0; |
|
685 |
break; |
|
686 |
case 16: // Ctrl-p |
|
687 |
scr_LogPrint("Ctrl-p not yet implemented"); |
|
688 |
break; |
|
689 |
case 14: // Ctrl-n |
|
690 |
scr_LogPrint("Ctrl-n not yet implemented"); |
|
691 |
break; |
|
692 |
case 27: // ESC |
|
693 |
currentWindow = NULL; |
|
694 |
chatmode = FALSE; |
|
695 |
top_panel(chatPanel); |
|
696 |
top_panel(inputPanel); |
|
697 |
break; |
|
698 |
default: |
|
699 |
scr_LogPrint("Unkown key=%d", key); |
|
700 |
} |
|
701 |
} |
|
702 |
mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
|
703 |
wclrtoeol(inputWnd); |
|
704 |
if (*ptr_inputline) { |
|
705 |
wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
|
706 |
} |
|
707 |
update_panels(); |
|
708 |
doupdate(); |
|
709 |
return 0; |
|
710 |
} |