PatchworkOS
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
2
3#include <errno.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8static image_t* image;
9
10static uint64_t procedure(window_t* win, element_t* elem, const event_t* event)
11{
12 (void)win; // Unused
13
14 switch (event->type)
15 {
16 case LEVENT_INIT:
17 {
18 }
19 break;
20 case LEVENT_REDRAW:
21 {
22 if (image == NULL)
23 {
24 printf("wall: image failed to load (%s)\n", strerror(errno));
25 break;
26 }
27
29
30 drawable_t draw;
31 element_draw_begin(elem, &draw);
32
33 point_t srcPoint = {.x = (image_width(image) - RECT_WIDTH(&rect)) / 2,
34 .y = (image_height(image) - RECT_HEIGHT(&rect)) / 2};
35 draw_image(&draw, image, &rect, &srcPoint);
36
37 element_draw_end(elem, &draw);
38 }
39 break;
40 }
41
42 return 0;
43}
44
45int main(void)
46{
47 fd_t klog = open("/dev/klog");
48 if (klog == ERR)
49 {
50 printf("taskbar: failed to open klog\n");
51 return EXIT_FAILURE;
52 }
54 {
55 printf("taskbar: failed to redirect stdout/stderr to klog\n");
56 close(klog);
57 return EXIT_FAILURE;
58 }
59 close(klog);
60
61 display_t* disp = display_new();
62 if (disp == NULL)
63 {
64 printf("wall: failed to create display (%s)\n", strerror(errno));
65 return EXIT_FAILURE;
66 }
67
68 if (display_unsubscribe(disp, EVENT_KBD) == ERR)
69 {
70 printf("wall: failed to unsubscribe from keyboard events (%s)\n", strerror(errno));
71 display_free(disp);
72 return EXIT_FAILURE;
73 }
75 {
76 printf("wall: failed to unsubscribe from mouse events (%s)\n", strerror(errno));
77 display_free(disp);
78 return EXIT_FAILURE;
79 }
80
81 rect_t rect;
82 display_get_screen(disp, &rect, 0);
83
86 if (image == NULL)
87 {
88 printf("wall: failed to load image '%s' (%s)\n", theme->wallpaper, strerror(errno));
89 display_free(disp);
90 return EXIT_FAILURE;
91 }
92
93 window_t* win = window_new(disp, "Wallpaper", &rect, SURFACE_WALL, WINDOW_NONE, procedure, NULL);
94 if (win == NULL)
95 {
96 printf("wall: failed to create window (%s)\n", strerror(errno));
98 display_free(disp);
99 return EXIT_FAILURE;
100 }
101
102 if (window_set_visible(win, true) == ERR)
103 {
104 printf("wall: failed to show window (%s)\n", strerror(errno));
105 window_free(win);
107 display_free(disp);
108 return EXIT_FAILURE;
109 }
110
111 event_t event = {0};
112 while (display_next(disp, &event, CLOCKS_NEVER) != ERR)
113 {
114 display_dispatch(disp, &event);
115 }
116
117 window_free(win);
119 display_free(disp);
120 return 0;
121}
#define CLOCKS_NEVER
Definition clock_t.h:16
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_unsubscribe(display_t *disp, event_type_t type)
Unsubscribe from events of a specific type.
Definition display.c:528
uint64_t display_dispatch(display_t *disp, const event_t *event)
Dispatch an event to the appropriate surface.
Definition display.c:440
uint64_t display_get_screen(display_t *disp, rect_t *rect, uint64_t index)
Get the rectangle of a screen.
Definition display.c:603
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_image(drawable_t *draw, image_t *image, const rect_t *destRect, const point_t *srcPoint)
Draw an image,.
Definition drawable.c:545
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_MOUSE
Definition event.h:85
#define LEVENT_REDRAW
Definition event.h:101
#define LEVENT_INIT
Definition event.h:99
#define EVENT_KBD
Definition event.h:84
@ SURFACE_WALL
Definition surface.h:38
theme_t * theme_global_get(void)
Get the global theme.
Definition theme.c:97
uint64_t window_set_visible(window_t *win, bool isVisible)
Set the visibility of the window.
Definition window.c:692
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
@ WINDOW_NONE
Definition window.h:45
#define errno
Error number variable.
Definition errno.h:27
fd_t dup2(fd_t oldFd, fd_t newFd)
System call for duplicating file descriptors, with a destination.
Definition dup2.c:9
fd_t open(const char *path)
System call for opening files.
Definition open.c:9
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:9
#define STDOUT_FILENO
Definition io.h:45
#define STDERR_FILENO
Definition io.h:46
#define NULL
Pointer error value.
Definition NULL.h:23
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
void image_free(image_t *image)
Definition image.c:74
uint64_t image_height(image_t *image)
Definition image.c:99
uint64_t image_width(image_t *image)
Definition image.c:94
image_t * image_new(display_t *disp, const char *path)
Definition image.c:27
#define ERR
Definition main.c:44
int main()
Definition main.c:97
static uint64_t procedure(window_t *win, element_t *elem, const event_t *event)
Definition main.c:46
static image_t * image
Definition main.c:5
int64_t y
Definition main.c:153
#define RECT_HEIGHT(rect)
Definition rect.h:39
#define RECT_WIDTH(rect)
Definition rect.h:38
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:5
#define EXIT_FAILURE
Definition stdlib.h:47
_PUBLIC char * strerror(int errnum)
Definition strerror.c:6
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 x
Definition point.h:13
Definition rect.h:13
Theme structure.
Definition theme.h:71
char wallpaper[MAX_PATH]
Definition theme.h:77
Opaque window structure.
Definition internal.h:44
static theme_t theme
Definition theme.c:12
fd_t klog
Definition thrd_create.c:11