source: mainline/uspace/srv/hid/output/output.c@ 35f2bb1b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 35f2bb1b was 8d2dd7f2, checked in by Jakub Jermar <jakub@…>, 8 years ago

Reduce the number of files that include <sys/types.h>

  • Property mode set to 100644
File size: 11.3 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#include <stddef.h>
30#include <errno.h>
31#include <malloc.h>
32#include <macros.h>
33#include <as.h>
34#include <task.h>
35#include <ipc/output.h>
36#include <config.h>
37#include "port/ega.h"
38#include "port/kchar.h"
39#include "port/niagara.h"
40#include "port/ski.h"
41#include "port/chardev.h"
42#include "output.h"
43
44#define MAX_COLS 128
45#define MAX_ROWS 128
46
47typedef struct {
48 link_t link;
49
50 size_t size;
51 unsigned int flags;
52 void *data;
53} frontbuf_t;
54
55static LIST_INITIALIZE(outdevs);
56static LIST_INITIALIZE(frontbufs);
57
58outdev_t *outdev_register(outdev_ops_t *ops, void *data)
59{
60 assert(ops->get_dimensions);
61
62 outdev_t *dev = (outdev_t *) malloc(sizeof(outdev_t));
63 if (dev == NULL)
64 return NULL;
65
66 link_initialize(&dev->link);
67
68 dev->ops = *ops;
69 dev->data = data;
70
71 ops->get_dimensions(dev, &dev->cols, &dev->rows);
72 dev->backbuf = chargrid_create(dev->cols, dev->rows,
73 CHARGRID_FLAG_NONE);
74 if (dev->backbuf == NULL) {
75 free(dev);
76 return NULL;
77 }
78
79 list_append(&dev->link, &outdevs);
80 return dev;
81}
82
83static void srv_yield(ipc_callid_t iid, ipc_call_t *icall)
84{
85 int ret = EOK;
86
87 list_foreach(outdevs, link, outdev_t, dev) {
88 assert(dev->ops.yield);
89
90 int rc = dev->ops.yield(dev);
91 if (rc != EOK)
92 ret = rc;
93 }
94
95 async_answer_0(iid, ret);
96}
97
98static void srv_claim(ipc_callid_t iid, ipc_call_t *icall)
99{
100 int ret = EOK;
101
102 list_foreach(outdevs, link, outdev_t, dev) {
103 assert(dev->ops.claim);
104
105 int rc = dev->ops.claim(dev);
106 if (rc != EOK)
107 ret = rc;
108 }
109
110 async_answer_0(iid, ret);
111}
112
113static void srv_get_dimensions(ipc_callid_t iid, ipc_call_t *icall)
114{
115 sysarg_t cols = MAX_COLS;
116 sysarg_t rows = MAX_ROWS;
117
118 list_foreach(outdevs, link, outdev_t, dev) {
119 cols = min(cols, dev->cols);
120 rows = min(rows, dev->rows);
121 }
122
123 async_answer_2(iid, EOK, cols, rows);
124}
125
126static void srv_get_caps(ipc_callid_t iid, ipc_call_t *icall)
127{
128 console_caps_t caps = 0;
129
130 list_foreach(outdevs, link, outdev_t, dev) {
131 assert(dev->ops.get_caps);
132
133 caps |= dev->ops.get_caps(dev);
134 }
135
136 async_answer_1(iid, EOK, caps);
137}
138
139static frontbuf_t *resolve_frontbuf(sysarg_t handle, ipc_callid_t iid)
140{
141 frontbuf_t *frontbuf = NULL;
142 list_foreach(frontbufs, link, frontbuf_t, cur) {
143 if (cur == (frontbuf_t *) handle) {
144 frontbuf = cur;
145 break;
146 }
147 }
148
149 if (frontbuf == NULL) {
150 async_answer_0(iid, ENOENT);
151 return NULL;
152 }
153
154 return frontbuf;
155}
156
157static void srv_frontbuf_create(ipc_callid_t iid, ipc_call_t *icall)
158{
159 frontbuf_t *frontbuf = (frontbuf_t *) malloc(sizeof(frontbuf_t));
160 if (frontbuf == NULL) {
161 async_answer_0(iid, ENOMEM);
162 return;
163 }
164
165 link_initialize(&frontbuf->link);
166
167 ipc_callid_t callid;
168 if (!async_share_out_receive(&callid, &frontbuf->size,
169 &frontbuf->flags)) {
170 free(frontbuf);
171 async_answer_0(iid, EINVAL);
172 return;
173 }
174
175 int rc = async_share_out_finalize(callid, &frontbuf->data);
176 if ((rc != EOK) || (frontbuf->data == AS_MAP_FAILED)) {
177 free(frontbuf);
178 async_answer_0(iid, ENOMEM);
179 return;
180 }
181
182 list_append(&frontbuf->link, &frontbufs);
183 async_answer_1(iid, EOK, (sysarg_t) frontbuf);
184}
185
186static void srv_frontbuf_destroy(ipc_callid_t iid, ipc_call_t *icall)
187{
188 frontbuf_t *frontbuf = resolve_frontbuf(IPC_GET_ARG1(*icall), iid);
189 if (frontbuf == NULL)
190 return;
191
192 list_remove(&frontbuf->link);
193 as_area_destroy(frontbuf->data);
194 free(frontbuf);
195
196 async_answer_0(iid, EOK);
197}
198
199static void srv_cursor_update(ipc_callid_t iid, ipc_call_t *icall)
200{
201 frontbuf_t *frontbuf = resolve_frontbuf(IPC_GET_ARG1(*icall), iid);
202 if (frontbuf == NULL)
203 return;
204
205 chargrid_t *buf = (chargrid_t *) frontbuf->data;
206 bool visible = chargrid_get_cursor_visibility(buf);
207
208 sysarg_t col;
209 sysarg_t row;
210 chargrid_get_cursor(buf, &col, &row);
211
212 list_foreach(outdevs, link, outdev_t, dev) {
213 assert(dev->ops.cursor_update);
214
215 sysarg_t prev_col;
216 sysarg_t prev_row;
217 chargrid_get_cursor(dev->backbuf, &prev_col, &prev_row);
218
219 chargrid_set_cursor(dev->backbuf, col, row);
220 chargrid_set_cursor_visibility(dev->backbuf, visible);
221
222 dev->ops.cursor_update(dev, prev_col, prev_row, col, row,
223 visible);
224 }
225
226 async_answer_0(iid, EOK);
227}
228
229static void srv_set_style(ipc_callid_t iid, ipc_call_t *icall)
230{
231 list_foreach(outdevs, link, outdev_t, dev) {
232 dev->attrs.type = CHAR_ATTR_STYLE;
233 dev->attrs.val.style =
234 (console_style_t) IPC_GET_ARG1(*icall);
235 }
236
237 async_answer_0(iid, EOK);
238}
239
240static void srv_set_color(ipc_callid_t iid, ipc_call_t *icall)
241{
242 list_foreach(outdevs, link, outdev_t, dev) {
243 dev->attrs.type = CHAR_ATTR_INDEX;
244 dev->attrs.val.index.bgcolor =
245 (console_color_t) IPC_GET_ARG1(*icall);
246 dev->attrs.val.index.fgcolor =
247 (console_color_t) IPC_GET_ARG2(*icall);
248 dev->attrs.val.index.attr =
249 (console_color_attr_t) IPC_GET_ARG3(*icall);
250 }
251
252 async_answer_0(iid, EOK);
253}
254
255static void srv_set_rgb_color(ipc_callid_t iid, ipc_call_t *icall)
256{
257 list_foreach(outdevs, link, outdev_t, dev) {
258 dev->attrs.type = CHAR_ATTR_RGB;
259 dev->attrs.val.rgb.bgcolor = IPC_GET_ARG1(*icall);
260 dev->attrs.val.rgb.fgcolor = IPC_GET_ARG2(*icall);
261 }
262
263 async_answer_0(iid, EOK);
264}
265
266static bool srv_update_scroll(outdev_t *dev, chargrid_t *buf)
267{
268 assert(dev->ops.char_update);
269
270 sysarg_t top_row = chargrid_get_top_row(buf);
271
272 if (dev->top_row == top_row)
273 return false;
274
275 dev->top_row = top_row;
276
277 for (sysarg_t y = 0; y < dev->rows; y++) {
278 for (sysarg_t x = 0; x < dev->cols; x++) {
279 charfield_t *front_field =
280 chargrid_charfield_at(buf, x, y);
281 charfield_t *back_field =
282 chargrid_charfield_at(dev->backbuf, x, y);
283 bool update = false;
284
285 if (front_field->ch != back_field->ch) {
286 back_field->ch = front_field->ch;
287 update = true;
288 }
289
290 if (!attrs_same(front_field->attrs, back_field->attrs)) {
291 back_field->attrs = front_field->attrs;
292 update = true;
293 }
294
295 front_field->flags &= ~CHAR_FLAG_DIRTY;
296
297 if (update)
298 dev->ops.char_update(dev, x, y);
299 }
300 }
301
302 return true;
303}
304
305static void srv_update(ipc_callid_t iid, ipc_call_t *icall)
306{
307 frontbuf_t *frontbuf = resolve_frontbuf(IPC_GET_ARG1(*icall), iid);
308 if (frontbuf == NULL)
309 return;
310
311 chargrid_t *buf = (chargrid_t *) frontbuf->data;
312
313 list_foreach(outdevs, link, outdev_t, dev) {
314 assert(dev->ops.char_update);
315
316 if (srv_update_scroll(dev, buf))
317 continue;
318
319 for (sysarg_t y = 0; y < dev->rows; y++) {
320 for (sysarg_t x = 0; x < dev->cols; x++) {
321 charfield_t *front_field =
322 chargrid_charfield_at(buf, x, y);
323 charfield_t *back_field =
324 chargrid_charfield_at(dev->backbuf, x, y);
325 bool update = false;
326
327 if ((front_field->flags & CHAR_FLAG_DIRTY) ==
328 CHAR_FLAG_DIRTY) {
329 if (front_field->ch != back_field->ch) {
330 back_field->ch = front_field->ch;
331 update = true;
332 }
333
334 if (!attrs_same(front_field->attrs,
335 back_field->attrs)) {
336 back_field->attrs = front_field->attrs;
337 update = true;
338 }
339
340 front_field->flags &= ~CHAR_FLAG_DIRTY;
341 }
342
343 if (update)
344 dev->ops.char_update(dev, x, y);
345 }
346 }
347 }
348
349 async_answer_0(iid, EOK);
350}
351
352static void srv_damage(ipc_callid_t iid, ipc_call_t *icall)
353{
354 frontbuf_t *frontbuf = resolve_frontbuf(IPC_GET_ARG1(*icall), iid);
355 if (frontbuf == NULL)
356 return;
357
358 chargrid_t *buf = (chargrid_t *) frontbuf->data;
359
360 list_foreach(outdevs, link, outdev_t, dev) {
361 assert(dev->ops.char_update);
362
363 if (srv_update_scroll(dev, buf))
364 continue;
365
366 sysarg_t col = IPC_GET_ARG2(*icall);
367 sysarg_t row = IPC_GET_ARG3(*icall);
368
369 sysarg_t cols = IPC_GET_ARG4(*icall);
370 sysarg_t rows = IPC_GET_ARG5(*icall);
371
372 for (sysarg_t y = 0; y < rows; y++) {
373 for (sysarg_t x = 0; x < cols; x++) {
374 charfield_t *front_field =
375 chargrid_charfield_at(buf, col + x, row + y);
376 charfield_t *back_field =
377 chargrid_charfield_at(dev->backbuf, col + x, row + y);
378
379 back_field->ch = front_field->ch;
380 back_field->attrs = front_field->attrs;
381 front_field->flags &= ~CHAR_FLAG_DIRTY;
382 dev->ops.char_update(dev, col + x, row + y);
383 }
384 }
385 }
386
387 async_answer_0(iid, EOK);
388}
389
390static void client_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
391{
392 /* Accept the connection */
393 async_answer_0(iid, EOK);
394
395 while (true) {
396 ipc_call_t call;
397 ipc_callid_t callid = async_get_call(&call);
398
399 if (!IPC_GET_IMETHOD(call)) {
400 async_answer_0(callid, EOK);
401 break;
402 }
403
404 switch (IPC_GET_IMETHOD(call)) {
405 case OUTPUT_YIELD:
406 srv_yield(callid, &call);
407 break;
408 case OUTPUT_CLAIM:
409 srv_claim(callid, &call);
410 break;
411 case OUTPUT_GET_DIMENSIONS:
412 srv_get_dimensions(callid, &call);
413 break;
414 case OUTPUT_GET_CAPS:
415 srv_get_caps(callid, &call);
416 break;
417
418 case OUTPUT_FRONTBUF_CREATE:
419 srv_frontbuf_create(callid, &call);
420 break;
421 case OUTPUT_FRONTBUF_DESTROY:
422 srv_frontbuf_destroy(callid, &call);
423 break;
424
425 case OUTPUT_CURSOR_UPDATE:
426 srv_cursor_update(callid, &call);
427 break;
428 case OUTPUT_SET_STYLE:
429 srv_set_style(callid, &call);
430 break;
431 case OUTPUT_SET_COLOR:
432 srv_set_color(callid, &call);
433 break;
434 case OUTPUT_SET_RGB_COLOR:
435 srv_set_rgb_color(callid, &call);
436 break;
437 case OUTPUT_UPDATE:
438 srv_update(callid, &call);
439 break;
440 case OUTPUT_DAMAGE:
441 srv_damage(callid, &call);
442 break;
443
444 default:
445 async_answer_0(callid, EINVAL);
446 }
447 }
448}
449
450static void usage(char *name)
451{
452 printf("Usage: %s <service_name>\n", name);
453}
454
455int main(int argc, char *argv[])
456{
457 if (argc < 2) {
458 usage(argv[0]);
459 return 1;
460 }
461
462 printf("%s: HelenOS output service\n", NAME);
463
464 /* Register server */
465 async_set_fallback_port_handler(client_connection, NULL);
466 int rc = loc_server_register(NAME);
467 if (rc != EOK) {
468 printf("%s: Unable to register driver\n", NAME);
469 return rc;
470 }
471
472 service_id_t service_id;
473 rc = loc_service_register(argv[1], &service_id);
474 if (rc != EOK) {
475 printf("%s: Unable to register service %s\n", NAME, argv[1]);
476 return rc;
477 }
478
479 if (!config_key_exists("console")) {
480 ega_init();
481 kchar_init();
482 niagara_init();
483 ski_init();
484 } else {
485 chardev_init();
486 }
487
488 printf("%s: Accepting connections\n", NAME);
489 task_retval(0);
490 async_manager();
491
492 /* Never reached */
493 return 0;
494}
Note: See TracBrowser for help on using the repository browser.