source: mainline/uspace/srv/hid/output/output.c@ acd7ac2

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since acd7ac2 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
RevLine 
[6d5e378]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
[4122410]29/** @addtogroup output
30 * @{
31 */
32
[6d5e378]33#include <errno.h>
[38d150e]34#include <stddef.h>
35#include <stdlib.h>
[6d5e378]36#include <macros.h>
37#include <as.h>
[2f6ad06]38#include <task.h>
[6d5e378]39#include <ipc/output.h>
[73d8600]40#include <config.h>
[6d5e378]41#include "port/ega.h"
[e53794c]42#include "port/chardev.h"
[1382446]43#include "port/ddev.h"
[6d5e378]44#include "output.h"
45
46typedef struct {
47 link_t link;
[a35b458]48
[6d5e378]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);
[a35b458]60
[6d5e378]61 outdev_t *dev = (outdev_t *) malloc(sizeof(outdev_t));
62 if (dev == NULL)
63 return NULL;
[a35b458]64
[6d5e378]65 link_initialize(&dev->link);
[a35b458]66
[6d5e378]67 dev->ops = *ops;
68 dev->data = data;
[a35b458]69
[6d5e378]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 }
[a35b458]77
[6d5e378]78 list_append(&dev->link, &outdevs);
79 return dev;
80}
81
[984a9ba]82static void srv_yield(ipc_call_t *icall)
[6d5e378]83{
[b7fd2a0]84 errno_t ret = EOK;
[a35b458]85
[feeac0d]86 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]87 assert(dev->ops.yield);
[a35b458]88
[b7fd2a0]89 errno_t rc = dev->ops.yield(dev);
[6d5e378]90 if (rc != EOK)
91 ret = rc;
92 }
[a35b458]93
[984a9ba]94 async_answer_0(icall, ret);
[6d5e378]95}
96
[984a9ba]97static void srv_claim(ipc_call_t *icall)
[6d5e378]98{
[b7fd2a0]99 errno_t ret = EOK;
[a35b458]100
[feeac0d]101 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]102 assert(dev->ops.claim);
[a35b458]103
[b7fd2a0]104 errno_t rc = dev->ops.claim(dev);
[6d5e378]105 if (rc != EOK)
106 ret = rc;
107 }
[a35b458]108
[984a9ba]109 async_answer_0(icall, ret);
[6d5e378]110}
111
[984a9ba]112static void srv_get_dimensions(ipc_call_t *icall)
[6d5e378]113{
[7fc81da]114 sysarg_t cols = 0;
115 sysarg_t rows = 0;
116 bool first;
[a35b458]117
[7fc81da]118 first = true;
[feeac0d]119 list_foreach(outdevs, link, outdev_t, dev) {
[7fc81da]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 }
[6d5e378]128 }
[a35b458]129
[984a9ba]130 async_answer_2(icall, EOK, cols, rows);
[6d5e378]131}
132
[984a9ba]133static void srv_get_caps(ipc_call_t *icall)
[6d5e378]134{
135 console_caps_t caps = 0;
[a35b458]136
[feeac0d]137 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]138 assert(dev->ops.get_caps);
[a35b458]139
[6d5e378]140 caps |= dev->ops.get_caps(dev);
141 }
[a35b458]142
[984a9ba]143 async_answer_1(icall, EOK, caps);
[6d5e378]144}
145
[984a9ba]146static frontbuf_t *resolve_frontbuf(sysarg_t handle, ipc_call_t *icall)
[6d5e378]147{
148 frontbuf_t *frontbuf = NULL;
[feeac0d]149 list_foreach(frontbufs, link, frontbuf_t, cur) {
[6d5e378]150 if (cur == (frontbuf_t *) handle) {
151 frontbuf = cur;
152 break;
153 }
154 }
[a35b458]155
[6d5e378]156 if (frontbuf == NULL) {
[984a9ba]157 async_answer_0(icall, ENOENT);
[6d5e378]158 return NULL;
159 }
[a35b458]160
[6d5e378]161 return frontbuf;
162}
163
[984a9ba]164static void srv_frontbuf_create(ipc_call_t *icall)
[6d5e378]165{
166 frontbuf_t *frontbuf = (frontbuf_t *) malloc(sizeof(frontbuf_t));
167 if (frontbuf == NULL) {
[984a9ba]168 async_answer_0(icall, ENOMEM);
[6d5e378]169 return;
170 }
[a35b458]171
[6d5e378]172 link_initialize(&frontbuf->link);
[a35b458]173
[984a9ba]174 ipc_call_t call;
175 if (!async_share_out_receive(&call, &frontbuf->size,
[6d5e378]176 &frontbuf->flags)) {
177 free(frontbuf);
[984a9ba]178 async_answer_0(icall, EINVAL);
[6d5e378]179 return;
180 }
[a35b458]181
[984a9ba]182 errno_t rc = async_share_out_finalize(&call, &frontbuf->data);
[6d5e378]183 if ((rc != EOK) || (frontbuf->data == AS_MAP_FAILED)) {
184 free(frontbuf);
[984a9ba]185 async_answer_0(icall, ENOMEM);
[6d5e378]186 return;
187 }
[a35b458]188
[6d5e378]189 list_append(&frontbuf->link, &frontbufs);
[984a9ba]190 async_answer_1(icall, EOK, (sysarg_t) frontbuf);
[6d5e378]191}
192
[984a9ba]193static void srv_frontbuf_destroy(ipc_call_t *icall)
[6d5e378]194{
[fafb8e5]195 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
[6d5e378]196 if (frontbuf == NULL)
197 return;
[a35b458]198
[6d5e378]199 list_remove(&frontbuf->link);
200 as_area_destroy(frontbuf->data);
201 free(frontbuf);
[a35b458]202
[984a9ba]203 async_answer_0(icall, EOK);
[6d5e378]204}
205
[984a9ba]206static void srv_cursor_update(ipc_call_t *icall)
[6d5e378]207{
[fafb8e5]208 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
[6d5e378]209 if (frontbuf == NULL)
210 return;
[a35b458]211
[6d5e378]212 chargrid_t *buf = (chargrid_t *) frontbuf->data;
213 bool visible = chargrid_get_cursor_visibility(buf);
[a35b458]214
[6d5e378]215 sysarg_t col;
216 sysarg_t row;
217 chargrid_get_cursor(buf, &col, &row);
[a35b458]218
[feeac0d]219 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]220 assert(dev->ops.cursor_update);
[a35b458]221
[6d5e378]222 sysarg_t prev_col;
223 sysarg_t prev_row;
224 chargrid_get_cursor(dev->backbuf, &prev_col, &prev_row);
[a35b458]225
[6d5e378]226 chargrid_set_cursor(dev->backbuf, col, row);
227 chargrid_set_cursor_visibility(dev->backbuf, visible);
[a35b458]228
[6d5e378]229 dev->ops.cursor_update(dev, prev_col, prev_row, col, row,
230 visible);
[bbc6277]231 dev->ops.flush(dev);
232
[6d5e378]233 }
[a35b458]234
[984a9ba]235 async_answer_0(icall, EOK);
[6d5e378]236}
237
[984a9ba]238static void srv_set_style(ipc_call_t *icall)
[6d5e378]239{
[feeac0d]240 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]241 dev->attrs.type = CHAR_ATTR_STYLE;
242 dev->attrs.val.style =
[fafb8e5]243 (console_style_t) ipc_get_arg1(icall);
[6d5e378]244 }
[a35b458]245
[984a9ba]246 async_answer_0(icall, EOK);
[6d5e378]247}
248
[984a9ba]249static void srv_set_color(ipc_call_t *icall)
[6d5e378]250{
[feeac0d]251 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]252 dev->attrs.type = CHAR_ATTR_INDEX;
253 dev->attrs.val.index.bgcolor =
[fafb8e5]254 (console_color_t) ipc_get_arg1(icall);
[6d5e378]255 dev->attrs.val.index.fgcolor =
[fafb8e5]256 (console_color_t) ipc_get_arg2(icall);
[6d5e378]257 dev->attrs.val.index.attr =
[fafb8e5]258 (console_color_attr_t) ipc_get_arg3(icall);
[6d5e378]259 }
[a35b458]260
[984a9ba]261 async_answer_0(icall, EOK);
[6d5e378]262}
263
[984a9ba]264static void srv_set_rgb_color(ipc_call_t *icall)
[6d5e378]265{
[feeac0d]266 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]267 dev->attrs.type = CHAR_ATTR_RGB;
[fafb8e5]268 dev->attrs.val.rgb.bgcolor = ipc_get_arg1(icall);
269 dev->attrs.val.rgb.fgcolor = ipc_get_arg2(icall);
[6d5e378]270 }
[a35b458]271
[984a9ba]272 async_answer_0(icall, EOK);
[6d5e378]273}
274
275static bool srv_update_scroll(outdev_t *dev, chargrid_t *buf)
276{
277 assert(dev->ops.char_update);
[a35b458]278
[6d5e378]279 sysarg_t top_row = chargrid_get_top_row(buf);
[a35b458]280
[6d5e378]281 if (dev->top_row == top_row)
282 return false;
[a35b458]283
[6d5e378]284 dev->top_row = top_row;
[a35b458]285
[6d5e378]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;
[a35b458]293
[6d5e378]294 if (front_field->ch != back_field->ch) {
295 back_field->ch = front_field->ch;
296 update = true;
297 }
[a35b458]298
[6d5e378]299 if (!attrs_same(front_field->attrs, back_field->attrs)) {
300 back_field->attrs = front_field->attrs;
301 update = true;
302 }
[a35b458]303
[6d5e378]304 front_field->flags &= ~CHAR_FLAG_DIRTY;
[a35b458]305
[6d5e378]306 if (update)
307 dev->ops.char_update(dev, x, y);
308 }
309 }
[a35b458]310
[6d5e378]311 return true;
312}
313
[984a9ba]314static void srv_update(ipc_call_t *icall)
[6d5e378]315{
[fafb8e5]316 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
[6d5e378]317 if (frontbuf == NULL)
318 return;
[a35b458]319
[6d5e378]320 chargrid_t *buf = (chargrid_t *) frontbuf->data;
[a35b458]321
[feeac0d]322 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]323 assert(dev->ops.char_update);
[a35b458]324
[6d5e378]325 if (srv_update_scroll(dev, buf))
326 continue;
[a35b458]327
[6d5e378]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;
[a35b458]335
[6d5e378]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 }
[a35b458]342
[6d5e378]343 if (!attrs_same(front_field->attrs,
344 back_field->attrs)) {
345 back_field->attrs = front_field->attrs;
346 update = true;
347 }
[a35b458]348
[6d5e378]349 front_field->flags &= ~CHAR_FLAG_DIRTY;
350 }
[a35b458]351
[6d5e378]352 if (update)
353 dev->ops.char_update(dev, x, y);
354 }
355 }
[a35b458]356
[bbc6277]357 dev->ops.flush(dev);
[6d5e378]358 }
[a35b458]359
[984a9ba]360 async_answer_0(icall, EOK);
[6d5e378]361}
362
[984a9ba]363static void srv_damage(ipc_call_t *icall)
[6d5e378]364{
[fafb8e5]365 frontbuf_t *frontbuf = resolve_frontbuf(ipc_get_arg1(icall), icall);
[6d5e378]366 if (frontbuf == NULL)
367 return;
[a35b458]368
[6d5e378]369 chargrid_t *buf = (chargrid_t *) frontbuf->data;
[a35b458]370
[feeac0d]371 list_foreach(outdevs, link, outdev_t, dev) {
[6d5e378]372 assert(dev->ops.char_update);
[a35b458]373
[6d5e378]374 if (srv_update_scroll(dev, buf))
375 continue;
[a35b458]376
[fafb8e5]377 sysarg_t col = ipc_get_arg2(icall);
378 sysarg_t row = ipc_get_arg3(icall);
[a35b458]379
[fafb8e5]380 sysarg_t cols = ipc_get_arg4(icall);
381 sysarg_t rows = ipc_get_arg5(icall);
[a35b458]382
[6d5e378]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);
[a35b458]389
[6d5e378]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 }
[bbc6277]396 dev->ops.flush(dev);
397
[6d5e378]398 }
[984a9ba]399
400 async_answer_0(icall, EOK);
[6d5e378]401}
402
[984a9ba]403static void client_connection(ipc_call_t *icall, void *arg)
[6d5e378]404{
405 /* Accept the connection */
[beb83c1]406 async_accept_0(icall);
[a35b458]407
[6d5e378]408 while (true) {
409 ipc_call_t call;
[984a9ba]410 async_get_call(&call);
[a35b458]411
[fafb8e5]412 if (!ipc_get_imethod(&call)) {
[984a9ba]413 async_answer_0(&call, EOK);
[6d5e378]414 break;
415 }
[a35b458]416
[fafb8e5]417 switch (ipc_get_imethod(&call)) {
[6feb444]418 case OUTPUT_YIELD:
[984a9ba]419 srv_yield(&call);
[6feb444]420 break;
421 case OUTPUT_CLAIM:
[984a9ba]422 srv_claim(&call);
[6feb444]423 break;
424 case OUTPUT_GET_DIMENSIONS:
[984a9ba]425 srv_get_dimensions(&call);
[6feb444]426 break;
427 case OUTPUT_GET_CAPS:
[984a9ba]428 srv_get_caps(&call);
[6feb444]429 break;
[a35b458]430
[6feb444]431 case OUTPUT_FRONTBUF_CREATE:
[984a9ba]432 srv_frontbuf_create(&call);
[6feb444]433 break;
434 case OUTPUT_FRONTBUF_DESTROY:
[984a9ba]435 srv_frontbuf_destroy(&call);
[6feb444]436 break;
[a35b458]437
[6feb444]438 case OUTPUT_CURSOR_UPDATE:
[984a9ba]439 srv_cursor_update(&call);
[6feb444]440 break;
441 case OUTPUT_SET_STYLE:
[984a9ba]442 srv_set_style(&call);
[6feb444]443 break;
444 case OUTPUT_SET_COLOR:
[984a9ba]445 srv_set_color(&call);
[6feb444]446 break;
447 case OUTPUT_SET_RGB_COLOR:
[984a9ba]448 srv_set_rgb_color(&call);
[6feb444]449 break;
450 case OUTPUT_UPDATE:
[984a9ba]451 srv_update(&call);
[6feb444]452 break;
453 case OUTPUT_DAMAGE:
[984a9ba]454 srv_damage(&call);
[6feb444]455 break;
[a35b458]456
[6feb444]457 default:
[984a9ba]458 async_answer_0(&call, EINVAL);
[6d5e378]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 }
[a35b458]474
[6d5e378]475 printf("%s: HelenOS output service\n", NAME);
[a35b458]476
[6d5e378]477 /* Register server */
[b688fd8]478 async_set_fallback_port_handler(client_connection, NULL);
[b7fd2a0]479 errno_t rc = loc_server_register(NAME);
[6d5e378]480 if (rc != EOK) {
481 printf("%s: Unable to register driver\n", NAME);
482 return rc;
483 }
[a35b458]484
[6d5e378]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 }
[a35b458]491
[73d8600]492 if (!config_key_exists("console")) {
493 ega_init();
[1382446]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
[73d8600]502 }
[a35b458]503
[9940ce0]504 chardev_init();
[a35b458]505
[6d5e378]506 printf("%s: Accepting connections\n", NAME);
507 task_retval(0);
508 async_manager();
[a35b458]509
[6d5e378]510 /* Never reached */
511 return 0;
512}
[4122410]513
514/** @}
515 */
Note: See TracBrowser for help on using the repository browser.