PatchworkOS  10941b4
A non-POSIX operating system.
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
2#include <math.h>
3#include <stdint.h>
4#include <stdlib.h>
5#include <string.h>
6#include <sys/defs.h>
7#include <time.h>
8
9#define WINDOW_WIDTH 500
10#define WINDOW_HEIGHT 500
11
12static point_t hourMarker[] = {{-3, 0}, {3, 0}, {2, -30}, {-2, -30}};
13
14static point_t minuteMarker[] = {{-1, 0}, {1, 0}, {1, -15}, {-1, -15}};
15
16static point_t hourHand[] = {{-8, 15}, {8, 15}, {6, -50}, {3, -75}, {0, -85}, {-3, -75}, {-6, -50}};
17
18static point_t minuteHand[] = {{-6, 15}, {6, 15}, {4, -120}, {2, -145}, {0, -155}, {-2, -145}, {-4, -120}};
19
20static point_t secondHand[] = {{-2, 30}, {2, 30}, {2, 0}, {1, -165}, {0, -175}, {-1, -165}, {-2, 0}};
21
22static void draw_marker(drawable_t* draw, point_t center, int64_t radius, const point_t* markerPoints,
23 uint64_t pointCount, double angle, pixel_t pixel)
24{
25 point_t rotatedMarker[pointCount];
26 memcpy(rotatedMarker, markerPoints, sizeof(point_t) * pointCount);
27
28 for (uint64_t i = 0; i < 4; i++)
29 {
30 rotatedMarker[i].x += center.x;
31 rotatedMarker[i].y += center.y - radius;
32 }
33
34 polygon_rotate(rotatedMarker, pointCount, angle, center);
35 draw_polygon(draw, rotatedMarker, pointCount, pixel);
36}
37
38static void draw_hand(drawable_t* draw, point_t center, const point_t* handPoints, uint64_t pointCount, double angle,
39 pixel_t pixel)
40{
41 point_t rotatedHand[pointCount];
42 memcpy(rotatedHand, handPoints, sizeof(point_t) * pointCount);
43 for (uint64_t i = 0; i < pointCount; i++)
44 {
45 rotatedHand[i].x += center.x;
46 rotatedHand[i].y += center.y;
47 }
48 polygon_rotate(rotatedHand, pointCount, angle, center);
49 draw_polygon(draw, rotatedHand, pointCount, pixel);
50}
51
52static uint64_t procedure(window_t* win, element_t* elem, const event_t* event)
53{
54 UNUSED(win);
55
56 switch (event->type)
57 {
58 case EVENT_LIB_INIT:
59 {
61 }
62 break;
64 {
65 }
66 break;
67 case EVENT_LIB_QUIT:
68 {
70 }
71 break;
72 case EVENT_TIMER:
74 {
75 drawable_t draw;
76 element_draw_begin(elem, &draw);
77
78 time_t now = time(NULL);
79 struct tm* t = localtime(&now);
80
81 const theme_t* theme = element_get_theme(elem);
82
83 rect_t clockRect = element_get_content_rect(elem);
84 draw_rect(&draw, &clockRect, theme->deco.backgroundNormal);
85 point_t center = {clockRect.left + RECT_WIDTH(&clockRect) / 2, clockRect.top + RECT_HEIGHT(&clockRect) / 2};
86 uint64_t radius = RECT_WIDTH(&clockRect) / 2 - 75;
87
88 for (int i = 0; i < 12; i++)
89 {
90 double angle = i * (M_PI / 6);
91 draw_marker(&draw, center, radius, hourMarker, ARRAY_SIZE(hourMarker), angle, PIXEL_ARGB(255, 0, 0, 0));
92 }
93
94 for (int i = 0; i < 60; i++)
95 {
96 if (i % 5 != 0)
97 {
98 double angle = i * (M_PI / 30);
99 draw_marker(&draw, center, radius, minuteMarker, ARRAY_SIZE(minuteMarker), angle,
100 PIXEL_ARGB(255, 100, 100, 100));
101 }
102 }
103
104 double hourAngle = ((t->tm_hour % 12) + (double)t->tm_min / 60.0) * (M_PI / 6);
105 double minuteAngle = (t->tm_min + (double)t->tm_sec / 60.0) * (M_PI / 30);
106 double secondAngle = t->tm_sec * (M_PI / 30);
107
108 draw_hand(&draw, center, hourHand, ARRAY_SIZE(hourHand), hourAngle, PIXEL_ARGB(255, 0, 0, 0));
109 draw_hand(&draw, center, minuteHand, ARRAY_SIZE(minuteHand), minuteAngle, PIXEL_ARGB(255, 0, 0, 0));
110 draw_hand(&draw, center, secondHand, ARRAY_SIZE(secondHand), secondAngle, PIXEL_ARGB(255, 255, 0, 0));
111 element_draw_end(elem, &draw);
112 }
113 break;
114 }
115 return 0;
116}
117
118int main(void)
119{
120 display_t* disp = display_new();
121 if (disp == NULL)
122 {
123 return EXIT_FAILURE;
124 }
125
127 window_t* win = window_new(disp, "Clock", &rect, SURFACE_WINDOW, WINDOW_DECO, procedure, NULL);
128 if (win == NULL)
129 {
130 display_free(disp);
131 return EXIT_FAILURE;
132 }
133
134 if (window_set_visible(win, true) == ERR)
135 {
136 window_free(win);
137 display_free(disp);
138 return EXIT_FAILURE;
139 }
140
141 event_t event = {0};
142 while (display_next(disp, &event, CLOCKS_NEVER) != ERR)
143 {
144 display_dispatch(disp, &event);
145 }
146
147 window_free(win);
148 display_free(disp);
149 return EXIT_SUCCESS;
150}
#define WINDOW_WIDTH
Definition main.c:20
#define WINDOW_HEIGHT
Definition main.c:21
static uint64_t procedure(window_t *win, element_t *elem, const event_t *event)
Definition main.c:46
static point_t hourMarker[]
Definition main.c:12
static point_t minuteHand[]
Definition main.c:18
static void draw_hand(drawable_t *draw, point_t center, const point_t *handPoints, uint64_t pointCount, double angle, pixel_t pixel)
Definition main.c:38
static void draw_marker(drawable_t *draw, point_t center, int64_t radius, const point_t *markerPoints, uint64_t pointCount, double angle, pixel_t pixel)
Definition main.c:22
static point_t minuteMarker[]
Definition main.c:14
static point_t secondHand[]
Definition main.c:20
static point_t hourHand[]
Definition main.c:16
int main(void)
Definition main.c:5
#define CLOCKS_NEVER
Definition clock_t.h:16
#define CLOCKS_PER_SEC
Definition clock_t.h:15
@ TIMER_REPEAT
Definition cmd.h:77
void display_disconnect(display_t *disp)
Disconnect the display connection.
Definition display.c:159
uint64_t display_next(display_t *disp, event_t *event, clock_t timeout)
Retrieve the next event from the display connection.
Definition display.c:211
uint64_t display_dispatch(display_t *disp, const event_t *event)
Dispatch an event to the appropriate surface.
Definition display.c:410
void display_free(display_t *disp)
Free a display connection.
Definition display.c:111
display_t * display_new(void)
Create a new display connection.
Definition display.c:46
void draw_polygon(drawable_t *draw, const point_t *points, uint64_t pointCount, pixel_t pixel)
Draw a filled polygon.
Definition drawable.c:48
void draw_rect(drawable_t *draw, const rect_t *rect, pixel_t pixel)
Draw a filled rectangle.
Definition drawable.c:7
theme_t * element_get_theme(element_t *elem)
Get the theme of an element.
Definition element.c:401
void element_draw_end(element_t *elem, drawable_t *draw)
End drawing to an element.
Definition element.c:427
rect_t element_get_content_rect(element_t *elem)
Get the element's rectangle in local coordinates.
Definition element.c:213
void element_draw_begin(element_t *elem, drawable_t *draw)
Begin drawing to an element.
Definition element.c:411
#define EVENT_LIB_QUIT
Definition event.h:102
#define EVENT_LIB_DEINIT
Definition event.h:99
#define EVENT_LIB_INIT
Definition event.h:98
#define EVENT_LIB_REDRAW
Definition event.h:100
#define EVENT_TIMER
Definition event.h:85
void polygon_rotate(point_t *points, uint64_t pointCount, double angle, point_t center)
Rotate a polygon around a center point.
Definition polygon.c:4
@ SURFACE_WINDOW
Definition surface.h:35
uint64_t window_set_visible(window_t *win, bool isVisible)
Set the visibility of the window.
Definition window.c:704
uint64_t window_set_timer(window_t *win, timer_flags_t flags, clock_t timeout)
Set the window timer.
Definition window.c:545
window_t * window_new(display_t *disp, const char *name, const rect_t *rect, surface_type_t type, window_flags_t flags, procedure_t procedure, void *private)
Allocate and initialize a new window.
Definition window.c:313
void window_free(window_t *win)
Free a window.
Definition window.c:427
display_t * window_get_display(window_t *win)
Get the display associated with the window.
Definition window.c:479
@ WINDOW_DECO
Enable decorations (titlebar, close/minimize buttons, etc).
Definition window.h:46
#define ARRAY_SIZE(x)
Get the number of elements in a static array.
Definition defs.h:108
#define UNUSED(x)
Mark a variable as unused.
Definition defs.h:100
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
#define M_PI
Definition math.h:11
uint32_t pixel_t
Definition pixel.h:11
#define PIXEL_ARGB(a, r, g, b)
Definition pixel.h:18
#define RECT_INIT_DIM(x, y, width, height)
Definition rect.h:32
#define RECT_HEIGHT(rect)
Definition rect.h:39
#define RECT_WIDTH(rect)
Definition rect.h:38
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
#define EXIT_SUCCESS
Definition stdlib.h:46
#define EXIT_FAILURE
Definition stdlib.h:47
_PUBLIC void * memcpy(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
Definition memcpy.c:61
Opaque display structure.
Definition internal.h:68
Drawable structure.
Definition drawable.h:35
Opaque element structure.
Definition internal.h:23
Event structure.
Definition event.h:306
event_type_t type
Definition event.h:307
int64_t y
Definition point.h:14
int64_t x
Definition point.h:13
Definition rect.h:13
int32_t top
Definition rect.h:15
int32_t left
Definition rect.h:14
pixel_t backgroundNormal
Definition theme.h:35
Theme structure.
Definition theme.h:71
theme_color_set_t deco
Definition theme.h:76
Definition time.h:21
int tm_hour
Definition time.h:24
int tm_sec
Definition time.h:22
int tm_min
Definition time.h:23
Opaque window structure.
Definition internal.h:44
static theme_t theme
Definition theme.c:12
_PUBLIC time_t time(time_t *timer)
Definition time.c:5
_PUBLIC struct tm * localtime(const time_t *timer)
Definition localtime.c:5
long long unsigned time_t
Definition time_t.h:4