source: mainline/uspace/app/taskbar/clock.c@ 3fd38b2

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

Size taskbar based on display size, fix text mode

  • Property mode set to 100644
File size: 7.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 clock.
33 *
34 * Displays the current time in an inset frame.
35 */
36
37#include <errno.h>
38#include <gfx/render.h>
39#include <gfx/text.h>
40#include <stdlib.h>
41#include <stdio.h>
42#include <task.h>
43#include <ui/control.h>
44#include <ui/paint.h>
45#include <ui/resource.h>
46#include <ui/ui.h>
47#include "clock.h"
48
49static void taskbar_clock_ctl_destroy(void *);
50static errno_t taskbar_clock_ctl_paint(void *);
51static ui_evclaim_t taskbar_clock_ctl_kbd_event(void *, kbd_event_t *);
52static ui_evclaim_t taskbar_clock_ctl_pos_event(void *, pos_event_t *);
53static void taskbar_clock_timer(void *);
54
55/** Task bar clock control ops */
56static ui_control_ops_t taskbar_clock_ctl_ops = {
57 .destroy = taskbar_clock_ctl_destroy,
58 .paint = taskbar_clock_ctl_paint,
59 .kbd_event = taskbar_clock_ctl_kbd_event,
60 .pos_event = taskbar_clock_ctl_pos_event
61};
62
63/** Create task bar clock.
64 *
65 * @param window Containing window
66 * @param rclock Place to store pointer to new clock
67 * @return EOK on success or an error code
68 */
69errno_t taskbar_clock_create(ui_window_t *window, taskbar_clock_t **rclock)
70{
71 taskbar_clock_t *clock;
72 errno_t rc;
73
74 clock = calloc(1, sizeof(taskbar_clock_t));
75 if (clock == NULL)
76 return ENOMEM;
77
78 rc = ui_control_new(&taskbar_clock_ctl_ops, (void *)clock,
79 &clock->control);
80 if (rc != EOK) {
81 free(clock);
82 return rc;
83 }
84
85 clock->timer = fibril_timer_create(NULL);
86 if (clock->timer == NULL) {
87 rc = ENOMEM;
88 goto error;
89 }
90
91 fibril_timer_set(clock->timer, 1000000, taskbar_clock_timer, clock);
92
93 clock->window = window;
94 *rclock = clock;
95 return EOK;
96error:
97 ui_control_delete(clock->control);
98 free(clock);
99 return rc;
100}
101
102/** Destroy task bar clock.
103 *
104 * @param clock Task bar clock
105 */
106void taskbar_clock_destroy(taskbar_clock_t *clock)
107{
108 fibril_timer_destroy(clock->timer);
109 ui_control_delete(clock->control);
110 free(clock);
111}
112
113static errno_t taskbar_clock_get_text(taskbar_clock_t *clock, char *buf,
114 size_t bsize)
115{
116 struct timespec ts;
117 struct tm tm;
118 errno_t rc;
119
120 getrealtime(&ts);
121 rc = time_utc2tm(ts.tv_sec, &tm);
122 if (rc != EOK)
123 return rc;
124
125 snprintf(buf, bsize, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
126 tm.tm_sec);
127 return EOK;
128}
129
130/** Paint task bar clock.
131 *
132 * @param clock Task bar clock
133 */
134errno_t taskbar_clock_paint(taskbar_clock_t *clock)
135{
136 gfx_context_t *gc = ui_window_get_gc(clock->window);
137 ui_resource_t *res = ui_window_get_res(clock->window);
138 ui_t *ui = ui_window_get_ui(clock->window);
139 char buf[10];
140 gfx_text_fmt_t fmt;
141 gfx_coord2_t pos;
142 gfx_rect_t irect;
143 errno_t rc;
144
145 if (!ui_is_textmode(ui)) {
146 /* Paint frame */
147 rc = ui_paint_inset_frame(res, &clock->rect, &irect);
148 if (rc != EOK)
149 goto error;
150 } else {
151 irect = clock->rect;
152 }
153
154 rc = gfx_set_color(gc, ui_resource_get_wnd_face_color(res));
155 if (rc != EOK)
156 goto error;
157
158 /* Fill background */
159 rc = gfx_fill_rect(gc, &irect);
160 if (rc != EOK)
161 goto error;
162
163 pos.x = (irect.p0.x + irect.p1.x) / 2;
164 pos.y = (irect.p0.y + irect.p1.y) / 2;
165
166 gfx_text_fmt_init(&fmt);
167 fmt.font = ui_resource_get_font(res);
168 fmt.color = ui_resource_get_wnd_text_color(res);
169 fmt.halign = gfx_halign_center;
170 fmt.valign = gfx_valign_center;
171
172 rc = taskbar_clock_get_text(clock, buf, sizeof(buf));
173 if (rc != EOK)
174 goto error;
175
176 rc = gfx_puttext(&pos, &fmt, buf);
177 if (rc != EOK)
178 goto error;
179
180 rc = gfx_update(gc);
181 if (rc != EOK)
182 goto error;
183
184 return EOK;
185error:
186 return rc;
187}
188
189/** Handle task bar clock keyboard event.
190 *
191 * @param clock Task bar clock
192 * @param event Keyboard event
193 * @return ui_claimed iff event was claimed
194 */
195ui_evclaim_t taskbar_clock_kbd_event(taskbar_clock_t *clock, kbd_event_t *event)
196{
197 return ui_unclaimed;
198}
199
200/** Handle task bar clock position event.
201 *
202 * @param clock Task bar clock
203 * @param event Position event
204 * @return ui_claimed iff event was claimed
205 */
206ui_evclaim_t taskbar_clock_pos_event(taskbar_clock_t *clock, pos_event_t *event)
207{
208 gfx_coord2_t pos;
209
210 pos.x = event->hpos;
211 pos.y = event->vpos;
212 if (!gfx_pix_inside_rect(&pos, &clock->rect))
213 return ui_unclaimed;
214
215 return ui_claimed;
216}
217
218/** Get base control for task bar clock.
219 *
220 * @param clock Task bar clock
221 * @return Base UI control
222 */
223ui_control_t *taskbar_clock_ctl(taskbar_clock_t *clock)
224{
225 return clock->control;
226}
227
228/** Set task bar clock rectangle.
229 *
230 * @param clock Task bar clock
231 * @param rect Rectangle
232 */
233void taskbar_clock_set_rect(taskbar_clock_t *clock, gfx_rect_t *rect)
234{
235 gfx_rect_t irect;
236
237 clock->rect = *rect;
238
239 irect.p0.x = clock->rect.p0.x + 1;
240 irect.p0.y = clock->rect.p0.y + 1;
241 irect.p1.x = clock->rect.p1.x;
242 irect.p1.y = clock->rect.p1.y - 1;
243
244 (void)irect;
245}
246
247/** Destroy clock control.
248 *
249 * @param arg Argument (taskbar_clock_t *)
250 */
251void taskbar_clock_ctl_destroy(void *arg)
252{
253 taskbar_clock_t *clock = (taskbar_clock_t *) arg;
254
255 taskbar_clock_destroy(clock);
256}
257
258/** Paint task bar clock control.
259 *
260 * @param arg Argument (taskbar_clock_t *)
261 * @return EOK on success or an error code
262 */
263errno_t taskbar_clock_ctl_paint(void *arg)
264{
265 taskbar_clock_t *clock = (taskbar_clock_t *) arg;
266
267 return taskbar_clock_paint(clock);
268}
269
270/** Handle task bar clock control keyboard event.
271 *
272 * @param arg Argument (taskbar_clock_t *)
273 * @param kbd_event Keyboard event
274 * @return @c ui_claimed iff the event is claimed
275 */
276ui_evclaim_t taskbar_clock_ctl_kbd_event(void *arg, kbd_event_t *event)
277{
278 taskbar_clock_t *clock = (taskbar_clock_t *) arg;
279
280 return taskbar_clock_kbd_event(clock, event);
281}
282
283/** Handle task bar clock control position event.
284 *
285 * @param arg Argument (taskbar_clock_t *)
286 * @param pos_event Position event
287 * @return @c ui_claimed iff the event is claimed
288 */
289ui_evclaim_t taskbar_clock_ctl_pos_event(void *arg, pos_event_t *event)
290{
291 taskbar_clock_t *clock = (taskbar_clock_t *) arg;
292
293 return taskbar_clock_pos_event(clock, event);
294}
295
296/** Taskbar clock timer handler.
297 *
298 * @param arg Argument (taskbar_clock_t *)
299 */
300static void taskbar_clock_timer(void *arg)
301{
302 taskbar_clock_t *clock = (taskbar_clock_t *) arg;
303
304 (void) taskbar_clock_paint(clock);
305 fibril_timer_set(clock->timer, 1000000, taskbar_clock_timer, clock);
306}
307
308/** @}
309 */
Note: See TracBrowser for help on using the repository browser.