source: mainline/uspace/app/taskbar/taskbar.c@ 901b302

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 901b302 was 5d62130, checked in by Jiri Svoboda <jiri@…>, 3 years ago

Taskbar should be always on top

We add support for topmost windows and make the taskbar window topmost.

  • Property mode set to 100644
File size: 7.0 KB
RevLine 
[2139676]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>
[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/label.h>
42#include <ui/resource.h>
43#include <ui/ui.h>
44#include <ui/window.h>
[1766326]45#include <wndmgt.h>
[50a16d9]46#include "clock.h"
[2139676]47#include "taskbar.h"
[e0e612b]48#include "wndlist.h"
[2139676]49
[a6492460]50static void taskbar_wnd_close(ui_window_t *, void *);
51static void taskbar_wnd_pos(ui_window_t *, void *, pos_event_t *);
[2139676]52
53static ui_window_cb_t window_cb = {
[a6492460]54 .close = taskbar_wnd_close,
55 .pos = taskbar_wnd_pos
[2139676]56};
57
58/** Window close button was clicked.
59 *
60 * @param window Window
61 * @param arg Argument (taskbar)
62 */
[a6492460]63static void taskbar_wnd_close(ui_window_t *window, void *arg)
[2139676]64{
65 taskbar_t *taskbar = (taskbar_t *) arg;
66
67 ui_quit(taskbar->ui);
68}
69
[a6492460]70/** Window received position event.
71 *
72 * @param window Window
73 * @param arg Argument (taskbar)
74 * @param event Position event
75 */
76static void taskbar_wnd_pos(ui_window_t *window, void *arg, pos_event_t *event)
77{
78 taskbar_t *taskbar = (taskbar_t *) arg;
79
80 /* Remember ID of device that sent the last event */
81 taskbar->wndlist->ev_pos_id = event->pos_id;
82
83 ui_window_def_pos(window, event);
84}
85
[2139676]86/** Create task bar.
87 *
88 * @param display_spec Display specification
[1766326]89 * @param wndmgt_svc Window management service (or WNDMGT_DEFAULT)
[2139676]90 * @param rtaskbar Place to store pointer to new task bar
91 * @return @c EOK on success or an error coe
92 */
[1766326]93errno_t taskbar_create(const char *display_spec, const char *wndmgt_svc,
94 taskbar_t **rtaskbar)
[2139676]95{
96 ui_wnd_params_t params;
97 taskbar_t *taskbar = NULL;
[3fd38b2]98 gfx_rect_t scr_rect;
[2139676]99 gfx_rect_t rect;
100 ui_resource_t *ui_res;
101 errno_t rc;
102
103 taskbar = calloc(1, sizeof(taskbar_t));
104 if (taskbar == NULL) {
105 rc = ENOMEM;
106 goto error;
107 }
108
109 rc = ui_create(display_spec, &taskbar->ui);
110 if (rc != EOK) {
111 printf("Error creating UI on display %s.\n", display_spec);
112 goto error;
113 }
114
[3fd38b2]115 rc = ui_get_rect(taskbar->ui, &scr_rect);
116 if (rc != EOK) {
[c77cfd8]117 if (str_cmp(display_spec, UI_DISPLAY_NULL) != 0) {
118 printf("Error getting screen dimensions.\n");
119 goto error;
120 }
121
122 /* For the sake of unit tests */
123 scr_rect.p0.x = 0;
124 scr_rect.p0.y = 0;
125 scr_rect.p1.x = 100;
126 scr_rect.p1.y = 100;
[3fd38b2]127 }
128
[2139676]129 ui_wnd_params_init(&params);
130 params.caption = "Task Bar";
131 params.placement = ui_wnd_place_bottom_left;
[c48192e]132
133 /* Window has no titlebar */
[2139676]134 params.style &= ~ui_wds_titlebar;
135
[5d62130]136 /* Window is not obscured by other windows */
137 params.flags |= ui_wndf_topmost;
138
[c48192e]139 /* Prevent taskbar window from being listed in taskbar */
140 params.flags |= ui_wndf_system;
141
[3fd38b2]142 params.rect.p0.x = 0;
143 params.rect.p0.y = 0;
144 params.rect.p1.x = scr_rect.p1.x - scr_rect.p0.x;
145
[2139676]146 if (ui_is_textmode(taskbar->ui)) {
[50a16d9]147 params.rect.p1.y = 1;
148 params.style &= ~ui_wds_frame;
[2139676]149 } else {
150 params.rect.p1.y = 32;
151 }
152
153 rc = ui_window_create(taskbar->ui, &params, &taskbar->window);
154 if (rc != EOK) {
155 printf("Error creating window.\n");
156 goto error;
157 }
158
[a6492460]159 ui_window_set_cb(taskbar->window, &window_cb, (void *)taskbar);
[2139676]160 ui_res = ui_window_get_res(taskbar->window);
161
162 rc = ui_fixed_create(&taskbar->fixed);
163 if (rc != EOK) {
164 printf("Error creating fixed layout.\n");
165 goto error;
166 }
167
[e0e612b]168 rc = ui_label_create(ui_res, "HelenOS", &taskbar->label);
[2139676]169 if (rc != EOK) {
170 printf("Error creating label.\n");
171 goto error;
172 }
173
174 ui_window_get_app_rect(taskbar->window, &rect);
[e0e612b]175 if (ui_is_textmode(taskbar->ui)) {
176 rect.p0.x += 1;
177 } else {
178 rect.p0.x += 10;
179 }
[2139676]180 ui_label_set_rect(taskbar->label, &rect);
[e0e612b]181 ui_label_set_halign(taskbar->label, gfx_halign_left);
[2139676]182 ui_label_set_valign(taskbar->label, gfx_valign_center);
183
184 rc = ui_fixed_add(taskbar->fixed, ui_label_ctl(taskbar->label));
185 if (rc != EOK) {
186 printf("Error adding control to layout.\n");
187 ui_label_destroy(taskbar->label);
188 goto error;
189 }
190
[1b92d4b]191 rc = wndlist_create(taskbar->window, taskbar->fixed, &taskbar->wndlist);
[e0e612b]192 if (rc != EOK) {
193 printf("Error creating window list.\n");
194 goto error;
195 }
196
[2f106b0]197 if (ui_is_textmode(taskbar->ui)) {
198 rect.p0.x = params.rect.p0.x + 9;
199 rect.p0.y = 0;
200 rect.p1.x = params.rect.p1.x - 10;
201 rect.p1.y = 1;
202 } else {
203 rect.p0.x = params.rect.p0.x + 90;
204 rect.p0.y = 4;
205 rect.p1.x = params.rect.p1.x - 84;
206 rect.p1.y = 32 - 4;
207 }
208 wndlist_set_rect(taskbar->wndlist, &rect);
209
[1b92d4b]210 rc = wndlist_open_wm(taskbar->wndlist, wndmgt_svc);
[e0e612b]211 if (rc != EOK) {
[7a05d924]212 printf("Error attaching window management service.\n");
[e0e612b]213 goto error;
214 }
215
[50a16d9]216 rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
217 if (rc != EOK)
218 goto error;
219
[3fd38b2]220 if (ui_is_textmode(taskbar->ui)) {
221 rect.p0.x = params.rect.p1.x - 10;
222 rect.p0.y = 0;
223 rect.p1.x = params.rect.p1.x;
224 rect.p1.y = 1;
225 } else {
226 rect.p0.x = params.rect.p1.x - 80;
227 rect.p0.y = 4;
228 rect.p1.x = params.rect.p1.x - 4;
229 rect.p1.y = 32 - 4;
230 }
231
[50a16d9]232 taskbar_clock_set_rect(taskbar->clock, &rect);
233
234 rc = ui_fixed_add(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
235 if (rc != EOK) {
236 printf("Error adding control to layout.\n");
237 taskbar_clock_destroy(taskbar->clock);
238 goto error;
239 }
240
[2139676]241 ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
242
243 rc = ui_window_paint(taskbar->window);
244 if (rc != EOK) {
245 printf("Error painting window.\n");
246 goto error;
247 }
248
249 *rtaskbar = taskbar;
250 return EOK;
251error:
[50a16d9]252 if (taskbar->clock != NULL)
253 taskbar_clock_destroy(taskbar->clock);
[e0e612b]254 if (taskbar->wndlist != NULL)
255 wndlist_destroy(taskbar->wndlist);
[2139676]256 if (taskbar->window != NULL)
257 ui_window_destroy(taskbar->window);
258 if (taskbar->ui != NULL)
259 ui_destroy(taskbar->ui);
260 return rc;
261
262}
263
264/** Destroy task bar. */
265void taskbar_destroy(taskbar_t *taskbar)
266{
[2f106b0]267 ui_fixed_remove(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
[50a16d9]268 taskbar_clock_destroy(taskbar->clock);
[2139676]269 ui_window_destroy(taskbar->window);
270 ui_destroy(taskbar->ui);
[913add60]271}
272
[2139676]273/** @}
274 */
Note: See TracBrowser for help on using the repository browser.