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 display
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file GFX window backend
|
---|
34 | *
|
---|
35 | * This implements a graphics context over display server window.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #include <gfx/color.h>
|
---|
39 | #include <gfx/context.h>
|
---|
40 | #include <gfx/render.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include "display.h"
|
---|
44 | #include "window.h"
|
---|
45 |
|
---|
46 | static errno_t ds_window_set_color(void *, gfx_color_t *);
|
---|
47 | static errno_t ds_window_fill_rect(void *, gfx_rect_t *);
|
---|
48 |
|
---|
49 | gfx_context_ops_t ds_window_ops = {
|
---|
50 | .set_color = ds_window_set_color,
|
---|
51 | .fill_rect = ds_window_fill_rect
|
---|
52 | };
|
---|
53 |
|
---|
54 | /** Set color on window GC.
|
---|
55 | *
|
---|
56 | * Set drawing color on window GC.
|
---|
57 | *
|
---|
58 | * @param arg Console GC
|
---|
59 | * @param color Color
|
---|
60 | *
|
---|
61 | * @return EOK on success or an error code
|
---|
62 | */
|
---|
63 | static errno_t ds_window_set_color(void *arg, gfx_color_t *color)
|
---|
64 | {
|
---|
65 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
66 |
|
---|
67 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_set_color");
|
---|
68 | return gfx_set_color(wnd->display->gc, color);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Fill rectangle on window GC.
|
---|
72 | *
|
---|
73 | * @param arg Window GC
|
---|
74 | * @param rect Rectangle
|
---|
75 | *
|
---|
76 | * @return EOK on success or an error code
|
---|
77 | */
|
---|
78 | static errno_t ds_window_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
79 | {
|
---|
80 | ds_window_t *wnd = (ds_window_t *) arg;
|
---|
81 |
|
---|
82 | log_msg(LOG_DEFAULT, LVL_NOTE, "gc_fill_rect");
|
---|
83 | return gfx_fill_rect(wnd->display->gc, rect);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Create window.
|
---|
87 | *
|
---|
88 | * Create graphics context for rendering into a window.
|
---|
89 | *
|
---|
90 | * @param disp Display to create window on
|
---|
91 | * @param rgc Place to store pointer to new GC.
|
---|
92 | *
|
---|
93 | * @return EOK on success or an error code
|
---|
94 | */
|
---|
95 | errno_t ds_window_create(ds_display_t *disp, ds_window_t **rgc)
|
---|
96 | {
|
---|
97 | ds_window_t *wnd = NULL;
|
---|
98 | gfx_context_t *gc = NULL;
|
---|
99 | errno_t rc;
|
---|
100 |
|
---|
101 | wnd = calloc(1, sizeof(ds_window_t));
|
---|
102 | if (wnd == NULL) {
|
---|
103 | rc = ENOMEM;
|
---|
104 | goto error;
|
---|
105 | }
|
---|
106 |
|
---|
107 | rc = gfx_context_new(&ds_window_ops, wnd, &gc);
|
---|
108 | if (rc != EOK)
|
---|
109 | goto error;
|
---|
110 |
|
---|
111 | ds_display_add_window(disp, wnd);
|
---|
112 |
|
---|
113 | wnd->gc = gc;
|
---|
114 | *rgc = wnd;
|
---|
115 | return EOK;
|
---|
116 | error:
|
---|
117 | if (wnd != NULL)
|
---|
118 | free(wnd);
|
---|
119 | gfx_context_delete(gc);
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /** Delete window GC.
|
---|
124 | *
|
---|
125 | * @param wnd Window GC
|
---|
126 | */
|
---|
127 | errno_t ds_window_delete(ds_window_t *wnd)
|
---|
128 | {
|
---|
129 | errno_t rc;
|
---|
130 |
|
---|
131 | ds_display_remove_window(wnd);
|
---|
132 |
|
---|
133 | rc = gfx_context_delete(wnd->gc);
|
---|
134 | if (rc != EOK)
|
---|
135 | return rc;
|
---|
136 |
|
---|
137 | free(wnd);
|
---|
138 | return EOK;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Get generic graphic context from window.
|
---|
142 | *
|
---|
143 | * @param wnd Window
|
---|
144 | * @return Graphic context
|
---|
145 | */
|
---|
146 | gfx_context_t *ds_window_get_ctx(ds_window_t *wnd)
|
---|
147 | {
|
---|
148 | return wnd->gc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** @}
|
---|
152 | */
|
---|