Rev 4164 | Rev 4226 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4164 | Rev 4168 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Josef Cejka |
2 | * Copyright (c) 2006 Josef Cejka |
3 | * Copyright (c) 2006 Jakub Vana |
3 | * Copyright (c) 2006 Jakub Vana |
4 | * Copyright (c) 2008 Jiri Svoboda |
4 | * Copyright (c) 2008 Jiri Svoboda |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
9 | * are met: |
10 | * |
10 | * |
11 | * - Redistributions of source code must retain the above copyright |
11 | * - Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * - Redistributions in binary form must reproduce the above copyright |
13 | * - Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
17 | * derived from this software without specific prior written permission. |
18 | * |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | /** @addtogroup libc |
31 | /** @addtogroup libc |
32 | * @{ |
32 | * @{ |
33 | */ |
33 | */ |
34 | /** @file |
34 | /** @file |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <async.h> |
37 | #include <async.h> |
38 | #include <io/stream.h> |
38 | #include <io/stream.h> |
39 | #include <ipc/console.h> |
39 | #include <ipc/console.h> |
40 | #include <ipc/services.h> |
40 | #include <ipc/services.h> |
41 | #include <errno.h> |
41 | #include <errno.h> |
42 | #include <string.h> |
42 | #include <string.h> |
43 | #include <console.h> |
43 | #include <console.h> |
44 | 44 | ||
45 | static int console_phone = -1; |
45 | static int console_phone = -1; |
46 | 46 | ||
47 | /** Size of cbuffer. */ |
47 | /** Size of cbuffer. */ |
48 | #define CBUFFER_SIZE 256 |
48 | #define CBUFFER_SIZE 256 |
49 | 49 | ||
50 | /** Buffer for writing characters to the console. */ |
50 | /** Buffer for writing characters to the console. */ |
51 | static char cbuffer[CBUFFER_SIZE]; |
51 | static char cbuffer[CBUFFER_SIZE]; |
52 | 52 | ||
53 | /** Pointer to end of cbuffer. */ |
53 | /** Pointer to end of cbuffer. */ |
54 | static char *cbuffer_end = cbuffer + CBUFFER_SIZE; |
54 | static char *cbuffer_end = cbuffer + CBUFFER_SIZE; |
55 | 55 | ||
56 | /** Pointer to first available field in cbuffer. */ |
56 | /** Pointer to first available field in cbuffer. */ |
57 | static char *cbp = cbuffer; |
57 | static char *cbp = cbuffer; |
58 | 58 | ||
59 | static ssize_t cons_write(const char *buf, size_t nbyte); |
59 | static ssize_t cons_write(const char *buf, size_t nbyte); |
60 | static void cons_putchar(int c); |
60 | static void cons_putchar(int c); |
61 | 61 | ||
62 | static void cbuffer_flush(void); |
62 | static void cbuffer_flush(void); |
63 | static void cbuffer_drain(void); |
63 | static void cbuffer_drain(void); |
64 | static void cbuffer_putc(int c); |
64 | static void cbuffer_putc(int c); |
65 | 65 | ||
66 | 66 | ||
67 | void console_open(bool blocking) |
67 | void console_open(bool blocking) |
68 | { |
68 | { |
69 | if (console_phone < 0) { |
69 | if (console_phone < 0) { |
70 | int phone; |
70 | int phone; |
71 | if (blocking) { |
71 | if (blocking) { |
72 | phone = ipc_connect_me_to_blocking(PHONE_NS, |
72 | phone = ipc_connect_me_to_blocking(PHONE_NS, |
73 | SERVICE_CONSOLE, 0, 0); |
73 | SERVICE_CONSOLE, 0, 0); |
74 | } else { |
74 | } else { |
75 | phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0, |
75 | phone = ipc_connect_me_to(PHONE_NS, SERVICE_CONSOLE, 0, |
76 | 0); |
76 | 0); |
77 | } |
77 | } |
78 | if (phone >= 0) |
78 | if (phone >= 0) |
79 | console_phone = phone; |
79 | console_phone = phone; |
80 | } |
80 | } |
81 | } |
81 | } |
82 | 82 | ||
83 | void console_close(void) |
83 | void console_close(void) |
84 | { |
84 | { |
85 | if (console_phone >= 0) { |
85 | if (console_phone >= 0) { |
86 | if (ipc_hangup(console_phone) == 0) { |
86 | if (ipc_hangup(console_phone) == 0) { |
87 | console_phone = -1; |
87 | console_phone = -1; |
88 | } |
88 | } |
89 | } |
89 | } |
90 | } |
90 | } |
91 | 91 | ||
92 | int console_phone_get(bool blocking) |
92 | int console_phone_get(bool blocking) |
93 | { |
93 | { |
94 | if (console_phone < 0) |
94 | if (console_phone < 0) |
95 | console_open(blocking); |
95 | console_open(blocking); |
96 | 96 | ||
97 | return console_phone; |
97 | return console_phone; |
98 | } |
98 | } |
99 | 99 | ||
100 | void console_wait(void) |
100 | void console_wait(void) |
101 | { |
101 | { |
102 | while (console_phone < 0) |
102 | while (console_phone < 0) |
103 | console_open(true); |
103 | console_open(true); |
104 | } |
104 | } |
105 | 105 | ||
106 | void console_clear(void) |
106 | void console_clear(void) |
107 | { |
107 | { |
108 | int cons_phone = console_phone_get(true); |
108 | int cons_phone = console_phone_get(true); |
109 | 109 | ||
110 | cbuffer_drain(); |
110 | cbuffer_drain(); |
111 | async_msg_0(cons_phone, CONSOLE_CLEAR); |
111 | async_msg_0(cons_phone, CONSOLE_CLEAR); |
112 | } |
112 | } |
113 | 113 | ||
114 | void console_goto(int row, int col) |
114 | void console_goto(int row, int col) |
115 | { |
115 | { |
116 | int cons_phone = console_phone_get(true); |
116 | int cons_phone = console_phone_get(true); |
117 | 117 | ||
118 | cbuffer_flush(); |
118 | cbuffer_flush(); |
119 | async_msg_2(cons_phone, CONSOLE_GOTO, row, col); |
119 | async_msg_2(cons_phone, CONSOLE_GOTO, row, col); |
120 | } |
120 | } |
121 | 121 | ||
122 | void console_putchar(int c) |
122 | void console_putchar(int c) |
123 | { |
123 | { |
124 | cbuffer_putc(c); |
124 | cbuffer_putc(c); |
125 | } |
125 | } |
126 | 126 | ||
127 | /** Write all data from output buffer to the console. */ |
127 | /** Write all data from output buffer to the console. */ |
128 | static void cbuffer_flush(void) |
128 | static void cbuffer_flush(void) |
129 | { |
129 | { |
130 | int rc; |
130 | int rc; |
131 | int len; |
131 | int len; |
132 | 132 | ||
133 | len = cbp - cbuffer; |
133 | len = cbp - cbuffer; |
134 | 134 | ||
135 | while (len > 0) { |
135 | while (len > 0) { |
136 | rc = cons_write(cbuffer, cbp - cbuffer); |
136 | rc = cons_write(cbuffer, cbp - cbuffer); |
137 | if (rc < 0) |
137 | if (rc < 0) |
138 | return; |
138 | return; |
139 | 139 | ||
140 | len -= rc; |
140 | len -= rc; |
141 | } |
141 | } |
142 | 142 | ||
143 | cbp = cbuffer; |
143 | cbp = cbuffer; |
144 | } |
144 | } |
145 | 145 | ||
146 | /** Drop all data in console output buffer. */ |
146 | /** Drop all data in console output buffer. */ |
147 | static void cbuffer_drain(void) |
147 | static void cbuffer_drain(void) |
148 | { |
148 | { |
149 | cbp = cbuffer; |
149 | cbp = cbuffer; |
150 | } |
150 | } |
151 | 151 | ||
152 | /** Write one character to the output buffer. */ |
152 | /** Write one character to the output buffer. */ |
153 | static inline void cbuffer_putc(int c) |
153 | static inline void cbuffer_putc(int c) |
154 | { |
154 | { |
155 | if (cbp == cbuffer_end) |
155 | if (cbp == cbuffer_end) |
156 | cbuffer_flush(); |
156 | cbuffer_flush(); |
157 | 157 | ||
158 | *cbp++ = c; |
158 | *cbp++ = c; |
159 | 159 | ||
160 | if (c == '\n') |
160 | if (c == '\n') |
161 | cbuffer_flush(); |
161 | cbuffer_flush(); |
162 | } |
162 | } |
163 | 163 | ||
164 | /** Write one character to the console via IPC. */ |
164 | /** Write one character to the console via IPC. */ |
165 | static void cons_putchar(int c) |
165 | static void cons_putchar(int c) |
166 | { |
166 | { |
167 | int cons_phone = console_phone_get(true); |
167 | int cons_phone = console_phone_get(true); |
168 | async_msg_1(cons_phone, CONSOLE_PUTCHAR, c); |
168 | async_msg_1(cons_phone, CONSOLE_PUTCHAR, c); |
169 | } |
169 | } |
170 | 170 | ||
171 | /** Write characters to the console via IPC. */ |
171 | /** Write characters to the console via IPC. */ |
172 | static ssize_t cons_write(const char *buf, size_t nbyte) |
172 | static ssize_t cons_write(const char *buf, size_t nbyte) |
173 | { |
173 | { |
174 | int cons_phone = console_phone_get(true); |
174 | int cons_phone = console_phone_get(true); |
175 | ipcarg_t rc; |
175 | ipcarg_t rc; |
176 | ipc_call_t answer; |
176 | ipc_call_t answer; |
177 | aid_t req; |
177 | aid_t req; |
178 | 178 | ||
179 | async_serialize_start(); |
179 | async_serialize_start(); |
180 | 180 | ||
181 | req = async_send_0(cons_phone, CONSOLE_WRITE, &answer); |
181 | req = async_send_0(cons_phone, CONSOLE_WRITE, &answer); |
182 | rc = ipc_data_write_start(cons_phone, (void *) buf, nbyte); |
182 | rc = ipc_data_write_start(cons_phone, (void *) buf, nbyte); |
183 | 183 | ||
184 | if (rc != EOK) { |
184 | if (rc != EOK) { |
185 | async_wait_for(req, NULL); |
185 | async_wait_for(req, NULL); |
186 | async_serialize_end(); |
186 | async_serialize_end(); |
187 | return (ssize_t) rc; |
187 | return (ssize_t) rc; |
188 | } |
188 | } |
189 | 189 | ||
190 | async_wait_for(req, &rc); |
190 | async_wait_for(req, &rc); |
191 | async_serialize_end(); |
191 | async_serialize_end(); |
192 | 192 | ||
193 | if (rc == EOK) |
193 | if (rc == EOK) |
194 | return (ssize_t) IPC_GET_ARG1(answer); |
194 | return (ssize_t) IPC_GET_ARG1(answer); |
195 | else |
195 | else |
196 | return -1; |
196 | return -1; |
197 | } |
197 | } |
198 | 198 | ||
199 | /** Write characters to the console. */ |
199 | /** Write characters to the console. */ |
200 | ssize_t console_write(const char *buf, size_t nbyte) |
200 | ssize_t console_write(const char *buf, size_t nbyte) |
201 | { |
201 | { |
202 | size_t left; |
202 | size_t left; |
203 | 203 | ||
204 | left = nbyte; |
204 | left = nbyte; |
205 | 205 | ||
206 | while (left > 0) { |
206 | while (left > 0) { |
207 | cbuffer_putc(*buf++); |
207 | cbuffer_putc(*buf++); |
208 | --left; |
208 | --left; |
209 | } |
209 | } |
210 | 210 | ||
211 | return nbyte; |
211 | return nbyte; |
212 | } |
212 | } |
213 | 213 | ||
214 | /** Write a NULL-terminated string to the console. */ |
214 | /** Write a NULL-terminated string to the console. */ |
215 | void console_putstr(const char *s) |
215 | void console_putstr(const char *s) |
216 | { |
216 | { |
217 | size_t len; |
217 | size_t len; |
218 | ssize_t rc; |
218 | ssize_t rc; |
219 | 219 | ||
220 | len = strlen(s); |
220 | len = strlen(s); |
221 | while (len > 0) { |
221 | while (len > 0) { |
222 | rc = console_write(s, len); |
222 | rc = console_write(s, len); |
223 | if (rc < 0) |
223 | if (rc < 0) |
224 | return; /* Error */ |
224 | return; /* Error */ |
225 | s += rc; |
225 | s += rc; |
226 | len -= rc; |
226 | len -= rc; |
227 | } |
227 | } |
228 | } |
228 | } |
229 | 229 | ||
230 | /** Flush all output to the console. */ |
230 | /** Flush all output to the console. */ |
231 | void console_flush(void) |
231 | void console_flush(void) |
232 | { |
232 | { |
233 | int cons_phone = console_phone_get(false); |
233 | int cons_phone = console_phone_get(false); |
234 | 234 | ||
235 | cbuffer_flush(); |
235 | cbuffer_flush(); |
236 | async_msg_0(cons_phone, CONSOLE_FLUSH); |
236 | async_msg_0(cons_phone, CONSOLE_FLUSH); |
237 | } |
237 | } |
238 | 238 | ||
239 | int console_get_size(int *rows, int *cols) |
239 | int console_get_size(int *rows, int *cols) |
240 | { |
240 | { |
241 | int cons_phone = console_phone_get(true); |
241 | int cons_phone = console_phone_get(true); |
242 | ipcarg_t r, c; |
242 | ipcarg_t r, c; |
243 | int rc; |
243 | int rc; |
244 | 244 | ||
245 | rc = async_req_0_2(cons_phone, CONSOLE_GETSIZE, &r, &c); |
245 | rc = async_req_0_2(cons_phone, CONSOLE_GETSIZE, &r, &c); |
246 | 246 | ||
247 | *rows = (int) r; |
247 | *rows = (int) r; |
248 | *cols = (int) c; |
248 | *cols = (int) c; |
249 | 249 | ||
250 | return rc; |
250 | return rc; |
251 | } |
251 | } |
252 | 252 | ||
253 | void console_set_style(int style) |
253 | void console_set_style(int style) |
254 | { |
254 | { |
255 | int cons_phone = console_phone_get(true); |
255 | int cons_phone = console_phone_get(true); |
256 | 256 | ||
257 | cbuffer_flush(); |
257 | cbuffer_flush(); |
258 | async_msg_1(cons_phone, CONSOLE_SET_STYLE, style); |
258 | async_msg_1(cons_phone, CONSOLE_SET_STYLE, style); |
259 | } |
259 | } |
260 | 260 | ||
261 | void console_set_color(int fg_color, int bg_color, int flags) |
261 | void console_set_color(int fg_color, int bg_color, int flags) |
262 | { |
262 | { |
263 | int cons_phone = console_phone_get(true); |
263 | int cons_phone = console_phone_get(true); |
264 | 264 | ||
265 | cbuffer_flush(); |
265 | cbuffer_flush(); |
266 | async_msg_3(cons_phone, CONSOLE_SET_COLOR, fg_color, bg_color, flags); |
266 | async_msg_3(cons_phone, CONSOLE_SET_COLOR, fg_color, bg_color, flags); |
267 | } |
267 | } |
268 | 268 | ||
269 | void console_set_rgb_color(int fg_color, int bg_color) |
269 | void console_set_rgb_color(int fg_color, int bg_color) |
270 | { |
270 | { |
271 | int cons_phone = console_phone_get(true); |
271 | int cons_phone = console_phone_get(true); |
272 | 272 | ||
273 | cbuffer_flush(); |
273 | cbuffer_flush(); |
274 | async_msg_2(cons_phone, CONSOLE_SET_RGB_COLOR, fg_color, bg_color); |
274 | async_msg_2(cons_phone, CONSOLE_SET_RGB_COLOR, fg_color, bg_color); |
275 | } |
275 | } |
276 | 276 | ||
277 | void console_cursor_visibility(int show) |
277 | void console_cursor_visibility(int show) |
278 | { |
278 | { |
279 | int cons_phone = console_phone_get(true); |
279 | int cons_phone = console_phone_get(true); |
280 | 280 | ||
281 | cbuffer_flush(); |
281 | cbuffer_flush(); |
282 | async_msg_1(cons_phone, CONSOLE_CURSOR_VISIBILITY, show != 0); |
282 | async_msg_1(cons_phone, CONSOLE_CURSOR_VISIBILITY, show != 0); |
283 | } |
283 | } |
284 | 284 | ||
- | 285 | void console_kcon_enable(void) |
|
- | 286 | { |
|
- | 287 | int cons_phone = console_phone_get(true); |
|
- | 288 | ||
- | 289 | cbuffer_flush(); |
|
- | 290 | async_msg_0(cons_phone, CONSOLE_KCON_ENABLE); |
|
- | 291 | } |
|
- | 292 | ||
285 | /** @} |
293 | /** @} |
286 | */ |
294 | */ |
287 | 295 |