[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 | */
|
---|
| 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/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] | 50 | static void taskbar_wnd_close(ui_window_t *, void *);
|
---|
| 51 | static void taskbar_wnd_pos(ui_window_t *, void *, pos_event_t *);
|
---|
[2139676] | 52 |
|
---|
| 53 | static 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] | 63 | static 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 | */
|
---|
| 76 | static 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] | 93 | errno_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 | errno_t rc;
|
---|
| 101 |
|
---|
| 102 | taskbar = calloc(1, sizeof(taskbar_t));
|
---|
| 103 | if (taskbar == NULL) {
|
---|
| 104 | rc = ENOMEM;
|
---|
| 105 | goto error;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | rc = ui_create(display_spec, &taskbar->ui);
|
---|
| 109 | if (rc != EOK) {
|
---|
| 110 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
| 111 | goto error;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[3fd38b2] | 114 | rc = ui_get_rect(taskbar->ui, &scr_rect);
|
---|
| 115 | if (rc != EOK) {
|
---|
[c77cfd8] | 116 | if (str_cmp(display_spec, UI_DISPLAY_NULL) != 0) {
|
---|
| 117 | printf("Error getting screen dimensions.\n");
|
---|
| 118 | goto error;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /* For the sake of unit tests */
|
---|
| 122 | scr_rect.p0.x = 0;
|
---|
| 123 | scr_rect.p0.y = 0;
|
---|
| 124 | scr_rect.p1.x = 100;
|
---|
| 125 | scr_rect.p1.y = 100;
|
---|
[3fd38b2] | 126 | }
|
---|
| 127 |
|
---|
[2139676] | 128 | ui_wnd_params_init(¶ms);
|
---|
| 129 | params.caption = "Task Bar";
|
---|
| 130 | params.placement = ui_wnd_place_bottom_left;
|
---|
[c48192e] | 131 |
|
---|
| 132 | /* Window has no titlebar */
|
---|
[2139676] | 133 | params.style &= ~ui_wds_titlebar;
|
---|
| 134 |
|
---|
[5d62130] | 135 | /* Window is not obscured by other windows */
|
---|
| 136 | params.flags |= ui_wndf_topmost;
|
---|
| 137 |
|
---|
[c48192e] | 138 | /* Prevent taskbar window from being listed in taskbar */
|
---|
| 139 | params.flags |= ui_wndf_system;
|
---|
| 140 |
|
---|
[29a5a99] | 141 | /* Make maximized windows avoid taskbar */
|
---|
| 142 | params.flags |= ui_wndf_avoid;
|
---|
| 143 |
|
---|
[3fd38b2] | 144 | params.rect.p0.x = 0;
|
---|
| 145 | params.rect.p0.y = 0;
|
---|
| 146 | params.rect.p1.x = scr_rect.p1.x - scr_rect.p0.x;
|
---|
| 147 |
|
---|
[2139676] | 148 | if (ui_is_textmode(taskbar->ui)) {
|
---|
[50a16d9] | 149 | params.rect.p1.y = 1;
|
---|
| 150 | params.style &= ~ui_wds_frame;
|
---|
[2139676] | 151 | } else {
|
---|
| 152 | params.rect.p1.y = 32;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | rc = ui_window_create(taskbar->ui, ¶ms, &taskbar->window);
|
---|
| 156 | if (rc != EOK) {
|
---|
| 157 | printf("Error creating window.\n");
|
---|
| 158 | goto error;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[a6492460] | 161 | ui_window_set_cb(taskbar->window, &window_cb, (void *)taskbar);
|
---|
[2139676] | 162 |
|
---|
| 163 | rc = ui_fixed_create(&taskbar->fixed);
|
---|
| 164 | if (rc != EOK) {
|
---|
| 165 | printf("Error creating fixed layout.\n");
|
---|
| 166 | goto error;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[06a61fc] | 169 | rc = tbsmenu_create(taskbar->window, taskbar->fixed, &taskbar->tbsmenu);
|
---|
[2139676] | 170 | if (rc != EOK) {
|
---|
[06a61fc] | 171 | printf("Error creating start menu.\n");
|
---|
[2139676] | 172 | goto error;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[e0e612b] | 175 | if (ui_is_textmode(taskbar->ui)) {
|
---|
[06a61fc] | 176 | rect.p0.x = params.rect.p0.x + 1;
|
---|
| 177 | rect.p0.y = 0;
|
---|
| 178 | rect.p1.x = params.rect.p0.x + 9;
|
---|
| 179 | rect.p1.y = 1;
|
---|
[e0e612b] | 180 | } else {
|
---|
[06a61fc] | 181 | rect.p0.x = params.rect.p0.x + 5;
|
---|
| 182 | rect.p0.y = 4;
|
---|
| 183 | rect.p1.x = params.rect.p0.x + 84;
|
---|
| 184 | rect.p1.y = 32 - 4;
|
---|
[e0e612b] | 185 | }
|
---|
[2139676] | 186 |
|
---|
[06a61fc] | 187 | tbsmenu_set_rect(taskbar->tbsmenu, &rect);
|
---|
[2139676] | 188 |
|
---|
[1b92d4b] | 189 | rc = wndlist_create(taskbar->window, taskbar->fixed, &taskbar->wndlist);
|
---|
[e0e612b] | 190 | if (rc != EOK) {
|
---|
| 191 | printf("Error creating window list.\n");
|
---|
| 192 | goto error;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[2f106b0] | 195 | if (ui_is_textmode(taskbar->ui)) {
|
---|
[06a61fc] | 196 | rect.p0.x = params.rect.p0.x + 10;
|
---|
[2f106b0] | 197 | rect.p0.y = 0;
|
---|
| 198 | rect.p1.x = params.rect.p1.x - 10;
|
---|
| 199 | rect.p1.y = 1;
|
---|
| 200 | } else {
|
---|
| 201 | rect.p0.x = params.rect.p0.x + 90;
|
---|
| 202 | rect.p0.y = 4;
|
---|
| 203 | rect.p1.x = params.rect.p1.x - 84;
|
---|
| 204 | rect.p1.y = 32 - 4;
|
---|
| 205 | }
|
---|
| 206 | wndlist_set_rect(taskbar->wndlist, &rect);
|
---|
| 207 |
|
---|
[1b92d4b] | 208 | rc = wndlist_open_wm(taskbar->wndlist, wndmgt_svc);
|
---|
[e0e612b] | 209 | if (rc != EOK) {
|
---|
[7a05d924] | 210 | printf("Error attaching window management service.\n");
|
---|
[e0e612b] | 211 | goto error;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[50a16d9] | 214 | rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
|
---|
| 215 | if (rc != EOK)
|
---|
| 216 | goto error;
|
---|
| 217 |
|
---|
[3fd38b2] | 218 | if (ui_is_textmode(taskbar->ui)) {
|
---|
| 219 | rect.p0.x = params.rect.p1.x - 10;
|
---|
| 220 | rect.p0.y = 0;
|
---|
| 221 | rect.p1.x = params.rect.p1.x;
|
---|
| 222 | rect.p1.y = 1;
|
---|
| 223 | } else {
|
---|
| 224 | rect.p0.x = params.rect.p1.x - 80;
|
---|
| 225 | rect.p0.y = 4;
|
---|
| 226 | rect.p1.x = params.rect.p1.x - 4;
|
---|
| 227 | rect.p1.y = 32 - 4;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[50a16d9] | 230 | taskbar_clock_set_rect(taskbar->clock, &rect);
|
---|
| 231 |
|
---|
| 232 | rc = ui_fixed_add(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
|
---|
| 233 | if (rc != EOK) {
|
---|
| 234 | printf("Error adding control to layout.\n");
|
---|
| 235 | taskbar_clock_destroy(taskbar->clock);
|
---|
| 236 | goto error;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[2139676] | 239 | ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
|
---|
| 240 |
|
---|
| 241 | rc = ui_window_paint(taskbar->window);
|
---|
| 242 | if (rc != EOK) {
|
---|
| 243 | printf("Error painting window.\n");
|
---|
| 244 | goto error;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | *rtaskbar = taskbar;
|
---|
| 248 | return EOK;
|
---|
| 249 | error:
|
---|
[50a16d9] | 250 | if (taskbar->clock != NULL)
|
---|
| 251 | taskbar_clock_destroy(taskbar->clock);
|
---|
[e0e612b] | 252 | if (taskbar->wndlist != NULL)
|
---|
| 253 | wndlist_destroy(taskbar->wndlist);
|
---|
[06a61fc] | 254 | if (taskbar->tbsmenu != NULL)
|
---|
| 255 | tbsmenu_destroy(taskbar->tbsmenu);
|
---|
[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. */
|
---|
| 265 | void 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);
|
---|
[06a61fc] | 269 | wndlist_destroy(taskbar->wndlist);
|
---|
| 270 | tbsmenu_destroy(taskbar->tbsmenu);
|
---|
[2139676] | 271 | ui_window_destroy(taskbar->window);
|
---|
| 272 | ui_destroy(taskbar->ui);
|
---|
[913add60] | 273 | }
|
---|
| 274 |
|
---|
[2139676] | 275 | /** @}
|
---|
| 276 | */
|
---|