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