chiark / gitweb /
terminal: make utf8 decoder return length
[elogind.git] / src / libsystemd-terminal / term.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #pragma once
23
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include "util.h"
28
29 typedef struct term_color term_color;
30 typedef struct term_attr term_attr;
31
32 typedef struct term_utf8 term_utf8;
33 typedef struct term_seq term_seq;
34 typedef struct term_parser term_parser;
35
36 typedef struct term_screen term_screen;
37
38 /*
39  * Ageing
40  */
41
42 typedef uint64_t term_age_t;
43
44 #define TERM_AGE_NULL 0
45
46 /*
47  * Attributes
48  */
49
50 enum {
51         /* special color-codes */
52         TERM_CCODE_DEFAULT,                                             /* default foreground/background color */
53         TERM_CCODE_256,                                                 /* 256color code */
54         TERM_CCODE_RGB,                                                 /* color is specified as RGB */
55
56         /* dark color-codes */
57         TERM_CCODE_BLACK,
58         TERM_CCODE_RED,
59         TERM_CCODE_GREEN,
60         TERM_CCODE_YELLOW,
61         TERM_CCODE_BLUE,
62         TERM_CCODE_MAGENTA,
63         TERM_CCODE_CYAN,
64         TERM_CCODE_WHITE,                                               /* technically: light grey */
65
66         /* light color-codes */
67         TERM_CCODE_LIGHT_BLACK          = TERM_CCODE_BLACK + 8,         /* technically: dark grey */
68         TERM_CCODE_LIGHT_RED            = TERM_CCODE_RED + 8,
69         TERM_CCODE_LIGHT_GREEN          = TERM_CCODE_GREEN + 8,
70         TERM_CCODE_LIGHT_YELLOW         = TERM_CCODE_YELLOW + 8,
71         TERM_CCODE_LIGHT_BLUE           = TERM_CCODE_BLUE + 8,
72         TERM_CCODE_LIGHT_MAGENTA        = TERM_CCODE_MAGENTA + 8,
73         TERM_CCODE_LIGHT_CYAN           = TERM_CCODE_CYAN + 8,
74         TERM_CCODE_LIGHT_WHITE          = TERM_CCODE_WHITE + 8,
75
76         TERM_CCODE_CNT,
77 };
78
79 struct term_color {
80         uint8_t ccode;
81         uint8_t c256;
82         uint8_t red;
83         uint8_t green;
84         uint8_t blue;
85 };
86
87 struct term_attr {
88         term_color fg;                          /* foreground color */
89         term_color bg;                          /* background color */
90
91         unsigned int bold : 1;                  /* bold font */
92         unsigned int italic : 1;                /* italic font */
93         unsigned int underline : 1;             /* underline text */
94         unsigned int inverse : 1;               /* inverse fg/bg */
95         unsigned int protect : 1;               /* protect from erase */
96         unsigned int blink : 1;                 /* blink text */
97         unsigned int hidden : 1;                /* hidden */
98 };
99
100 /*
101  * UTF-8
102  */
103
104 struct term_utf8 {
105         uint32_t chars[5];
106         uint32_t ucs4;
107
108         unsigned int i_bytes : 3;
109         unsigned int n_bytes : 3;
110         unsigned int valid : 1;
111 };
112
113 size_t term_utf8_encode(char *out_utf8, uint32_t g);
114 size_t term_utf8_decode(term_utf8 *p, uint32_t **out_buf, char c);
115
116 /*
117  * Parsers
118  */
119
120 int term_parser_new(term_parser **out, bool host);
121 term_parser *term_parser_free(term_parser *parser);
122 int term_parser_feed(term_parser *parser, const term_seq **seq_out, uint32_t raw);
123
124 #define _term_parser_free_ _cleanup_(term_parser_freep)
125 DEFINE_TRIVIAL_CLEANUP_FUNC(term_parser*, term_parser_free);
126
127 /*
128  * Screens
129  */
130
131 typedef int (*term_screen_write_fn) (term_screen *screen, void *userdata, const void *buf, size_t size);
132 typedef int (*term_screen_cmd_fn) (term_screen *screen, void *userdata, unsigned int cmd, const term_seq *seq);
133
134 int term_screen_new(term_screen **out, term_screen_write_fn write_fn, void *write_fn_data, term_screen_cmd_fn cmd_fn, void *cmd_fn_data);
135 term_screen *term_screen_ref(term_screen *screen);
136 term_screen *term_screen_unref(term_screen *screen);
137
138 DEFINE_TRIVIAL_CLEANUP_FUNC(term_screen*, term_screen_unref);
139
140 unsigned int term_screen_get_width(term_screen *screen);
141 unsigned int term_screen_get_height(term_screen *screen);
142
143 int term_screen_feed_text(term_screen *screen, const uint8_t *in, size_t size);
144 int term_screen_feed_keyboard(term_screen *screen, uint32_t keysym, uint32_t ascii, uint32_t ucs4, unsigned int mods);
145 int term_screen_resize(term_screen *screen, unsigned int width, unsigned int height);
146 void term_screen_soft_reset(term_screen *screen);
147 void term_screen_hard_reset(term_screen *screen);
148
149 int term_screen_set_answerback(term_screen *screen, const char *answerback);