source: mainline/uspace/app/taskbar/taskbar.c@ 95fc538

topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 95fc538 was 95fc538, checked in by Jiri Svoboda <jiri@…>, 20 months ago

It should be Taskbar not Task Bar

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[2139676]1/*
[06a61fc]2 * Copyright (c) 2023 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
[a6492460]50static void taskbar_wnd_close(ui_window_t *, void *);
[5d9403d5]51static void taskbar_wnd_kbd(ui_window_t *, void *, kbd_event_t *);
[a6492460]52static void taskbar_wnd_pos(ui_window_t *, void *, pos_event_t *);
[2139676]53
54static ui_window_cb_t window_cb = {
[a6492460]55 .close = taskbar_wnd_close,
[5d9403d5]56 .kbd = taskbar_wnd_kbd,
[a6492460]57 .pos = taskbar_wnd_pos
[2139676]58};
59
60/** Window close button was clicked.
61 *
62 * @param window Window
63 * @param arg Argument (taskbar)
64 */
[a6492460]65static void taskbar_wnd_close(ui_window_t *window, void *arg)
[2139676]66{
67 taskbar_t *taskbar = (taskbar_t *) arg;
68
69 ui_quit(taskbar->ui);
70}
71
[5d9403d5]72/** Window received keyboard event.
73 *
74 * @param window Window
75 * @param arg Argument (taskbar)
76 * @param event Keyboard event
77 */
78static 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
[a6492460]99/** Window received position event.
100 *
101 * @param window Window
102 * @param arg Argument (taskbar)
103 * @param event Position event
104 */
105static 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 */
[5d9403d5]110 taskbar->wndlist->ev_idev_id = event->pos_id;
111 taskbar->tbsmenu->ev_idev_id = event->pos_id;
[a6492460]112
113 ui_window_def_pos(window, event);
114}
115
[95fc538]116/** Create taskbar.
[2139676]117 *
118 * @param display_spec Display specification
[1766326]119 * @param wndmgt_svc Window management service (or WNDMGT_DEFAULT)
[95fc538]120 * @param rtaskbar Place to store pointer to new taskbar
[2139676]121 * @return @c EOK on success or an error coe
122 */
[1766326]123errno_t taskbar_create(const char *display_spec, const char *wndmgt_svc,
124 taskbar_t **rtaskbar)
[2139676]125{
126 ui_wnd_params_t params;
127 taskbar_t *taskbar = NULL;
[3fd38b2]128 gfx_rect_t scr_rect;
[2139676]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
[3fd38b2]144 rc = ui_get_rect(taskbar->ui, &scr_rect);
145 if (rc != EOK) {
[c77cfd8]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;
[3fd38b2]156 }
157
[2139676]158 ui_wnd_params_init(&params);
[95fc538]159 params.caption = "Taskbar";
[2139676]160 params.placement = ui_wnd_place_bottom_left;
[c48192e]161
162 /* Window has no titlebar */
[2139676]163 params.style &= ~ui_wds_titlebar;
164
[5d62130]165 /* Window is not obscured by other windows */
166 params.flags |= ui_wndf_topmost;
167
[c48192e]168 /* Prevent taskbar window from being listed in taskbar */
169 params.flags |= ui_wndf_system;
170
[29a5a99]171 /* Make maximized windows avoid taskbar */
172 params.flags |= ui_wndf_avoid;
173
[3fd38b2]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
[2139676]178 if (ui_is_textmode(taskbar->ui)) {
[50a16d9]179 params.rect.p1.y = 1;
180 params.style &= ~ui_wds_frame;
[2139676]181 } else {
182 params.rect.p1.y = 32;
183 }
184
185 rc = ui_window_create(taskbar->ui, &params, &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
[06a61fc]197 rc = tbsmenu_create(taskbar->window, taskbar->fixed, &taskbar->tbsmenu);
[2139676]198 if (rc != EOK) {
[06a61fc]199 printf("Error creating start menu.\n");
[2139676]200 goto error;
201 }
202
[b279899]203 rc = tbsmenu_load(taskbar->tbsmenu, "/cfg/taskbar.sif");
[7d78e466]204 if (rc != EOK) {
205 printf("Error loading start menu from '%s'.\n",
[b279899]206 "/cfg/taskbar.sif");
[7d78e466]207 goto error;
208 }
209
[e0e612b]210 if (ui_is_textmode(taskbar->ui)) {
[06a61fc]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;
[e0e612b]215 } else {
[06a61fc]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;
[e0e612b]220 }
[2139676]221
[06a61fc]222 tbsmenu_set_rect(taskbar->tbsmenu, &rect);
[2139676]223
[1b92d4b]224 rc = wndlist_create(taskbar->window, taskbar->fixed, &taskbar->wndlist);
[e0e612b]225 if (rc != EOK) {
226 printf("Error creating window list.\n");
227 goto error;
228 }
229
[2f106b0]230 if (ui_is_textmode(taskbar->ui)) {
[06a61fc]231 rect.p0.x = params.rect.p0.x + 10;
[2f106b0]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
[489f405]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 */
[1b92d4b]247 rc = wndlist_open_wm(taskbar->wndlist, wndmgt_svc);
[489f405]248 if (rc != EOK && rc != ENOENT) {
[7a05d924]249 printf("Error attaching window management service.\n");
[e0e612b]250 goto error;
251 }
252
[50a16d9]253 rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
254 if (rc != EOK)
255 goto error;
256
[3fd38b2]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
[50a16d9]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
[2139676]278 ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
[7aa9fa1b]279 ui_window_set_cb(taskbar->window, &window_cb, (void *)taskbar);
[2139676]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;
289error:
[50a16d9]290 if (taskbar->clock != NULL)
291 taskbar_clock_destroy(taskbar->clock);
[e0e612b]292 if (taskbar->wndlist != NULL)
293 wndlist_destroy(taskbar->wndlist);
[06a61fc]294 if (taskbar->tbsmenu != NULL)
295 tbsmenu_destroy(taskbar->tbsmenu);
[2139676]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
[95fc538]304/** Destroy taskbar. */
[2139676]305void taskbar_destroy(taskbar_t *taskbar)
306{
[2f106b0]307 ui_fixed_remove(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
[50a16d9]308 taskbar_clock_destroy(taskbar->clock);
[06a61fc]309 wndlist_destroy(taskbar->wndlist);
310 tbsmenu_destroy(taskbar->tbsmenu);
[2139676]311 ui_window_destroy(taskbar->window);
312 ui_destroy(taskbar->ui);
[913add60]313}
314
[2139676]315/** @}
316 */
Note: See TracBrowser for help on using the repository browser.