source: mainline/uspace/lib/c/generic/io/console.c@ b83c5e4

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b83c5e4 was b48e680f, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Allow console application to set the terminal window caption

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 * Copyright (c) 2021 Jiri Svoboda
3 * Copyright (c) 2006 Josef Cejka
4 * Copyright (c) 2006 Jakub Vana
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/** @addtogroup libc
32 * @{
33 */
34/** @file
35 */
36
37#include <as.h>
38#include <libc.h>
39#include <async.h>
40#include <errno.h>
41#include <stdlib.h>
42#include <str.h>
43#include <vfs/vfs_sess.h>
44#include <io/console.h>
45#include <ipc/console.h>
46
47console_ctrl_t *console_init(FILE *ifile, FILE *ofile)
48{
49 console_ctrl_t *ctrl = malloc(sizeof(console_ctrl_t));
50 if (!ctrl)
51 return NULL;
52
53 ctrl->input_sess = vfs_fsession(ifile, INTERFACE_CONSOLE);
54 if (!ctrl->input_sess) {
55 free(ctrl);
56 return NULL;
57 }
58
59 ctrl->output_sess = vfs_fsession(ofile, INTERFACE_CONSOLE);
60 if (!ctrl->output_sess) {
61 free(ctrl);
62 return NULL;
63 }
64
65 ctrl->input = ifile;
66 ctrl->output = ofile;
67 ctrl->input_aid = 0;
68
69 return ctrl;
70}
71
72void console_done(console_ctrl_t *ctrl)
73{
74 free(ctrl);
75}
76
77bool console_kcon(void)
78{
79 return __SYSCALL0(SYS_DEBUG_CONSOLE);
80}
81
82void console_flush(console_ctrl_t *ctrl)
83{
84 fflush(ctrl->output);
85}
86
87void console_clear(console_ctrl_t *ctrl)
88{
89 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
90 async_req_0_0(exch, CONSOLE_CLEAR);
91 async_exchange_end(exch);
92}
93
94errno_t console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
95{
96 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
97 errno_t rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
98 async_exchange_end(exch);
99
100 return rc;
101}
102
103void console_set_style(console_ctrl_t *ctrl, uint8_t style)
104{
105 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
106 async_req_1_0(exch, CONSOLE_SET_STYLE, style);
107 async_exchange_end(exch);
108}
109
110void console_set_color(console_ctrl_t *ctrl, uint8_t bgcolor, uint8_t fgcolor,
111 uint8_t flags)
112{
113 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
114 async_req_3_0(exch, CONSOLE_SET_COLOR, bgcolor, fgcolor, flags);
115 async_exchange_end(exch);
116}
117
118void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t bgcolor,
119 uint32_t fgcolor)
120{
121 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
122 async_req_2_0(exch, CONSOLE_SET_RGB_COLOR, bgcolor, fgcolor);
123 async_exchange_end(exch);
124}
125
126void console_cursor_visibility(console_ctrl_t *ctrl, bool show)
127{
128 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
129 async_req_1_0(exch, CONSOLE_SET_CURSOR_VISIBILITY, (show != false));
130 async_exchange_end(exch);
131}
132
133/** Set console caption.
134 *
135 * Set caption text for the console (if the console suports captions).
136 *
137 * @param ctrl Console
138 * @param caption Caption text
139 * @return EOK on success or an error code
140 */
141errno_t console_set_caption(console_ctrl_t *ctrl, const char *caption)
142{
143 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
144 ipc_call_t answer;
145 aid_t req = async_send_0(exch, CONSOLE_SET_CAPTION, &answer);
146 errno_t retval = async_data_write_start(exch, caption, str_size(caption));
147
148 if (retval != EOK) {
149 async_forget(req);
150 async_exchange_end(exch);
151 return retval;
152 }
153
154 async_wait_for(req, &retval);
155 async_exchange_end(exch);
156 return EOK;
157}
158
159errno_t console_get_color_cap(console_ctrl_t *ctrl, sysarg_t *ccap)
160{
161 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
162 errno_t rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
163 async_exchange_end(exch);
164
165 return rc;
166}
167
168errno_t console_get_pos(console_ctrl_t *ctrl, sysarg_t *col, sysarg_t *row)
169{
170 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
171 errno_t rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
172 async_exchange_end(exch);
173
174 return rc;
175}
176
177void console_set_pos(console_ctrl_t *ctrl, sysarg_t col, sysarg_t row)
178{
179 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
180 async_req_2_0(exch, CONSOLE_SET_POS, col, row);
181 async_exchange_end(exch);
182}
183
184static errno_t console_ev_decode(ipc_call_t *call, cons_event_t *event)
185{
186 event->type = ipc_get_arg1(call);
187
188 switch (event->type) {
189 case CEV_KEY:
190 event->ev.key.type = ipc_get_arg2(call);
191 event->ev.key.key = ipc_get_arg3(call);
192 event->ev.key.mods = ipc_get_arg4(call);
193 event->ev.key.c = ipc_get_arg5(call);
194 break;
195 case CEV_POS:
196 event->ev.pos.pos_id = ipc_get_arg2(call) >> 16;
197 event->ev.pos.type = ipc_get_arg2(call) & 0xffff;
198 event->ev.pos.btn_num = ipc_get_arg3(call);
199 event->ev.pos.hpos = ipc_get_arg4(call);
200 event->ev.pos.vpos = ipc_get_arg5(call);
201 break;
202 default:
203 return EIO;
204 }
205
206 return EOK;
207}
208
209/** Get console event.
210 *
211 * @param ctrl Console
212 * @param event Place to store event
213 * @return EOK on success, EIO on failure
214 */
215errno_t console_get_event(console_ctrl_t *ctrl, cons_event_t *event)
216{
217 if (ctrl->input_aid == 0) {
218 ipc_call_t result;
219
220 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
221 aid_t aid = async_send_0(exch, CONSOLE_GET_EVENT, &result);
222 async_exchange_end(exch);
223
224 errno_t rc;
225 async_wait_for(aid, &rc);
226
227 if (rc != EOK)
228 return EIO;
229
230 rc = console_ev_decode(&result, event);
231 if (rc != EOK)
232 return EIO;
233 } else {
234 errno_t retval;
235 async_wait_for(ctrl->input_aid, &retval);
236
237 ctrl->input_aid = 0;
238
239 if (retval != EOK)
240 return EIO;
241
242 errno_t rc = console_ev_decode(&ctrl->input_call, event);
243 if (rc != EOK)
244 return EIO;
245 }
246
247 return EOK;
248}
249
250/** Get console event with timeout.
251 *
252 * @param ctrl Console
253 * @param event Place to store event
254 * @param timeout Pointer to timeout. This will be updated to reflect
255 * the remaining time in case of timeout.
256 * @return EOK on success (event received), ETIMEOUT on time out,
257 * EIO on I/O error (e.g. lost console connection), ENOMEM
258 * if out of memory
259 */
260errno_t console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
261 usec_t *timeout)
262{
263 struct timespec t0;
264 getuptime(&t0);
265
266 if (ctrl->input_aid == 0) {
267 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
268 ctrl->input_aid = async_send_0(exch, CONSOLE_GET_EVENT,
269 &ctrl->input_call);
270 async_exchange_end(exch);
271 }
272
273 errno_t retval;
274 errno_t rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
275 if (rc != EOK) {
276 if (rc == ENOMEM)
277 return ENOMEM;
278 *timeout = 0;
279 return ETIMEOUT;
280 }
281
282 ctrl->input_aid = 0;
283
284 if (retval != EOK)
285 return EIO;
286
287 rc = console_ev_decode(&ctrl->input_call, event);
288 if (rc != EOK)
289 return EIO;
290
291 /* Update timeout */
292 struct timespec t1;
293 getuptime(&t1);
294 *timeout -= NSEC2USEC(ts_sub_diff(&t1, &t0));
295
296 return EOK;
297}
298
299/** Create a shared buffer for fast rendering to the console.
300 *
301 * @param ctrl Console
302 * @param cols Number of columns
303 * @param rows Number of rows
304 * @param rbuf Place to store pointer to the shared buffer
305 * @return EOK on success or an error code
306 */
307errno_t console_map(console_ctrl_t *ctrl, sysarg_t cols, sysarg_t rows,
308 charfield_t **rbuf)
309{
310 async_exch_t *exch = NULL;
311 void *buf;
312 aid_t req;
313 ipc_call_t answer;
314 size_t asize;
315 errno_t rc;
316
317 exch = async_exchange_begin(ctrl->output_sess);
318 req = async_send_2(exch, CONSOLE_MAP, cols, rows, &answer);
319
320 asize = PAGES2SIZE(SIZE2PAGES(cols * rows * sizeof(charfield_t)));
321
322 rc = async_share_in_start_0_0(exch, asize, &buf);
323 if (rc != EOK) {
324 async_forget(req);
325 goto error;
326 }
327
328 async_exchange_end(exch);
329 exch = NULL;
330
331 async_wait_for(req, &rc);
332 if (rc != EOK)
333 goto error;
334
335 *rbuf = (charfield_t *)buf;
336 return EOK;
337error:
338 if (exch != NULL)
339 async_exchange_end(exch);
340 return rc;
341}
342
343/** Unmap console shared buffer.
344 *
345 * @param ctrl Console
346 * @param buf Buffer
347 */
348void console_unmap(console_ctrl_t *ctrl, charfield_t *buf)
349{
350 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
351 (void) async_req_0_0(exch, CONSOLE_UNMAP);
352 async_exchange_end(exch);
353
354 as_area_destroy(buf);
355}
356
357/** Update console rectangle from shared buffer.
358 *
359 * @param ctrl Console
360 * @param c0 Column coordinate of top-left corner (inclusive)
361 * @param r0 Row coordinate of top-left corner (inclusive)
362 * @param c1 Column coordinate of bottom-right corner (exclusive)
363 * @param r1 Row coordinate of bottom-right corner (exclusive)
364 *
365 * @return EOK on sucess or an error code
366 */
367errno_t console_update(console_ctrl_t *ctrl, sysarg_t c0, sysarg_t r0,
368 sysarg_t c1, sysarg_t r1)
369{
370 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
371 errno_t rc = async_req_4_0(exch, CONSOLE_UPDATE, c0, r0, c1, r1);
372 async_exchange_end(exch);
373
374 return rc;
375}
376
377/** @}
378 */
Note: See TracBrowser for help on using the repository browser.