1 | /*
|
---|
2 | * Copyright (c) 2022 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 taskbar
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Task Bar
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <gfx/coord.h>
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <str.h>
|
---|
39 | #include <ui/fixed.h>
|
---|
40 | #include <ui/label.h>
|
---|
41 | #include <ui/resource.h>
|
---|
42 | #include <ui/ui.h>
|
---|
43 | #include <ui/window.h>
|
---|
44 | #include <wndmgt.h>
|
---|
45 | #include "clock.h"
|
---|
46 | #include "taskbar.h"
|
---|
47 | #include "wndlist.h"
|
---|
48 |
|
---|
49 | static void wnd_close(ui_window_t *, void *);
|
---|
50 |
|
---|
51 | static ui_window_cb_t window_cb = {
|
---|
52 | .close = wnd_close
|
---|
53 | };
|
---|
54 |
|
---|
55 | static void taskbar_wm_window_added(void *, sysarg_t);
|
---|
56 | static void taskbar_wm_window_removed(void *, sysarg_t);
|
---|
57 |
|
---|
58 | static wndmgt_cb_t taskbar_wndmgt_cb = {
|
---|
59 | .window_added = taskbar_wm_window_added,
|
---|
60 | .window_removed = taskbar_wm_window_removed
|
---|
61 | };
|
---|
62 |
|
---|
63 | /** Window close button was clicked.
|
---|
64 | *
|
---|
65 | * @param window Window
|
---|
66 | * @param arg Argument (taskbar)
|
---|
67 | */
|
---|
68 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
69 | {
|
---|
70 | taskbar_t *taskbar = (taskbar_t *) arg;
|
---|
71 |
|
---|
72 | ui_quit(taskbar->ui);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Create task bar.
|
---|
76 | *
|
---|
77 | * @param display_spec Display specification
|
---|
78 | * @param wndmgt_svc Window management service (or WNDMGT_DEFAULT)
|
---|
79 | * @param rtaskbar Place to store pointer to new task bar
|
---|
80 | * @return @c EOK on success or an error coe
|
---|
81 | */
|
---|
82 | errno_t taskbar_create(const char *display_spec, const char *wndmgt_svc,
|
---|
83 | taskbar_t **rtaskbar)
|
---|
84 | {
|
---|
85 | ui_wnd_params_t params;
|
---|
86 | taskbar_t *taskbar = NULL;
|
---|
87 | gfx_rect_t scr_rect;
|
---|
88 | gfx_rect_t rect;
|
---|
89 | ui_resource_t *ui_res;
|
---|
90 | errno_t rc;
|
---|
91 |
|
---|
92 | taskbar = calloc(1, sizeof(taskbar_t));
|
---|
93 | if (taskbar == NULL) {
|
---|
94 | rc = ENOMEM;
|
---|
95 | goto error;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (wndmgt_svc != NULL) {
|
---|
99 | rc = wndmgt_open(wndmgt_svc, &taskbar_wndmgt_cb,
|
---|
100 | (void *)taskbar, &taskbar->wndmgt);
|
---|
101 | if (rc != EOK)
|
---|
102 | goto error;
|
---|
103 | }
|
---|
104 |
|
---|
105 | rc = ui_create(display_spec, &taskbar->ui);
|
---|
106 | if (rc != EOK) {
|
---|
107 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
108 | goto error;
|
---|
109 | }
|
---|
110 |
|
---|
111 | rc = ui_get_rect(taskbar->ui, &scr_rect);
|
---|
112 | if (rc != EOK) {
|
---|
113 | if (str_cmp(display_spec, UI_DISPLAY_NULL) != 0) {
|
---|
114 | printf("Error getting screen dimensions.\n");
|
---|
115 | goto error;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* For the sake of unit tests */
|
---|
119 | scr_rect.p0.x = 0;
|
---|
120 | scr_rect.p0.y = 0;
|
---|
121 | scr_rect.p1.x = 100;
|
---|
122 | scr_rect.p1.y = 100;
|
---|
123 | }
|
---|
124 |
|
---|
125 | ui_wnd_params_init(¶ms);
|
---|
126 | params.caption = "Task Bar";
|
---|
127 | params.placement = ui_wnd_place_bottom_left;
|
---|
128 | params.style &= ~ui_wds_titlebar;
|
---|
129 |
|
---|
130 | params.rect.p0.x = 0;
|
---|
131 | params.rect.p0.y = 0;
|
---|
132 | params.rect.p1.x = scr_rect.p1.x - scr_rect.p0.x;
|
---|
133 |
|
---|
134 | if (ui_is_textmode(taskbar->ui)) {
|
---|
135 | params.rect.p1.y = 1;
|
---|
136 | params.style &= ~ui_wds_frame;
|
---|
137 | } else {
|
---|
138 | params.rect.p1.y = 32;
|
---|
139 | }
|
---|
140 |
|
---|
141 | rc = ui_window_create(taskbar->ui, ¶ms, &taskbar->window);
|
---|
142 | if (rc != EOK) {
|
---|
143 | printf("Error creating window.\n");
|
---|
144 | goto error;
|
---|
145 | }
|
---|
146 |
|
---|
147 | ui_window_set_cb(taskbar->window, &window_cb, (void *) &taskbar);
|
---|
148 | ui_res = ui_window_get_res(taskbar->window);
|
---|
149 |
|
---|
150 | rc = ui_fixed_create(&taskbar->fixed);
|
---|
151 | if (rc != EOK) {
|
---|
152 | printf("Error creating fixed layout.\n");
|
---|
153 | goto error;
|
---|
154 | }
|
---|
155 |
|
---|
156 | rc = ui_label_create(ui_res, "HelenOS", &taskbar->label);
|
---|
157 | if (rc != EOK) {
|
---|
158 | printf("Error creating label.\n");
|
---|
159 | goto error;
|
---|
160 | }
|
---|
161 |
|
---|
162 | ui_window_get_app_rect(taskbar->window, &rect);
|
---|
163 | if (ui_is_textmode(taskbar->ui)) {
|
---|
164 | rect.p0.x += 1;
|
---|
165 | } else {
|
---|
166 | rect.p0.x += 10;
|
---|
167 | }
|
---|
168 | ui_label_set_rect(taskbar->label, &rect);
|
---|
169 | ui_label_set_halign(taskbar->label, gfx_halign_left);
|
---|
170 | ui_label_set_valign(taskbar->label, gfx_valign_center);
|
---|
171 |
|
---|
172 | rc = ui_fixed_add(taskbar->fixed, ui_label_ctl(taskbar->label));
|
---|
173 | if (rc != EOK) {
|
---|
174 | printf("Error adding control to layout.\n");
|
---|
175 | ui_label_destroy(taskbar->label);
|
---|
176 | goto error;
|
---|
177 | }
|
---|
178 |
|
---|
179 | rc = wndlist_create(ui_res, taskbar->fixed, &taskbar->wndlist);
|
---|
180 | if (rc != EOK) {
|
---|
181 | printf("Error creating window list.\n");
|
---|
182 | goto error;
|
---|
183 | }
|
---|
184 |
|
---|
185 | rc = wndlist_attach_wm(taskbar->wndlist, taskbar->wndmgt);
|
---|
186 | if (rc != EOK) {
|
---|
187 | printf("Error attaching window management service.\n");
|
---|
188 | goto error;
|
---|
189 | }
|
---|
190 |
|
---|
191 | rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
|
---|
192 | if (rc != EOK)
|
---|
193 | goto error;
|
---|
194 |
|
---|
195 | if (ui_is_textmode(taskbar->ui)) {
|
---|
196 | rect.p0.x = params.rect.p1.x - 10;
|
---|
197 | rect.p0.y = 0;
|
---|
198 | rect.p1.x = params.rect.p1.x;
|
---|
199 | rect.p1.y = 1;
|
---|
200 | } else {
|
---|
201 | rect.p0.x = params.rect.p1.x - 80;
|
---|
202 | rect.p0.y = 4;
|
---|
203 | rect.p1.x = params.rect.p1.x - 4;
|
---|
204 | rect.p1.y = 32 - 4;
|
---|
205 | }
|
---|
206 |
|
---|
207 | taskbar_clock_set_rect(taskbar->clock, &rect);
|
---|
208 |
|
---|
209 | rc = ui_fixed_add(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
|
---|
210 | if (rc != EOK) {
|
---|
211 | printf("Error adding control to layout.\n");
|
---|
212 | taskbar_clock_destroy(taskbar->clock);
|
---|
213 | goto error;
|
---|
214 | }
|
---|
215 |
|
---|
216 | ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
|
---|
217 |
|
---|
218 | rc = ui_window_paint(taskbar->window);
|
---|
219 | if (rc != EOK) {
|
---|
220 | printf("Error painting window.\n");
|
---|
221 | goto error;
|
---|
222 | }
|
---|
223 |
|
---|
224 | *rtaskbar = taskbar;
|
---|
225 | return EOK;
|
---|
226 | error:
|
---|
227 | if (taskbar->clock != NULL)
|
---|
228 | taskbar_clock_destroy(taskbar->clock);
|
---|
229 | if (taskbar->wndlist != NULL)
|
---|
230 | wndlist_destroy(taskbar->wndlist);
|
---|
231 | if (taskbar->window != NULL)
|
---|
232 | ui_window_destroy(taskbar->window);
|
---|
233 | if (taskbar->ui != NULL)
|
---|
234 | ui_destroy(taskbar->ui);
|
---|
235 | if (taskbar->wndmgt != NULL)
|
---|
236 | wndmgt_close(taskbar->wndmgt);
|
---|
237 | return rc;
|
---|
238 |
|
---|
239 | }
|
---|
240 |
|
---|
241 | /** Destroy task bar. */
|
---|
242 | void taskbar_destroy(taskbar_t *taskbar)
|
---|
243 | {
|
---|
244 | ui_fixed_remove(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
|
---|
245 | taskbar_clock_destroy(taskbar->clock);
|
---|
246 | ui_window_destroy(taskbar->window);
|
---|
247 | ui_destroy(taskbar->ui);
|
---|
248 | if (taskbar->wndmgt != NULL)
|
---|
249 | wndmgt_close(taskbar->wndmgt);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** Handle WM window added event.
|
---|
253 | *
|
---|
254 | * @param arg Argument (taskbar_t *)
|
---|
255 | * @param wnd_id Window ID
|
---|
256 | */
|
---|
257 | static void taskbar_wm_window_added(void *arg, sysarg_t wnd_id)
|
---|
258 | {
|
---|
259 | taskbar_t *taskbar = (taskbar_t *)arg;
|
---|
260 |
|
---|
261 | printf("wm_window_added: taskbar=%p wnd_id=%zu\n",
|
---|
262 | (void *)taskbar, wnd_id);
|
---|
263 | }
|
---|
264 |
|
---|
265 | /** Handle WM window removed event.
|
---|
266 | *
|
---|
267 | * @param arg Argument (taskbar_t *)
|
---|
268 | * @param wnd_id Window ID
|
---|
269 | */
|
---|
270 | static void taskbar_wm_window_removed(void *arg, sysarg_t wnd_id)
|
---|
271 | {
|
---|
272 | taskbar_t *taskbar = (taskbar_t *)arg;
|
---|
273 |
|
---|
274 | printf("wm_window_removed: taskbar=%p wnd_id=%zu\n",
|
---|
275 | (void *)taskbar, wnd_id);
|
---|
276 | }
|
---|
277 |
|
---|
278 | /** @}
|
---|
279 | */
|
---|