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" |
|
16 |
#include "lang.h" |
81
|
17 |
#include "utf8.h" |
|
18 |
#include "utils.h" |
24
|
19 |
#include "list.h" |
|
20 |
|
|
21 |
#define window_entry(n) list_entry(n, window_entry_t, list) |
|
22 |
|
|
23 |
LIST_HEAD(window_list); |
|
24 |
|
|
25 |
typedef struct _window_entry_t { |
|
26 |
WINDOW *win; |
|
27 |
PANEL *panel; |
|
28 |
char *name; |
74
|
29 |
GList *hbuf; |
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 |
"jidonlineselected", |
|
131 |
"jidonline", |
|
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 |
|
|
228 |
// We will show the last CHAT_WIN_HEIGHT lines. |
|
229 |
// Let's find out where it begins. |
|
230 |
win_entry->hbuf = g_list_last(win_entry->hbuf); |
|
231 |
hbuf_head = win_entry->hbuf; |
|
232 |
for (n=0; hbuf_head && n<(CHAT_WIN_HEIGHT-1) && g_list_previous(hbuf_head); n++) |
|
233 |
hbuf_head = g_list_previous(hbuf_head); |
|
234 |
|
|
235 |
// Get the last CHAT_WIN_HEIGHT lines. |
|
236 |
lines = hbuf_get_lines(hbuf_head, CHAT_WIN_HEIGHT); |
|
237 |
|
|
238 |
// Display these lines |
|
239 |
width = scr_WindowWidth(win_entry->win); |
|
240 |
wmove(win_entry->win, 0, 0); |
|
241 |
for (n = 0; n < CHAT_WIN_HEIGHT; n++) { |
|
242 |
int r = width; |
75
|
243 |
if (*(lines+2*n)) { |
|
244 |
if (**(lines+2*n)) |
|
245 |
wprintw(win_entry->win, "%s", *(lines+2*n)); // prefix |
|
246 |
else { |
|
247 |
wprintw(win_entry->win, " "); |
|
248 |
r -= 12; |
|
249 |
} |
|
250 |
wprintw(win_entry->win, "%s", *(lines+2*n+1)); // line |
|
251 |
// Calculate the number of blank characters to empty the line |
|
252 |
r -= strlen(*(lines+2*n)) + strlen(*(lines+2*n+1)); |
|
253 |
} |
74
|
254 |
for ( ; r>0 ; r--) { |
|
255 |
wprintw(win_entry->win, " "); |
|
256 |
} |
|
257 |
//// wclrtoeol(win_entry->win); does not work :( |
|
258 |
} |
|
259 |
g_free(lines); |
|
260 |
} |
|
261 |
|
50
|
262 |
void scr_ShowWindow(const char *winId) |
24
|
263 |
{ |
74
|
264 |
window_entry_t *win_entry = scr_SearchWindow(winId); |
|
265 |
|
|
266 |
if (win_entry != NULL) { |
|
267 |
top_panel(win_entry->panel); |
|
268 |
currentWindow = win_entry; |
24
|
269 |
chatmode = TRUE; |
81
|
270 |
roster_setflags(winId, ROSTER_FLAG_MSG, FALSE); |
30
|
271 |
update_roster = TRUE; |
74
|
272 |
|
|
273 |
// Refresh the window entry |
|
274 |
scr_UpdateWindow(win_entry); |
|
275 |
|
|
276 |
// Finished :) |
24
|
277 |
update_panels(); |
|
278 |
doupdate(); |
|
279 |
} else { |
|
280 |
top_panel(chatPanel); |
74
|
281 |
currentWindow = win_entry; // == NULL (current window empty) |
24
|
282 |
} |
|
283 |
} |
|
284 |
|
|
285 |
void scr_ShowBuddyWindow(void) |
|
286 |
{ |
81
|
287 |
const gchar *jid = CURRENT_JID; |
|
288 |
if (jid != NULL) |
|
289 |
scr_ShowWindow(jid); |
24
|
290 |
top_panel(inputPanel); |
|
291 |
} |
|
292 |
|
|
293 |
|
75
|
294 |
void scr_WriteInWindow(const char *winId, const char *text, int TimeStamp, |
|
295 |
const char *prefix, int force_show) |
24
|
296 |
{ |
75
|
297 |
char *fullprefix = NULL; |
74
|
298 |
window_entry_t *win_entry; |
24
|
299 |
int dont_show = FALSE; |
|
300 |
|
75
|
301 |
// Prepare the prefix |
|
302 |
if (prefix || TimeStamp) { |
|
303 |
if (!prefix) prefix = ""; |
|
304 |
fullprefix = calloc(1, strlen(prefix)+16); |
|
305 |
if (TimeStamp) { |
|
306 |
time_t now = time(NULL); |
|
307 |
strftime(fullprefix, 12, "[%H:%M] ", localtime(&now)); |
|
308 |
} else { |
|
309 |
strcpy(fullprefix, " "); |
|
310 |
} |
|
311 |
strcat(fullprefix, prefix); |
74
|
312 |
} |
|
313 |
|
|
314 |
// Look for the window entry. |
|
315 |
win_entry = scr_SearchWindow(winId); |
|
316 |
|
|
317 |
// Do we have to really show the window? |
24
|
318 |
if (!chatmode) |
|
319 |
dont_show = TRUE; |
74
|
320 |
else if ((!force_show) && ((!currentWindow || (currentWindow != win_entry)))) |
24
|
321 |
dont_show = TRUE; |
|
322 |
|
74
|
323 |
// If the window entry doesn't exist yet, let's create it. |
|
324 |
if (win_entry == NULL) { |
|
325 |
win_entry = scr_CreatePanel(winId, ROSTER_WIDTH, 0, CHAT_WIN_HEIGHT, |
|
326 |
maxX - ROSTER_WIDTH, dont_show); |
24
|
327 |
} |
|
328 |
|
75
|
329 |
hbuf_add_line(&win_entry->hbuf, text, fullprefix, |
74
|
330 |
maxX - scr_WindowWidth(rosterWnd) - 14); |
75
|
331 |
free(fullprefix); |
74
|
332 |
|
24
|
333 |
if (!dont_show) { |
74
|
334 |
// Show and refresh the window |
|
335 |
top_panel(win_entry->panel); |
|
336 |
scr_UpdateWindow(win_entry); |
24
|
337 |
update_panels(); |
|
338 |
doupdate(); |
|
339 |
} else { |
81
|
340 |
roster_setflags(winId, ROSTER_FLAG_MSG, TRUE); |
30
|
341 |
update_roster = TRUE; |
24
|
342 |
} |
|
343 |
} |
|
344 |
|
|
345 |
void scr_InitCurses(void) |
|
346 |
{ |
|
347 |
initscr(); |
|
348 |
noecho(); |
|
349 |
raw(); |
35
|
350 |
halfdelay(5); |
24
|
351 |
start_color(); |
|
352 |
use_default_colors(); |
|
353 |
|
|
354 |
ParseColors(); |
|
355 |
|
|
356 |
getmaxyx(stdscr, maxY, maxX); |
|
357 |
inputLine[0] = 0; |
|
358 |
ptr_inputline = inputLine; |
|
359 |
|
35
|
360 |
setlocale(LC_CTYPE, ""); |
24
|
361 |
|
|
362 |
return; |
|
363 |
} |
|
364 |
|
81
|
365 |
void scr_TerminateCurses(void) |
|
366 |
{ |
|
367 |
clear(); |
|
368 |
refresh(); |
|
369 |
endwin(); |
|
370 |
return; |
|
371 |
} |
|
372 |
|
24
|
373 |
void scr_DrawMainWindow(void) |
|
374 |
{ |
74
|
375 |
int l; |
|
376 |
|
24
|
377 |
/* Draw main panels */ |
74
|
378 |
rosterWnd = newwin(CHAT_WIN_HEIGHT, ROSTER_WIDTH, 0, 0); |
24
|
379 |
rosterPanel = new_panel(rosterWnd); |
74
|
380 |
scr_clear_box(rosterWnd, 0, 0, CHAT_WIN_HEIGHT, ROSTER_WIDTH, |
|
381 |
COLOR_GENERAL); |
|
382 |
for (l=0 ; l < CHAT_WIN_HEIGHT ; l++) |
|
383 |
mvwaddch(rosterWnd, l, ROSTER_WIDTH-1, ACS_VLINE); |
24
|
384 |
|
74
|
385 |
chatWnd = newwin(CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, 0, ROSTER_WIDTH); |
24
|
386 |
chatPanel = new_panel(chatWnd); |
74
|
387 |
scr_clear_box(chatWnd, 0, 0, CHAT_WIN_HEIGHT, maxX - ROSTER_WIDTH, |
|
388 |
COLOR_GENERAL); |
|
389 |
scrollok(chatWnd, TRUE); |
|
390 |
mvwprintw(chatWnd, 0, 0, "This is the status window"); |
24
|
391 |
|
|
392 |
logWnd_border = newwin(LOG_WIN_HEIGHT, maxX, CHAT_WIN_HEIGHT, 0); |
|
393 |
logPanel_border = new_panel(logWnd_border); |
|
394 |
scr_draw_box(logWnd_border, 0, 0, LOG_WIN_HEIGHT, maxX, COLOR_GENERAL, 0, 0); |
|
395 |
logWnd = derwin(logWnd_border, LOG_WIN_HEIGHT-2, maxX-2, 1, 1); |
|
396 |
logPanel = new_panel(logWnd); |
|
397 |
wbkgd(logWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
398 |
|
74
|
399 |
scrollok(logWnd, TRUE); |
24
|
400 |
|
|
401 |
inputWnd = newwin(1, maxX, maxY-1, 0); |
|
402 |
inputPanel = new_panel(inputWnd); |
|
403 |
|
81
|
404 |
scr_DrawRoster(); |
24
|
405 |
update_panels(); |
|
406 |
doupdate(); |
|
407 |
return; |
|
408 |
} |
|
409 |
|
81
|
410 |
void scr_DrawRoster(void) |
24
|
411 |
{ |
81
|
412 |
static guint offset = 0; |
|
413 |
char name[ROSTER_WIDTH]; |
|
414 |
int maxx, maxy; |
|
415 |
GList *buddy; |
|
416 |
int i, n; |
|
417 |
int rOffset; |
|
418 |
|
|
419 |
getmaxyx(rosterWnd, maxy, maxx); |
|
420 |
maxx --; // last char is for vertical border |
|
421 |
name[ROSTER_WIDTH-7] = 0; |
|
422 |
|
|
423 |
// cleanup of roster window |
|
424 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_GENERAL)); |
|
425 |
for (i = 0; i < maxy; i++) { |
|
426 |
mvwprintw(rosterWnd, i, 0, ""); |
|
427 |
for (n = 0; n < maxx; n++) |
|
428 |
waddch(rosterWnd, ' '); |
|
429 |
} |
|
430 |
|
|
431 |
// Leave now if buddylist is empty |
|
432 |
if (!buddylist) { |
|
433 |
offset = 0; |
|
434 |
return; |
|
435 |
} |
|
436 |
|
84
|
437 |
// Update offset if necessary |
|
438 |
i = g_list_position(buddylist, current_buddy); |
|
439 |
if (i == -1) { // This is bad |
|
440 |
scr_LogPrint("Doh! Can't find current selected buddy!!"); |
|
441 |
return; |
|
442 |
} else if (i < offset) { |
|
443 |
offset = i; |
|
444 |
} else if (i+1 > offset + maxy) { |
|
445 |
offset = i + 1 - maxy; |
|
446 |
} |
81
|
447 |
|
|
448 |
buddy = buddylist; |
|
449 |
rOffset = offset; |
|
450 |
|
84
|
451 |
for (i=0; i<maxy && buddy; buddy = g_list_next(buddy)) { |
81
|
452 |
|
|
453 |
char status = '?'; |
|
454 |
char pending = ' '; |
|
455 |
enum imstatus budstate; |
|
456 |
|
|
457 |
if (rOffset > 0) { |
|
458 |
rOffset--; |
|
459 |
continue; |
|
460 |
} |
|
461 |
|
|
462 |
if (buddy_getflags(BUDDATA(buddy)) & ROSTER_FLAG_MSG) { |
|
463 |
pending = '#'; |
|
464 |
} |
|
465 |
|
|
466 |
budstate = buddy_getstatus(BUDDATA(buddy)); |
|
467 |
if (budstate >= 0 && budstate < imstatus_size) |
|
468 |
status = imstatus2char[budstate]; |
|
469 |
if (buddy == current_buddy) { |
|
470 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DESSEL)); |
|
471 |
// The 3 following lines aim to color the whole line |
|
472 |
wmove(rosterWnd, i, 0); |
|
473 |
for (n = 0; n < maxx; n++) |
|
474 |
waddch(rosterWnd, ' '); |
|
475 |
} else { |
|
476 |
wattrset(rosterWnd, COLOR_PAIR(COLOR_BD_DES)); |
|
477 |
} |
|
478 |
|
|
479 |
strncpy(name, buddy_getname(BUDDATA(buddy)), ROSTER_WIDTH-7); |
|
480 |
// TODO: status is meaningless for groups: |
|
481 |
if (buddy_gettype(BUDDATA(buddy)) & ROSTER_TYPE_GROUP) status='G'; |
|
482 |
mvwprintw(rosterWnd, i, 0, " %c[%c] %s", pending, status, name); |
84
|
483 |
|
|
484 |
i++; |
81
|
485 |
} |
|
486 |
|
|
487 |
update_panels(); |
|
488 |
doupdate(); |
|
489 |
update_roster = FALSE; |
24
|
490 |
} |
|
491 |
|
50
|
492 |
void scr_WriteMessage(const char *jid, const char *text, char *prefix) |
24
|
493 |
{ |
75
|
494 |
scr_WriteInWindow(jid, text, TRUE, prefix, FALSE); |
47
|
495 |
} |
|
496 |
|
50
|
497 |
void scr_WriteIncomingMessage(const char *jidfrom, const char *text) |
47
|
498 |
{ |
|
499 |
char *buffer = utf8_decode(text); |
75
|
500 |
// FIXME expand tabs / filter out special chars... |
47
|
501 |
scr_WriteMessage(jidfrom, buffer, "<== "); |
|
502 |
free(buffer); |
65
|
503 |
top_panel(inputPanel); |
24
|
504 |
update_panels(); |
|
505 |
doupdate(); |
|
506 |
} |
|
507 |
|
50
|
508 |
void scr_WriteOutgoingMessage(const char *jidto, const char *text) |
47
|
509 |
{ |
65
|
510 |
scr_WriteMessage(jidto, text, "--> "); |
47
|
511 |
scr_ShowWindow(jidto); |
65
|
512 |
top_panel(inputPanel); |
47
|
513 |
} |
|
514 |
|
24
|
515 |
int scr_Getch(void) |
|
516 |
{ |
|
517 |
int ch; |
|
518 |
// keypad(inputWnd, TRUE); |
|
519 |
ch = wgetch(inputWnd); |
|
520 |
return ch; |
|
521 |
} |
|
522 |
|
|
523 |
WINDOW *scr_GetRosterWindow(void) |
|
524 |
{ |
|
525 |
return rosterWnd; |
|
526 |
} |
|
527 |
|
|
528 |
WINDOW *scr_GetStatusWindow(void) |
|
529 |
{ |
|
530 |
return chatWnd; |
|
531 |
} |
|
532 |
|
|
533 |
WINDOW *scr_GetInputWindow(void) |
|
534 |
{ |
|
535 |
return inputWnd; |
|
536 |
} |
|
537 |
|
81
|
538 |
void scr_RosterUp() |
|
539 |
{ |
|
540 |
if (current_buddy) { |
|
541 |
if (g_list_previous(current_buddy)) { |
|
542 |
current_buddy = g_list_previous(current_buddy); |
|
543 |
scr_DrawRoster(); |
|
544 |
} |
|
545 |
} |
|
546 |
// XXX We should rebuild the buddylist but perhaps not everytime? |
|
547 |
} |
|
548 |
|
|
549 |
void scr_RosterDown() |
|
550 |
{ |
|
551 |
if (current_buddy) { |
|
552 |
if (g_list_next(current_buddy)) { |
|
553 |
current_buddy = g_list_next(current_buddy); |
|
554 |
scr_DrawRoster(); |
|
555 |
} |
|
556 |
} |
|
557 |
// XXX We should rebuild the buddylist but perhaps not everytime? |
|
558 |
} |
|
559 |
|
44
|
560 |
// scr_LogPrint(...) |
|
561 |
// Display a message in the log window. |
24
|
562 |
void scr_LogPrint(const char *fmt, ...) |
|
563 |
{ |
|
564 |
time_t timestamp; |
|
565 |
char *buffer; |
|
566 |
va_list ap; |
|
567 |
|
|
568 |
buffer = (char *) calloc(1, 4096); |
|
569 |
|
|
570 |
timestamp = time(NULL); |
|
571 |
strftime(buffer, 64, "[%H:%M:%S] ", localtime(×tamp)); |
|
572 |
wprintw(logWnd, "\n%s", buffer); |
|
573 |
|
|
574 |
va_start(ap, fmt); |
|
575 |
vsnprintf(buffer, 4096, fmt, ap); |
|
576 |
va_end(ap); |
|
577 |
|
|
578 |
wprintw(logWnd, "%s", buffer); |
|
579 |
free(buffer); |
|
580 |
|
|
581 |
update_panels(); |
|
582 |
doupdate(); |
|
583 |
} |
|
584 |
|
98
|
585 |
// which_row() |
|
586 |
// Tells which row our cursor is in, in the command line. |
|
587 |
// -1 -> normal text |
|
588 |
// 0 -> command |
|
589 |
// 1 -> parameter 1 (etc.) |
102
|
590 |
// If > 0, then *p_row is set to the beginning of the row |
|
591 |
int which_row(char **p_row) |
98
|
592 |
{ |
|
593 |
int row = -1; |
|
594 |
char *p; |
|
595 |
int quote = FALSE; |
|
596 |
|
|
597 |
// Not a command? |
|
598 |
if ((ptr_inputline == inputLine) || (inputLine[0] != '/')) |
|
599 |
return -1; |
|
600 |
|
|
601 |
// This is a command |
|
602 |
row = 0; |
|
603 |
for (p = inputLine ; p < ptr_inputline ; p++) { |
|
604 |
if (quote) { |
|
605 |
if (*p == '"' && *(p-1) != '\\') |
|
606 |
quote = FALSE; |
|
607 |
continue; |
|
608 |
} |
|
609 |
if (*p == '"' && *(p-1) != '\\') { |
|
610 |
quote = TRUE; |
102
|
611 |
} else if (*p == ' ' && *(p-1) != ' ') { |
98
|
612 |
row++; |
102
|
613 |
*p_row = p+1; |
|
614 |
} |
98
|
615 |
} |
|
616 |
return row; |
|
617 |
} |
|
618 |
|
|
619 |
void scr_insert_text(const char *text) |
|
620 |
{ |
|
621 |
char tmpLine[INPUTLINE_LENGTH+1]; |
|
622 |
int len = strlen(text); |
|
623 |
// Check the line isn't too long |
|
624 |
if (strlen(inputLine) + len >= INPUTLINE_LENGTH) { |
|
625 |
scr_LogPrint("Cannot insert text, line too long."); |
|
626 |
return; |
|
627 |
} |
|
628 |
|
|
629 |
strcpy(tmpLine, ptr_inputline); |
|
630 |
strcpy(ptr_inputline, text); ptr_inputline += len; |
|
631 |
strcpy(ptr_inputline, tmpLine); |
|
632 |
} |
|
633 |
|
|
634 |
void scr_handle_tab(void) |
|
635 |
{ |
102
|
636 |
int nrow; |
|
637 |
cmd *com; |
|
638 |
char *row; |
|
639 |
const char *cchar; |
98
|
640 |
|
102
|
641 |
nrow = which_row(&row); |
98
|
642 |
|
102
|
643 |
if (nrow == -1) return; // No completion if no leading slash |
|
644 |
|
|
645 |
if (nrow == 0) { // Command completion |
98
|
646 |
if (!completion_started) { |
|
647 |
GSList *list = compl_get_category_list(COMPL_CMD); |
|
648 |
if (list) { |
99
|
649 |
char *prefix = g_strndup(&inputLine[1], ptr_inputline-inputLine-1); |
100
|
650 |
// Init completion |
98
|
651 |
new_completion(prefix, list); |
100
|
652 |
g_free(prefix); |
|
653 |
// Now complete |
98
|
654 |
cchar = complete(); |
|
655 |
if (cchar) |
|
656 |
scr_insert_text(cchar); |
|
657 |
completion_started = TRUE; |
|
658 |
} |
102
|
659 |
} else { // Completion already initialized |
98
|
660 |
char *c; |
|
661 |
guint back = cancel_completion(); |
|
662 |
// Remove $back chars |
|
663 |
ptr_inputline -= back; |
|
664 |
c = ptr_inputline; |
|
665 |
for ( ; *c ; c++) |
|
666 |
*c = *(c+back); |
|
667 |
// Now complete again |
|
668 |
cchar = complete(); |
|
669 |
if (cchar) |
|
670 |
scr_insert_text(cchar); |
|
671 |
} |
|
672 |
return; |
|
673 |
} |
100
|
674 |
|
|
675 |
// Other completion, depending on the command |
102
|
676 |
com = cmd_get(inputLine); |
|
677 |
if (!com) { |
|
678 |
scr_LogPrint("I cannot complete for this command"); |
|
679 |
return; |
|
680 |
} |
|
681 |
// Now we have the command and the column. |
|
682 |
scr_LogPrint("CMD_FLAGR1=%d COL=%d row=[%s]", com->completion_flags[0], nrow, row); |
|
683 |
|
|
684 |
// We can't have more than 2 parameters (we use 2 flags) |
|
685 |
if (nrow > 2) |
|
686 |
return; |
|
687 |
|
|
688 |
// Dirty copy & paste |
|
689 |
if (!completion_started) { |
|
690 |
GSList *list = compl_get_category_list(com->completion_flags[nrow-1]); |
|
691 |
if (list) { |
|
692 |
char *prefix = g_strndup(row, ptr_inputline-row); |
|
693 |
// Init completion |
|
694 |
new_completion(prefix, list); |
|
695 |
g_free(prefix); |
|
696 |
// Now complete |
|
697 |
cchar = complete(); |
|
698 |
if (cchar) |
|
699 |
scr_insert_text(cchar); |
|
700 |
completion_started = TRUE; |
|
701 |
} |
|
702 |
} else { // Completion already initialized |
|
703 |
char *c; |
|
704 |
guint back = cancel_completion(); |
|
705 |
// Remove $back chars |
|
706 |
ptr_inputline -= back; |
|
707 |
c = ptr_inputline; |
|
708 |
for ( ; *c ; c++) |
|
709 |
*c = *(c+back); |
|
710 |
// Now complete again |
|
711 |
cchar = complete(); |
|
712 |
if (cchar) |
|
713 |
scr_insert_text(cchar); |
|
714 |
} |
98
|
715 |
} |
|
716 |
|
|
717 |
void scr_cancel_current_completion(void) |
|
718 |
{ |
|
719 |
char *c; |
|
720 |
guint back = cancel_completion(); |
|
721 |
// Remove $back chars |
|
722 |
ptr_inputline -= back; |
|
723 |
c = ptr_inputline; |
|
724 |
for ( ; *c ; c++) |
|
725 |
*c = *(c+back); |
|
726 |
} |
|
727 |
|
|
728 |
void scr_end_current_completion(void) |
|
729 |
{ |
|
730 |
done_completion(); |
|
731 |
completion_started = FALSE; |
|
732 |
} |
|
733 |
|
24
|
734 |
// check_offset(int direction) |
|
735 |
// Check inputline_offset value, and make sure the cursor is inside the |
|
736 |
// screen. |
|
737 |
inline void check_offset(int direction) |
|
738 |
{ |
|
739 |
// Left side |
|
740 |
if (inputline_offset && direction <= 0) { |
|
741 |
while (ptr_inputline <= (char*)&inputLine + inputline_offset) { |
|
742 |
if (inputline_offset) { |
|
743 |
inputline_offset -= 5; |
|
744 |
if (inputline_offset < 0) |
|
745 |
inputline_offset = 0; |
|
746 |
} |
|
747 |
} |
|
748 |
} |
|
749 |
// Right side |
|
750 |
if (direction >= 0) { |
|
751 |
while (ptr_inputline >= inputline_offset + (char*)&inputLine + maxX) |
|
752 |
inputline_offset += 5; |
|
753 |
} |
|
754 |
} |
|
755 |
|
44
|
756 |
// process_key(key) |
|
757 |
// Handle the pressed key, in the command line (bottom). |
29
|
758 |
int process_key(int key) |
24
|
759 |
{ |
|
760 |
if (isprint(key)) { |
|
761 |
char tmpLine[INPUTLINE_LENGTH+1]; |
|
762 |
|
|
763 |
// Check the line isn't too long |
|
764 |
if (strlen(inputLine) >= INPUTLINE_LENGTH) |
|
765 |
return 0; |
|
766 |
|
|
767 |
// Insert char |
|
768 |
strcpy(tmpLine, ptr_inputline); |
|
769 |
*ptr_inputline++ = key; |
|
770 |
strcpy(ptr_inputline, tmpLine); |
|
771 |
check_offset(1); |
|
772 |
} else { |
|
773 |
switch(key) { |
|
774 |
case KEY_BACKSPACE: |
|
775 |
if (ptr_inputline != (char*)&inputLine) { |
42
|
776 |
char *c = --ptr_inputline; |
|
777 |
for ( ; *c ; c++) |
|
778 |
*c = *(c+1); |
24
|
779 |
check_offset(-1); |
|
780 |
} |
|
781 |
break; |
|
782 |
case KEY_DC: |
|
783 |
if (*ptr_inputline) |
|
784 |
strcpy(ptr_inputline, ptr_inputline+1); |
|
785 |
break; |
|
786 |
case KEY_LEFT: |
|
787 |
if (ptr_inputline != (char*)&inputLine) { |
|
788 |
ptr_inputline--; |
|
789 |
check_offset(-1); |
|
790 |
} |
|
791 |
break; |
|
792 |
case KEY_RIGHT: |
|
793 |
if (*ptr_inputline) |
|
794 |
ptr_inputline++; |
|
795 |
check_offset(1); |
|
796 |
break; |
98
|
797 |
case 7: // Ctrl-g |
|
798 |
scr_cancel_current_completion(); |
|
799 |
scr_end_current_completion(); |
|
800 |
check_offset(-1); |
|
801 |
break; |
24
|
802 |
case 9: // Tab |
98
|
803 |
scr_handle_tab(); |
|
804 |
check_offset(0); |
24
|
805 |
break; |
|
806 |
case '\n': // Enter |
|
807 |
chatmode = TRUE; |
|
808 |
if (inputLine[0] == 0) { |
|
809 |
scr_ShowBuddyWindow(); |
|
810 |
break; |
|
811 |
} |
29
|
812 |
if (process_line(inputLine)) |
24
|
813 |
return 255; |
|
814 |
ptr_inputline = inputLine; |
|
815 |
*ptr_inputline = 0; |
|
816 |
inputline_offset = 0; |
|
817 |
break; |
|
818 |
case KEY_UP: |
81
|
819 |
scr_RosterUp(); |
24
|
820 |
if (chatmode) |
|
821 |
scr_ShowBuddyWindow(); |
|
822 |
break; |
|
823 |
case KEY_DOWN: |
81
|
824 |
scr_RosterDown(); |
24
|
825 |
if (chatmode) |
|
826 |
scr_ShowBuddyWindow(); |
|
827 |
break; |
|
828 |
case KEY_PPAGE: |
|
829 |
scr_LogPrint("PageUp??"); |
|
830 |
break; |
|
831 |
case KEY_NPAGE: |
|
832 |
scr_LogPrint("PageDown??"); |
|
833 |
break; |
|
834 |
case KEY_HOME: |
|
835 |
case 1: |
|
836 |
ptr_inputline = inputLine; |
|
837 |
inputline_offset = 0; |
|
838 |
break; |
|
839 |
case KEY_END: |
|
840 |
case 5: |
|
841 |
for (; *ptr_inputline; ptr_inputline++) ; |
|
842 |
check_offset(1); |
|
843 |
break; |
|
844 |
case 21: // Ctrl-u |
|
845 |
strcpy(inputLine, ptr_inputline); |
|
846 |
ptr_inputline = inputLine; |
|
847 |
inputline_offset = 0; |
|
848 |
break; |
|
849 |
case KEY_EOL: |
|
850 |
case 11: // Ctrl-k |
|
851 |
*ptr_inputline = 0; |
|
852 |
break; |
|
853 |
case 16: // Ctrl-p |
|
854 |
scr_LogPrint("Ctrl-p not yet implemented"); |
|
855 |
break; |
|
856 |
case 14: // Ctrl-n |
|
857 |
scr_LogPrint("Ctrl-n not yet implemented"); |
|
858 |
break; |
|
859 |
case 27: // ESC |
|
860 |
currentWindow = NULL; |
|
861 |
chatmode = FALSE; |
|
862 |
top_panel(chatPanel); |
|
863 |
top_panel(inputPanel); |
|
864 |
break; |
|
865 |
default: |
|
866 |
scr_LogPrint("Unkown key=%d", key); |
|
867 |
} |
|
868 |
} |
98
|
869 |
if (completion_started && key != 9) |
|
870 |
scr_end_current_completion(); |
24
|
871 |
mvwprintw(inputWnd, 0,0, "%s", inputLine + inputline_offset); |
|
872 |
wclrtoeol(inputWnd); |
|
873 |
if (*ptr_inputline) { |
|
874 |
wmove(inputWnd, 0, ptr_inputline - (char*)&inputLine - inputline_offset); |
|
875 |
} |
|
876 |
update_panels(); |
|
877 |
doupdate(); |
|
878 |
return 0; |
|
879 |
} |