1 | /*
|
---|
2 | * Copyright (c) 2023 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 Taskbar
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <gfx/coord.h>
|
---|
36 | #include <io/pos_event.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <ui/fixed.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 "tbsmenu.h"
|
---|
48 | #include "wndlist.h"
|
---|
49 |
|
---|
50 | static void taskbar_wnd_close(ui_window_t *, void *);
|
---|
51 | static void taskbar_wnd_kbd(ui_window_t *, void *, kbd_event_t *);
|
---|
52 | static void taskbar_wnd_pos(ui_window_t *, void *, pos_event_t *);
|
---|
53 |
|
---|
54 | static ui_window_cb_t window_cb = {
|
---|
55 | .close = taskbar_wnd_close,
|
---|
56 | .kbd = taskbar_wnd_kbd,
|
---|
57 | .pos = taskbar_wnd_pos
|
---|
58 | };
|
---|
59 |
|
---|
60 | /** Window close button was clicked.
|
---|
61 | *
|
---|
62 | * @param window Window
|
---|
63 | * @param arg Argument (taskbar)
|
---|
64 | */
|
---|
65 | static void taskbar_wnd_close(ui_window_t *window, void *arg)
|
---|
66 | {
|
---|
67 | taskbar_t *taskbar = (taskbar_t *) arg;
|
---|
68 |
|
---|
69 | ui_quit(taskbar->ui);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /** Window received keyboard event.
|
---|
73 | *
|
---|
74 | * @param window Window
|
---|
75 | * @param arg Argument (taskbar)
|
---|
76 | * @param event Keyboard event
|
---|
77 | */
|
---|
78 | static void taskbar_wnd_kbd(ui_window_t *window, void *arg, kbd_event_t *event)
|
---|
79 | {
|
---|
80 | taskbar_t *taskbar = (taskbar_t *) arg;
|
---|
81 | ui_evclaim_t claim;
|
---|
82 |
|
---|
83 | /* Remember ID of device that sent the last event */
|
---|
84 | taskbar->wndlist->ev_idev_id = event->kbd_id;
|
---|
85 | taskbar->tbsmenu->ev_idev_id = event->kbd_id;
|
---|
86 |
|
---|
87 | claim = ui_window_def_kbd(window, event);
|
---|
88 | if (claim == ui_claimed)
|
---|
89 | return;
|
---|
90 |
|
---|
91 | if (event->type == KEY_PRESS && (event->mods & KM_CTRL) == 0 &&
|
---|
92 | (event->mods & KM_ALT) == 0 && (event->mods & KM_SHIFT) == 0 &&
|
---|
93 | event->key == KC_ENTER) {
|
---|
94 | if (!tbsmenu_is_open(taskbar->tbsmenu))
|
---|
95 | tbsmenu_open(taskbar->tbsmenu);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Window received position event.
|
---|
100 | *
|
---|
101 | * @param window Window
|
---|
102 | * @param arg Argument (taskbar)
|
---|
103 | * @param event Position event
|
---|
104 | */
|
---|
105 | static void taskbar_wnd_pos(ui_window_t *window, void *arg, pos_event_t *event)
|
---|
106 | {
|
---|
107 | taskbar_t *taskbar = (taskbar_t *) arg;
|
---|
108 |
|
---|
109 | /* Remember ID of device that sent the last event */
|
---|
110 | taskbar->wndlist->ev_idev_id = event->pos_id;
|
---|
111 | taskbar->tbsmenu->ev_idev_id = event->pos_id;
|
---|
112 |
|
---|
113 | ui_window_def_pos(window, event);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Create taskbar.
|
---|
117 | *
|
---|
118 | * @param display_spec Display specification
|
---|
119 | * @param wndmgt_svc Window management service (or WNDMGT_DEFAULT)
|
---|
120 | * @param rtaskbar Place to store pointer to new taskbar
|
---|
121 | * @return @c EOK on success or an error coe
|
---|
122 | */
|
---|
123 | errno_t taskbar_create(const char *display_spec, const char *wndmgt_svc,
|
---|
124 | taskbar_t **rtaskbar)
|
---|
125 | {
|
---|
126 | ui_wnd_params_t params;
|
---|
127 | taskbar_t *taskbar = NULL;
|
---|
128 | gfx_rect_t scr_rect;
|
---|
129 | gfx_rect_t rect;
|
---|
130 | errno_t rc;
|
---|
131 |
|
---|
132 | taskbar = calloc(1, sizeof(taskbar_t));
|
---|
133 | if (taskbar == NULL) {
|
---|
134 | rc = ENOMEM;
|
---|
135 | goto error;
|
---|
136 | }
|
---|
137 |
|
---|
138 | rc = ui_create(display_spec, &taskbar->ui);
|
---|
139 | if (rc != EOK) {
|
---|
140 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
141 | goto error;
|
---|
142 | }
|
---|
143 |
|
---|
144 | rc = ui_get_rect(taskbar->ui, &scr_rect);
|
---|
145 | if (rc != EOK) {
|
---|
146 | if (str_cmp(display_spec, UI_DISPLAY_NULL) != 0) {
|
---|
147 | printf("Error getting screen dimensions.\n");
|
---|
148 | goto error;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* For the sake of unit tests */
|
---|
152 | scr_rect.p0.x = 0;
|
---|
153 | scr_rect.p0.y = 0;
|
---|
154 | scr_rect.p1.x = 100;
|
---|
155 | scr_rect.p1.y = 100;
|
---|
156 | }
|
---|
157 |
|
---|
158 | ui_wnd_params_init(¶ms);
|
---|
159 | params.caption = "Taskbar";
|
---|
160 | params.placement = ui_wnd_place_bottom_left;
|
---|
161 |
|
---|
162 | /* Window has no titlebar */
|
---|
163 | params.style &= ~ui_wds_titlebar;
|
---|
164 |
|
---|
165 | /* Window is not obscured by other windows */
|
---|
166 | params.flags |= ui_wndf_topmost;
|
---|
167 |
|
---|
168 | /* Prevent taskbar window from being listed in taskbar */
|
---|
169 | params.flags |= ui_wndf_system;
|
---|
170 |
|
---|
171 | /* Make maximized windows avoid taskbar */
|
---|
172 | params.flags |= ui_wndf_avoid;
|
---|
173 |
|
---|
174 | params.rect.p0.x = 0;
|
---|
175 | params.rect.p0.y = 0;
|
---|
176 | params.rect.p1.x = scr_rect.p1.x - scr_rect.p0.x;
|
---|
177 |
|
---|
178 | if (ui_is_textmode(taskbar->ui)) {
|
---|
179 | params.rect.p1.y = 1;
|
---|
180 | params.style &= ~ui_wds_frame;
|
---|
181 | } else {
|
---|
182 | params.rect.p1.y = 32;
|
---|
183 | }
|
---|
184 |
|
---|
185 | rc = ui_window_create(taskbar->ui, ¶ms, &taskbar->window);
|
---|
186 | if (rc != EOK) {
|
---|
187 | printf("Error creating window.\n");
|
---|
188 | goto error;
|
---|
189 | }
|
---|
190 |
|
---|
191 | rc = ui_fixed_create(&taskbar->fixed);
|
---|
192 | if (rc != EOK) {
|
---|
193 | printf("Error creating fixed layout.\n");
|
---|
194 | goto error;
|
---|
195 | }
|
---|
196 |
|
---|
197 | rc = tbsmenu_create(taskbar->window, taskbar->fixed, &taskbar->tbsmenu);
|
---|
198 | if (rc != EOK) {
|
---|
199 | printf("Error creating start menu.\n");
|
---|
200 | goto error;
|
---|
201 | }
|
---|
202 |
|
---|
203 | rc = tbsmenu_load(taskbar->tbsmenu, "/cfg/taskbar.sif");
|
---|
204 | if (rc != EOK) {
|
---|
205 | printf("Error loading start menu from '%s'.\n",
|
---|
206 | "/cfg/taskbar.sif");
|
---|
207 | goto error;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (ui_is_textmode(taskbar->ui)) {
|
---|
211 | rect.p0.x = params.rect.p0.x + 1;
|
---|
212 | rect.p0.y = 0;
|
---|
213 | rect.p1.x = params.rect.p0.x + 9;
|
---|
214 | rect.p1.y = 1;
|
---|
215 | } else {
|
---|
216 | rect.p0.x = params.rect.p0.x + 5;
|
---|
217 | rect.p0.y = 4;
|
---|
218 | rect.p1.x = params.rect.p0.x + 84;
|
---|
219 | rect.p1.y = 32 - 4;
|
---|
220 | }
|
---|
221 |
|
---|
222 | tbsmenu_set_rect(taskbar->tbsmenu, &rect);
|
---|
223 |
|
---|
224 | rc = wndlist_create(taskbar->window, taskbar->fixed, &taskbar->wndlist);
|
---|
225 | if (rc != EOK) {
|
---|
226 | printf("Error creating window list.\n");
|
---|
227 | goto error;
|
---|
228 | }
|
---|
229 |
|
---|
230 | if (ui_is_textmode(taskbar->ui)) {
|
---|
231 | rect.p0.x = params.rect.p0.x + 10;
|
---|
232 | rect.p0.y = 0;
|
---|
233 | rect.p1.x = params.rect.p1.x - 10;
|
---|
234 | rect.p1.y = 1;
|
---|
235 | } else {
|
---|
236 | rect.p0.x = params.rect.p0.x + 90;
|
---|
237 | rect.p0.y = 4;
|
---|
238 | rect.p1.x = params.rect.p1.x - 84;
|
---|
239 | rect.p1.y = 32 - 4;
|
---|
240 | }
|
---|
241 | wndlist_set_rect(taskbar->wndlist, &rect);
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * We may not be able to open WM service if display server is not
|
---|
245 | * running. That's okay, there simply are no windows to manage.
|
---|
246 | */
|
---|
247 | rc = wndlist_open_wm(taskbar->wndlist, wndmgt_svc);
|
---|
248 | if (rc != EOK && rc != ENOENT) {
|
---|
249 | printf("Error attaching window management service.\n");
|
---|
250 | goto error;
|
---|
251 | }
|
---|
252 |
|
---|
253 | rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
|
---|
254 | if (rc != EOK)
|
---|
255 | goto error;
|
---|
256 |
|
---|
257 | if (ui_is_textmode(taskbar->ui)) {
|
---|
258 | rect.p0.x = params.rect.p1.x - 10;
|
---|
259 | rect.p0.y = 0;
|
---|
260 | rect.p1.x = params.rect.p1.x;
|
---|
261 | rect.p1.y = 1;
|
---|
262 | } else {
|
---|
263 | rect.p0.x = params.rect.p1.x - 80;
|
---|
264 | rect.p0.y = 4;
|
---|
265 | rect.p1.x = params.rect.p1.x - 4;
|
---|
266 | rect.p1.y = 32 - 4;
|
---|
267 | }
|
---|
268 |
|
---|
269 | taskbar_clock_set_rect(taskbar->clock, &rect);
|
---|
270 |
|
---|
271 | rc = ui_fixed_add(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
|
---|
272 | if (rc != EOK) {
|
---|
273 | printf("Error adding control to layout.\n");
|
---|
274 | taskbar_clock_destroy(taskbar->clock);
|
---|
275 | goto error;
|
---|
276 | }
|
---|
277 |
|
---|
278 | ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
|
---|
279 | ui_window_set_cb(taskbar->window, &window_cb, (void *)taskbar);
|
---|
280 |
|
---|
281 | rc = ui_window_paint(taskbar->window);
|
---|
282 | if (rc != EOK) {
|
---|
283 | printf("Error painting window.\n");
|
---|
284 | goto error;
|
---|
285 | }
|
---|
286 |
|
---|
287 | *rtaskbar = taskbar;
|
---|
288 | return EOK;
|
---|
289 | error:
|
---|
290 | if (taskbar->clock != NULL)
|
---|
291 | taskbar_clock_destroy(taskbar->clock);
|
---|
292 | if (taskbar->wndlist != NULL)
|
---|
293 | wndlist_destroy(taskbar->wndlist);
|
---|
294 | if (taskbar->tbsmenu != NULL)
|
---|
295 | tbsmenu_destroy(taskbar->tbsmenu);
|
---|
296 | if (taskbar->window != NULL)
|
---|
297 | ui_window_destroy(taskbar->window);
|
---|
298 | if (taskbar->ui != NULL)
|
---|
299 | ui_destroy(taskbar->ui);
|
---|
300 | return rc;
|
---|
301 |
|
---|
302 | }
|
---|
303 |
|
---|
304 | /** Destroy taskbar. */
|
---|
305 | void taskbar_destroy(taskbar_t *taskbar)
|
---|
306 | {
|
---|
307 | ui_fixed_remove(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
|
---|
308 | taskbar_clock_destroy(taskbar->clock);
|
---|
309 | wndlist_destroy(taskbar->wndlist);
|
---|
310 | tbsmenu_destroy(taskbar->tbsmenu);
|
---|
311 | ui_window_destroy(taskbar->window);
|
---|
312 | ui_destroy(taskbar->ui);
|
---|
313 | }
|
---|
314 |
|
---|
315 | /** @}
|
---|
316 | */
|
---|