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 <wndmgt.h>
|
---|
45 | #include "clock.h"
|
---|
46 | #include "taskbar.h"
|
---|
47 | #include "wndlist.h"
|
---|
48 |
|
---|
49 | static void wnd_close(ui_window_t *, void *);
|
---|
50 |
|
---|
51 | static ui_window_cb_t window_cb = {
|
---|
52 | .close = wnd_close
|
---|
53 | };
|
---|
54 |
|
---|
55 | /** Window close button was clicked.
|
---|
56 | *
|
---|
57 | * @param window Window
|
---|
58 | * @param arg Argument (taskbar)
|
---|
59 | */
|
---|
60 | static void wnd_close(ui_window_t *window, void *arg)
|
---|
61 | {
|
---|
62 | taskbar_t *taskbar = (taskbar_t *) arg;
|
---|
63 |
|
---|
64 | ui_quit(taskbar->ui);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** Create task bar.
|
---|
68 | *
|
---|
69 | * @param display_spec Display specification
|
---|
70 | * @param wndmgt_svc Window management service (or WNDMGT_DEFAULT)
|
---|
71 | * @param rtaskbar Place to store pointer to new task bar
|
---|
72 | * @return @c EOK on success or an error coe
|
---|
73 | */
|
---|
74 | errno_t taskbar_create(const char *display_spec, const char *wndmgt_svc,
|
---|
75 | taskbar_t **rtaskbar)
|
---|
76 | {
|
---|
77 | ui_wnd_params_t params;
|
---|
78 | taskbar_t *taskbar = NULL;
|
---|
79 | gfx_rect_t scr_rect;
|
---|
80 | gfx_rect_t rect;
|
---|
81 | ui_resource_t *ui_res;
|
---|
82 | errno_t rc;
|
---|
83 |
|
---|
84 | taskbar = calloc(1, sizeof(taskbar_t));
|
---|
85 | if (taskbar == NULL) {
|
---|
86 | rc = ENOMEM;
|
---|
87 | goto error;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (wndmgt_svc != NULL) {
|
---|
91 | rc = wndmgt_open(wndmgt_svc, NULL, NULL, &taskbar->wndmgt);
|
---|
92 | if (rc != EOK)
|
---|
93 | goto error;
|
---|
94 | }
|
---|
95 |
|
---|
96 | rc = ui_create(display_spec, &taskbar->ui);
|
---|
97 | if (rc != EOK) {
|
---|
98 | printf("Error creating UI on display %s.\n", display_spec);
|
---|
99 | goto error;
|
---|
100 | }
|
---|
101 |
|
---|
102 | rc = ui_get_rect(taskbar->ui, &scr_rect);
|
---|
103 | if (rc != EOK) {
|
---|
104 | if (str_cmp(display_spec, UI_DISPLAY_NULL) != 0) {
|
---|
105 | printf("Error getting screen dimensions.\n");
|
---|
106 | goto error;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* For the sake of unit tests */
|
---|
110 | scr_rect.p0.x = 0;
|
---|
111 | scr_rect.p0.y = 0;
|
---|
112 | scr_rect.p1.x = 100;
|
---|
113 | scr_rect.p1.y = 100;
|
---|
114 | }
|
---|
115 |
|
---|
116 | ui_wnd_params_init(¶ms);
|
---|
117 | params.caption = "Task Bar";
|
---|
118 | params.placement = ui_wnd_place_bottom_left;
|
---|
119 | params.style &= ~ui_wds_titlebar;
|
---|
120 |
|
---|
121 | params.rect.p0.x = 0;
|
---|
122 | params.rect.p0.y = 0;
|
---|
123 | params.rect.p1.x = scr_rect.p1.x - scr_rect.p0.x;
|
---|
124 |
|
---|
125 | if (ui_is_textmode(taskbar->ui)) {
|
---|
126 | params.rect.p1.y = 1;
|
---|
127 | params.style &= ~ui_wds_frame;
|
---|
128 | } else {
|
---|
129 | params.rect.p1.y = 32;
|
---|
130 | }
|
---|
131 |
|
---|
132 | rc = ui_window_create(taskbar->ui, ¶ms, &taskbar->window);
|
---|
133 | if (rc != EOK) {
|
---|
134 | printf("Error creating window.\n");
|
---|
135 | goto error;
|
---|
136 | }
|
---|
137 |
|
---|
138 | ui_window_set_cb(taskbar->window, &window_cb, (void *) &taskbar);
|
---|
139 | ui_res = ui_window_get_res(taskbar->window);
|
---|
140 |
|
---|
141 | rc = ui_fixed_create(&taskbar->fixed);
|
---|
142 | if (rc != EOK) {
|
---|
143 | printf("Error creating fixed layout.\n");
|
---|
144 | goto error;
|
---|
145 | }
|
---|
146 |
|
---|
147 | rc = ui_label_create(ui_res, "HelenOS", &taskbar->label);
|
---|
148 | if (rc != EOK) {
|
---|
149 | printf("Error creating label.\n");
|
---|
150 | goto error;
|
---|
151 | }
|
---|
152 |
|
---|
153 | ui_window_get_app_rect(taskbar->window, &rect);
|
---|
154 | if (ui_is_textmode(taskbar->ui)) {
|
---|
155 | rect.p0.x += 1;
|
---|
156 | } else {
|
---|
157 | rect.p0.x += 10;
|
---|
158 | }
|
---|
159 | ui_label_set_rect(taskbar->label, &rect);
|
---|
160 | ui_label_set_halign(taskbar->label, gfx_halign_left);
|
---|
161 | ui_label_set_valign(taskbar->label, gfx_valign_center);
|
---|
162 |
|
---|
163 | rc = ui_fixed_add(taskbar->fixed, ui_label_ctl(taskbar->label));
|
---|
164 | if (rc != EOK) {
|
---|
165 | printf("Error adding control to layout.\n");
|
---|
166 | ui_label_destroy(taskbar->label);
|
---|
167 | goto error;
|
---|
168 | }
|
---|
169 |
|
---|
170 | rc = wndlist_create(ui_res, taskbar->fixed, &taskbar->wndlist);
|
---|
171 | if (rc != EOK) {
|
---|
172 | printf("Error creating window list.\n");
|
---|
173 | goto error;
|
---|
174 | }
|
---|
175 |
|
---|
176 | rc = wndlist_attach_wm(taskbar->wndlist, taskbar->wndmgt);
|
---|
177 | if (rc != EOK) {
|
---|
178 | printf("Error attaching window management service.\n");
|
---|
179 | goto error;
|
---|
180 | }
|
---|
181 |
|
---|
182 | rc = taskbar_clock_create(taskbar->window, &taskbar->clock);
|
---|
183 | if (rc != EOK)
|
---|
184 | goto error;
|
---|
185 |
|
---|
186 | if (ui_is_textmode(taskbar->ui)) {
|
---|
187 | rect.p0.x = params.rect.p1.x - 10;
|
---|
188 | rect.p0.y = 0;
|
---|
189 | rect.p1.x = params.rect.p1.x;
|
---|
190 | rect.p1.y = 1;
|
---|
191 | } else {
|
---|
192 | rect.p0.x = params.rect.p1.x - 80;
|
---|
193 | rect.p0.y = 4;
|
---|
194 | rect.p1.x = params.rect.p1.x - 4;
|
---|
195 | rect.p1.y = 32 - 4;
|
---|
196 | }
|
---|
197 |
|
---|
198 | taskbar_clock_set_rect(taskbar->clock, &rect);
|
---|
199 |
|
---|
200 | rc = ui_fixed_add(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
|
---|
201 | if (rc != EOK) {
|
---|
202 | printf("Error adding control to layout.\n");
|
---|
203 | taskbar_clock_destroy(taskbar->clock);
|
---|
204 | goto error;
|
---|
205 | }
|
---|
206 |
|
---|
207 | ui_window_add(taskbar->window, ui_fixed_ctl(taskbar->fixed));
|
---|
208 |
|
---|
209 | rc = ui_window_paint(taskbar->window);
|
---|
210 | if (rc != EOK) {
|
---|
211 | printf("Error painting window.\n");
|
---|
212 | goto error;
|
---|
213 | }
|
---|
214 |
|
---|
215 | *rtaskbar = taskbar;
|
---|
216 | return EOK;
|
---|
217 | error:
|
---|
218 | if (taskbar->clock != NULL)
|
---|
219 | taskbar_clock_destroy(taskbar->clock);
|
---|
220 | if (taskbar->wndlist != NULL)
|
---|
221 | wndlist_destroy(taskbar->wndlist);
|
---|
222 | if (taskbar->window != NULL)
|
---|
223 | ui_window_destroy(taskbar->window);
|
---|
224 | if (taskbar->ui != NULL)
|
---|
225 | ui_destroy(taskbar->ui);
|
---|
226 | if (taskbar->wndmgt != NULL)
|
---|
227 | wndmgt_close(taskbar->wndmgt);
|
---|
228 | return rc;
|
---|
229 |
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Destroy task bar. */
|
---|
233 | void taskbar_destroy(taskbar_t *taskbar)
|
---|
234 | {
|
---|
235 | ui_fixed_remove(taskbar->fixed, taskbar_clock_ctl(taskbar->clock));
|
---|
236 | taskbar_clock_destroy(taskbar->clock);
|
---|
237 | ui_window_destroy(taskbar->window);
|
---|
238 | ui_destroy(taskbar->ui);
|
---|
239 | if (taskbar->wndmgt != NULL)
|
---|
240 | wndmgt_close(taskbar->wndmgt);
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** @}
|
---|
244 | */
|
---|