source: mainline/uspace/lib/gfx/src/backend/console.c@ 9be2358

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9be2358 was 9be2358, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Caller needs entire console GC to be able to destroy it properly

  • Property mode set to 100644
File size: 4.2 KB
Line 
1/*
2 * Copyright (c) 2019 Jiri Svoboda
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 libgfx
30 * @{
31 */
32/**
33 * @file GFX console backend
34 *
35 * This implements a graphics context over a classic console interface.
36 * This is just for experimentation purposes. In the end we want the
37 * console to actually directly suport GFX interface.
38 */
39
40#include <gfx/backend/console.h>
41#include <gfx/context.h>
42#include <gfx/render.h>
43#include <io/pixel.h>
44#include <stdlib.h>
45#include "../../private/backend/console.h"
46#include "../../private/color.h"
47
48static errno_t console_gc_set_color(void *, gfx_color_t *);
49static errno_t console_gc_fill_rect(void *, gfx_rect_t *);
50
51gfx_context_ops_t console_gc_ops = {
52 .set_color = console_gc_set_color,
53 .fill_rect = console_gc_fill_rect
54};
55
56/** Set color on console GC.
57 *
58 * Set drawing color on console GC.
59 *
60 * @param arg Console GC
61 * @param color Color
62 *
63 * @return EOK on success or an error code
64 */
65static errno_t console_gc_set_color(void *arg, gfx_color_t *color)
66{
67 console_gc_t *cgc = (console_gc_t *) arg;
68 pixel_t clr;
69
70 clr = PIXEL(0, color->r >> 8, color->g >> 8, color->b >> 8);
71
72 console_set_rgb_color(cgc->con, clr, clr);
73 return EOK;
74}
75
76/** Fill rectangle on console GC.
77 *
78 * @param arg Console GC
79 * @param rect Rectangle
80 *
81 * @return EOK on success or an error code
82 */
83static errno_t console_gc_fill_rect(void *arg, gfx_rect_t *rect)
84{
85 console_gc_t *cgc = (console_gc_t *) arg;
86 int rv;
87 int x, y;
88
89 // XXX We should handle p0.x > p1.x and p0.y > p1.y
90
91 for (y = rect->p0.y; y < rect->p1.y; y++) {
92 console_set_pos(cgc->con, rect->p0.x, y);
93
94 for (x = rect->p0.x; x < rect->p1.x; x++) {
95 rv = fputc('X', cgc->fout);
96 if (rv < 0)
97 return EIO;
98 }
99
100 console_flush(cgc->con);
101 }
102
103 return EOK;
104}
105
106/** Create console GC.
107 *
108 * Create graphics context for rendering into a console.
109 *
110 * @param con Console object
111 * @param fout File to which characters are written (console)
112 * @param rgc Place to store pointer to new GC.
113 *
114 * @return EOK on success or an error code
115 */
116errno_t console_gc_create(console_ctrl_t *con, FILE *fout,
117 console_gc_t **rgc)
118{
119 console_gc_t *cgc = NULL;
120 gfx_context_t *gc = NULL;
121 errno_t rc;
122
123 cgc = calloc(1, sizeof(console_gc_t));
124 if (cgc == NULL) {
125 rc = ENOMEM;
126 goto error;
127 }
128
129 rc = gfx_context_new(&console_gc_ops, cgc, &gc);
130 if (rc != EOK)
131 goto error;
132
133 cgc->gc = gc;
134 cgc->con = con;
135 cgc->fout = fout;
136 *rgc = cgc;
137 return EOK;
138error:
139 if (cgc != NULL)
140 free(cgc);
141 gfx_context_delete(gc);
142 return rc;
143}
144
145/** Delete console GC.
146 *
147 * @param cgc Console GC
148 */
149errno_t console_gc_delete(console_gc_t *cgc)
150{
151 errno_t rc;
152
153 rc = gfx_context_delete(cgc->gc);
154 if (rc != EOK)
155 return rc;
156
157 free(cgc);
158 return EOK;
159}
160
161/** Get generic graphic context from console GC.
162 *
163 * @param cgc Console GC
164 * @return Graphic context
165 */
166gfx_context_t *console_gc_get_ctx(console_gc_t *cgc)
167{
168 return cgc->gc;
169}
170
171/** @}
172 */
Note: See TracBrowser for help on using the repository browser.