chiark / gitweb /
bus-proxy: fix policy for expected/non-expected reply tags
[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 void term_attr_to_argb32(const term_attr *attr, uint32_t *fg, uint32_t *bg, const uint8_t *palette);
101
102 /*
103  * UTF-8
104  */
105
106 struct term_utf8 {
107         uint32_t chars[5];
108         uint32_t ucs4;
109
110         unsigned int i_bytes : 3;
111         unsigned int n_bytes : 3;
112         unsigned int valid : 1;
113 };
114
115 size_t term_utf8_decode(term_utf8 *p, uint32_t **out_buf, char c);
116
117 /*
118  * Parsers
119  */
120
121 int term_parser_new(term_parser **out, bool host);
122 term_parser *term_parser_free(term_parser *parser);
123 int term_parser_feed(term_parser *parser, const term_seq **seq_out, uint32_t raw);
124
125 #define _term_parser_free_ _cleanup_(term_parser_freep)
126 DEFINE_TRIVIAL_CLEANUP_FUNC(term_parser*, term_parser_free);
127
128 /*
129  * Screens
130  */
131
132 enum {
133         TERM_KBDMOD_IDX_SHIFT,
134         TERM_KBDMOD_IDX_CTRL,
135         TERM_KBDMOD_IDX_ALT,
136         TERM_KBDMOD_IDX_LINUX,
137         TERM_KBDMOD_IDX_CAPS,
138         TERM_KBDMOD_CNT,
139
140         TERM_KBDMOD_SHIFT               = 1 << TERM_KBDMOD_IDX_SHIFT,
141         TERM_KBDMOD_CTRL                = 1 << TERM_KBDMOD_IDX_CTRL,
142         TERM_KBDMOD_ALT                 = 1 << TERM_KBDMOD_IDX_ALT,
143         TERM_KBDMOD_LINUX               = 1 << TERM_KBDMOD_IDX_LINUX,
144         TERM_KBDMOD_CAPS                = 1 << TERM_KBDMOD_IDX_CAPS,
145 };
146
147 typedef int (*term_screen_write_fn) (term_screen *screen, void *userdata, const void *buf, size_t size);
148 typedef int (*term_screen_cmd_fn) (term_screen *screen, void *userdata, unsigned int cmd, const term_seq *seq);
149
150 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);
151 term_screen *term_screen_ref(term_screen *screen);
152 term_screen *term_screen_unref(term_screen *screen);
153
154 DEFINE_TRIVIAL_CLEANUP_FUNC(term_screen*, term_screen_unref);
155
156 unsigned int term_screen_get_width(term_screen *screen);
157 unsigned int term_screen_get_height(term_screen *screen);
158 uint64_t term_screen_get_age(term_screen *screen);
159
160 int term_screen_feed_text(term_screen *screen, const uint8_t *in, size_t size);
161 int term_screen_feed_keyboard(term_screen *screen,
162                               const uint32_t *keysyms,
163                               size_t n_syms,
164                               uint32_t ascii,
165                               const uint32_t *ucs4,
166                               unsigned int mods);
167 int term_screen_resize(term_screen *screen, unsigned int width, unsigned int height);
168 void term_screen_soft_reset(term_screen *screen);
169 void term_screen_hard_reset(term_screen *screen);
170
171 int term_screen_set_answerback(term_screen *screen, const char *answerback);
172
173 int term_screen_draw(term_screen *screen,
174                      int (*draw_fn) (term_screen *screen,
175                                      void *userdata,
176                                      unsigned int x,
177                                      unsigned int y,
178                                      const term_attr *attr,
179                                      const uint32_t *ch,
180                                      size_t n_ch,
181                                      unsigned int ch_width),
182                      void *userdata,
183                      uint64_t *fb_age);