source: mainline/uspace/app/taskbar/taskbar.c@ d92b8e8f

Last change on this file since d92b8e8f was d92b8e8f, checked in by Jiri Svoboda <jiri@…>, 16 months ago

Start menu support for passing input device ID (multiseat)

  • Property mode set to 100644
File size: 9.3 KB
RevLine 
[2139676]1/*
[ee3b28a9]2 * Copyright (c) 2024 Jiri Svoboda
[2139676]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 */
[95fc538]32/** @file Taskbar
[2139676]33 */
34
35#include <gfx/coord.h>
[a6492460]36#include <io/pos_event.h>
[2139676]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>
[1766326]44#include <wndmgt.h>
[50a16d9]45#include "clock.h"
[2139676]46#include "taskbar.h"
[06a61fc]47#include "tbsmenu.h"
[e0e612b]48#include "wndlist.h"
[2139676]49
[ee3b28a9]50#define TASKBAR_CONFIG_FILE "/cfg/taskbar.sif"
51
[a6492460]52static void taskbar_wnd_close(ui_window_t *, void *);
[5d9403d5]53static void taskbar_wnd_kbd(ui_window_t *, void *, kbd_event_t *);
[a6492460]54static void taskbar_wnd_pos(ui_window_t *, void *, pos_event_t *);
[ee3b28a9]55static void taskbar_notif_cb(void *);
[2139676]56
57static ui_window_cb_t window_cb = {
[a6492460]58 .close = taskbar_wnd_close,
[5d9403d5]59 .kbd = taskbar_wnd_kbd,
[a6492460]60 .pos = taskbar_wnd_pos
[2139676]61};
62
63/** Window close button was clicked.
64 *
65 * @param window Window
66 * @param arg Argument (taskbar)
67 */
[a6492460]68static void taskbar_wnd_close(ui_window_t *window, void *arg)
[2139676]69{
70 taskbar_t *taskbar = (taskbar_t *) arg;
71
72 ui_quit(taskbar->ui);
73}
74
[5d9403d5]75/** Window received keyboard event.
76 *
77 * @param window Window
78 * @param arg Argument (taskbar)
79 * @param event Keyboard event
80 */
81static 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
[a6492460]102/** Window received position event.
103 *
104 * @param window Window
105 * @param arg Argument (taskbar)
106 * @param event Position event
107 */
108static 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 */
[5d9403d5]113 taskbar->wndlist->ev_idev_id = event->pos_id;
114 taskbar->tbsmenu->ev_idev_id = event->pos_id;
[a6492460]115
116 ui_window_def_pos(window, event);
117}
118
[95fc538]119/** Create taskbar.
[2139676]120 *
121 * @param display_spec Display specification
[1766326]122 * @param wndmgt_svc Window management service (or WNDMGT_DEFAULT)
[95fc538]123 * @param rtaskbar Place to store pointer to new taskbar
[2139676]124 * @return @c EOK on success or an error coe
125 */
[1766326]126errno_t taskbar_create(const char *display_spec, const char *wndmgt_svc,
127 taskbar_t **rtaskbar)
[2139676]128{
129 ui_wnd_params_t params;
130 taskbar_t *taskbar = NULL;
[3fd38b2]131 gfx_rect_t scr_rect;
[2139676]132 gfx_rect_t rect;
[d92b8e8f]133 char *dspec = NULL;
134 char *qmark;
[2139676]135 errno_t rc;
136
137 taskbar = calloc(1, sizeof(taskbar_t));
138 if (taskbar == NULL) {
[d92b8e8f]139 printf("Out of memory.\n");
[2139676]140 rc = ENOMEM;
141 goto error;
142 }
143
[d92b8e8f]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
[2139676]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
[3fd38b2]162 rc = ui_get_rect(taskbar->ui, &scr_rect);
163 if (rc != EOK) {
[c77cfd8]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;
[3fd38b2]174 }
175
[2139676]176 ui_wnd_params_init(&params);
[95fc538]177 params.caption = "Taskbar";
[2139676]178 params.placement = ui_wnd_place_bottom_left;
[c48192e]179
180 /* Window has no titlebar */
[2139676]181 params.style &= ~ui_wds_titlebar;
182
[5d62130]183 /* Window is not obscured by other windows */
184 params.flags |= ui_wndf_topmost;
185
[c48192e]186 /* Prevent taskbar window from being listed in taskbar */
187 params.flags |= ui_wndf_system;
188
[29a5a99]189 /* Make maximized windows avoid taskbar */
190 params.flags |= ui_wndf_avoid;
191
[3fd38b2]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
[2139676]196 if (ui_is_textmode(taskbar->ui)) {
[50a16d9]197 params.rect.p1.y = 1;
198 params.style &= ~ui_wds_frame;
[2139676]199 } else {
200 params.rect.p1.y = 32;
201 }
202
203 rc = ui_window_create(taskbar->ui, &params, &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
[d92b8e8f]215 rc = tbsmenu_create(taskbar->window, taskbar->fixed, dspec,
216 &taskbar->tbsmenu);
[2139676]217 if (rc != EOK) {
[06a61fc]218 printf("Error creating start menu.\n");
[2139676]219 goto error;
220 }
221
[ee3b28a9]222 rc = tbsmenu_load(taskbar->tbsmenu, TASKBAR_CONFIG_FILE);
[7d78e466]223 if (rc != EOK) {
224 printf("Error loading start menu from '%s'.\n",
[ee3b28a9]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");
[7d78e466]232 }
233
[e0e612b]234 if (ui_is_textmode(taskbar->ui)) {
[06a61fc]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;
[e0e612b]239 } else {
[06a61fc]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;
[e0e612b]244 }
[2139676]245
[06a61fc]246 tbsmenu_set_rect(taskbar->tbsmenu, &rect);
[2139676]247
[1b92d4b]248 rc = wndlist_create(taskbar->window, taskbar->fixed, &taskbar->wndlist);
[e0e612b]249 if (rc != EOK) {
250 printf("Error creating window list.\n");
251 goto error;
252 }
253
[2f106b0]254 if (ui_is_textmode(taskbar->ui)) {
[06a61fc]255 rect.p0.x = params.rect.p0.x + 10;
[2f106b0]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
[489f405]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 */
[1b92d4b]271 rc = wndlist_open_wm(taskbar->wndlist, wndmgt_svc);
[489f405]272 if (rc != EOK && rc != ENOENT) {
[7a05d924]273 printf("Error attaching window management service.\n");
[e0e612b]274 goto error;
275 }
276
[50a16d9]277 rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
278 if (rc != EOK)
279 goto error;
280
[3fd38b2]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
[50a16d9]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
[2139676]302 ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
[7aa9fa1b]303 ui_window_set_cb(taskbar->window, &window_cb, (void *)taskbar);
[2139676]304
305 rc = ui_window_paint(taskbar->window);
306 if (rc != EOK) {
307 printf("Error painting window.\n");
308 goto error;
309 }
310
[d92b8e8f]311 free(dspec);
[2139676]312 *rtaskbar = taskbar;
313 return EOK;
314error:
[d92b8e8f]315 if (dspec != NULL)
316 free(dspec);
[ee3b28a9]317 if (taskbar->lst != NULL)
318 tbarcfg_listener_destroy(taskbar->lst);
[50a16d9]319 if (taskbar->clock != NULL)
320 taskbar_clock_destroy(taskbar->clock);
[e0e612b]321 if (taskbar->wndlist != NULL)
322 wndlist_destroy(taskbar->wndlist);
[06a61fc]323 if (taskbar->tbsmenu != NULL)
324 tbsmenu_destroy(taskbar->tbsmenu);
[2139676]325 if (taskbar->window != NULL)
326 ui_window_destroy(taskbar->window);
327 if (taskbar->ui != NULL)
328 ui_destroy(taskbar->ui);
[d92b8e8f]329 free(taskbar);
[2139676]330 return rc;
331
332}
333
[95fc538]334/** Destroy taskbar. */
[2139676]335void taskbar_destroy(taskbar_t *taskbar)
336{
[ee3b28a9]337 if (taskbar->lst != NULL)
338 tbarcfg_listener_destroy(taskbar->lst);
[2f106b0]339 ui_fixed_remove(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
[50a16d9]340 taskbar_clock_destroy(taskbar->clock);
[06a61fc]341 wndlist_destroy(taskbar->wndlist);
342 tbsmenu_destroy(taskbar->tbsmenu);
[2139676]343 ui_window_destroy(taskbar->window);
344 ui_destroy(taskbar->ui);
[913add60]345}
346
[ee3b28a9]347/** Configuration change notification callback.
348 *
349 * Called when configuration changed.
350 *
351 * @param arg Argument (taskbar_t *)
352 */
353static 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
[2139676]362/** @}
363 */
Note: See TracBrowser for help on using the repository browser.