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

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

Make gettimeofday() return the actual microseconds.

Enhance struct tm to also have a field to hold microseconds and make
sure that this information propagates from the RTC driver.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * Copyright (c) 2006 Josef Cejka
3 * Copyright (c) 2006 Jakub Vana
4 * Copyright (c) 2008 Jiri Svoboda
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 <libc.h>
38#include <async.h>
39#include <errno.h>
40#include <malloc.h>
41#include <vfs/vfs_sess.h>
42#include <io/console.h>
43#include <ipc/console.h>
44
45console_ctrl_t *console_init(FILE *ifile, FILE *ofile)
46{
47 console_ctrl_t *ctrl = malloc(sizeof(console_ctrl_t));
48 if (!ctrl)
49 return NULL;
50
51 ctrl->input_sess = fsession(EXCHANGE_SERIALIZE, ifile);
52 if (!ctrl->input_sess) {
53 free(ctrl);
54 return NULL;
55 }
56
57 ctrl->output_sess = fsession(EXCHANGE_SERIALIZE, ofile);
58 if (!ctrl->output_sess) {
59 free(ctrl);
60 return NULL;
61 }
62
63 ctrl->input = ifile;
64 ctrl->output = ofile;
65 ctrl->input_aid = 0;
66
67 return ctrl;
68}
69
70void console_done(console_ctrl_t *ctrl)
71{
72 free(ctrl);
73}
74
75bool console_kcon(void)
76{
77 return __SYSCALL0(SYS_DEBUG_CONSOLE);
78}
79
80void console_flush(console_ctrl_t *ctrl)
81{
82 fflush(ctrl->output);
83}
84
85void console_clear(console_ctrl_t *ctrl)
86{
87 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
88 async_req_0_0(exch, CONSOLE_CLEAR);
89 async_exchange_end(exch);
90}
91
92int console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
93{
94 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
95 int rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
96 async_exchange_end(exch);
97
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);
104 async_req_1_0(exch, CONSOLE_SET_STYLE, style);
105 async_exchange_end(exch);
106}
107
108void console_set_color(console_ctrl_t *ctrl, uint8_t bgcolor, uint8_t fgcolor,
109 uint8_t flags)
110{
111 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
112 async_req_3_0(exch, CONSOLE_SET_COLOR, bgcolor, fgcolor, flags);
113 async_exchange_end(exch);
114}
115
116void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t bgcolor,
117 uint32_t fgcolor)
118{
119 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
120 async_req_2_0(exch, CONSOLE_SET_RGB_COLOR, bgcolor, fgcolor);
121 async_exchange_end(exch);
122}
123
124void console_cursor_visibility(console_ctrl_t *ctrl, bool show)
125{
126 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
127 async_req_1_0(exch, CONSOLE_SET_CURSOR_VISIBILITY, (show != false));
128 async_exchange_end(exch);
129}
130
131int console_get_color_cap(console_ctrl_t *ctrl, sysarg_t *ccap)
132{
133 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
134 int rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
135 async_exchange_end(exch);
136
137 return rc;
138}
139
140int console_get_pos(console_ctrl_t *ctrl, sysarg_t *col, sysarg_t *row)
141{
142 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
143 int rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
144 async_exchange_end(exch);
145
146 return rc;
147}
148
149void console_set_pos(console_ctrl_t *ctrl, sysarg_t col, sysarg_t row)
150{
151 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
152 async_req_2_0(exch, CONSOLE_SET_POS, col, row);
153 async_exchange_end(exch);
154}
155
156static int console_ev_decode(ipc_call_t *call, cons_event_t *event)
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
181bool console_get_event(console_ctrl_t *ctrl, cons_event_t *event)
182{
183 if (ctrl->input_aid == 0) {
184 ipc_call_t result;
185
186 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
187 aid_t aid = async_send_0(exch, CONSOLE_GET_EVENT, &result);
188 async_exchange_end(exch);
189
190 sysarg_t rc;
191 async_wait_for(aid, &rc);
192
193 if (rc != EOK) {
194 errno = rc;
195 return false;
196 }
197
198 rc = console_ev_decode(&result, event);
199 if (rc != EOK) {
200 errno = rc;
201 return false;
202 }
203 } else {
204 sysarg_t retval;
205 async_wait_for(ctrl->input_aid, &retval);
206
207 ctrl->input_aid = 0;
208
209 if (retval != EOK) {
210 errno = (int) retval;
211 return false;
212 }
213
214 int rc = console_ev_decode(&ctrl->input_call, event);
215 if (rc != EOK) {
216 errno = rc;
217 return false;
218 }
219 }
220
221 return true;
222}
223
224bool console_get_event_timeout(console_ctrl_t *ctrl, cons_event_t *event,
225 suseconds_t *timeout)
226{
227 struct timeval t0;
228 gettimeofday(&t0, NULL);
229
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 }
236
237 sysarg_t retval;
238 int rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
239 if (rc != EOK) {
240 *timeout = 0;
241 errno = rc;
242 return false;
243 }
244
245 ctrl->input_aid = 0;
246
247 if (retval != EOK) {
248 errno = (int) retval;
249 return false;
250 }
251
252 rc = console_ev_decode(&ctrl->input_call, event);
253 if (rc != EOK) {
254 errno = rc;
255 return false;
256 }
257
258 /* Update timeout */
259 struct timeval t1;
260 gettimeofday(&t1, NULL);
261 *timeout -= tv_sub_diff(&t1, &t0);
262
263 return true;
264}
265
266/** @}
267 */
Note: See TracBrowser for help on using the repository browser.