30 #include <sys/cdefs.h>
33 #include <sys/param.h>
35 #include <sys/consio.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
41 #include <sys/terminal.h>
44 #include <machine/stdarg.h>
46 static MALLOC_DEFINE(M_TERMINAL,
"terminal",
"terminal device");
57 #define TERMINAL_LOCK(tm) do { \
58 if ((tm)->tm_flags & TF_CONS) \
59 mtx_lock_spin(&(tm)->tm_mtx); \
60 else if ((tm)->tm_tty != NULL) \
61 tty_lock((tm)->tm_tty); \
63 #define TERMINAL_UNLOCK(tm) do { \
64 if ((tm)->tm_flags & TF_CONS) \
65 mtx_unlock_spin(&(tm)->tm_mtx); \
66 else if ((tm)->tm_tty != NULL) \
67 tty_unlock((tm)->tm_tty); \
69 #define TERMINAL_LOCK_TTY(tm) do { \
70 if ((tm)->tm_flags & TF_CONS) \
71 mtx_lock_spin(&(tm)->tm_mtx); \
73 #define TERMINAL_UNLOCK_TTY(tm) do { \
74 if ((tm)->tm_flags & TF_CONS) \
75 mtx_unlock_spin(&(tm)->tm_mtx); \
77 #define TERMINAL_LOCK_CONS(tm) mtx_lock_spin(&(tm)->tm_mtx)
78 #define TERMINAL_UNLOCK_CONS(tm) mtx_unlock_spin(&(tm)->tm_mtx)
122 .ta_fgcolor = TC_WHITE,
123 .ta_bgcolor = TC_BLACK,
124 .ta_format = TF_BOLD,
128 .ta_fgcolor = TC_WHITE,
129 .ta_bgcolor = TC_BLACK,
132 #define TCHAR_CREATE(c, a) ((c) | \
133 (a)->ta_format << 21 | \
134 teken_256to8((a)->ta_fgcolor) << 26 | \
135 teken_256to8((a)->ta_bgcolor) << 29)
141 if (tm->tm_flags & TF_CONS)
142 mtx_init(&tm->tm_mtx,
"trmlck", NULL, MTX_SPIN);
152 tm =
malloc(
sizeof(
struct terminal), M_TERMINAL, M_WAITOK|M_ZERO);
156 tm->tm_softc = softc;
198 tm->tm_winsize = *size;
200 r.tr_begin.tp_row = r.tr_begin.tp_col = 0;
201 r.tr_end.tp_row = size->ws_row;
202 r.tr_end.tp_col = size->ws_col;
206 teken_set_winsize_noreset(&tm->tm_emulator, &r.tr_end);
208 teken_set_winsize(&tm->tm_emulator, &r.tr_end);
211 if ((blank != 0) && !(tm->tm_flags & TF_MUTE))
212 tm->tm_class->tc_fill(tm, &r,
TCHAR_CREATE((teken_char_t)
' ',
237 tm->tm_flags |= TF_MUTE;
239 tm->tm_flags &= ~TF_MUTE;
257 if (TCHAR_FORMAT(c) & TF_CJK_RIGHT)
259 c = TCHAR_CHARACTER(c);
267 }
else if (c < 0x800) {
274 }
else if (c < 0x10000) {
277 0x80 | ((c >> 6) & 0x3f),
285 0x80 | ((c >> 12) & 0x3f),
286 0x80 | ((c >> 6) & 0x3f),
321 str = teken_get_sequence(&tm->tm_emulator, k);
338 struct terminal *tm = tty_softc(tp);
340 tm->tm_class->tc_opened(tm, 1);
347 struct terminal *tm = tty_softc(tp);
349 tm->tm_class->tc_opened(tm, 0);
355 struct terminal *tm = tty_softc(tp);
358 unsigned int flags = 0;
360 while ((olen =
ttydisc_getc(tp, obuf,
sizeof obuf)) > 0) {
362 if (!(tm->tm_flags & TF_MUTE)) {
363 tm->tm_flags &= ~TF_BELL;
364 teken_input(&tm->tm_emulator, obuf, olen);
365 flags |= tm->tm_flags;
370 tm->tm_class->tc_done(tm);
372 tm->tm_class->tc_bell(tm);
378 struct terminal *tm = tty_softc(tp);
383 vid_info_t *vi = (vid_info_t *)data;
384 const teken_pos_t *p;
387 if (vi->size !=
sizeof(vid_info_t))
391 p = teken_get_cursor(&tm->tm_emulator);
392 vi->mv_row = p->tp_row;
393 vi->mv_col = p->tp_col;
395 p = teken_get_winsize(&tm->tm_emulator);
396 vi->mv_rsz = p->tp_row;
397 vi->mv_csz = p->tp_col;
399 teken_get_defattr_cons25(&tm->tm_emulator, &fg, &bg);
400 vi->mv_norm.fore = fg;
401 vi->mv_norm.back = bg;
403 vi->mv_rev.fore = bg;
404 vi->mv_rev.back = fg & 0x7;
415 error = tm->tm_class->tc_ioctl(tm, cmd, data, td);
422 int nprot, vm_memattr_t *memattr)
424 struct terminal *tm = tty_softc(tp);
426 return (tm->tm_class->tc_mmap(tm, offset, paddr, nprot, memattr));
458 cp =
malloc(
sizeof(
struct consdev), M_TERMINAL,
462 cp->cn_pri = CN_INTERNAL;
465 tm->tm_flags = TF_CONS;
490 struct terminal *tm = cp->cn_arg;
493 cp->cn_pri = CN_DEAD;
500 tm->tm_class->tc_cnprobe(tm, cp);
518 struct terminal *tm = cp->cn_arg;
520 return (tm->tm_class->tc_cngetc(tm));
526 struct terminal *tm = cp->cn_arg;
531 if (!(tm->tm_flags & TF_MUTE)) {
532 backup = *teken_get_curattr(&tm->tm_emulator);
534 teken_input(&tm->tm_emulator, &cv, 1);
535 teken_set_curattr(&tm->tm_emulator, &backup);
539 tm->tm_class->tc_done(tm);
549 struct terminal *tm = softc;
551 tm->tm_flags |= TF_BELL;
557 struct terminal *tm = softc;
559 tm->tm_class->tc_cursor(tm, p);
564 const teken_attr_t *a)
566 struct terminal *tm = softc;
573 const teken_attr_t *a)
575 struct terminal *tm = softc;
583 struct terminal *tm = softc;
585 tm->tm_class->tc_copy(tm, r, p);
591 struct terminal *tm = softc;
593 tm->tm_class->tc_param(tm, cmd, arg);
600 struct terminal *tm = softc;
static tsw_mmap_t termtty_mmap
static cn_grab_t termcn_cngrab
struct terminal * terminal_alloc(const struct terminal_class *tc, void *softc)
int vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
static cn_init_t termcn_cninit
void terminal_input_raw(struct terminal *tm, char c)
static cn_term_t termcn_cnterm
void terminal_input_char(struct terminal *tm, term_char_t c)
int sprintf(char *buf, const char *cfmt,...)
#define TERMINAL_LOCK(tm)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
const struct consdev_ops termcn_cnops
static cn_putc_t termcn_cnputc
#define TERMINAL_UNLOCK_CONS(tm)
static cn_getc_t termcn_cngetc
#define TERMINAL_UNLOCK_TTY(tm)
size_t ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
void terminal_mute(struct terminal *tm, int yes)
static cn_probe_t termcn_cnprobe
static const teken_attr_t default_message
static teken_funcs_t terminal_drawmethods
void ttydisc_rint_done(struct tty *tp)
void tty_set_winsize(struct tty *tp, const struct winsize *wsz)
void terminal_set_winsize_blank(struct terminal *tm, const struct winsize *size, int blank)
static tsw_ioctl_t termtty_ioctl
void terminal_set_winsize(struct terminal *tm, const struct winsize *size)
static tf_putchar_t termteken_putchar
struct tty * tty_alloc(struct ttydevsw *tsw, void *sc)
static tf_respond_t termteken_respond
void termcn_cnregister(struct terminal *tm)
static tsw_close_t termtty_close
static tf_cursor_t termteken_cursor
static tf_copy_t termteken_copy
void terminal_maketty(struct terminal *tm, const char *fmt,...)
static cn_ungrab_t termcn_cnungrab
int ttydisc_rint(struct tty *tp, char c, int flags)
static tf_bell_t termteken_bell
size_t ttydisc_getc(struct tty *tp, void *buf, size_t len)
static void terminal_sync_ttysize(struct terminal *tm)
#define TCHAR_CREATE(c, a)
void terminal_input_special(struct terminal *tm, unsigned int k)
static struct ttydevsw terminal_tty_class
static tf_param_t termteken_param
void mtx_init(struct mtx *m, const char *name, const char *type, int opts)
void tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt,...)
static tf_fill_t termteken_fill
#define TERMINAL_LOCK_TTY(tm)
static void terminal_init(struct terminal *tm)
int cnadd(struct consdev *cn)
#define TERMINAL_LOCK_CONS(tm)
static tsw_open_t termtty_open
#define TERMINAL_UNLOCK(tm)
static MALLOC_DEFINE(M_TERMINAL,"terminal","terminal device")
static const teken_attr_t kernel_message
static tsw_outwakeup_t termtty_outwakeup