source: mainline/uspace/app/taskbar/clock.c@ 50a16d9

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

Add simple digital clock display to task bar

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