aboutsummaryrefslogtreecommitdiff
path: root/src/compositor.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-01-11 23:47:35 +0100
committerdec05eba <dec05eba@protonmail.com>2024-01-11 23:47:35 +0100
commit705ad3bee5de67725311d4085f0481fa7107ba41 (patch)
tree78879a96cdab04878072db18d51465560d95e094 /src/compositor.c
parentc4fb95cf0025b34ca14c22f4fbbfcef14d56d71c (diff)
Render wallpaper
Diffstat (limited to 'src/compositor.c')
-rw-r--r--src/compositor.c74
1 files changed, 59 insertions, 15 deletions
diff --git a/src/compositor.c b/src/compositor.c
index 1e75d77..994404e 100644
--- a/src/compositor.c
+++ b/src/compositor.c
@@ -1,9 +1,14 @@
#include "compositor.h"
+
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
+#include <X11/Xatom.h>
+
+/* TODO: Handle wallpaper change and update on root window resize */
+
static double clock_get_monotonic_seconds(void) {
struct timespec ts;
ts.tv_sec = 0;
@@ -24,15 +29,50 @@ static ngxc_window* ngxc_compositor_get_window_by_id(ngxc_compositor *self, Wind
return NULL;
}
+static Pixmap x11_wallpaper_get_pixmap(ngxc_compositor *self) {
+ const char *background_props[] = {
+ "_XROOTPMAP_ID",
+ "_XSETROOT_ID",
+ NULL
+ };
+
+ const Window root_window = DefaultRootWindow(self->dpy);
+ Pixmap wallpaper_pixmap = None;
+
+ for(int i = 0; background_props[i]; ++i) {
+ const Atom atom = XInternAtom(self->dpy, background_props[i], False);
+ Atom actual_type;
+ int actual_format;
+ unsigned long items_count;
+ unsigned long bytes_after;
+ unsigned char *prop = NULL;
+ if(XGetWindowProperty(self->dpy, root_window, atom, 0, 1, False, XA_PIXMAP, &actual_type, &actual_format, &items_count, &bytes_after, &prop) == Success && prop) {
+ wallpaper_pixmap = *(Pixmap*)prop;
+ XFree(prop);
+ break;
+ }
+
+ if(prop)
+ XFree(prop);
+ }
+
+ return wallpaper_pixmap;
+}
+
void ngxc_compositor_init(ngxc_compositor *self, Display *dpy, Window composite_window) {
memset(self, 0, sizeof(*self));
self->dpy = dpy;
self->composite_window = composite_window;
self->frame_timer = clock_get_monotonic_seconds();
+
+ const Pixmap wallpaper_pixmap = x11_wallpaper_get_pixmap(self);
+ if(!ngxc_pixmap_texture_init(&self->wallpaper_texture, self->dpy, wallpaper_pixmap)) {
+ fprintf(stderr, "error: failed to create texture for wallpaper pixmap %ld\n", wallpaper_pixmap);
+ }
}
void ngxc_compositor_deinit(ngxc_compositor *self) {
-
+ /* TODO: Cleanup? */
}
void ngxc_compositor_add_window(ngxc_compositor *self, Window window) {
@@ -101,7 +141,7 @@ void ngxc_compositor_remove_window(ngxc_compositor *self, Window window) {
}
}
-static void render_texture(const ngxc_window *window) {
+static void render_texture(GLuint texture_id, int x, int y, int width, int height) {
float matrix[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
@@ -109,28 +149,28 @@ static void render_texture(const ngxc_window *window) {
0.0f, 0.0f, 0.0f, 1.0f,
};
- matrix[0] = 1.0f / (float)window->texture.width;
- matrix[5] = 1.0f / (float)window->texture.height;
+ matrix[0] = 1.0f / (float)width;
+ matrix[5] = 1.0f / (float)height;
glMatrixMode(GL_TEXTURE);
glLoadMatrixf(matrix);
glMatrixMode(GL_MODELVIEW);
- glBindTexture(GL_TEXTURE_2D, window->texture.texture_id);
+ glBindTexture(GL_TEXTURE_2D, texture_id);
glColor4ub(255, 255, 255, 255);
- //glTranslatef(window->x, window->y, 0.0f);
+ //glTranslatef(x, y, 0.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
- glVertex2f(window->x, window->y);
+ glVertex2f(x, y);
- glTexCoord2f(window->texture.width, 0.0f);
- glVertex2f(window->x + window->texture.width, window->y);
+ glTexCoord2f(width, 0.0f);
+ glVertex2f(x + width, y);
- glTexCoord2f(window->texture.width, window->texture.height);
- glVertex2f(window->x + window->texture.width, window->y + window->texture.height);
+ glTexCoord2f(width, height);
+ glVertex2f(x + width, y + height);
- glTexCoord2f(0.0f, window->texture.height);
- glVertex2f(window->x, window->y + window->texture.height);
+ glTexCoord2f(0.0f, height);
+ glVertex2f(x, y + height);
glEnd();
glLoadIdentity();
@@ -140,12 +180,16 @@ static void render_texture(const ngxc_window *window) {
/* TODO: Skip windows that are not visible on the screen */
/* TODO: Disable compositing for fullscreen windows, if that option is enabled */
void ngxc_compositor_render(ngxc_compositor *self) {
- glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
+ glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
+ if(self->wallpaper_texture.texture_id > 0) {
+ render_texture(self->wallpaper_texture.texture_id, 0, 0, self->wallpaper_texture.width, self->wallpaper_texture.height);
+ }
+
for(int i = 0; i < self->num_windows; ++i) {
const ngxc_window *window_obj = &self->windows[i];
- render_texture(window_obj);
+ render_texture(window_obj->texture.texture_id, window_obj->x, window_obj->y, window_obj->texture.width, window_obj->texture.height);
}
const double now = clock_get_monotonic_seconds();