PatchworkOS
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 <time.h>
7
8#define WINDOW_WIDTH 500
9#define WINDOW_HEIGHT 500
10
11static point_t hourMarker[] = {{-3, 0}, {3, 0}, {2, -30}, {-2, -30}};
12
13static point_t minuteMarker[] = {{-1, 0}, {1, 0}, {1, -15}, {-1, -15}};
14
15static point_t hourHand[] = {{-8, 15}, {8, 15}, {6, -50}, {3, -75}, {0, -85}, {-3, -75}, {-6, -50}};
16
17static point_t minuteHand[] = {{-6, 15}, {6, 15}, {4, -120}, {2, -145}, {0, -155}, {-2, -145}, {-4, -120}};
18
19static point_t secondHand[] = {{-2, 30}, {2, 30}, {2, 0}, {1, -165}, {0, -175}, {-1, -165}, {-2, 0}};
20
21static void draw_marker(drawable_t* draw, point_t center, int64_t radius, const point_t* markerPoints,
22 uint64_t pointCount, double angle, pixel_t pixel)
23{
24 point_t rotatedMarker[pointCount];
25 memcpy(rotatedMarker, markerPoints, sizeof(point_t) * pointCount);
26
27 for (uint64_t i = 0; i < 4; i++)
28 {
29 rotatedMarker[i].x += center.x;
30 rotatedMarker[i].y += center.y - radius;
31 }
32
33 polygon_rotate(rotatedMarker, pointCount, angle, center);
34 draw_polygon(draw, rotatedMarker, pointCount, pixel);
35}
36
37static void draw_hand(drawable_t* draw, point_t center, const point_t* handPoints, uint64_t pointCount, double angle,
38 pixel_t pixel)
39{
40 point_t rotatedHand[pointCount];
41 memcpy(rotatedHand, handPoints, sizeof(point_t) * pointCount);
42 for (uint64_t i = 0; i < pointCount; i++)
43 {
44 rotatedHand[i].x += center.x;
45 rotatedHand[i].y += center.y;
46 }
47 polygon_rotate(rotatedHand, pointCount, angle, center);
48 draw_polygon(draw, rotatedHand, pointCount, pixel);
49}
50
51static uint64_t procedure(window_t* win, element_t* elem, const event_t* event)
52{
53 (void)win;
54
55 switch (event->type)
56 {
57 case LEVENT_INIT:
58 {
60 }
61 break;
62 case LEVENT_DEINIT:
63 {
64 }
65 break;
66 case LEVENT_QUIT:
67 {
69 }
70 break;
71 case EVENT_TIMER:
72 case LEVENT_REDRAW:
73 {
74 drawable_t draw;
75 element_draw_begin(elem, &draw);
76
77 time_t now = time(NULL);
78 struct tm* t = localtime(&now);
79
80 const theme_t* theme = element_get_theme(elem);
81
82 rect_t clockRect = element_get_content_rect(elem);
83 draw_rect(&draw, &clockRect, theme->deco.backgroundNormal);
84 point_t center = {clockRect.left + RECT_WIDTH(&clockRect) / 2, clockRect.top + RECT_HEIGHT(&clockRect) / 2};
85 uint64_t radius = RECT_WIDTH(&clockRect) / 2 - 75;
86
87 for (int i = 0; i < 12; i++)
88 {
89 double angle = i * (M_PI / 6);
90 draw_marker(&draw, center, radius, hourMarker, sizeof(hourMarker) / sizeof(hourMarker[0]), angle,
91 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, sizeof(minuteMarker) / sizeof(minuteMarker[0]), 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, sizeof(hourHand) / sizeof(hourHand[0]), hourAngle, PIXEL_ARGB(255, 0, 0, 0));
109 draw_hand(&draw, center, minuteHand, sizeof(minuteHand) / sizeof(minuteHand[0]), minuteAngle,
110 PIXEL_ARGB(255, 0, 0, 0));
111 draw_hand(&draw, center, secondHand, sizeof(secondHand) / sizeof(secondHand[0]), secondAngle,
112 PIXEL_ARGB(255, 255, 0, 0));
113 element_draw_end(elem, &draw);
114 }
115 break;
116 }
117 return 0;
118}
119
120int main(void)
121{
122 display_t* disp = display_new();
123 if (disp == NULL)
124 {
125 return EXIT_FAILURE;
126 }
127
129 window_t* win = window_new(disp, "Clock", &rect, SURFACE_WINDOW, WINDOW_DECO, procedure, NULL);
130 if (win == NULL)
131 {
132 display_free(disp);
133 return EXIT_FAILURE;
134 }
135
136 if (window_set_visible(win, true) == ERR)
137 {
138 window_free(win);
139 display_free(disp);
140 return EXIT_FAILURE;
141 }
142
143 event_t event = {0};
144 while (display_next(disp, &event, CLOCKS_NEVER) != ERR)
145 {
146 display_dispatch(disp, &event);
147 }
148
149 window_free(win);
150 display_free(disp);
151 return EXIT_SUCCESS;
152}
#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:161
uint64_t display_next(display_t *disp, event_t *event, clock_t timeout)
Retrieve the next event from the display connection.
Definition display.c:213
uint64_t display_dispatch(display_t *disp, const event_t *event)
Dispatch an event to the appropriate surface.
Definition display.c:440
void display_free(display_t *disp)
Free a display connection.
Definition display.c:113
display_t * display_new(void)
Create a new display connection.
Definition display.c:36
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 LEVENT_QUIT
Definition event.h:103
#define LEVENT_DEINIT
Definition event.h:100
#define LEVENT_REDRAW
Definition event.h:101
#define LEVENT_INIT
Definition event.h:99
#define EVENT_TIMER
Definition event.h:86
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:692
uint64_t window_set_timer(window_t *win, timer_flags_t flags, clock_t timeout)
Set the window timer.
Definition window.c:533
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:301
void window_free(window_t *win)
Free a window.
Definition window.c:415
display_t * window_get_display(window_t *win)
Get the display associated with the window.
Definition window.c:467
@ WINDOW_DECO
Enable decorations (titlebar, close/minimize buttons, etc).
Definition window.h:46
#define NULL
Pointer error value.
Definition NULL.h:23
#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 ERR
Definition main.c:44
int main()
Definition main.c:97
#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:11
static point_t minuteHand[]
Definition main.c:17
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:37
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:21
static point_t minuteMarker[]
Definition main.c:13
static point_t secondHand[]
Definition main.c:19
static point_t hourHand[]
Definition main.c:15
#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:4
Opaque display structure.
Definition internal.h:61
Drawable structure.
Definition drawable.h:35
Opaque element structure.
Definition internal.h:23
Event structure.
Definition event.h:271
event_type_t type
Definition event.h:272
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