Rev 3905 | Rev 3924 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3905 | Rev 3923 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Josef Cejka |
2 | * Copyright (c) 2006 Josef Cejka |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup console |
29 | /** @addtogroup console |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <libc.h> |
35 | #include <libc.h> |
36 | #include <fb.h> |
36 | #include <fb.h> |
37 | #include <ipc/ipc.h> |
37 | #include <ipc/ipc.h> |
38 | #include <keys.h> |
38 | #include <keys.h> |
- | 39 | #include <kbd/keycode.h> |
|
39 | #include <ipc/fb.h> |
40 | #include <ipc/fb.h> |
40 | #include <ipc/services.h> |
41 | #include <ipc/services.h> |
41 | #include <errno.h> |
42 | #include <errno.h> |
42 | #include <key_buffer.h> |
43 | #include <key_buffer.h> |
43 | #include <ipc/console.h> |
44 | #include <ipc/console.h> |
44 | #include <unistd.h> |
45 | #include <unistd.h> |
45 | #include <async.h> |
46 | #include <async.h> |
46 | #include <libadt/fifo.h> |
47 | #include <libadt/fifo.h> |
47 | #include <screenbuffer.h> |
48 | #include <screenbuffer.h> |
48 | #include <sys/mman.h> |
49 | #include <sys/mman.h> |
49 | #include <stdio.h> |
50 | #include <stdio.h> |
50 | #include <sysinfo.h> |
51 | #include <sysinfo.h> |
51 | 52 | ||
52 | #include "console.h" |
53 | #include "console.h" |
53 | #include "gcons.h" |
54 | #include "gcons.h" |
54 | 55 | ||
55 | #define MAX_KEYREQUESTS_BUFFERED 32 |
56 | #define MAX_KEYREQUESTS_BUFFERED 32 |
56 | 57 | ||
57 | #define NAME "console" |
58 | #define NAME "console" |
58 | 59 | ||
59 | /** Index of currently used virtual console. |
60 | /** Index of currently used virtual console. |
60 | */ |
61 | */ |
61 | int active_console = 0; |
62 | int active_console = 0; |
62 | int prev_console = 0; |
63 | int prev_console = 0; |
63 | 64 | ||
64 | /** Information about framebuffer |
65 | /** Information about framebuffer |
65 | */ |
66 | */ |
66 | struct { |
67 | struct { |
67 | int phone; /**< Framebuffer phone */ |
68 | int phone; /**< Framebuffer phone */ |
68 | ipcarg_t rows; /**< Framebuffer rows */ |
69 | ipcarg_t rows; /**< Framebuffer rows */ |
69 | ipcarg_t cols; /**< Framebuffer columns */ |
70 | ipcarg_t cols; /**< Framebuffer columns */ |
70 | } fb_info; |
71 | } fb_info; |
71 | 72 | ||
72 | typedef struct { |
73 | typedef struct { |
73 | keybuffer_t keybuffer; /**< Buffer for incoming keys. */ |
74 | keybuffer_t keybuffer; /**< Buffer for incoming keys. */ |
74 | /** Buffer for unsatisfied request for keys. */ |
75 | /** Buffer for unsatisfied request for keys. */ |
75 | FIFO_CREATE_STATIC(keyrequests, ipc_callid_t, |
76 | FIFO_CREATE_STATIC(keyrequests, ipc_callid_t, |
76 | MAX_KEYREQUESTS_BUFFERED); |
77 | MAX_KEYREQUESTS_BUFFERED); |
77 | int keyrequest_counter; /**< Number of requests in buffer. */ |
78 | int keyrequest_counter; /**< Number of requests in buffer. */ |
78 | int client_phone; /**< Phone to connected client. */ |
79 | int client_phone; /**< Phone to connected client. */ |
79 | int used; /**< 1 if this virtual console is |
80 | int used; /**< 1 if this virtual console is |
80 | * connected to some client.*/ |
81 | * connected to some client.*/ |
81 | screenbuffer_t screenbuffer; /**< Screenbuffer for saving screen |
82 | screenbuffer_t screenbuffer; /**< Screenbuffer for saving screen |
82 | * contents and related settings. */ |
83 | * contents and related settings. */ |
83 | } connection_t; |
84 | } connection_t; |
84 | 85 | ||
85 | static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual |
86 | static connection_t connections[CONSOLE_COUNT]; /**< Array of data for virtual |
86 | * consoles */ |
87 | * consoles */ |
87 | static keyfield_t *interbuffer = NULL; /**< Pointer to memory shared |
88 | static keyfield_t *interbuffer = NULL; /**< Pointer to memory shared |
88 | * with framebufer used for |
89 | * with framebufer used for |
89 | * faster virtual console |
90 | * faster virtual console |
90 | * switching */ |
91 | * switching */ |
91 | 92 | ||
92 | 93 | ||
93 | /** Find unused virtual console. |
94 | /** Find unused virtual console. |
94 | * |
95 | * |
95 | */ |
96 | */ |
96 | static int find_free_connection(void) |
97 | static int find_free_connection(void) |
97 | { |
98 | { |
98 | int i; |
99 | int i; |
99 | 100 | ||
100 | for (i = 0; i < CONSOLE_COUNT; i++) { |
101 | for (i = 0; i < CONSOLE_COUNT; i++) { |
101 | if (!connections[i].used) |
102 | if (!connections[i].used) |
102 | return i; |
103 | return i; |
103 | } |
104 | } |
104 | return -1; |
105 | return -1; |
105 | } |
106 | } |
106 | 107 | ||
107 | static void clrscr(void) |
108 | static void clrscr(void) |
108 | { |
109 | { |
109 | async_msg_0(fb_info.phone, FB_CLEAR); |
110 | async_msg_0(fb_info.phone, FB_CLEAR); |
110 | } |
111 | } |
111 | 112 | ||
112 | static void curs_visibility(bool visible) |
113 | static void curs_visibility(bool visible) |
113 | { |
114 | { |
114 | async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible); |
115 | async_msg_1(fb_info.phone, FB_CURSOR_VISIBILITY, visible); |
115 | } |
116 | } |
116 | 117 | ||
117 | static void curs_hide_sync(void) |
118 | static void curs_hide_sync(void) |
118 | { |
119 | { |
119 | ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false); |
120 | ipc_call_sync_1_0(fb_info.phone, FB_CURSOR_VISIBILITY, false); |
120 | } |
121 | } |
121 | 122 | ||
122 | static void curs_goto(int row, int col) |
123 | static void curs_goto(int row, int col) |
123 | { |
124 | { |
124 | async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col); |
125 | async_msg_2(fb_info.phone, FB_CURSOR_GOTO, row, col); |
125 | } |
126 | } |
126 | 127 | ||
127 | static void set_style(int style) |
128 | static void set_style(int style) |
128 | { |
129 | { |
129 | async_msg_1(fb_info.phone, FB_SET_STYLE, style); |
130 | async_msg_1(fb_info.phone, FB_SET_STYLE, style); |
130 | } |
131 | } |
131 | 132 | ||
132 | static void set_color(int fgcolor, int bgcolor, int flags) |
133 | static void set_color(int fgcolor, int bgcolor, int flags) |
133 | { |
134 | { |
134 | async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags); |
135 | async_msg_3(fb_info.phone, FB_SET_COLOR, fgcolor, bgcolor, flags); |
135 | } |
136 | } |
136 | 137 | ||
137 | static void set_rgb_color(int fgcolor, int bgcolor) |
138 | static void set_rgb_color(int fgcolor, int bgcolor) |
138 | { |
139 | { |
139 | async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor); |
140 | async_msg_2(fb_info.phone, FB_SET_RGB_COLOR, fgcolor, bgcolor); |
140 | } |
141 | } |
141 | 142 | ||
142 | static void set_attrs(attrs_t *attrs) |
143 | static void set_attrs(attrs_t *attrs) |
143 | { |
144 | { |
144 | switch (attrs->t) { |
145 | switch (attrs->t) { |
145 | case at_style: |
146 | case at_style: |
146 | set_style(attrs->a.s.style); |
147 | set_style(attrs->a.s.style); |
147 | break; |
148 | break; |
148 | 149 | ||
149 | case at_idx: |
150 | case at_idx: |
150 | set_color(attrs->a.i.fg_color, attrs->a.i.bg_color, |
151 | set_color(attrs->a.i.fg_color, attrs->a.i.bg_color, |
151 | attrs->a.i.flags); |
152 | attrs->a.i.flags); |
152 | break; |
153 | break; |
153 | 154 | ||
154 | case at_rgb: |
155 | case at_rgb: |
155 | set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color); |
156 | set_rgb_color(attrs->a.r.fg_color, attrs->a.r.bg_color); |
156 | break; |
157 | break; |
157 | } |
158 | } |
158 | } |
159 | } |
159 | 160 | ||
160 | static void prtchr(char c, int row, int col) |
161 | static void prtchr(char c, int row, int col) |
161 | { |
162 | { |
162 | async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col); |
163 | async_msg_3(fb_info.phone, FB_PUTCHAR, c, row, col); |
163 | } |
164 | } |
164 | 165 | ||
165 | /** Check key and process special keys. |
166 | /** Check key and process special keys. |
166 | * |
167 | * |
167 | * |
168 | * |
168 | */ |
169 | */ |
169 | static void write_char(int console, char key) |
170 | static void write_char(int console, char key) |
170 | { |
171 | { |
171 | screenbuffer_t *scr = &(connections[console].screenbuffer); |
172 | screenbuffer_t *scr = &(connections[console].screenbuffer); |
172 | 173 | ||
173 | switch (key) { |
174 | switch (key) { |
174 | case '\n': |
175 | case '\n': |
175 | scr->position_y++; |
176 | scr->position_y++; |
176 | scr->position_x = 0; |
177 | scr->position_x = 0; |
177 | break; |
178 | break; |
178 | case '\r': |
179 | case '\r': |
179 | break; |
180 | break; |
180 | case '\t': |
181 | case '\t': |
181 | scr->position_x += 8; |
182 | scr->position_x += 8; |
182 | scr->position_x -= scr->position_x % 8; |
183 | scr->position_x -= scr->position_x % 8; |
183 | break; |
184 | break; |
184 | case '\b': |
185 | case '\b': |
185 | if (scr->position_x == 0) |
186 | if (scr->position_x == 0) |
186 | break; |
187 | break; |
187 | scr->position_x--; |
188 | scr->position_x--; |
188 | if (console == active_console) |
189 | if (console == active_console) |
189 | prtchr(' ', scr->position_y, scr->position_x); |
190 | prtchr(' ', scr->position_y, scr->position_x); |
190 | screenbuffer_putchar(scr, ' '); |
191 | screenbuffer_putchar(scr, ' '); |
191 | break; |
192 | break; |
192 | default: |
193 | default: |
193 | if (console == active_console) |
194 | if (console == active_console) |
194 | prtchr(key, scr->position_y, scr->position_x); |
195 | prtchr(key, scr->position_y, scr->position_x); |
195 | 196 | ||
196 | screenbuffer_putchar(scr, key); |
197 | screenbuffer_putchar(scr, key); |
197 | scr->position_x++; |
198 | scr->position_x++; |
198 | } |
199 | } |
199 | 200 | ||
200 | scr->position_y += (scr->position_x >= scr->size_x); |
201 | scr->position_y += (scr->position_x >= scr->size_x); |
201 | 202 | ||
202 | if (scr->position_y >= scr->size_y) { |
203 | if (scr->position_y >= scr->size_y) { |
203 | scr->position_y = scr->size_y - 1; |
204 | scr->position_y = scr->size_y - 1; |
204 | screenbuffer_clear_line(scr, scr->top_line); |
205 | screenbuffer_clear_line(scr, scr->top_line); |
205 | scr->top_line = (scr->top_line + 1) % scr->size_y; |
206 | scr->top_line = (scr->top_line + 1) % scr->size_y; |
206 | if (console == active_console) |
207 | if (console == active_console) |
207 | async_msg_1(fb_info.phone, FB_SCROLL, 1); |
208 | async_msg_1(fb_info.phone, FB_SCROLL, 1); |
208 | } |
209 | } |
209 | 210 | ||
210 | scr->position_x = scr->position_x % scr->size_x; |
211 | scr->position_x = scr->position_x % scr->size_x; |
211 | 212 | ||
212 | if (console == active_console) |
213 | if (console == active_console) |
213 | curs_goto(scr->position_y, scr->position_x); |
214 | curs_goto(scr->position_y, scr->position_x); |
214 | 215 | ||
215 | } |
216 | } |
216 | 217 | ||
217 | /** Switch to new console */ |
218 | /** Switch to new console */ |
218 | static void change_console(int newcons) |
219 | static void change_console(int newcons) |
219 | { |
220 | { |
220 | connection_t *conn; |
221 | connection_t *conn; |
221 | int i, j, rc; |
222 | int i, j, rc; |
222 | keyfield_t *field; |
223 | keyfield_t *field; |
223 | attrs_t *attrs; |
224 | attrs_t *attrs; |
224 | 225 | ||
225 | if (newcons == active_console) |
226 | if (newcons == active_console) |
226 | return; |
227 | return; |
227 | 228 | ||
228 | if (newcons == KERNEL_CONSOLE) { |
229 | if (newcons == KERNEL_CONSOLE) { |
229 | async_serialize_start(); |
230 | async_serialize_start(); |
230 | curs_hide_sync(); |
231 | curs_hide_sync(); |
231 | gcons_in_kernel(); |
232 | gcons_in_kernel(); |
232 | async_serialize_end(); |
233 | async_serialize_end(); |
233 | 234 | ||
234 | if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) { |
235 | if (__SYSCALL0(SYS_DEBUG_ENABLE_CONSOLE)) { |
235 | prev_console = active_console; |
236 | prev_console = active_console; |
236 | active_console = KERNEL_CONSOLE; |
237 | active_console = KERNEL_CONSOLE; |
237 | } else |
238 | } else |
238 | newcons = active_console; |
239 | newcons = active_console; |
239 | } |
240 | } |
240 | 241 | ||
241 | if (newcons != KERNEL_CONSOLE) { |
242 | if (newcons != KERNEL_CONSOLE) { |
242 | async_serialize_start(); |
243 | async_serialize_start(); |
243 | 244 | ||
244 | if (active_console == KERNEL_CONSOLE) |
245 | if (active_console == KERNEL_CONSOLE) |
245 | gcons_redraw_console(); |
246 | gcons_redraw_console(); |
246 | 247 | ||
247 | active_console = newcons; |
248 | active_console = newcons; |
248 | gcons_change_console(newcons); |
249 | gcons_change_console(newcons); |
249 | conn = &connections[active_console]; |
250 | conn = &connections[active_console]; |
250 | 251 | ||
251 | set_attrs(&conn->screenbuffer.attrs); |
252 | set_attrs(&conn->screenbuffer.attrs); |
252 | curs_visibility(false); |
253 | curs_visibility(false); |
253 | if (interbuffer) { |
254 | if (interbuffer) { |
254 | for (i = 0; i < conn->screenbuffer.size_x; i++) |
255 | for (i = 0; i < conn->screenbuffer.size_x; i++) |
255 | for (j = 0; j < conn->screenbuffer.size_y; j++) { |
256 | for (j = 0; j < conn->screenbuffer.size_y; j++) { |
256 | unsigned int size_x; |
257 | unsigned int size_x; |
257 | 258 | ||
258 | size_x = conn->screenbuffer.size_x; |
259 | size_x = conn->screenbuffer.size_x; |
259 | interbuffer[i + j * size_x] = |
260 | interbuffer[i + j * size_x] = |
260 | *get_field_at(&conn->screenbuffer, i, j); |
261 | *get_field_at(&conn->screenbuffer, i, j); |
261 | } |
262 | } |
262 | /* This call can preempt, but we are already at the end */ |
263 | /* This call can preempt, but we are already at the end */ |
263 | rc = async_req_0_0(fb_info.phone, FB_DRAW_TEXT_DATA); |
264 | rc = async_req_0_0(fb_info.phone, FB_DRAW_TEXT_DATA); |
264 | } |
265 | } |
265 | 266 | ||
266 | if ((!interbuffer) || (rc != 0)) { |
267 | if ((!interbuffer) || (rc != 0)) { |
267 | set_attrs(&conn->screenbuffer.attrs); |
268 | set_attrs(&conn->screenbuffer.attrs); |
268 | clrscr(); |
269 | clrscr(); |
269 | attrs = &conn->screenbuffer.attrs; |
270 | attrs = &conn->screenbuffer.attrs; |
270 | 271 | ||
271 | for (j = 0; j < conn->screenbuffer.size_y; j++) |
272 | for (j = 0; j < conn->screenbuffer.size_y; j++) |
272 | for (i = 0; i < conn->screenbuffer.size_x; i++) { |
273 | for (i = 0; i < conn->screenbuffer.size_x; i++) { |
273 | field = get_field_at(&conn->screenbuffer, i, j); |
274 | field = get_field_at(&conn->screenbuffer, i, j); |
274 | if (!attrs_same(*attrs, field->attrs)) |
275 | if (!attrs_same(*attrs, field->attrs)) |
275 | set_attrs(&field->attrs); |
276 | set_attrs(&field->attrs); |
276 | attrs = &field->attrs; |
277 | attrs = &field->attrs; |
277 | if ((field->character == ' ') && |
278 | if ((field->character == ' ') && |
278 | (attrs_same(field->attrs, |
279 | (attrs_same(field->attrs, |
279 | conn->screenbuffer.attrs))) |
280 | conn->screenbuffer.attrs))) |
280 | continue; |
281 | continue; |
281 | 282 | ||
282 | prtchr(field->character, j, i); |
283 | prtchr(field->character, j, i); |
283 | } |
284 | } |
284 | } |
285 | } |
285 | 286 | ||
286 | curs_goto(conn->screenbuffer.position_y, |
287 | curs_goto(conn->screenbuffer.position_y, |
287 | conn->screenbuffer.position_x); |
288 | conn->screenbuffer.position_x); |
288 | curs_visibility(conn->screenbuffer.is_cursor_visible); |
289 | curs_visibility(conn->screenbuffer.is_cursor_visible); |
289 | 290 | ||
290 | async_serialize_end(); |
291 | async_serialize_end(); |
291 | } |
292 | } |
292 | } |
293 | } |
293 | 294 | ||
294 | /** Handler for keyboard */ |
295 | /** Handler for keyboard */ |
295 | static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall) |
296 | static void keyboard_events(ipc_callid_t iid, ipc_call_t *icall) |
296 | { |
297 | { |
297 | ipc_callid_t callid; |
298 | ipc_callid_t callid; |
298 | ipc_call_t call; |
299 | ipc_call_t call; |
299 | int retval; |
300 | int retval; |
300 | kbd_event_t ev; |
301 | kbd_event_t ev; |
301 | connection_t *conn; |
302 | connection_t *conn; |
302 | int newcon; |
303 | int newcon; |
303 | 304 | ||
304 | /* Ignore parameters, the connection is alread opened */ |
305 | /* Ignore parameters, the connection is alread opened */ |
305 | while (1) { |
306 | while (1) { |
306 | callid = async_get_call(&call); |
307 | callid = async_get_call(&call); |
307 | switch (IPC_GET_METHOD(call)) { |
308 | switch (IPC_GET_METHOD(call)) { |
308 | case IPC_M_PHONE_HUNGUP: |
309 | case IPC_M_PHONE_HUNGUP: |
309 | /* TODO: Handle hangup */ |
310 | /* TODO: Handle hangup */ |
310 | return; |
311 | return; |
311 | case KBD_MS_LEFT: |
312 | case KBD_MS_LEFT: |
312 | newcon = gcons_mouse_btn(IPC_GET_ARG1(call)); |
313 | newcon = gcons_mouse_btn(IPC_GET_ARG1(call)); |
313 | if (newcon != -1) |
314 | if (newcon != -1) |
314 | change_console(newcon); |
315 | change_console(newcon); |
315 | retval = 0; |
316 | retval = 0; |
316 | break; |
317 | break; |
317 | case KBD_MS_MOVE: |
318 | case KBD_MS_MOVE: |
318 | gcons_mouse_move(IPC_GET_ARG1(call), |
319 | gcons_mouse_move(IPC_GET_ARG1(call), |
319 | IPC_GET_ARG2(call)); |
320 | IPC_GET_ARG2(call)); |
320 | retval = 0; |
321 | retval = 0; |
321 | break; |
322 | break; |
322 | case KBD_EVENT: |
323 | case KBD_EVENT: |
323 | /* Got event from keyboard driver. */ |
324 | /* Got event from keyboard driver. */ |
324 | retval = 0; |
325 | retval = 0; |
325 | ev.type = IPC_GET_ARG1(call); |
326 | ev.type = IPC_GET_ARG1(call); |
326 | ev.key = IPC_GET_ARG2(call); |
327 | ev.key = IPC_GET_ARG2(call); |
327 | ev.mods = IPC_GET_ARG3(call); |
328 | ev.mods = IPC_GET_ARG3(call); |
328 | ev.c = IPC_GET_ARG4(call); |
329 | ev.c = IPC_GET_ARG4(call); |
329 | 330 | ||
330 | /* switch to another virtual console */ |
331 | /* switch to another virtual console */ |
331 | 332 | ||
332 | conn = &connections[active_console]; |
333 | conn = &connections[active_console]; |
333 | 334 | ||
334 | if ((ev.key >= 0x101) && (ev.key < 0x101 + |
335 | if ((ev.key >= KC_F1) && (ev.key < KC_F1 + |
335 | CONSOLE_COUNT)) { |
336 | CONSOLE_COUNT)) { |
336 | if (ev.key == 0x112) |
337 | if (ev.key == KC_F12) |
337 | change_console(KERNEL_CONSOLE); |
338 | change_console(KERNEL_CONSOLE); |
338 | else |
339 | else |
339 | change_console(ev.key - 0x101); |
340 | change_console(ev.key - KC_F1); |
340 | break; |
341 | break; |
341 | } |
342 | } |
342 | 343 | ||
343 | /* if client is awaiting key, send it */ |
344 | /* if client is awaiting key, send it */ |
344 | if (conn->keyrequest_counter > 0) { |
345 | if (conn->keyrequest_counter > 0) { |
345 | conn->keyrequest_counter--; |
346 | conn->keyrequest_counter--; |
346 | ipc_answer_4(fifo_pop(conn->keyrequests), EOK, |
347 | ipc_answer_4(fifo_pop(conn->keyrequests), EOK, |
347 | ev.type, ev.key, ev.mods, ev.c); |
348 | ev.type, ev.key, ev.mods, ev.c); |
348 | break; |
349 | break; |
349 | } |
350 | } |
350 | 351 | ||
351 | keybuffer_push(&conn->keybuffer, &ev); |
352 | keybuffer_push(&conn->keybuffer, &ev); |
352 | retval = 0; |
353 | retval = 0; |
353 | 354 | ||
354 | break; |
355 | break; |
355 | default: |
356 | default: |
356 | retval = ENOENT; |
357 | retval = ENOENT; |
357 | } |
358 | } |
358 | ipc_answer_0(callid, retval); |
359 | ipc_answer_0(callid, retval); |
359 | } |
360 | } |
360 | } |
361 | } |
361 | 362 | ||
362 | /** Default thread for new connections */ |
363 | /** Default thread for new connections */ |
363 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall) |
364 | static void client_connection(ipc_callid_t iid, ipc_call_t *icall) |
364 | { |
365 | { |
365 | ipc_callid_t callid; |
366 | ipc_callid_t callid; |
366 | ipc_call_t call; |
367 | ipc_call_t call; |
367 | int consnum; |
368 | int consnum; |
368 | ipcarg_t arg1, arg2, arg3, arg4; |
369 | ipcarg_t arg1, arg2, arg3, arg4; |
369 | connection_t *conn; |
370 | connection_t *conn; |
370 | 371 | ||
371 | if ((consnum = find_free_connection()) == -1) { |
372 | if ((consnum = find_free_connection()) == -1) { |
372 | ipc_answer_0(iid, ELIMIT); |
373 | ipc_answer_0(iid, ELIMIT); |
373 | return; |
374 | return; |
374 | } |
375 | } |
375 | conn = &connections[consnum]; |
376 | conn = &connections[consnum]; |
376 | conn->used = 1; |
377 | conn->used = 1; |
377 | 378 | ||
378 | async_serialize_start(); |
379 | async_serialize_start(); |
379 | gcons_notify_connect(consnum); |
380 | gcons_notify_connect(consnum); |
380 | conn->client_phone = IPC_GET_ARG5(*icall); |
381 | conn->client_phone = IPC_GET_ARG5(*icall); |
381 | screenbuffer_clear(&conn->screenbuffer); |
382 | screenbuffer_clear(&conn->screenbuffer); |
382 | 383 | ||
383 | /* Accept the connection */ |
384 | /* Accept the connection */ |
384 | ipc_answer_0(iid, EOK); |
385 | ipc_answer_0(iid, EOK); |
385 | 386 | ||
386 | while (1) { |
387 | while (1) { |
387 | async_serialize_end(); |
388 | async_serialize_end(); |
388 | callid = async_get_call(&call); |
389 | callid = async_get_call(&call); |
389 | async_serialize_start(); |
390 | async_serialize_start(); |
390 | 391 | ||
391 | arg1 = 0; |
392 | arg1 = 0; |
392 | arg2 = 0; |
393 | arg2 = 0; |
393 | arg3 = 0; |
394 | arg3 = 0; |
394 | arg4 = 0; |
395 | arg4 = 0; |
395 | 396 | ||
396 | switch (IPC_GET_METHOD(call)) { |
397 | switch (IPC_GET_METHOD(call)) { |
397 | case IPC_M_PHONE_HUNGUP: |
398 | case IPC_M_PHONE_HUNGUP: |
398 | gcons_notify_disconnect(consnum); |
399 | gcons_notify_disconnect(consnum); |
399 | 400 | ||
400 | /* Answer all pending requests */ |
401 | /* Answer all pending requests */ |
401 | while (conn->keyrequest_counter > 0) { |
402 | while (conn->keyrequest_counter > 0) { |
402 | conn->keyrequest_counter--; |
403 | conn->keyrequest_counter--; |
403 | ipc_answer_0(fifo_pop(conn->keyrequests), |
404 | ipc_answer_0(fifo_pop(conn->keyrequests), |
404 | ENOENT); |
405 | ENOENT); |
405 | break; |
406 | break; |
406 | } |
407 | } |
407 | conn->used = 0; |
408 | conn->used = 0; |
408 | return; |
409 | return; |
409 | case CONSOLE_PUTCHAR: |
410 | case CONSOLE_PUTCHAR: |
410 | write_char(consnum, IPC_GET_ARG1(call)); |
411 | write_char(consnum, IPC_GET_ARG1(call)); |
411 | gcons_notify_char(consnum); |
412 | gcons_notify_char(consnum); |
412 | break; |
413 | break; |
413 | case CONSOLE_CLEAR: |
414 | case CONSOLE_CLEAR: |
414 | /* Send message to fb */ |
415 | /* Send message to fb */ |
415 | if (consnum == active_console) { |
416 | if (consnum == active_console) { |
416 | async_msg_0(fb_info.phone, FB_CLEAR); |
417 | async_msg_0(fb_info.phone, FB_CLEAR); |
417 | } |
418 | } |
418 | 419 | ||
419 | screenbuffer_clear(&conn->screenbuffer); |
420 | screenbuffer_clear(&conn->screenbuffer); |
420 | 421 | ||
421 | break; |
422 | break; |
422 | case CONSOLE_GOTO: |
423 | case CONSOLE_GOTO: |
423 | screenbuffer_goto(&conn->screenbuffer, |
424 | screenbuffer_goto(&conn->screenbuffer, |
424 | IPC_GET_ARG2(call), IPC_GET_ARG1(call)); |
425 | IPC_GET_ARG2(call), IPC_GET_ARG1(call)); |
425 | if (consnum == active_console) |
426 | if (consnum == active_console) |
426 | curs_goto(IPC_GET_ARG1(call), |
427 | curs_goto(IPC_GET_ARG1(call), |
427 | IPC_GET_ARG2(call)); |
428 | IPC_GET_ARG2(call)); |
428 | break; |
429 | break; |
429 | case CONSOLE_GETSIZE: |
430 | case CONSOLE_GETSIZE: |
430 | arg1 = fb_info.rows; |
431 | arg1 = fb_info.rows; |
431 | arg2 = fb_info.cols; |
432 | arg2 = fb_info.cols; |
432 | break; |
433 | break; |
433 | case CONSOLE_FLUSH: |
434 | case CONSOLE_FLUSH: |
434 | if (consnum == active_console) |
435 | if (consnum == active_console) |
435 | async_req_0_0(fb_info.phone, FB_FLUSH); |
436 | async_req_0_0(fb_info.phone, FB_FLUSH); |
436 | break; |
437 | break; |
437 | case CONSOLE_SET_STYLE: |
438 | case CONSOLE_SET_STYLE: |
438 | arg1 = IPC_GET_ARG1(call); |
439 | arg1 = IPC_GET_ARG1(call); |
439 | screenbuffer_set_style(&conn->screenbuffer, arg1); |
440 | screenbuffer_set_style(&conn->screenbuffer, arg1); |
440 | if (consnum == active_console) |
441 | if (consnum == active_console) |
441 | set_style(arg1); |
442 | set_style(arg1); |
442 | break; |
443 | break; |
443 | case CONSOLE_SET_COLOR: |
444 | case CONSOLE_SET_COLOR: |
444 | arg1 = IPC_GET_ARG1(call); |
445 | arg1 = IPC_GET_ARG1(call); |
445 | arg2 = IPC_GET_ARG2(call); |
446 | arg2 = IPC_GET_ARG2(call); |
446 | arg3 = IPC_GET_ARG3(call); |
447 | arg3 = IPC_GET_ARG3(call); |
447 | screenbuffer_set_color(&conn->screenbuffer, arg1, |
448 | screenbuffer_set_color(&conn->screenbuffer, arg1, |
448 | arg2, arg3); |
449 | arg2, arg3); |
449 | if (consnum == active_console) |
450 | if (consnum == active_console) |
450 | set_color(arg1, arg2, arg3); |
451 | set_color(arg1, arg2, arg3); |
451 | break; |
452 | break; |
452 | case CONSOLE_SET_RGB_COLOR: |
453 | case CONSOLE_SET_RGB_COLOR: |
453 | arg1 = IPC_GET_ARG1(call); |
454 | arg1 = IPC_GET_ARG1(call); |
454 | arg2 = IPC_GET_ARG2(call); |
455 | arg2 = IPC_GET_ARG2(call); |
455 | screenbuffer_set_rgb_color(&conn->screenbuffer, arg1, |
456 | screenbuffer_set_rgb_color(&conn->screenbuffer, arg1, |
456 | arg2); |
457 | arg2); |
457 | if (consnum == active_console) |
458 | if (consnum == active_console) |
458 | set_rgb_color(arg1, arg2); |
459 | set_rgb_color(arg1, arg2); |
459 | break; |
460 | break; |
460 | case CONSOLE_CURSOR_VISIBILITY: |
461 | case CONSOLE_CURSOR_VISIBILITY: |
461 | arg1 = IPC_GET_ARG1(call); |
462 | arg1 = IPC_GET_ARG1(call); |
462 | conn->screenbuffer.is_cursor_visible = arg1; |
463 | conn->screenbuffer.is_cursor_visible = arg1; |
463 | if (consnum == active_console) |
464 | if (consnum == active_console) |
464 | curs_visibility(arg1); |
465 | curs_visibility(arg1); |
465 | break; |
466 | break; |
466 | case CONSOLE_GETKEY: |
467 | case CONSOLE_GETKEY: |
467 | if (keybuffer_empty(&conn->keybuffer)) { |
468 | if (keybuffer_empty(&conn->keybuffer)) { |
468 | /* buffer is empty -> store request */ |
469 | /* buffer is empty -> store request */ |
469 | if (conn->keyrequest_counter < |
470 | if (conn->keyrequest_counter < |
470 | MAX_KEYREQUESTS_BUFFERED) { |
471 | MAX_KEYREQUESTS_BUFFERED) { |
471 | fifo_push(conn->keyrequests, callid); |
472 | fifo_push(conn->keyrequests, callid); |
472 | conn->keyrequest_counter++; |
473 | conn->keyrequest_counter++; |
473 | } else { |
474 | } else { |
474 | /* |
475 | /* |
475 | * No key available and too many |
476 | * No key available and too many |
476 | * requests => fail. |
477 | * requests => fail. |
477 | */ |
478 | */ |
478 | ipc_answer_0(callid, ELIMIT); |
479 | ipc_answer_0(callid, ELIMIT); |
479 | } |
480 | } |
480 | continue; |
481 | continue; |
481 | } |
482 | } |
482 | kbd_event_t ev; |
483 | kbd_event_t ev; |
483 | keybuffer_pop(&conn->keybuffer, &ev); |
484 | keybuffer_pop(&conn->keybuffer, &ev); |
484 | arg1 = ev.type; |
485 | arg1 = ev.type; |
485 | arg2 = ev.key; |
486 | arg2 = ev.key; |
486 | arg3 = ev.mods; |
487 | arg3 = ev.mods; |
487 | arg4 = ev.c; |
488 | arg4 = ev.c; |
488 | break; |
489 | break; |
489 | } |
490 | } |
490 | ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4); |
491 | ipc_answer_4(callid, EOK, arg1, arg2, arg3, arg4); |
491 | } |
492 | } |
492 | } |
493 | } |
493 | 494 | ||
494 | static void interrupt_received(ipc_callid_t callid, ipc_call_t *call) |
495 | static void interrupt_received(ipc_callid_t callid, ipc_call_t *call) |
495 | { |
496 | { |
496 | change_console(prev_console); |
497 | change_console(prev_console); |
497 | } |
498 | } |
498 | 499 | ||
499 | int main(int argc, char *argv[]) |
500 | int main(int argc, char *argv[]) |
500 | { |
501 | { |
501 | printf(NAME ": HelenOS Console service\n"); |
502 | printf(NAME ": HelenOS Console service\n"); |
502 | 503 | ||
503 | ipcarg_t phonehash; |
504 | ipcarg_t phonehash; |
504 | int kbd_phone; |
505 | int kbd_phone; |
505 | size_t ib_size; |
506 | size_t ib_size; |
506 | int i; |
507 | int i; |
507 | 508 | ||
508 | async_set_client_connection(client_connection); |
509 | async_set_client_connection(client_connection); |
509 | 510 | ||
510 | /* Connect to keyboard driver */ |
511 | /* Connect to keyboard driver */ |
511 | kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0); |
512 | kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0); |
512 | while (kbd_phone < 0) { |
513 | while (kbd_phone < 0) { |
513 | usleep(10000); |
514 | usleep(10000); |
514 | kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0); |
515 | kbd_phone = ipc_connect_me_to(PHONE_NS, SERVICE_KEYBOARD, 0, 0); |
515 | } |
516 | } |
516 | 517 | ||
517 | if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) |
518 | if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) |
518 | return -1; |
519 | return -1; |
519 | async_new_connection(phonehash, 0, NULL, keyboard_events); |
520 | async_new_connection(phonehash, 0, NULL, keyboard_events); |
520 | 521 | ||
521 | /* Connect to framebuffer driver */ |
522 | /* Connect to framebuffer driver */ |
522 | fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0); |
523 | fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0); |
523 | while (fb_info.phone < 0) { |
524 | while (fb_info.phone < 0) { |
524 | usleep(10000); |
525 | usleep(10000); |
525 | fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0); |
526 | fb_info.phone = ipc_connect_me_to(PHONE_NS, SERVICE_VIDEO, 0, 0); |
526 | } |
527 | } |
527 | 528 | ||
528 | /* Disable kernel output to the console */ |
529 | /* Disable kernel output to the console */ |
529 | __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE); |
530 | __SYSCALL0(SYS_DEBUG_DISABLE_CONSOLE); |
530 | 531 | ||
531 | /* Initialize gcons */ |
532 | /* Initialize gcons */ |
532 | gcons_init(fb_info.phone); |
533 | gcons_init(fb_info.phone); |
533 | /* Synchronize, the gcons can have something in queue */ |
534 | /* Synchronize, the gcons can have something in queue */ |
534 | async_req_0_0(fb_info.phone, FB_FLUSH); |
535 | async_req_0_0(fb_info.phone, FB_FLUSH); |
535 | 536 | ||
536 | async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows, |
537 | async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.rows, |
537 | &fb_info.cols); |
538 | &fb_info.cols); |
538 | set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND); |
539 | set_rgb_color(DEFAULT_FOREGROUND, DEFAULT_BACKGROUND); |
539 | clrscr(); |
540 | clrscr(); |
540 | 541 | ||
541 | /* Init virtual consoles */ |
542 | /* Init virtual consoles */ |
542 | for (i = 0; i < CONSOLE_COUNT; i++) { |
543 | for (i = 0; i < CONSOLE_COUNT; i++) { |
543 | connections[i].used = 0; |
544 | connections[i].used = 0; |
544 | keybuffer_init(&connections[i].keybuffer); |
545 | keybuffer_init(&connections[i].keybuffer); |
545 | 546 | ||
546 | connections[i].keyrequests.head = 0; |
547 | connections[i].keyrequests.head = 0; |
547 | connections[i].keyrequests.tail = 0; |
548 | connections[i].keyrequests.tail = 0; |
548 | connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED; |
549 | connections[i].keyrequests.items = MAX_KEYREQUESTS_BUFFERED; |
549 | connections[i].keyrequest_counter = 0; |
550 | connections[i].keyrequest_counter = 0; |
550 | 551 | ||
551 | if (screenbuffer_init(&connections[i].screenbuffer, |
552 | if (screenbuffer_init(&connections[i].screenbuffer, |
552 | fb_info.cols, fb_info.rows) == NULL) { |
553 | fb_info.cols, fb_info.rows) == NULL) { |
553 | /* FIXME: handle error */ |
554 | /* FIXME: handle error */ |
554 | return -1; |
555 | return -1; |
555 | } |
556 | } |
556 | } |
557 | } |
557 | connections[KERNEL_CONSOLE].used = 1; |
558 | connections[KERNEL_CONSOLE].used = 1; |
558 | 559 | ||
559 | /* Set up shared memory buffer. */ |
560 | /* Set up shared memory buffer. */ |
560 | ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows; |
561 | ib_size = sizeof(keyfield_t) * fb_info.cols * fb_info.rows; |
561 | interbuffer = as_get_mappable_page(ib_size); |
562 | interbuffer = as_get_mappable_page(ib_size); |
562 | 563 | ||
563 | if (as_area_create(interbuffer, ib_size, AS_AREA_READ | |
564 | if (as_area_create(interbuffer, ib_size, AS_AREA_READ | |
564 | AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) { |
565 | AS_AREA_WRITE | AS_AREA_CACHEABLE) != interbuffer) { |
565 | interbuffer = NULL; |
566 | interbuffer = NULL; |
566 | } |
567 | } |
567 | 568 | ||
568 | if (interbuffer) { |
569 | if (interbuffer) { |
569 | if (ipc_share_out_start(fb_info.phone, interbuffer, |
570 | if (ipc_share_out_start(fb_info.phone, interbuffer, |
570 | AS_AREA_READ) != EOK) { |
571 | AS_AREA_READ) != EOK) { |
571 | as_area_destroy(interbuffer); |
572 | as_area_destroy(interbuffer); |
572 | interbuffer = NULL; |
573 | interbuffer = NULL; |
573 | } |
574 | } |
574 | } |
575 | } |
575 | 576 | ||
576 | curs_goto(0, 0); |
577 | curs_goto(0, 0); |
577 | curs_visibility( |
578 | curs_visibility( |
578 | connections[active_console].screenbuffer.is_cursor_visible); |
579 | connections[active_console].screenbuffer.is_cursor_visible); |
579 | 580 | ||
580 | /* Register at NS */ |
581 | /* Register at NS */ |
581 | if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) |
582 | if (ipc_connect_to_me(PHONE_NS, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) |
582 | return -1; |
583 | return -1; |
583 | 584 | ||
584 | /* Receive kernel notifications */ |
585 | /* Receive kernel notifications */ |
585 | if (sysinfo_value("kconsole.present")) { |
586 | if (sysinfo_value("kconsole.present")) { |
586 | int devno = sysinfo_value("kconsole.devno"); |
587 | int devno = sysinfo_value("kconsole.devno"); |
587 | int inr = sysinfo_value("kconsole.inr"); |
588 | int inr = sysinfo_value("kconsole.inr"); |
588 | if (ipc_register_irq(inr, devno, 0, NULL) != EOK) |
589 | if (ipc_register_irq(inr, devno, 0, NULL) != EOK) |
589 | printf(NAME ": Error registering kconsole notifications\n"); |
590 | printf(NAME ": Error registering kconsole notifications\n"); |
590 | 591 | ||
591 | async_set_interrupt_received(interrupt_received); |
592 | async_set_interrupt_received(interrupt_received); |
592 | } |
593 | } |
593 | 594 | ||
594 | // FIXME: avoid connectiong to itself, keep using klog |
595 | // FIXME: avoid connectiong to itself, keep using klog |
595 | // printf(NAME ": Accepting connections\n"); |
596 | // printf(NAME ": Accepting connections\n"); |
596 | async_manager(); |
597 | async_manager(); |
597 | 598 | ||
598 | return 0; |
599 | return 0; |
599 | } |
600 | } |
600 | 601 | ||
601 | /** @} |
602 | /** @} |
602 | */ |
603 | */ |
603 | 604 |