-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclock.c
257 lines (210 loc) · 7.81 KB
/
clock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Twin - A Tiny Window System
* Copyright (c) 2004 Keith Packard <[email protected]>
* All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include "twin_private.h"
#include "apps_clock.h"
#define D(x) twin_double_to_fixed(x)
#define APPS_CLOCK_BACKGROUND 0xff3b80ae
#define APPS_CLOCK_HOUR 0x80808080
#define APPS_CLOCK_HOUR_OUT 0x30000000
#define APPS_CLOCK_MINUTE 0x80808080
#define APPS_CLOCK_MINUTE_OUT 0x30000000
#define APPS_CLOCK_SECOND 0x80808080
#define APPS_CLOCK_SECOND_OUT 0x30000000
#define APPS_CLOCK_TIC 0xffbababa
#define APPS_CLOCK_NUMBERS 0xffdedede
#define APPS_CLOCK_WATER 0x60200000
#define APPS_CLOCK_WATER_OUT 0x40404040
#define APPS_CLOCK_WATER_UNDER 0x60400000
#define APPS_CLOCK_BORDER 0xffbababa
#define APPS_CLOCK_BORDER_WIDTH D(0.01)
#define _apps_clock_pixmap(clock) ((clock)->widget.window->pixmap)
typedef struct {
twin_widget_t widget;
twin_timeout_t *timeout;
} apps_clock_t;
static void apps_clock_set_transform(apps_clock_t *clock, twin_path_t *path)
{
twin_fixed_t scale;
scale = (TWIN_FIXED_ONE - APPS_CLOCK_BORDER_WIDTH * 3) / 2;
twin_path_scale(path, _twin_widget_width(clock) * scale,
_twin_widget_height(clock) * scale);
twin_path_translate(path, TWIN_FIXED_ONE + APPS_CLOCK_BORDER_WIDTH * 3,
TWIN_FIXED_ONE + APPS_CLOCK_BORDER_WIDTH * 3);
twin_path_rotate(path, -TWIN_ANGLE_90);
}
static void apps_clock_hand(apps_clock_t *clock,
twin_angle_t angle,
twin_fixed_t len,
twin_fixed_t fill_width,
twin_fixed_t out_width,
twin_argb32_t fill_pixel,
twin_argb32_t out_pixel)
{
twin_path_t *stroke = twin_path_create();
twin_path_t *pen = twin_path_create();
twin_path_t *path = twin_path_create();
twin_matrix_t m;
apps_clock_set_transform(clock, stroke);
twin_path_rotate(stroke, angle);
twin_path_move(stroke, D(0), D(0));
twin_path_draw(stroke, len, D(0));
m = twin_path_current_matrix(stroke);
m.m[2][0] = 0;
m.m[2][1] = 0;
twin_path_set_matrix(pen, m);
twin_path_set_matrix(path, m);
twin_path_circle(pen, 0, 0, fill_width);
twin_path_convolve(path, stroke, pen);
twin_paint_path(_apps_clock_pixmap(clock), fill_pixel, path);
twin_paint_stroke(_apps_clock_pixmap(clock), out_pixel, path, out_width);
twin_path_destroy(path);
twin_path_destroy(pen);
twin_path_destroy(stroke);
}
static void _apps_clock_date(apps_clock_t *clock, struct tm *t)
{
char text[7];
twin_text_metrics_t metrics;
twin_fixed_t height, width;
strftime(text, sizeof(text), "%b %d", t);
twin_path_t *path = twin_path_create();
if (!path)
return;
apps_clock_set_transform(clock, path);
twin_path_rotate(path, TWIN_ANGLE_90);
twin_path_translate(path, D(0.8), 0);
twin_path_set_font_size(path, D(0.25));
twin_path_set_font_style(path, TwinStyleUnhinted);
twin_text_metrics_utf8(path, text, &metrics);
height = metrics.ascent + metrics.descent;
width = metrics.right_side_bearing - metrics.left_side_bearing;
twin_path_move(path, -width, metrics.ascent - height / 2);
twin_path_utf8(path, text);
twin_paint_path(_apps_clock_pixmap(clock), APPS_CLOCK_WATER, path);
twin_path_destroy(path);
}
static twin_angle_t apps_clock_minute_angle(int min)
{
return min * TWIN_ANGLE_360 / 60;
}
static void _apps_clock_face(apps_clock_t *clock)
{
twin_path_t *path = twin_path_create();
int m;
apps_clock_set_transform(clock, path);
twin_path_circle(path, 0, 0, TWIN_FIXED_ONE);
twin_paint_path(_apps_clock_pixmap(clock), APPS_CLOCK_BACKGROUND, path);
twin_paint_stroke(_apps_clock_pixmap(clock), APPS_CLOCK_BORDER, path,
APPS_CLOCK_BORDER_WIDTH);
twin_path_set_font_size(path, D(0.2));
twin_path_set_font_style(path, TwinStyleUnhinted);
for (m = 1; m <= 60; m++) {
twin_state_t state = twin_path_save(path);
twin_path_rotate(path, apps_clock_minute_angle(m) + TWIN_ANGLE_90);
twin_path_empty(path);
if (m % 5 != 0) {
twin_path_move(path, 0, -TWIN_FIXED_ONE);
twin_path_draw(path, 0, -D(0.9));
twin_paint_stroke(_apps_clock_pixmap(clock), APPS_CLOCK_TIC, path,
D(0.01));
} else {
char hour[3];
twin_text_metrics_t metrics;
twin_fixed_t width;
twin_fixed_t left;
sprintf(hour, "%d", m / 5);
twin_text_metrics_utf8(path, hour, &metrics);
width = metrics.right_side_bearing - metrics.left_side_bearing;
left = -width / 2 - metrics.left_side_bearing;
twin_path_move(path, left, -D(0.98) + metrics.ascent);
twin_path_utf8(path, hour);
twin_paint_path(_apps_clock_pixmap(clock), APPS_CLOCK_NUMBERS,
path);
}
twin_path_restore(path, &state);
}
twin_path_destroy(path);
}
static twin_time_t _apps_clock_interval(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return 1000 - (tv.tv_usec / 1000);
}
static void _apps_clock_paint(apps_clock_t *clock)
{
struct timeval tv;
twin_angle_t second_angle, minute_angle, hour_angle;
struct tm t;
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &t);
_apps_clock_face(clock);
_apps_clock_date(clock, &t);
second_angle =
((t.tm_sec * 100 + tv.tv_usec / 10000) * TWIN_ANGLE_360) / 6000;
minute_angle = apps_clock_minute_angle(t.tm_min) + second_angle / 60;
hour_angle = (t.tm_hour * TWIN_ANGLE_360 + minute_angle) / 12;
apps_clock_hand(clock, hour_angle, D(0.4), D(0.07), D(0.01),
APPS_CLOCK_HOUR, APPS_CLOCK_HOUR_OUT);
apps_clock_hand(clock, minute_angle, D(0.8), D(0.05), D(0.01),
APPS_CLOCK_MINUTE, APPS_CLOCK_MINUTE_OUT);
apps_clock_hand(clock, second_angle, D(0.9), D(0.01), D(0.01),
APPS_CLOCK_SECOND, APPS_CLOCK_SECOND_OUT);
}
static twin_time_t _apps_clock_timeout(twin_time_t maybe_unused now,
void *closure)
{
apps_clock_t *clock = closure;
_twin_widget_queue_paint(&clock->widget);
return _apps_clock_interval();
}
static twin_dispatch_result_t _apps_clock_dispatch(twin_widget_t *widget,
twin_event_t *event)
{
apps_clock_t *clock = (apps_clock_t *) widget;
if (_twin_widget_dispatch(widget, event) == TwinDispatchDone)
return TwinDispatchDone;
switch (event->kind) {
case TwinEventPaint:
_apps_clock_paint(clock);
break;
default:
break;
}
return TwinDispatchContinue;
}
static void _apps_clock_init(apps_clock_t *clock,
twin_box_t *parent,
twin_dispatch_proc_t dispatch)
{
static const twin_widget_layout_t preferred = {0, 0, 1, 1};
_twin_widget_init(&clock->widget, parent, 0, preferred, dispatch);
clock->timeout =
twin_set_timeout(_apps_clock_timeout, _apps_clock_interval(), clock);
}
static apps_clock_t *apps_clock_create(twin_box_t *parent)
{
apps_clock_t *clock = malloc(sizeof(apps_clock_t));
_apps_clock_init(clock, parent, _apps_clock_dispatch);
return clock;
}
void apps_clock_start(twin_screen_t *screen,
const char *name,
int x,
int y,
int w,
int h)
{
twin_toplevel_t *toplevel = twin_toplevel_create(
screen, TWIN_ARGB32, TwinWindowApplication, x, y, w, h, name);
apps_clock_t *clock = apps_clock_create(&toplevel->box);
(void) clock;
twin_toplevel_show(toplevel);
}