1 |
/* ncursesw.c - The ncursesw console driver. |
/* ncursesw.c - The ncursesw console driver. |
2 |
Copyright (C) 2002 Free Software Foundation, Inc. |
Copyright (C) 2002, 2003 Free Software Foundation, Inc. |
3 |
Written by Marcus Brinkmann. |
Written by Marcus Brinkmann. |
4 |
|
|
5 |
This program is free software; you can redistribute it and/or |
This program is free software; you can redistribute it and/or |
34 |
|
|
35 |
/* ncurses is not thread-safe. This lock protects all calls into the |
/* ncurses is not thread-safe. This lock protects all calls into the |
36 |
ncurses library. */ |
ncurses library. */ |
37 |
struct mutex ncurses_lock; |
static struct mutex ncurses_lock; |
38 |
|
|
39 |
|
/* The current width and height the ncursesw driver is using. */ |
40 |
|
static int current_width; |
41 |
|
static int current_height; |
42 |
|
|
43 |
|
/* The window on which the console is shown. */ |
44 |
|
static WINDOW *conspad; |
45 |
|
|
46 |
|
/* The upper left corner shown in the pad. */ |
47 |
|
static int padx; |
48 |
|
static int pady; |
49 |
|
|
50 |
|
/* Autoscroll is on or off. Autoscroll makes scrolling dependant on |
51 |
|
the cursor position. */ |
52 |
|
static int autoscroll; |
53 |
|
|
54 |
/* Forward declaration. */ |
/* Forward declaration. */ |
55 |
static struct display_ops ncursesw_display_ops; |
static struct display_ops ncursesw_display_ops; |
273 |
return 1; |
return 1; |
274 |
} |
} |
275 |
|
|
276 |
|
static error_t |
277 |
|
refresh_screen (void) |
278 |
|
{ |
279 |
|
/* It is possible */ |
280 |
|
if (!current_width && !current_height) |
281 |
|
return 0; |
282 |
|
return prefresh (conspad, pady, padx, 0, 0, |
283 |
|
(current_height <= LINES ? current_height : LINES) - 1, |
284 |
|
(current_width <= COLS ? current_width : COLS) - 1); |
285 |
|
} |
286 |
|
|
287 |
static any_t |
static any_t |
288 |
input_loop (any_t unused) |
input_loop (any_t unused) |
289 |
{ |
{ |
308 |
size_t size = 0; |
size_t size = 0; |
309 |
|
|
310 |
mutex_lock (&ncurses_lock); |
mutex_lock (&ncurses_lock); |
311 |
while ((ret = getch ()) != ERR) |
while ((ret = wgetch (conspad)) != ERR) |
312 |
{ |
{ |
313 |
int i; |
int i; |
314 |
int found; |
int found; |
318 |
switch (ret) |
switch (ret) |
319 |
{ |
{ |
320 |
case 'x': |
case 'x': |
321 |
endwin (); |
mutex_unlock (&ncurses_lock); |
322 |
exit (0); |
console_exit (); |
323 |
break; |
break; |
324 |
case 23: /* ^W */ |
case 23: /* ^W */ |
325 |
assert (size < 100); |
assert (size < 100); |
339 |
console_switch (1 + (ret - '1'), 0); |
console_switch (1 + (ret - '1'), 0); |
340 |
mutex_lock (&ncurses_lock); |
mutex_lock (&ncurses_lock); |
341 |
break; |
break; |
342 |
|
case 'j': |
343 |
|
/* Scroll pad to left. */ |
344 |
|
if (padx > 0) |
345 |
|
{ |
346 |
|
padx--; |
347 |
|
refresh_screen (); |
348 |
|
} |
349 |
|
break; |
350 |
|
case 'k': |
351 |
|
/* Scroll pad down. */ |
352 |
|
if (pady < current_height - LINES) |
353 |
|
{ |
354 |
|
pady++; |
355 |
|
refresh_screen (); |
356 |
|
} |
357 |
|
break; |
358 |
|
case 'l': |
359 |
|
/* Scroll pad to right. */ |
360 |
|
if (padx < current_width - COLS) |
361 |
|
{ |
362 |
|
padx++; |
363 |
|
ncurses_refresh (); |
364 |
|
} |
365 |
|
break; |
366 |
|
case 'i': |
367 |
|
/* Scroll pad up. */ |
368 |
|
if (pady > 0) |
369 |
|
{ |
370 |
|
pady--; |
371 |
|
refresh_screen (); |
372 |
|
} |
373 |
|
break; |
374 |
|
case 'a': |
375 |
|
/* Switch autoscroll on/off. */ |
376 |
|
autoscroll = !autoscroll; |
377 |
|
break; |
378 |
default: |
default: |
379 |
break; |
break; |
380 |
} |
} |
388 |
break; |
break; |
389 |
default: |
default: |
390 |
found = 0; |
found = 0; |
391 |
for (i =0; i < sizeof(keycodes) / sizeof(keycodes[0]); i++) |
for (i = 0; i < sizeof (keycodes) / sizeof (keycodes[0]); |
392 |
|
i++) |
393 |
{ |
{ |
394 |
if (keycodes[i].curses == ret) |
if (keycodes[i].curses == ret) |
395 |
{ |
{ |
396 |
if (keycodes[i].cons) |
if (keycodes[i].cons) |
397 |
{ |
{ |
398 |
assert (size < 101 - strlen(keycodes[i].cons)); |
assert (size |
399 |
|
< 101 - strlen (keycodes[i].cons)); |
400 |
strcpy (&buf[size], keycodes[i].cons); |
strcpy (&buf[size], keycodes[i].cons); |
401 |
size += strlen (keycodes[i].cons); |
size += strlen (keycodes[i].cons); |
402 |
} |
} |
446 |
attr_t attr = conchar_attr_to_attr (str->attr); |
attr_t attr = conchar_attr_to_attr (str->attr); |
447 |
short color_pair = conchar_attr_to_color_pair (str->attr); |
short color_pair = conchar_attr_to_color_pair (str->attr); |
448 |
|
|
449 |
move (y, x); |
wmove (conspad, y, x); |
450 |
while (len) |
while (len) |
451 |
{ |
{ |
452 |
int ret; |
int ret; |
460 |
} |
} |
461 |
|
|
462 |
if (ucs4_to_altchar (str->chr, &ac)) |
if (ucs4_to_altchar (str->chr, &ac)) |
463 |
addch (ac | attr | color_pair); |
waddch (conspad, ac | attr | color_pair); |
464 |
else |
else |
465 |
{ |
{ |
466 |
wch[0] = str->chr; |
wch[0] = str->chr; |
473 |
assert (!"Do something if setcchar fails."); |
assert (!"Do something if setcchar fails."); |
474 |
} |
} |
475 |
#endif |
#endif |
476 |
ret = add_wch (&chr); |
ret = wadd_wch (conspad, &chr); |
477 |
#if 0 |
#if 0 |
478 |
if (ret == ERR) |
if (ret == ERR) |
479 |
{ |
{ |
493 |
ncursesw_update (void *handle) |
ncursesw_update (void *handle) |
494 |
{ |
{ |
495 |
mutex_lock (&ncurses_lock); |
mutex_lock (&ncurses_lock); |
496 |
refresh (); |
refresh_screen (); |
497 |
mutex_unlock (&ncurses_lock); |
mutex_unlock (&ncurses_lock); |
498 |
return 0; |
return 0; |
499 |
} |
} |
503 |
ncursesw_set_cursor_pos (void *handle, uint32_t col, uint32_t row) |
ncursesw_set_cursor_pos (void *handle, uint32_t col, uint32_t row) |
504 |
{ |
{ |
505 |
mutex_lock (&ncurses_lock); |
mutex_lock (&ncurses_lock); |
506 |
move (row, col); |
assert (current_width && current_height); |
507 |
|
if (autoscroll) |
508 |
|
{ |
509 |
|
/* Autoscroll to the right. */ |
510 |
|
if (col > COLS + padx) |
511 |
|
{ |
512 |
|
padx += COLS / 2; |
513 |
|
if (padx > COLS + current_width) |
514 |
|
padx = current_width - COLS; |
515 |
|
ncurses_refresh (); |
516 |
|
} |
517 |
|
/* Autoscroll to the left. */ |
518 |
|
else if (col < padx) |
519 |
|
{ |
520 |
|
padx -= COLS / 2; |
521 |
|
if (padx < 0) |
522 |
|
padx = 0; |
523 |
|
ncurses_refresh (); |
524 |
|
} |
525 |
|
/* Autoscroll down. */ |
526 |
|
if (row > LINES + pady) |
527 |
|
{ |
528 |
|
pady += LINES / 2; |
529 |
|
if (pady > LINES + current_height) |
530 |
|
pady = current_height - LINES; |
531 |
|
ncurses_refresh (); |
532 |
|
} |
533 |
|
/* Autoscroll up. */ |
534 |
|
else if (row < pady) |
535 |
|
{ |
536 |
|
pady -= LINES / 2; |
537 |
|
if (pady < 0) |
538 |
|
pady = 0; |
539 |
|
ncurses_refresh (); |
540 |
|
} |
541 |
|
} |
542 |
|
|
543 |
|
wmove (conspad, row, col); |
544 |
|
|
545 |
mutex_unlock (&ncurses_lock); |
mutex_unlock (&ncurses_lock); |
546 |
return 0; |
return 0; |
547 |
} |
} |
551 |
ncursesw_set_cursor_status (void *handle, uint32_t status) |
ncursesw_set_cursor_status (void *handle, uint32_t status) |
552 |
{ |
{ |
553 |
mutex_lock (&ncurses_lock); |
mutex_lock (&ncurses_lock); |
554 |
curs_set (status ? (status == 1 ? 1 : 2) : 0); |
|
555 |
|
/* If the cursor is invisible and switching to one visible state is |
556 |
|
impossible, switch to the other visible state or else the cursor |
557 |
|
will not be shown at all. */ |
558 |
|
if (curs_set (status) == -1 && status) |
559 |
|
curs_set (status == 1 ? 2 : 1); |
560 |
|
|
561 |
mutex_unlock (&ncurses_lock); |
mutex_unlock (&ncurses_lock); |
562 |
return 0; |
return 0; |
563 |
} |
} |
570 |
assert (delta >= 0); |
assert (delta >= 0); |
571 |
|
|
572 |
mutex_lock (&ncurses_lock); |
mutex_lock (&ncurses_lock); |
573 |
idlok (stdscr, TRUE); |
idlok (conspad, TRUE); |
574 |
scrollok (stdscr, TRUE); |
scrollok (conspad, TRUE); |
575 |
scrl (delta); |
wscrl (conspad, delta); |
576 |
idlok (stdscr, FALSE); |
idlok (conspad, FALSE); |
577 |
scrollok (stdscr, FALSE); |
scrollok (conspad, FALSE); |
578 |
mutex_unlock (&ncurses_lock); |
mutex_unlock (&ncurses_lock); |
579 |
return 0; |
return 0; |
580 |
} |
} |
588 |
int y; |
int y; |
589 |
|
|
590 |
mutex_lock (&ncurses_lock); |
mutex_lock (&ncurses_lock); |
591 |
getyx (stdscr, y, x); |
getyx (conspad, y, x); |
592 |
mvwputsn (str, length, col, row); |
mvwputsn (str, length, col, row); |
593 |
wmove (stdscr, y, x); |
wmove (conspad, y, x); |
594 |
mutex_unlock (&ncurses_lock); |
mutex_unlock (&ncurses_lock); |
595 |
return 0; |
return 0; |
596 |
} |
} |
639 |
raw (); |
raw (); |
640 |
noecho (); |
noecho (); |
641 |
nonl (); |
nonl (); |
642 |
intrflush (stdscr, FALSE); |
|
643 |
nodelay (stdscr, TRUE); |
/* Create a new pad with the size minimal size. This pad will be |
644 |
timeout (1); |
resized by ncursesw_set_dimension. */ |
645 |
keypad (stdscr, TRUE); |
conspad = newpad (1, 1); |
646 |
|
if (!conspad) |
647 |
|
return errno; |
648 |
|
|
649 |
|
intrflush (conspad, FALSE); |
650 |
|
nodelay (conspad, TRUE); |
651 |
|
wtimeout (conspad, 1); |
652 |
|
keypad (conspad, TRUE); |
653 |
|
|
654 |
err = driver_add_display (&ncursesw_display_ops, NULL); |
err = driver_add_display (&ncursesw_display_ops, NULL); |
655 |
if (err) |
if (err) |
674 |
} |
} |
675 |
|
|
676 |
cthread_detach (cthread_fork (input_loop, NULL)); |
cthread_detach (cthread_fork (input_loop, NULL)); |
|
endwin (); |
|
677 |
|
|
678 |
return err; |
return 0; |
679 |
} |
} |
680 |
|
|
681 |
/* Destroy the display HANDLE. */ |
/* Destroy the display HANDLE. */ |
693 |
return 0; |
return 0; |
694 |
} |
} |
695 |
|
|
696 |
|
static error_t |
697 |
|
ncursesw_set_dimension (void *handle, int width, int height) |
698 |
|
{ |
699 |
|
mutex_lock (&ncurses_lock); |
700 |
|
if (width != current_width || height != current_height) |
701 |
|
{ |
702 |
|
wresize (conspad, height, width); |
703 |
|
padx = 0; |
704 |
|
pady = 0; |
705 |
|
} |
706 |
|
current_width = width; |
707 |
|
current_height = height; |
708 |
|
mutex_unlock(&ncurses_lock); |
709 |
|
return 0; |
710 |
|
} |
711 |
|
|
712 |
|
|
713 |
struct driver_ops driver_ncursesw_ops = |
struct driver_ops driver_ncursesw_ops = |
714 |
{ |
{ |
726 |
ncursesw_write, |
ncursesw_write, |
727 |
ncursesw_update, |
ncursesw_update, |
728 |
ncursesw_flash, |
ncursesw_flash, |
729 |
NULL |
NULL, |
730 |
|
ncursesw_set_dimension |
731 |
}; |
}; |
732 |
|
|
733 |
static struct input_ops ncursesw_input_ops = |
static struct input_ops ncursesw_input_ops = |