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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bd41ac52 was bd41ac52, checked in by Jakub Jermar <jakub@…>, 7 years ago

Get rid of sys/time.h

This commit moves the POSIX-like time functionality from libc's
sys/time.h to libposix and introduces C11-like or HelenOS-specific
interfaces to libc.

Specifically, use of sys/time.h, struct timeval, suseconds_t and
gettimeofday is replaced by time.h (C11), struct timespec (C11), usec_t
(HelenOS) and getuptime / getrealtime (HelenOS).

Also attempt to fix the implementation of clock() to return microseconds
(clocks) rather than processor cycles and move it to libc.

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[b27a97bb]1/*
[df4ed85]2 * Copyright (c) 2006 Josef Cejka
3 * Copyright (c) 2006 Jakub Vana
[2595dab]4 * Copyright (c) 2008 Jiri Svoboda
[b27a97bb]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
[a46da63]31/** @addtogroup libc
[b2951e2]32 * @{
33 */
34/** @file
35 */
36
[afa6e74]37#include <libc.h>
[b917098]38#include <async.h>
[79ae36dd]39#include <errno.h>
[38d150e]40#include <stdlib.h>
[79ae36dd]41#include <vfs/vfs_sess.h>
[2595dab]42#include <io/console.h>
43#include <ipc/console.h>
[afa6e74]44
[79ae36dd]45console_ctrl_t *console_init(FILE *ifile, FILE *ofile)
[afa6e74]46{
[79ae36dd]47 console_ctrl_t *ctrl = malloc(sizeof(console_ctrl_t));
48 if (!ctrl)
49 return NULL;
[a35b458]50
[6afc9d7]51 ctrl->input_sess = vfs_fsession(ifile, INTERFACE_CONSOLE);
[79ae36dd]52 if (!ctrl->input_sess) {
53 free(ctrl);
54 return NULL;
55 }
[a35b458]56
[6afc9d7]57 ctrl->output_sess = vfs_fsession(ofile, INTERFACE_CONSOLE);
[79ae36dd]58 if (!ctrl->output_sess) {
59 free(ctrl);
60 return NULL;
61 }
[a35b458]62
[79ae36dd]63 ctrl->input = ifile;
64 ctrl->output = ofile;
65 ctrl->input_aid = 0;
[a35b458]66
[79ae36dd]67 return ctrl;
68}
69
70void console_done(console_ctrl_t *ctrl)
71{
72 free(ctrl);
[2595dab]73}
74
[79ae36dd]75bool console_kcon(void)
[2595dab]76{
[f6ab787]77 return __SYSCALL0(SYS_DEBUG_CONSOLE);
[79ae36dd]78}
79
80void console_flush(console_ctrl_t *ctrl)
81{
82 fflush(ctrl->output);
[2595dab]83}
84
[79ae36dd]85void console_clear(console_ctrl_t *ctrl)
[2595dab]86{
[79ae36dd]87 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[7c014d1]88 async_req_0_0(exch, CONSOLE_CLEAR);
[79ae36dd]89 async_exchange_end(exch);
[2595dab]90}
91
[b7fd2a0]92errno_t console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
[79ae36dd]93{
94 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[b7fd2a0]95 errno_t rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
[79ae36dd]96 async_exchange_end(exch);
[a35b458]97
[79ae36dd]98 return rc;
99}
100
101void console_set_style(console_ctrl_t *ctrl, uint8_t style)
102{
103 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[7c014d1]104 async_req_1_0(exch, CONSOLE_SET_STYLE, style);
[79ae36dd]105 async_exchange_end(exch);
106}
107
[7c014d1]108void console_set_color(console_ctrl_t *ctrl, uint8_t bgcolor, uint8_t fgcolor,
[9f1362d4]109 uint8_t flags)
[2595dab]110{
[79ae36dd]111 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[7c014d1]112 async_req_3_0(exch, CONSOLE_SET_COLOR, bgcolor, fgcolor, flags);
[79ae36dd]113 async_exchange_end(exch);
[d2cc7e1]114}
115
[7c014d1]116void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t bgcolor,
117 uint32_t fgcolor)
[0b6d70d]118{
[79ae36dd]119 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[7c014d1]120 async_req_2_0(exch, CONSOLE_SET_RGB_COLOR, bgcolor, fgcolor);
[79ae36dd]121 async_exchange_end(exch);
[0b6d70d]122}
123
[79ae36dd]124void console_cursor_visibility(console_ctrl_t *ctrl, bool show)
[1c03c17]125{
[79ae36dd]126 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[5d94b16c]127 async_req_1_0(exch, CONSOLE_SET_CURSOR_VISIBILITY, (show != false));
[79ae36dd]128 async_exchange_end(exch);
[2595dab]129}
130
[b7fd2a0]131errno_t console_get_color_cap(console_ctrl_t *ctrl, sysarg_t *ccap)
[50cfa6c]132{
[79ae36dd]133 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[b7fd2a0]134 errno_t rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
[79ae36dd]135 async_exchange_end(exch);
[a35b458]136
[79ae36dd]137 return rc;
[50cfa6c]138}
139
[b7fd2a0]140errno_t console_get_pos(console_ctrl_t *ctrl, sysarg_t *col, sysarg_t *row)
[2595dab]141{
[79ae36dd]142 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[b7fd2a0]143 errno_t rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
[79ae36dd]144 async_exchange_end(exch);
[a35b458]145
[79ae36dd]146 return rc;
[2595dab]147}
148
[79ae36dd]149void console_set_pos(console_ctrl_t *ctrl, sysarg_t col, sysarg_t row)
[19528516]150{
[79ae36dd]151 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
[5d94b16c]152 async_req_2_0(exch, CONSOLE_SET_POS, col, row);
[79ae36dd]153 async_exchange_end(exch);
[19528516]154}
155
[b7fd2a0]156static errno_t console_ev_decode(ipc_call_t *call, cons_event_t *event)
[902f0906]157{
158 event->type = IPC_GET_ARG1(*call);
159
160 switch (event->type) {
161 case CEV_KEY:
162 event->ev.key.type = IPC_GET_ARG2(*call);
163 event->ev.key.key = IPC_GET_ARG3(*call);
164 event->ev.key.mods = IPC_GET_ARG4(*call);
165 event->ev.key.c = IPC_GET_ARG5(*call);
166 break;
167 case CEV_POS:
168 event->ev.pos.pos_id = IPC_GET_ARG2(*call) >> 16;
169 event->ev.pos.type = IPC_GET_ARG2(*call) & 0xffff;
170 event->ev.pos.btn_num = IPC_GET_ARG3(*call);
171 event->ev.pos.hpos = IPC_GET_ARG4(*call);
172 event->ev.pos.vpos = IPC_GET_ARG5(*call);
173 break;
174 default:
175 return EIO;
176 }
177
178 return EOK;
179}
180
[07b7c48]181bool console_get_event(console_ctrl_t *ctrl, cons_event_t *event)
[2595dab]182{
[79ae36dd]183 if (ctrl->input_aid == 0) {
[902f0906]184 ipc_call_t result;
[a35b458]185
[79ae36dd]186 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
[902f0906]187 aid_t aid = async_send_0(exch, CONSOLE_GET_EVENT, &result);
[79ae36dd]188 async_exchange_end(exch);
[a35b458]189
[b7fd2a0]190 errno_t rc;
[902f0906]191 async_wait_for(aid, &rc);
[a35b458]192
[79ae36dd]193 if (rc != EOK) {
194 errno = rc;
195 return false;
196 }
[a35b458]197
[902f0906]198 rc = console_ev_decode(&result, event);
199 if (rc != EOK) {
200 errno = rc;
201 return false;
202 }
[79ae36dd]203 } else {
[b7fd2a0]204 errno_t retval;
[79ae36dd]205 async_wait_for(ctrl->input_aid, &retval);
[a35b458]206
[79ae36dd]207 ctrl->input_aid = 0;
[a35b458]208
[79ae36dd]209 if (retval != EOK) {
[25a179e]210 errno = retval;
[79ae36dd]211 return false;
212 }
[a35b458]213
[b7fd2a0]214 errno_t rc = console_ev_decode(&ctrl->input_call, event);
[902f0906]215 if (rc != EOK) {
216 errno = rc;
217 return false;
218 }
[79ae36dd]219 }
[a35b458]220
[79ae36dd]221 return true;
[2595dab]222}
223
[07b7c48]224bool console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
[bd41ac52]225 usec_t *timeout)
[2595dab]226{
[bd41ac52]227 struct timespec t0;
228 getuptime(&t0);
[a35b458]229
[79ae36dd]230 if (ctrl->input_aid == 0) {
231 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
232 ctrl->input_aid = async_send_0(exch, CONSOLE_GET_EVENT,
233 &ctrl->input_call);
234 async_exchange_end(exch);
235 }
[a35b458]236
[b7fd2a0]237 errno_t retval;
238 errno_t rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
[79ae36dd]239 if (rc != EOK) {
240 *timeout = 0;
241 errno = rc;
242 return false;
243 }
[a35b458]244
[79ae36dd]245 ctrl->input_aid = 0;
[a35b458]246
[79ae36dd]247 if (retval != EOK) {
[25a179e]248 errno = retval;
[2595dab]249 return false;
[79ae36dd]250 }
[a35b458]251
[902f0906]252 rc = console_ev_decode(&ctrl->input_call, event);
253 if (rc != EOK) {
254 errno = rc;
255 return false;
256 }
[a35b458]257
[79ae36dd]258 /* Update timeout */
[bd41ac52]259 struct timespec t1;
260 getuptime(&t1);
261 *timeout -= NSEC2USEC(ts_sub_diff(&t1, &t0));
[a35b458]262
[2595dab]263 return true;
[1c03c17]264}
265
[a46da63]266/** @}
[b2951e2]267 */
Note: See TracBrowser for help on using the repository browser.