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

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

Proper timer cleanup, unit tests

Destroying a timer that sets itself again (i.e., runs periodically) is
a little tricky. It requires a handshake (similar to destroying a worker
thread). Realizing that this handshake works correctly is a little bit
mind boggling.

  • Property mode set to 100644
File size: 5.4 KB
Line 
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>
36#include <stdio.h>
37#include <stdlib.h>
38#include <str.h>
39#include <ui/fixed.h>
40#include <ui/label.h>
41#include <ui/resource.h>
42#include <ui/ui.h>
43#include <ui/window.h>
44#include "clock.h"
45#include "taskbar.h"
46
47static void wnd_close(ui_window_t *, void *);
48
49static ui_window_cb_t window_cb = {
50 .close = wnd_close
51};
52
53/** Window close button was clicked.
54 *
55 * @param window Window
56 * @param arg Argument (taskbar)
57 */
58static void wnd_close(ui_window_t *window, void *arg)
59{
60 taskbar_t *taskbar = (taskbar_t *) arg;
61
62 ui_quit(taskbar->ui);
63}
64
65/** Create task bar.
66 *
67 * @param display_spec Display specification
68 * @param rtaskbar Place to store pointer to new task bar
69 * @return @c EOK on success or an error coe
70 */
71errno_t taskbar_create(const char *display_spec, taskbar_t **rtaskbar)
72{
73 ui_wnd_params_t params;
74 taskbar_t *taskbar = NULL;
75 gfx_rect_t scr_rect;
76 gfx_rect_t rect;
77 ui_resource_t *ui_res;
78 errno_t rc;
79
80 taskbar = calloc(1, sizeof(taskbar_t));
81 if (taskbar == NULL) {
82 rc = ENOMEM;
83 goto error;
84 }
85
86 rc = ui_create(display_spec, &taskbar->ui);
87 if (rc != EOK) {
88 printf("Error creating UI on display %s.\n", display_spec);
89 goto error;
90 }
91
92 rc = ui_get_rect(taskbar->ui, &scr_rect);
93 if (rc != EOK) {
94 if (str_cmp(display_spec, UI_DISPLAY_NULL) != 0) {
95 printf("Error getting screen dimensions.\n");
96 goto error;
97 }
98
99 /* For the sake of unit tests */
100 scr_rect.p0.x = 0;
101 scr_rect.p0.y = 0;
102 scr_rect.p1.x = 100;
103 scr_rect.p1.y = 100;
104 }
105
106 ui_wnd_params_init(&params);
107 params.caption = "Task Bar";
108 params.placement = ui_wnd_place_bottom_left;
109 params.style &= ~ui_wds_titlebar;
110
111 params.rect.p0.x = 0;
112 params.rect.p0.y = 0;
113 params.rect.p1.x = scr_rect.p1.x - scr_rect.p0.x;
114
115 if (ui_is_textmode(taskbar->ui)) {
116 params.rect.p1.y = 1;
117 params.style &= ~ui_wds_frame;
118 } else {
119 params.rect.p1.y = 32;
120 }
121
122 rc = ui_window_create(taskbar->ui, &params, &taskbar->window);
123 if (rc != EOK) {
124 printf("Error creating window.\n");
125 goto error;
126 }
127
128 ui_window_set_cb(taskbar->window, &window_cb, (void *) &taskbar);
129 ui_res = ui_window_get_res(taskbar->window);
130
131 rc = ui_fixed_create(&taskbar->fixed);
132 if (rc != EOK) {
133 printf("Error creating fixed layout.\n");
134 goto error;
135 }
136
137 rc = ui_label_create(ui_res, "Task bar!", &taskbar->label);
138 if (rc != EOK) {
139 printf("Error creating label.\n");
140 goto error;
141 }
142
143 ui_window_get_app_rect(taskbar->window, &rect);
144 ui_label_set_rect(taskbar->label, &rect);
145 ui_label_set_halign(taskbar->label, gfx_halign_center);
146 ui_label_set_valign(taskbar->label, gfx_valign_center);
147
148 rc = ui_fixed_add(taskbar->fixed, ui_label_ctl(taskbar->label));
149 if (rc != EOK) {
150 printf("Error adding control to layout.\n");
151 ui_label_destroy(taskbar->label);
152 goto error;
153 }
154
155 rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
156 if (rc != EOK)
157 goto error;
158
159 if (ui_is_textmode(taskbar->ui)) {
160 rect.p0.x = params.rect.p1.x - 10;
161 rect.p0.y = 0;
162 rect.p1.x = params.rect.p1.x;
163 rect.p1.y = 1;
164 } else {
165 rect.p0.x = params.rect.p1.x - 80;
166 rect.p0.y = 4;
167 rect.p1.x = params.rect.p1.x - 4;
168 rect.p1.y = 32 - 4;
169 }
170
171 taskbar_clock_set_rect(taskbar->clock, &rect);
172
173 rc = ui_fixed_add(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
174 if (rc != EOK) {
175 printf("Error adding control to layout.\n");
176 taskbar_clock_destroy(taskbar->clock);
177 goto error;
178 }
179
180 ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
181
182 rc = ui_window_paint(taskbar->window);
183 if (rc != EOK) {
184 printf("Error painting window.\n");
185 goto error;
186 }
187
188 *rtaskbar = taskbar;
189 return EOK;
190error:
191 if (taskbar->clock != NULL)
192 taskbar_clock_destroy(taskbar->clock);
193 if (taskbar->window != NULL)
194 ui_window_destroy(taskbar->window);
195 if (taskbar->ui != NULL)
196 ui_destroy(taskbar->ui);
197 return rc;
198
199}
200
201/** Destroy task bar. */
202void taskbar_destroy(taskbar_t *taskbar)
203{
204 ui_fixed_remove(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
205 taskbar_clock_destroy(taskbar->clock);
206 ui_window_destroy(taskbar->window);
207 ui_destroy(taskbar->ui);
208}
209
210/** @}
211 */
Note: See TracBrowser for help on using the repository browser.