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

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

Use of uninitialized variable causes UI to fail in terminal

You fail me yet again GCC

  • Property mode set to 100644
File size: 8.8 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 <vfs/vfs_sess.h>
43#include <io/console.h>
44#include <ipc/console.h>
45
46console_ctrl_t *console_init(FILE *ifile, FILE *ofile)
47{
48 console_ctrl_t *ctrl = malloc(sizeof(console_ctrl_t));
49 if (!ctrl)
50 return NULL;
51
52 ctrl->input_sess = vfs_fsession(ifile, INTERFACE_CONSOLE);
53 if (!ctrl->input_sess) {
54 free(ctrl);
55 return NULL;
56 }
57
58 ctrl->output_sess = vfs_fsession(ofile, INTERFACE_CONSOLE);
59 if (!ctrl->output_sess) {
60 free(ctrl);
61 return NULL;
62 }
63
64 ctrl->input = ifile;
65 ctrl->output = ofile;
66 ctrl->input_aid = 0;
67
68 return ctrl;
69}
70
71void console_done(console_ctrl_t *ctrl)
72{
73 free(ctrl);
74}
75
76bool console_kcon(void)
77{
78 return __SYSCALL0(SYS_DEBUG_CONSOLE);
79}
80
81void console_flush(console_ctrl_t *ctrl)
82{
83 fflush(ctrl->output);
84}
85
86void console_clear(console_ctrl_t *ctrl)
87{
88 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
89 async_req_0_0(exch, CONSOLE_CLEAR);
90 async_exchange_end(exch);
91}
92
93errno_t console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
94{
95 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
96 errno_t rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
97 async_exchange_end(exch);
98
99 return rc;
100}
101
102void console_set_style(console_ctrl_t *ctrl, uint8_t style)
103{
104 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
105 async_req_1_0(exch, CONSOLE_SET_STYLE, style);
106 async_exchange_end(exch);
107}
108
109void console_set_color(console_ctrl_t *ctrl, uint8_t bgcolor, uint8_t fgcolor,
110 uint8_t flags)
111{
112 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
113 async_req_3_0(exch, CONSOLE_SET_COLOR, bgcolor, fgcolor, flags);
114 async_exchange_end(exch);
115}
116
117void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t bgcolor,
118 uint32_t fgcolor)
119{
120 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
121 async_req_2_0(exch, CONSOLE_SET_RGB_COLOR, bgcolor, fgcolor);
122 async_exchange_end(exch);
123}
124
125void console_cursor_visibility(console_ctrl_t *ctrl, bool show)
126{
127 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
128 async_req_1_0(exch, CONSOLE_SET_CURSOR_VISIBILITY, (show != false));
129 async_exchange_end(exch);
130}
131
132errno_t console_get_color_cap(console_ctrl_t *ctrl, sysarg_t *ccap)
133{
134 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
135 errno_t rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
136 async_exchange_end(exch);
137
138 return rc;
139}
140
141errno_t console_get_pos(console_ctrl_t *ctrl, sysarg_t *col, sysarg_t *row)
142{
143 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
144 errno_t rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
145 async_exchange_end(exch);
146
147 return rc;
148}
149
150void console_set_pos(console_ctrl_t *ctrl, sysarg_t col, sysarg_t row)
151{
152 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
153 async_req_2_0(exch, CONSOLE_SET_POS, col, row);
154 async_exchange_end(exch);
155}
156
157static errno_t console_ev_decode(ipc_call_t *call, cons_event_t *event)
158{
159 event->type = ipc_get_arg1(call);
160
161 switch (event->type) {
162 case CEV_KEY:
163 event->ev.key.type = ipc_get_arg2(call);
164 event->ev.key.key = ipc_get_arg3(call);
165 event->ev.key.mods = ipc_get_arg4(call);
166 event->ev.key.c = ipc_get_arg5(call);
167 break;
168 case CEV_POS:
169 event->ev.pos.pos_id = ipc_get_arg2(call) >> 16;
170 event->ev.pos.type = ipc_get_arg2(call) & 0xffff;
171 event->ev.pos.btn_num = ipc_get_arg3(call);
172 event->ev.pos.hpos = ipc_get_arg4(call);
173 event->ev.pos.vpos = ipc_get_arg5(call);
174 break;
175 default:
176 return EIO;
177 }
178
179 return EOK;
180}
181
182/** Get console event.
183 *
184 * @param ctrl Console
185 * @param event Place to store event
186 * @return EOK on success, EIO on failure
187 */
188errno_t console_get_event(console_ctrl_t *ctrl, cons_event_t *event)
189{
190 if (ctrl->input_aid == 0) {
191 ipc_call_t result;
192
193 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
194 aid_t aid = async_send_0(exch, CONSOLE_GET_EVENT, &result);
195 async_exchange_end(exch);
196
197 errno_t rc;
198 async_wait_for(aid, &rc);
199
200 if (rc != EOK)
201 return EIO;
202
203 rc = console_ev_decode(&result, event);
204 if (rc != EOK)
205 return EIO;
206 } else {
207 errno_t retval;
208 async_wait_for(ctrl->input_aid, &retval);
209
210 ctrl->input_aid = 0;
211
212 if (retval != EOK)
213 return EIO;
214
215 errno_t rc = console_ev_decode(&ctrl->input_call, event);
216 if (rc != EOK)
217 return EIO;
218 }
219
220 return EOK;
221}
222
223/** Get console event with timeout.
224 *
225 * @param ctrl Console
226 * @param event Place to store event
227 * @param timeout Pointer to timeout. This will be updated to reflect
228 * the remaining time in case of timeout.
229 * @return EOK on success (event received), ETIMEOUT on time out,
230 * EIO on I/O error (e.g. lost console connection), ENOMEM
231 * if out of memory
232 */
233errno_t console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
234 usec_t *timeout)
235{
236 struct timespec t0;
237 getuptime(&t0);
238
239 if (ctrl->input_aid == 0) {
240 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
241 ctrl->input_aid = async_send_0(exch, CONSOLE_GET_EVENT,
242 &ctrl->input_call);
243 async_exchange_end(exch);
244 }
245
246 errno_t retval;
247 errno_t rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
248 if (rc != EOK) {
249 if (rc == ENOMEM)
250 return ENOMEM;
251 *timeout = 0;
252 return ETIMEOUT;
253 }
254
255 ctrl->input_aid = 0;
256
257 if (retval != EOK)
258 return EIO;
259
260 rc = console_ev_decode(&ctrl->input_call, event);
261 if (rc != EOK)
262 return EIO;
263
264 /* Update timeout */
265 struct timespec t1;
266 getuptime(&t1);
267 *timeout -= NSEC2USEC(ts_sub_diff(&t1, &t0));
268
269 return EOK;
270}
271
272/** Create a shared buffer for fast rendering to the console.
273 *
274 * @param ctrl Console
275 * @param cols Number of columns
276 * @param rows Number of rows
277 * @param rbuf Place to store pointer to the shared buffer
278 * @return EOK on success or an error code
279 */
280errno_t console_map(console_ctrl_t *ctrl, sysarg_t cols, sysarg_t rows,
281 charfield_t **rbuf)
282{
283 async_exch_t *exch = NULL;
284 void *buf;
285 aid_t req;
286 ipc_call_t answer;
287 size_t asize;
288 errno_t rc;
289
290 exch = async_exchange_begin(ctrl->output_sess);
291 req = async_send_2(exch, CONSOLE_MAP, cols, rows, &answer);
292
293 asize = PAGES2SIZE(SIZE2PAGES(cols * rows * sizeof(charfield_t)));
294
295 rc = async_share_in_start_0_0(exch, asize, &buf);
296 if (rc != EOK) {
297 async_forget(req);
298 goto error;
299 }
300
301 async_exchange_end(exch);
302 exch = NULL;
303
304 async_wait_for(req, &rc);
305 if (rc != EOK)
306 goto error;
307
308 *rbuf = (charfield_t *)buf;
309 return EOK;
310error:
311 if (exch != NULL)
312 async_exchange_end(exch);
313 return rc;
314}
315
316/** Unmap console shared buffer.
317 *
318 * @param ctrl Console
319 * @param buf Buffer
320 */
321void console_unmap(console_ctrl_t *ctrl, charfield_t *buf)
322{
323 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
324 (void) async_req_0_0(exch, CONSOLE_UNMAP);
325 async_exchange_end(exch);
326
327 as_area_destroy(buf);
328}
329
330/** Update console rectangle from shared buffer.
331 *
332 * @param ctrl Console
333 * @param c0 Column coordinate of top-left corner (inclusive)
334 * @param r0 Row coordinate of top-left corner (inclusive)
335 * @param c1 Column coordinate of bottom-right corner (exclusive)
336 * @param r1 Row coordinate of bottom-right corner (exclusive)
337 *
338 * @return EOK on sucess or an error code
339 */
340errno_t console_update(console_ctrl_t *ctrl, sysarg_t c0, sysarg_t r0,
341 sysarg_t c1, sysarg_t r1)
342{
343 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
344 errno_t rc = async_req_4_0(exch, CONSOLE_UPDATE, c0, r0, c1, r1);
345 async_exchange_end(exch);
346
347 return rc;
348}
349
350/** @}
351 */
Note: See TracBrowser for help on using the repository browser.