source: mainline/uspace/srv/hid/output/output.c@ 7cc30e9

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7cc30e9 was 7fc81da, checked in by Jiri Svoboda <jiri@…>, 4 years ago

128 columns is not enough for everybody

Fixes corrupted graphical console display in VESA modes > 1024x768.

  • Property mode set to 100644
File size: 11.2 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup output
30 * @{
31 */
32
33#include <errno.h>
34#include <stddef.h>
35#include <stdlib.h>
36#include <macros.h>
37#include <as.h>
38#include <task.h>
39#include <ipc/output.h>
40#include <config.h>
41#include "port/ega.h"
42#include "port/chardev.h"
43#include "port/ddev.h"
44#include "output.h"
45
46typedef struct {
47 link_t link;
48
49 size_t size;
50 unsigned int flags;
51 void *data;
52} frontbuf_t;
53
54static LIST_INITIALIZE(outdevs);
55static LIST_INITIALIZE(frontbufs);
56
57outdev_t *outdev_register(outdev_ops_t *ops, void *data)
58{
59 assert(ops->get_dimensions);
60
61 outdev_t *dev = (outdev_t *) malloc(sizeof(outdev_t));
62 if (dev == NULL)
63 return NULL;
64
65 link_initialize(&dev->link);
66
67 dev->ops = *ops;
68 dev->data = data;
69
70 ops->get_dimensions(dev, &dev->cols, &dev->rows);
71 dev->backbuf = chargrid_create(dev->cols, dev->rows,
72 CHARGRID_FLAG_NONE);
73 if (dev->backbuf == NULL) {
74 free(dev);
75 return NULL;
76 }
77
78 list_append(&dev->link, &outdevs);
79 return dev;
80}
81
82static void srv_yield(ipc_call_t *icall)
83{
84 errno_t ret = EOK;
85
86 list_foreach(outdevs, link, outdev_t, dev) {
87 assert(dev->ops.yield);
88
89 errno_t rc = dev->ops.yield(dev);
90 if (rc != EOK)
91 ret = rc;
92 }
93
94 async_answer_0(icall, ret);
95}
96
97static void srv_claim(ipc_call_t *icall)
98{
99 errno_t ret = EOK;
100
101 list_foreach(outdevs, link, outdev_t, dev) {
102 assert(dev->ops.claim);
103
104 errno_t rc = dev->ops.claim(dev);
105 if (rc != EOK)
106 ret = rc;
107 }
108
109 async_answer_0(icall, ret);
110}
111
112static void srv_get_dimensions(ipc_call_t *icall)
113{
114 sysarg_t cols = 0;
115 sysarg_t rows = 0;
116 bool first;
117
118 first = true;
119 list_foreach(outdevs, link, outdev_t, dev) {
120 if (first) {
121 cols = dev->cols;
122 rows = dev->rows;
123 first = false;
124 } else {
125 cols = min(cols, dev->cols);
126 rows = min(rows, dev->rows);
127 }
128 }
129
130 async_answer_2(icall, EOK, cols, rows);
131}
132
133static void srv_get_caps(ipc_call_t *icall)
134{
135 console_caps_t caps = 0;
136
137 list_foreach(outdevs, link, outdev_t, dev) {
138 assert(dev->ops.get_caps);
139
140 caps |= dev->ops.get_caps(dev);
141 }
142
143 async_answer_1(icall, EOK, caps);
144}
145
146static frontbuf_t *resolve_frontbuf(sysarg_t handle, ipc_call_t *icall)
147{
148 frontbuf_t *frontbuf = NULL;
149 list_foreach(frontbufs, link, frontbuf_t, cur) {
150 if (cur == (frontbuf_t *) handle) {
151 frontbuf = cur;
152 break;
153 }
154 }
155
156 if (frontbuf == NULL) {
157 async_answer_0(icall, ENOENT);
158 return NULL;
159 }
160
161 return frontbuf;
162}
163
164static void srv_frontbuf_create(ipc_call_t *icall)
165{
166 frontbuf_t *frontbuf = (frontbuf_t *) malloc(sizeof(frontbuf_t));
167 if (frontbuf == NULL) {
168 async_answer_0(icall, ENOMEM);
169 return;
170 }
171
172 link_initialize(&frontbuf->link);
173
174 ipc_call_t call;
175 if (!async_share_out_receive(&call, &frontbuf->size,
176 &frontbuf->flags)) {
177 free(frontbuf);
178 async_answer_0(icall, EINVAL);
179 return;
180 }
181
182 errno_t rc = async_share_out_finalize(&call, &frontbuf->data);
183 if ((rc != EOK) || (frontbuf->data == AS_MAP_FAILED)) {
184 free(frontbuf);
185 async_answer_0(icall, ENOMEM);
186 return;
187 }
188
189 list_append(&frontbuf->link, &frontbufs);
190 async_answer_1(icall, EOK, (sysarg_t) frontbuf);
191}
192
193static void srv_frontbuf_destroy(ipc_call_t *icall)
194{
195 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
196 if (frontbuf == NULL)
197 return;
198
199 list_remove(&frontbuf->link);
200 as_area_destroy(frontbuf->data);
201 free(frontbuf);
202
203 async_answer_0(icall, EOK);
204}
205
206static void srv_cursor_update(ipc_call_t *icall)
207{
208 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
209 if (frontbuf == NULL)
210 return;
211
212 chargrid_t *buf = (chargrid_t *) frontbuf->data;
213 bool visible = chargrid_get_cursor_visibility(buf);
214
215 sysarg_t col;
216 sysarg_t row;
217 chargrid_get_cursor(buf, &col, &row);
218
219 list_foreach(outdevs, link, outdev_t, dev) {
220 assert(dev->ops.cursor_update);
221
222 sysarg_t prev_col;
223 sysarg_t prev_row;
224 chargrid_get_cursor(dev->backbuf, &prev_col, &prev_row);
225
226 chargrid_set_cursor(dev->backbuf, col, row);
227 chargrid_set_cursor_visibility(dev->backbuf, visible);
228
229 dev->ops.cursor_update(dev, prev_col, prev_row, col, row,
230 visible);
231 dev->ops.flush(dev);
232
233 }
234
235 async_answer_0(icall, EOK);
236}
237
238static void srv_set_style(ipc_call_t *icall)
239{
240 list_foreach(outdevs, link, outdev_t, dev) {
241 dev->attrs.type = CHAR_ATTR_STYLE;
242 dev->attrs.val.style =
243 (console_style_t) ipc_get_arg1(icall);
244 }
245
246 async_answer_0(icall, EOK);
247}
248
249static void srv_set_color(ipc_call_t *icall)
250{
251 list_foreach(outdevs, link, outdev_t, dev) {
252 dev->attrs.type = CHAR_ATTR_INDEX;
253 dev->attrs.val.index.bgcolor =
254 (console_color_t) ipc_get_arg1(icall);
255 dev->attrs.val.index.fgcolor =
256 (console_color_t) ipc_get_arg2(icall);
257 dev->attrs.val.index.attr =
258 (console_color_attr_t) ipc_get_arg3(icall);
259 }
260
261 async_answer_0(icall, EOK);
262}
263
264static void srv_set_rgb_color(ipc_call_t *icall)
265{
266 list_foreach(outdevs, link, outdev_t, dev) {
267 dev->attrs.type = CHAR_ATTR_RGB;
268 dev->attrs.val.rgb.bgcolor = ipc_get_arg1(icall);
269 dev->attrs.val.rgb.fgcolor = ipc_get_arg2(icall);
270 }
271
272 async_answer_0(icall, EOK);
273}
274
275static bool srv_update_scroll(outdev_t *dev, chargrid_t *buf)
276{
277 assert(dev->ops.char_update);
278
279 sysarg_t top_row = chargrid_get_top_row(buf);
280
281 if (dev->top_row == top_row)
282 return false;
283
284 dev->top_row = top_row;
285
286 for (sysarg_t y = 0; y < dev->rows; y++) {
287 for (sysarg_t x = 0; x < dev->cols; x++) {
288 charfield_t *front_field =
289 chargrid_charfield_at(buf, x, y);
290 charfield_t *back_field =
291 chargrid_charfield_at(dev->backbuf, x, y);
292 bool update = false;
293
294 if (front_field->ch != back_field->ch) {
295 back_field->ch = front_field->ch;
296 update = true;
297 }
298
299 if (!attrs_same(front_field->attrs, back_field->attrs)) {
300 back_field->attrs = front_field->attrs;
301 update = true;
302 }
303
304 front_field->flags &= ~CHAR_FLAG_DIRTY;
305
306 if (update)
307 dev->ops.char_update(dev, x, y);
308 }
309 }
310
311 return true;
312}
313
314static void srv_update(ipc_call_t *icall)
315{
316 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
317 if (frontbuf == NULL)
318 return;
319
320 chargrid_t *buf = (chargrid_t *) frontbuf->data;
321
322 list_foreach(outdevs, link, outdev_t, dev) {
323 assert(dev->ops.char_update);
324
325 if (srv_update_scroll(dev, buf))
326 continue;
327
328 for (sysarg_t y = 0; y < dev->rows; y++) {
329 for (sysarg_t x = 0; x < dev->cols; x++) {
330 charfield_t *front_field =
331 chargrid_charfield_at(buf, x, y);
332 charfield_t *back_field =
333 chargrid_charfield_at(dev->backbuf, x, y);
334 bool update = false;
335
336 if ((front_field->flags & CHAR_FLAG_DIRTY) ==
337 CHAR_FLAG_DIRTY) {
338 if (front_field->ch != back_field->ch) {
339 back_field->ch = front_field->ch;
340 update = true;
341 }
342
343 if (!attrs_same(front_field->attrs,
344 back_field->attrs)) {
345 back_field->attrs = front_field->attrs;
346 update = true;
347 }
348
349 front_field->flags &= ~CHAR_FLAG_DIRTY;
350 }
351
352 if (update)
353 dev->ops.char_update(dev, x, y);
354 }
355 }
356
357 dev->ops.flush(dev);
358 }
359
360 async_answer_0(icall, EOK);
361}
362
363static void srv_damage(ipc_call_t *icall)
364{
365 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
366 if (frontbuf == NULL)
367 return;
368
369 chargrid_t *buf = (chargrid_t *) frontbuf->data;
370
371 list_foreach(outdevs, link, outdev_t, dev) {
372 assert(dev->ops.char_update);
373
374 if (srv_update_scroll(dev, buf))
375 continue;
376
377 sysarg_t col = ipc_get_arg2(icall);
378 sysarg_t row = ipc_get_arg3(icall);
379
380 sysarg_t cols = ipc_get_arg4(icall);
381 sysarg_t rows = ipc_get_arg5(icall);
382
383 for (sysarg_t y = 0; y < rows; y++) {
384 for (sysarg_t x = 0; x < cols; x++) {
385 charfield_t *front_field =
386 chargrid_charfield_at(buf, col + x, row + y);
387 charfield_t *back_field =
388 chargrid_charfield_at(dev->backbuf, col + x, row + y);
389
390 back_field->ch = front_field->ch;
391 back_field->attrs = front_field->attrs;
392 front_field->flags &= ~CHAR_FLAG_DIRTY;
393 dev->ops.char_update(dev, col + x, row + y);
394 }
395 }
396 dev->ops.flush(dev);
397
398 }
399
400 async_answer_0(icall, EOK);
401}
402
403static void client_connection(ipc_call_t *icall, void *arg)
404{
405 /* Accept the connection */
406 async_accept_0(icall);
407
408 while (true) {
409 ipc_call_t call;
410 async_get_call(&call);
411
412 if (!ipc_get_imethod(&call)) {
413 async_answer_0(&call, EOK);
414 break;
415 }
416
417 switch (ipc_get_imethod(&call)) {
418 case OUTPUT_YIELD:
419 srv_yield(&call);
420 break;
421 case OUTPUT_CLAIM:
422 srv_claim(&call);
423 break;
424 case OUTPUT_GET_DIMENSIONS:
425 srv_get_dimensions(&call);
426 break;
427 case OUTPUT_GET_CAPS:
428 srv_get_caps(&call);
429 break;
430
431 case OUTPUT_FRONTBUF_CREATE:
432 srv_frontbuf_create(&call);
433 break;
434 case OUTPUT_FRONTBUF_DESTROY:
435 srv_frontbuf_destroy(&call);
436 break;
437
438 case OUTPUT_CURSOR_UPDATE:
439 srv_cursor_update(&call);
440 break;
441 case OUTPUT_SET_STYLE:
442 srv_set_style(&call);
443 break;
444 case OUTPUT_SET_COLOR:
445 srv_set_color(&call);
446 break;
447 case OUTPUT_SET_RGB_COLOR:
448 srv_set_rgb_color(&call);
449 break;
450 case OUTPUT_UPDATE:
451 srv_update(&call);
452 break;
453 case OUTPUT_DAMAGE:
454 srv_damage(&call);
455 break;
456
457 default:
458 async_answer_0(&call, EINVAL);
459 }
460 }
461}
462
463static void usage(char *name)
464{
465 printf("Usage: %s <service_name>\n", name);
466}
467
468int main(int argc, char *argv[])
469{
470 if (argc < 2) {
471 usage(argv[0]);
472 return 1;
473 }
474
475 printf("%s: HelenOS output service\n", NAME);
476
477 /* Register server */
478 async_set_fallback_port_handler(client_connection, NULL);
479 errno_t rc = loc_server_register(NAME);
480 if (rc != EOK) {
481 printf("%s: Unable to register driver\n", NAME);
482 return rc;
483 }
484
485 service_id_t service_id;
486 rc = loc_service_register(argv[1], &service_id);
487 if (rc != EOK) {
488 printf("%s: Unable to register service %s\n", NAME, argv[1]);
489 return rc;
490 }
491
492 if (!config_key_exists("console")) {
493 ega_init();
494#if defined(CONFIG_FB) && !defined(CONFIG_WINSYS)
495 /*
496 * NOTE: We don't have a good way of detecting the absence
497 * of a KFB display device at run time because of the
498 * asunchronous nature of device discovery
499 */
500 output_ddev_init();
501#endif
502 }
503
504 chardev_init();
505
506 printf("%s: Accepting connections\n", NAME);
507 task_retval(0);
508 async_manager();
509
510 /* Never reached */
511 return 0;
512}
513
514/** @}
515 */
Note: See TracBrowser for help on using the repository browser.