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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 79ae36dd was 79ae36dd, checked in by Martin Decky <martin@…>, 14 years ago

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 6.1 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 <stdio.h>
41#include <malloc.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 = fsession(EXCHANGE_SERIALIZE, ifile);
53 if (!ctrl->input_sess) {
54 free(ctrl);
55 return NULL;
56 }
57
58 ctrl->output_sess = fsession(EXCHANGE_SERIALIZE, ofile);
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#if 0
79 return __SYSCALL0(SYS_DEBUG_ACTIVATE_CONSOLE);
80#endif
81
82 return false;
83}
84
85void console_flush(console_ctrl_t *ctrl)
86{
87 fflush(ctrl->output);
88}
89
90void console_clear(console_ctrl_t *ctrl)
91{
92 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
93 async_msg_0(exch, CONSOLE_CLEAR);
94 async_exchange_end(exch);
95}
96
97int console_get_size(console_ctrl_t *ctrl, sysarg_t *cols, sysarg_t *rows)
98{
99 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
100 int rc = async_req_0_2(exch, CONSOLE_GET_SIZE, cols, rows);
101 async_exchange_end(exch);
102
103 return rc;
104}
105
106void console_set_style(console_ctrl_t *ctrl, uint8_t style)
107{
108 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
109 async_msg_1(exch, CONSOLE_SET_STYLE, style);
110 async_exchange_end(exch);
111}
112
113void console_set_color(console_ctrl_t *ctrl, uint8_t fg_color, uint8_t bg_color,
114 uint8_t flags)
115{
116 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
117 async_msg_3(exch, CONSOLE_SET_COLOR, fg_color, bg_color, flags);
118 async_exchange_end(exch);
119}
120
121void console_set_rgb_color(console_ctrl_t *ctrl, uint32_t fg_color,
122 uint32_t bg_color)
123{
124 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
125 async_msg_2(exch, CONSOLE_SET_RGB_COLOR, fg_color, bg_color);
126 async_exchange_end(exch);
127}
128
129void console_cursor_visibility(console_ctrl_t *ctrl, bool show)
130{
131 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
132 async_msg_1(exch, CONSOLE_CURSOR_VISIBILITY, (show != false));
133 async_exchange_end(exch);
134}
135
136int console_get_color_cap(console_ctrl_t *ctrl, sysarg_t *ccap)
137{
138 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
139 int rc = async_req_0_1(exch, CONSOLE_GET_COLOR_CAP, ccap);
140 async_exchange_end(exch);
141
142 return rc;
143}
144
145int console_get_pos(console_ctrl_t *ctrl, sysarg_t *col, sysarg_t *row)
146{
147 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
148 int rc = async_req_0_2(exch, CONSOLE_GET_POS, col, row);
149 async_exchange_end(exch);
150
151 return rc;
152}
153
154void console_set_pos(console_ctrl_t *ctrl, sysarg_t col, sysarg_t row)
155{
156 async_exch_t *exch = async_exchange_begin(ctrl->output_sess);
157 async_msg_2(exch, CONSOLE_GOTO, col, row);
158 async_exchange_end(exch);
159}
160
161bool console_get_kbd_event(console_ctrl_t *ctrl, kbd_event_t *event)
162{
163 if (ctrl->input_aid == 0) {
164 sysarg_t type;
165 sysarg_t key;
166 sysarg_t mods;
167 sysarg_t c;
168
169 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
170 int rc = async_req_0_4(exch, CONSOLE_GET_EVENT, &type, &key, &mods, &c);
171 async_exchange_end(exch);
172
173 if (rc != EOK) {
174 errno = rc;
175 return false;
176 }
177
178 event->type = type;
179 event->key = key;
180 event->mods = mods;
181 event->c = c;
182 } else {
183 sysarg_t retval;
184 async_wait_for(ctrl->input_aid, &retval);
185
186 ctrl->input_aid = 0;
187
188 if (retval != EOK) {
189 errno = (int) retval;
190 return false;
191 }
192
193 event->type = IPC_GET_ARG1(ctrl->input_call);
194 event->key = IPC_GET_ARG2(ctrl->input_call);
195 event->mods = IPC_GET_ARG3(ctrl->input_call);
196 event->c = IPC_GET_ARG4(ctrl->input_call);
197 }
198
199 return true;
200}
201
202bool console_get_kbd_event_timeout(console_ctrl_t *ctrl, kbd_event_t *event,
203 suseconds_t *timeout)
204{
205 struct timeval t0;
206 gettimeofday(&t0, NULL);
207
208 if (ctrl->input_aid == 0) {
209 async_exch_t *exch = async_exchange_begin(ctrl->input_sess);
210 ctrl->input_aid = async_send_0(exch, CONSOLE_GET_EVENT,
211 &ctrl->input_call);
212 async_exchange_end(exch);
213 }
214
215 sysarg_t retval;
216 int rc = async_wait_timeout(ctrl->input_aid, &retval, *timeout);
217 if (rc != EOK) {
218 *timeout = 0;
219 errno = rc;
220 return false;
221 }
222
223 ctrl->input_aid = 0;
224
225 if (retval != EOK) {
226 errno = (int) retval;
227 return false;
228 }
229
230 event->type = IPC_GET_ARG1(ctrl->input_call);
231 event->key = IPC_GET_ARG2(ctrl->input_call);
232 event->mods = IPC_GET_ARG3(ctrl->input_call);
233 event->c = IPC_GET_ARG4(ctrl->input_call);
234
235 /* Update timeout */
236 struct timeval t1;
237 gettimeofday(&t1, NULL);
238 *timeout -= tv_sub(&t1, &t0);
239
240 return true;
241}
242
243/** @}
244 */
Note: See TracBrowser for help on using the repository browser.