PatchworkOS  10941b4
A non-POSIX operating system.
Loading...
Searching...
No Matches
screen.c
Go to the documentation of this file.
1#include "screen.h"
2
3#include "region.h"
4#include "surface.h"
5
6#include <errno.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/io.h>
11#include <sys/proc.h>
12
17static char format[MAX_NAME];
18
19static void* frontbuffer;
20static void* backbuffer;
21
24
25static void frontbuffer_init(void)
26{
27 char name[MAX_PATH] = {0};
28 if (readfile("/dev/fb/0/name", name, sizeof(name) - 1, 0) == ERR)
29 {
30 printf("dwm: failed to read framebuffer name (%s)\n", strerror(errno));
31 abort();
32 }
33
34 if (scanfile("/dev/fb/0/info", "%lu %lu %lu %s", &width, &height, &pitch, format) == ERR)
35 {
36 printf("dwm: failed to read framebuffer info (%s)\n", strerror(errno));
37 abort();
38 }
39
40 printf("dwm: using framebuffer '%s' width=%lu height=%lu pitch=%lu format=%s\n", name, width, height, pitch,
41 format);
42
43 stride = pitch / sizeof(pixel_t);
44
45 fd_t data = open("/dev/fb/0/data");
46 if (data == ERR)
47 {
48 printf("dwm: failed to open framebuffer device (%s)\n", strerror(errno));
49 abort();
50 }
51
53 if (frontbuffer == NULL)
54 {
55 printf("dwm: failed to map framebuffer memory (%s)\n", strerror(errno));
56 abort();
57 }
59
60 close(data);
61}
62
63static void backbuffer_init(void)
64{
66 if (backbuffer == NULL)
67 {
68 printf("dwm: failed to allocate backbuffer memory\n");
69 abort();
70 }
71}
72
73static void screen_invalidate(const rect_t* rect)
74{
75 rect_t fitRect = *rect;
78}
79
87
88void screen_deinit(void)
89{
92}
93
94void screen_transfer(surface_t* surface, const rect_t* rect)
95{
96 rect_t fitRect = *rect;
98
100 .x = MAX(fitRect.left - surface->pos.x, 0),
101 .y = MAX(fitRect.top - surface->pos.y, 0),
102 };
105 for (int64_t y = 0; y < height; y++)
106 {
107 memcpy(&((pixel_t*)backbuffer)[(fitRect.left) + (fitRect.top + y) * stride],
108 &surface->buffer[(srcPoint.x) + (srcPoint.y + y) * surface->width], width * sizeof(pixel_t));
109 }
110 screen_invalidate(rect);
111}
112
113void screen_transfer_blend(surface_t* surface, const rect_t* rect)
114{
115 rect_t fitRect = *rect;
117
118 point_t srcPoint = {
119 .x = MAX(fitRect.left - surface->pos.x, 0),
120 .y = MAX(fitRect.top - surface->pos.y, 0),
121 };
124 for (int64_t y = 0; y < height; y++)
125 {
126 for (int64_t x = 0; x < width; x++)
127 {
128 pixel_t* pixel = &surface->buffer[(srcPoint.x + x) + (srcPoint.y + y) * surface->width];
129 pixel_t* out = &((pixel_t*)backbuffer)[(fitRect.left + x) + (fitRect.top + y) * stride];
131 }
132 }
134}
135
137{
138 rect_t fitRect = *rect;
140
141 point_t srcPoint = {
142 .x = MAX(fitRect.left - surface->pos.x, 0),
143 .y = MAX(fitRect.top - surface->pos.y, 0),
144 };
145 for (int64_t y = 0; y < RECT_HEIGHT(&fitRect); y++)
146 {
147 memcpy(&((uint32_t*)frontbuffer)[(fitRect.left) + (fitRect.top + y) * stride],
148 &surface->buffer[(srcPoint.x) + (srcPoint.y + y) * surface->width],
149 RECT_WIDTH(&fitRect) * sizeof(uint32_t));
150 }
151
153}
154
155void screen_swap(void)
156{
157 for (uint64_t i = 0; i < invalidRegion.count; i++)
158 {
159 rect_t* rect = &invalidRegion.rects[i];
160 for (int64_t y = 0; y < RECT_HEIGHT(rect); y++)
161 {
162 memcpy(&((uint32_t*)frontbuffer)[(rect->left) + (rect->top + y) * stride],
163 &((pixel_t*)backbuffer)[(rect->left) + (rect->top + y) * stride], RECT_WIDTH(rect) * sizeof(uint32_t));
164 }
165 }
166
168}
169
171{
172 return width;
173}
174
176{
177 return height;
178}
179
181{
182 *rect = screenRect;
183}
#define MAX_NAME
Maximum length of names.
Definition MAX_NAME.h:11
#define MAX_PATH
Maximum length of filepaths.
Definition MAX_PATH.h:11
int64_t x
Definition main.c:152
int64_t y
Definition main.c:153
static void * frontbuffer
Definition screen.c:19
void screen_swap(void)
Definition screen.c:155
void screen_transfer(surface_t *surface, const rect_t *rect)
Definition screen.c:94
static uint64_t stride
Definition screen.c:16
void screen_deinit(void)
Definition screen.c:88
uint64_t screen_height(void)
Definition screen.c:175
void screen_transfer_blend(surface_t *surface, const rect_t *rect)
Definition screen.c:113
static char format[MAX_NAME]
Definition screen.c:17
void screen_rect(rect_t *rect)
Definition screen.c:180
static uint64_t height
Definition screen.c:14
static uint64_t pitch
Definition screen.c:15
static region_t invalidRegion
Definition screen.c:23
void screen_transfer_frontbuffer(surface_t *surface, const rect_t *rect)
Definition screen.c:136
static void * backbuffer
Definition screen.c:20
static uint64_t width
Definition screen.c:13
uint64_t screen_width(void)
Definition screen.c:170
static void frontbuffer_init(void)
Definition screen.c:25
static rect_t screenRect
Definition screen.c:22
static void backbuffer_init(void)
Definition screen.c:63
static fd_t data
Definition dwm.c:21
void screen_init(void)
Initialize and enable the screen logging.
Definition screen.c:80
#define errno
Error number variable.
Definition errno.h:27
fd_t open(const char *path)
System call for opening files.
Definition open.c:8
uint64_t scanfile(const char *path, const char *format,...)
Wrapper for reading from a file path using scan formatting.
Definition scanfile.c:6
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:8
size_t readfile(const char *path, void *buffer, size_t count, size_t offset)
Wrapper for reading a file directly using a path.
Definition readfile.c:3
#define MAX(x, y)
Definition math.h:15
void * mmap(fd_t fd, void *address, size_t length, prot_t prot)
System call to map memory from a file.
Definition mmap.c:6
void * munmap(void *address, size_t length)
System call to unmap mapped memory.
Definition munmap.c:6
@ PROT_READ
Readable memory.
Definition proc.h:128
@ PROT_WRITE
Writable memory.
Definition proc.h:129
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
static void screen_invalidate(const screen_pos_t *pos)
Definition screen.c:32
#define PIXEL_BLEND(dest, src)
Definition pixel.h:20
uint32_t pixel_t
Definition pixel.h:11
#define RECT_FIT(rect, parent)
Definition rect.h:73
#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
static void region_add(region_t *region, const rect_t *rect)
Definition region.h:32
static void region_init(region_t *region)
Definition region.h:17
static void region_clear(region_t *region)
Definition region.h:22
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
_PUBLIC int printf(const char *_RESTRICT format,...)
Definition printf.c:3
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC _NORETURN void abort(void)
Definition abort.c:9
_PUBLIC void free(void *ptr)
Definition free.c:11
_PUBLIC char * strerror(int errnum)
Definition strerror.c:6
_PUBLIC void * memcpy(void *_RESTRICT s1, const void *_RESTRICT s2, size_t n)
Definition memcpy.c:61
_PUBLIC void * memset(void *s, int c, size_t n)
Definition memset.c:4
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
rect_t rects[MAX_REGION_RECTS]
Definition region.h:11
uint64_t count
Definition region.h:12
pixel_t * buffer
Definition surface.h:27
uint32_t width
Definition surface.h:28
point_t pos
Definition surface.h:25