36#include <sys/consio.h>
37#include <sys/kernel.h>
39#include <sys/malloc.h>
42#include <sys/terminal.h>
45#include <machine/stdarg.h>
58#define TERMINAL_LOCK(tm) do { \
59 if ((tm)->tm_flags & TF_CONS) \
60 mtx_lock_spin(&(tm)->tm_mtx); \
61 else if ((tm)->tm_tty != NULL) \
62 tty_lock((tm)->tm_tty); \
64#define TERMINAL_UNLOCK(tm) do { \
65 if ((tm)->tm_flags & TF_CONS) \
66 mtx_unlock_spin(&(tm)->tm_mtx); \
67 else if ((tm)->tm_tty != NULL) \
68 tty_unlock((tm)->tm_tty); \
70#define TERMINAL_LOCK_TTY(tm) do { \
71 if ((tm)->tm_flags & TF_CONS) \
72 mtx_lock_spin(&(tm)->tm_mtx); \
74#define TERMINAL_UNLOCK_TTY(tm) do { \
75 if ((tm)->tm_flags & TF_CONS) \
76 mtx_unlock_spin(&(tm)->tm_mtx); \
78#define TERMINAL_LOCK_CONS(tm) mtx_lock_spin(&(tm)->tm_mtx)
79#define TERMINAL_UNLOCK_CONS(tm) mtx_unlock_spin(&(tm)->tm_mtx)
127 .ta_fgcolor = TCHAR_FGCOLOR(TERMINAL_KERN_ATTR),
128 .ta_bgcolor = TCHAR_BGCOLOR(TERMINAL_KERN_ATTR),
129 .ta_format = TCHAR_FORMAT(TERMINAL_KERN_ATTR)
133 .ta_fgcolor = TCHAR_FGCOLOR(TERMINAL_NORM_ATTR),
134 .ta_bgcolor = TCHAR_BGCOLOR(TERMINAL_NORM_ATTR),
135 .ta_format = TCHAR_FORMAT(TERMINAL_NORM_ATTR)
139#define TCOLOR_FG_FUDGED(color) __extension__ ({ \
143 TCOLOR_FG(_c & 7) | ((_c & 8) << 18); \
147#define TCOLOR_BG_FUDGED(color) __extension__ ({ \
151 TCOLOR_BG(_c & 7) | ((_c & 8) << 20); \
154#define TCOLOR_256TO16(color) __extension__ ({ \
159 _c = teken_256to16(_c); \
163#define TCHAR_CREATE(c, a) ((c) | TFORMAT((a)->ta_format) | \
164 TCOLOR_FG_FUDGED(TCOLOR_256TO16((a)->ta_fgcolor)) | \
165 TCOLOR_BG_FUDGED(TCOLOR_256TO16((a)->ta_bgcolor)))
172 if (tm->tm_flags & TF_CONS)
173 mtx_init(&tm->tm_mtx,
"trmlck", NULL, MTX_SPIN);
178 TUNABLE_INT_FETCH(
"teken.fg_color", &fg);
179 TUNABLE_INT_FETCH(
"teken.bg_color", &bg);
206 tm =
malloc(
sizeof(
struct terminal), M_TERMINAL, M_WAITOK|M_ZERO);
210 tm->tm_softc = softc;
241 tty_makedev(tp, NULL,
"%s",
name);
250 teken_set_cursor(&tm->tm_emulator, pos);
255 int blank,
const term_attr_t *attr)
259 tm->tm_winsize = *size;
261 r.tr_begin.tp_row = r.tr_begin.tp_col = 0;
262 r.tr_end.tp_row = size->ws_row;
263 r.tr_end.tp_col = size->ws_col;
267 teken_set_winsize_noreset(&tm->tm_emulator, &r.tr_end);
269 teken_set_winsize(&tm->tm_emulator, &r.tr_end);
272 if ((blank != 0) && !(tm->tm_flags & TF_MUTE))
273 tm->tm_class->tc_fill(tm, &r,
299 tm->tm_flags |= TF_MUTE;
301 tm->tm_flags &= ~TF_MUTE;
319 if (TCHAR_FORMAT(c) & TF_CJK_RIGHT)
321 c = TCHAR_CHARACTER(c);
329 }
else if (c < 0x800) {
336 }
else if (c < 0x10000) {
339 0x80 | ((c >> 6) & 0x3f),
347 0x80 | ((c >> 12) & 0x3f),
348 0x80 | ((c >> 6) & 0x3f),
383 str = teken_get_sequence(&tm->tm_emulator, k);
400 struct terminal *tm = tty_softc(tp);
402 tm->tm_class->tc_opened(tm, 1);
409 struct terminal *tm = tty_softc(tp);
411 tm->tm_class->tc_opened(tm, 0);
417 struct terminal *tm = tty_softc(tp);
420 unsigned int flags = 0;
422 while ((olen =
ttydisc_getc(tp, obuf,
sizeof obuf)) > 0) {
424 if (!(tm->tm_flags & TF_MUTE)) {
425 tm->tm_flags &= ~TF_BELL;
426 teken_input(&tm->tm_emulator, obuf, olen);
427 flags |= tm->tm_flags;
433 if (!(tm->tm_flags & TF_MUTE))
434 tm->tm_class->tc_done(tm);
437 tm->tm_class->tc_bell(tm);
443 struct terminal *tm = tty_softc(tp);
448 vid_info_t *vi = (vid_info_t *)
data;
449 const teken_pos_t *p;
452 if (vi->size !=
sizeof(vid_info_t))
456 p = teken_get_cursor(&tm->tm_emulator);
457 vi->mv_row = p->tp_row;
458 vi->mv_col = p->tp_col;
460 p = teken_get_winsize(&tm->tm_emulator);
461 vi->mv_rsz = p->tp_row;
462 vi->mv_csz = p->tp_col;
464 teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg);
465 vi->mv_norm.fore = fg;
466 vi->mv_norm.back = bg;
468 vi->mv_rev.fore = bg;
469 vi->mv_rev.back = fg & 0x7;
480 error = tm->tm_class->tc_ioctl(tm, cmd,
data, td);
482 if ((error == 0) && (cmd == CONS_CLRHIST)) {
490 teken_set_cursor(&tm->tm_emulator, &p);
497 int nprot, vm_memattr_t *memattr)
499 struct terminal *tm = tty_softc(tp);
501 return (tm->tm_class->tc_mmap(tm, offset, paddr, nprot, memattr));
533 cp =
malloc(
sizeof(
struct consdev), M_TERMINAL,
537 cp->cn_pri = CN_INTERNAL;
540 tm->tm_flags = TF_CONS;
553 struct terminal *tm = cp->cn_arg;
555 tm->tm_class->tc_cngrab(tm);
561 struct terminal *tm = cp->cn_arg;
563 tm->tm_class->tc_cnungrab(tm);
569 struct terminal *tm = cp->cn_arg;
572 cp->cn_pri = CN_DEAD;
579 tm->tm_class->tc_cnprobe(tm, cp);
597 struct terminal *tm = cp->cn_arg;
599 return (tm->tm_class->tc_cngetc(tm));
605 struct terminal *tm = cp->cn_arg;
610 if (!(tm->tm_flags & TF_MUTE)) {
611 backup = *teken_get_curattr(&tm->tm_emulator);
613 teken_input(&tm->tm_emulator, &cv, 1);
614 teken_set_curattr(&tm->tm_emulator, &backup);
615 tm->tm_class->tc_done(tm);
627 struct terminal *tm = softc;
629 tm->tm_flags |= TF_BELL;
635 struct terminal *tm = softc;
637 tm->tm_class->tc_cursor(tm, p);
642 const teken_attr_t *a)
644 struct terminal *tm = softc;
651 const teken_attr_t *a)
653 struct terminal *tm = softc;
661 struct terminal *tm = softc;
663 tm->tm_class->tc_copy(tm, r, p);
669 struct terminal *tm = softc;
671 tm->tm_class->tc_pre_input(tm);
677 struct terminal *tm = softc;
679 tm->tm_class->tc_post_input(tm);
685 struct terminal *tm = softc;
687 tm->tm_class->tc_param(tm, cmd, arg);
694 struct terminal *tm = softc;
int cnadd(struct consdev *cn)
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
int vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
int sprintf(char *buf, const char *cfmt,...)
static struct ttydevsw terminal_tty_class
void terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size, int blank, const term_attr_t *attr)
void terminal_input_raw(struct terminal *tm, char c)
static tsw_close_t termtty_close
void termcn_cnregister(struct terminal *tm)
void terminal_mute(struct terminal *tm, int yes)
static tf_post_input_t termteken_post_input
#define TERMINAL_UNLOCK_TTY(tm)
static tf_fill_t termteken_fill
void terminal_maketty(struct terminal *tm, const char *fmt,...)
#define TERMINAL_UNLOCK(tm)
static cn_term_t termcn_cnterm
#define TERMINAL_LOCK(tm)
#define TERMINAL_LOCK_TTY(tm)
static tsw_mmap_t termtty_mmap
#define TERMINAL_UNLOCK_CONS(tm)
static tf_respond_t termteken_respond
void terminal_input_special(struct terminal *tm, unsigned int k)
void terminal_set_winsize(struct terminal *tm, const struct winsize *size)
static cn_grab_t termcn_cngrab
#define TCHAR_CREATE(c, a)
static cn_init_t termcn_cninit
static tf_putchar_t termteken_putchar
static tf_bell_t termteken_bell
static cn_putc_t termcn_cnputc
const struct consdev_ops termcn_cnops
static tf_pre_input_t termteken_pre_input
static tf_copy_t termteken_copy
void terminal_input_char(struct terminal *tm, term_char_t c)
static tsw_open_t termtty_open
static teken_funcs_t terminal_drawmethods
struct terminal * terminal_alloc(const struct terminal_class *tc, void *softc)
static void terminal_sync_ttysize(struct terminal *tm)
#define TERMINAL_LOCK_CONS(tm)
static cn_probe_t termcn_cnprobe
static cn_ungrab_t termcn_cnungrab
static tsw_outwakeup_t termtty_outwakeup
static tf_cursor_t termteken_cursor
static teken_attr_t default_message
static tf_param_t termteken_param
void terminal_set_cursor(struct terminal *tm, const term_pos_t *pos)
static MALLOC_DEFINE(M_TERMINAL, "terminal", "terminal device")
static cn_getc_t termcn_cngetc
static tsw_ioctl_t termtty_ioctl
static void terminal_init(struct terminal *tm)
static teken_attr_t kernel_message
void tty_set_winsize(struct tty *tp, const struct winsize *wsz)
struct tty * tty_alloc(struct ttydevsw *tsw, void *sc)
int ttydisc_rint(struct tty *tp, char c, int flags)
void ttydisc_rint_done(struct tty *tp)
size_t ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
size_t ttydisc_getc(struct tty *tp, void *buf, size_t len)