source: mainline/uspace/app/gfxdemo/gfxdemo.c@ 78a71936

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

Display server scaffolding

  • Property mode set to 100644
File size: 5.4 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 gfxdemo
30 * @{
31 */
32/** @file Graphic demo
33 */
34
35#include <canvas.h>
36#include <congfx/console.h>
37#include <draw/surface.h>
38#include <display.h>
39#include <fibril.h>
40#include <guigfx/canvas.h>
41#include <gfx/color.h>
42#include <gfx/render.h>
43#include <io/console.h>
44#include <stdlib.h>
45#include <str.h>
46#include <window.h>
47
48/** Run rectangle demo on a graphic context.
49 *
50 * @param gc Graphic context
51 */
52static errno_t demo_rects(gfx_context_t *gc, int w, int h)
53{
54 gfx_color_t *color = NULL;
55 gfx_rect_t rect;
56 int i;
57 errno_t rc;
58
59 while (true) {
60 rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000,
61 rand() % 0x10000, &color);
62 if (rc != EOK)
63 return rc;
64
65 rc = gfx_set_color(gc, color);
66 if (rc != EOK)
67 return rc;
68
69 for (i = 0; i < 10; i++) {
70 rect.p0.x = rand() % (w - 1);
71 rect.p0.y = rand() % (h - 1);
72 rect.p1.x = rect.p0.x + rand() % (w - 1 - rect.p0.x);
73 rect.p1.y = rect.p0.y + rand() % (h - 1 - rect.p0.y);
74
75 rc = gfx_fill_rect(gc, &rect);
76 if (rc != EOK)
77 return rc;
78 }
79
80 gfx_color_delete(color);
81
82 fibril_usleep(500 * 1000);
83 }
84}
85
86/** Run demo on console. */
87static errno_t demo_console(void)
88{
89 console_ctrl_t *con = NULL;
90 console_gc_t *cgc = NULL;
91 gfx_context_t *gc;
92 errno_t rc;
93
94 printf("Init console..\n");
95 con = console_init(stdin, stdout);
96 if (con == NULL)
97 return EIO;
98
99 printf("Create console GC\n");
100 rc = console_gc_create(con, stdout, &cgc);
101 if (rc != EOK)
102 return rc;
103
104 gc = console_gc_get_ctx(cgc);
105
106 rc = demo_rects(gc, 80, 25);
107 if (rc != EOK)
108 return rc;
109
110 rc = console_gc_delete(cgc);
111 if (rc != EOK)
112 return rc;
113
114 return EOK;
115}
116
117/** Run demo on canvas. */
118static errno_t demo_canvas(void)
119{
120 canvas_gc_t *cgc = NULL;
121 gfx_context_t *gc;
122 window_t *window = NULL;
123 pixel_t *pixbuf = NULL;
124 surface_t *surface = NULL;
125 canvas_t *canvas = NULL;
126 int vw, vh;
127 errno_t rc;
128
129 printf("Init canvas..\n");
130
131 window = window_open("comp:0/winreg", NULL,
132 WINDOW_MAIN | WINDOW_DECORATED, "GFX Demo");
133 if (window == NULL) {
134 printf("Error creating window.\n");
135 return -1;
136 }
137
138 vw = 400;
139 vh = 300;
140
141 pixbuf = calloc(vw * vh, sizeof(pixel_t));
142 if (pixbuf == NULL) {
143 printf("Error allocating memory for pixel buffer.\n");
144 return ENOMEM;
145 }
146
147 surface = surface_create(vw, vh, pixbuf, 0);
148 if (surface == NULL) {
149 printf("Error creating surface.\n");
150 return EIO;
151 }
152
153 canvas = create_canvas(window_root(window), NULL, vw, vh,
154 surface);
155 if (canvas == NULL) {
156 printf("Error creating canvas.\n");
157 return EIO;
158 }
159
160 window_resize(window, 0, 0, vw + 10, vh + 30, WINDOW_PLACEMENT_ANY);
161 window_exec(window);
162
163 printf("Create canvas GC\n");
164 rc = canvas_gc_create(canvas, surface, &cgc);
165 if (rc != EOK)
166 return rc;
167
168 gc = canvas_gc_get_ctx(cgc);
169
170 rc = demo_rects(gc, 400, 300);
171 if (rc != EOK)
172 return rc;
173
174 rc = canvas_gc_delete(cgc);
175 if (rc != EOK)
176 return rc;
177
178 return EOK;
179}
180
181/** Run demo on display server. */
182static errno_t demo_display(void)
183{
184 display_t *display = NULL;
185 gfx_context_t *gc;
186 display_window_t *window = NULL;
187 errno_t rc;
188
189 printf("Init display..\n");
190
191 rc = display_open(NULL, &display);
192 if (rc != EOK) {
193 printf("Error opening display.\n");
194 return rc;
195 }
196
197 rc = display_window_create(display, &window);
198 if (rc != EOK) {
199 printf("Error creating window.\n");
200 return rc;
201 }
202
203 rc = display_window_get_gc(window, &gc);
204 if (rc != EOK) {
205 printf("Error getting graphics context.\n");
206 }
207
208 rc = demo_rects(gc, 400, 300);
209 if (rc != EOK)
210 return rc;
211
212 rc = gfx_context_delete(gc);
213 if (rc != EOK)
214 return rc;
215
216 return EOK;
217}
218
219static void print_syntax(void)
220{
221 printf("syntax: gfxdemo {canvas|console|display}\n");
222}
223
224int main(int argc, char *argv[])
225{
226 errno_t rc;
227
228 if (argc < 2) {
229 print_syntax();
230 return 1;
231 }
232
233 if (str_cmp(argv[1], "console") == 0) {
234 rc = demo_console();
235 if (rc != EOK)
236 return 1;
237 } else if (str_cmp(argv[1], "canvas") == 0) {
238 rc = demo_canvas();
239 if (rc != EOK)
240 return 1;
241 } else if (str_cmp(argv[1], "display") == 0) {
242 rc = demo_display();
243 if (rc != EOK)
244 return 1;
245 } else {
246 print_syntax();
247 return 1;
248 }
249}
250
251/** @}
252 */
Note: See TracBrowser for help on using the repository browser.