mirror of
https://github.com/wizbright/waybox.git
synced 2025-10-29 05:40:20 -04:00
Code Refactor
This commit is contained in:
parent
67140acc51
commit
ad1611e3ad
7 changed files with 252 additions and 155 deletions
35
include/waybox/output.h
Normal file
35
include/waybox/output.h
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef OUTPUT_H
|
||||
#define OUTPUT_H
|
||||
|
||||
#ifndef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <wlr/backend.h>
|
||||
#include <wlr/render/wlr_renderer.h>
|
||||
#include <wlr/render/wlr_texture.h>
|
||||
#include <wlr/types/wlr_compositor.h>
|
||||
|
||||
#include "waybox/server.h"
|
||||
|
||||
struct wb_output {
|
||||
struct wlr_output *wlr_output;
|
||||
struct wb_server *server;
|
||||
struct timespec last_frame;
|
||||
|
||||
struct wl_listener destroy;
|
||||
struct wl_listener frame;
|
||||
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
void output_frame_notify(struct wl_listener* listener, void *data);
|
||||
void output_destroy_notify(struct wl_listener* listener, void *data);
|
||||
void new_output_notify(struct wl_listener* listener, void *data);
|
||||
|
||||
#endif // output.h
|
||||
41
include/waybox/server.h
Normal file
41
include/waybox/server.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#ifndef SERVER_H
|
||||
#define SERVER_H
|
||||
|
||||
#ifndef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <wlr/backend.h>
|
||||
#include <wlr/render/wlr_renderer.h>
|
||||
#include <wlr/render/wlr_texture.h>
|
||||
#include <wlr/types/wlr_compositor.h>
|
||||
#include <wlr/types/wlr_idle.h>
|
||||
#include <wlr/types/wlr_screenshooter.h>
|
||||
#include <wlr/types/wlr_matrix.h>
|
||||
#include <wlr/types/wlr_gamma_control.h>
|
||||
#include <wlr/types/wlr_primary_selection.h>
|
||||
|
||||
#include "waybox/output.h"
|
||||
|
||||
|
||||
struct wb_server {
|
||||
struct wl_display *wl_display;
|
||||
struct wl_event_loop *wl_event_loop;
|
||||
|
||||
struct wlr_backend *backend;
|
||||
struct wlr_compositor *compositor;
|
||||
|
||||
struct wl_listener new_output;
|
||||
struct wl_list outputs; // wb_output::link
|
||||
};
|
||||
|
||||
bool init_wb(struct wb_server* server);
|
||||
bool start_wb(struct wb_server* server);
|
||||
bool terminate_wb(struct wb_server* server);
|
||||
|
||||
#endif // server.h
|
||||
20
meson.build
20
meson.build
|
|
@ -1,4 +1,4 @@
|
|||
# SirCmpwn is a god. (from mcwayface)
|
||||
# SirCmpwn is a god. (from mcwayface and bspwc)
|
||||
|
||||
project(
|
||||
'waybox',
|
||||
|
|
@ -25,6 +25,9 @@ endif
|
|||
# Hiding depreciation warnings
|
||||
add_project_arguments('-DWL_HIDE_DEPRECATED', language: 'c')
|
||||
|
||||
# Adding include directory
|
||||
inc_dir = include_directories('include')
|
||||
|
||||
pixman = dependency('pixman-1')
|
||||
wlroots = dependency('wlroots')
|
||||
wayland_server = dependency('wayland-server')
|
||||
|
|
@ -32,17 +35,4 @@ wayland_client = dependency('wayland-client')
|
|||
wayland_protos = dependency('wayland-protocols', version: '>=1.12')
|
||||
|
||||
subdir('protocol')
|
||||
|
||||
executable(
|
||||
'waybox',
|
||||
[
|
||||
'src/main.c'
|
||||
],
|
||||
dependencies: [
|
||||
pixman,
|
||||
wlroots,
|
||||
wayland_server,
|
||||
wayland_protos,
|
||||
wayland_client
|
||||
]
|
||||
)
|
||||
subdir('src')
|
||||
|
|
|
|||
156
src/main.c
156
src/main.c
|
|
@ -1,157 +1,33 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <wayland-server.h>
|
||||
#include <wlr/backend.h>
|
||||
#include <wlr/render/wlr_renderer.h>
|
||||
#include <wlr/render/wlr_texture.h>
|
||||
#include <wlr/types/wlr_compositor.h>
|
||||
#include <wlr/types/wlr_idle.h>
|
||||
#include <wlr/types/wlr_screenshooter.h>
|
||||
#include <wlr/types/wlr_matrix.h>
|
||||
//#include <wlr/types/wlr_xdg_shell_v6.h>
|
||||
#include <wlr/types/wlr_gamma_control.h>
|
||||
#include <wlr/types/wlr_primary_selection.h>
|
||||
|
||||
#include "waybox/server.h"
|
||||
|
||||
struct wb_server {
|
||||
struct wl_display *wl_display;
|
||||
struct wl_event_loop *wl_event_loop;
|
||||
|
||||
struct wlr_backend *backend;
|
||||
struct wlr_compositor *compositor;
|
||||
|
||||
struct wl_listener new_output;
|
||||
struct wl_list outputs; // wb_output::link
|
||||
};
|
||||
|
||||
struct wb_output {
|
||||
struct wlr_output *wlr_output;
|
||||
struct wb_server *server;
|
||||
struct timespec last_frame;
|
||||
|
||||
struct wl_listener destroy;
|
||||
struct wl_listener frame;
|
||||
|
||||
struct wl_list link;
|
||||
};
|
||||
|
||||
static void output_frame_notify(struct wl_listener *listener, void *data) {
|
||||
struct wb_output *output = wl_container_of(listener, output, frame);
|
||||
// struct wlr_backend *backend = output->server->backend;
|
||||
struct wlr_output *wlr_output = data;
|
||||
struct wlr_renderer *renderer = wlr_backend_get_renderer(
|
||||
wlr_output->backend);
|
||||
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
|
||||
wlr_output_make_current(wlr_output, NULL);
|
||||
wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height);
|
||||
|
||||
float color[4] = {0.4f, 0.4f, 0.4f, 1.0f};
|
||||
wlr_renderer_clear(renderer, color);
|
||||
|
||||
struct wl_resource *_surface;
|
||||
wl_resource_for_each(_surface, &output->server->compositor->surface_resources) {
|
||||
struct wlr_surface *surface = wlr_surface_from_resource(_surface);
|
||||
if (!wlr_surface_has_buffer(surface)) {
|
||||
continue;
|
||||
}
|
||||
struct wlr_box render_box = {
|
||||
.x = 20, .y = 20,
|
||||
.width = surface->current->width,
|
||||
.height = surface->current->height
|
||||
};
|
||||
float matrix[16];
|
||||
wlr_matrix_project_box(matrix, &render_box, surface->current->transform,
|
||||
0, wlr_output->transform_matrix);
|
||||
wlr_render_texture_with_matrix(renderer, surface->texture, matrix, 1.0f);
|
||||
wlr_surface_send_frame_done(surface, &now);
|
||||
}
|
||||
|
||||
wlr_output_swap_buffers(wlr_output, NULL, NULL);
|
||||
wlr_renderer_end(renderer);
|
||||
output->last_frame = now;
|
||||
}
|
||||
|
||||
static void output_destroy_notify(struct wl_listener *listener, void *data) {
|
||||
struct wb_output *output = wl_container_of(listener, output, destroy);
|
||||
wl_list_remove(&output->link);
|
||||
wl_list_remove(&output->destroy.link);
|
||||
wl_list_remove(&output->frame.link);
|
||||
free(output);
|
||||
}
|
||||
|
||||
static void new_output_notify(struct wl_listener *listener, void *data) {
|
||||
struct wb_server *server = wl_container_of(
|
||||
listener, server, new_output
|
||||
);
|
||||
struct wlr_output *wlr_output = data;
|
||||
|
||||
if (!wl_list_empty(&wlr_output->modes)) {
|
||||
struct wlr_output_mode *mode =
|
||||
wl_container_of(wlr_output->modes.prev, mode, link);
|
||||
wlr_output_set_mode(wlr_output, mode);
|
||||
|
||||
wlr_output_create_global(wlr_output);
|
||||
}
|
||||
|
||||
struct wb_output *output = calloc(1, sizeof(struct wb_output));
|
||||
clock_gettime(CLOCK_MONOTONIC, &output->last_frame);
|
||||
output->server = server;
|
||||
output->wlr_output = wlr_output;
|
||||
wl_list_insert(&server->outputs, &output->link);
|
||||
|
||||
output->destroy.notify = output_destroy_notify;
|
||||
wl_signal_add(&wlr_output->events.destroy, &output->destroy);
|
||||
output->frame.notify = output_frame_notify;
|
||||
wl_signal_add(&wlr_output->events.frame, &output->frame);
|
||||
}
|
||||
struct wl_display* display = NULL;
|
||||
|
||||
int main(int argc, char **argv){
|
||||
struct wb_server server;
|
||||
struct wb_server server = {0};
|
||||
|
||||
server.wl_display = wl_display_create();
|
||||
assert(server.wl_display);
|
||||
server.wl_event_loop = wl_display_get_event_loop(server.wl_display);
|
||||
assert(server.wl_event_loop);
|
||||
// Global display
|
||||
display = server.wl_display;
|
||||
|
||||
server.backend = wlr_backend_autocreate(server.wl_display, NULL);
|
||||
assert(server.backend);
|
||||
|
||||
wl_list_init(&server.outputs);
|
||||
|
||||
server.new_output.notify = new_output_notify;
|
||||
wl_signal_add(&server.backend->events.new_output, &server.new_output);
|
||||
|
||||
const char *socket = wl_display_add_socket_auto(server.wl_display);
|
||||
assert(socket);
|
||||
|
||||
if (!wlr_backend_start(server.backend)) {
|
||||
fprintf(stderr, "Failed to start backend\n");
|
||||
wl_display_destroy(server.wl_display);
|
||||
return 1;
|
||||
if (init_wb(&server) == false) {
|
||||
printf("Failed to create backend\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Running compositer on wayland display '%s'\n", socket);
|
||||
setenv("WAYLAND_DISPLAY", socket, true);
|
||||
|
||||
wl_display_init_shm(server.wl_display);
|
||||
wlr_gamma_control_manager_create(server.wl_display);
|
||||
wlr_screenshooter_create(server.wl_display);
|
||||
wlr_primary_selection_device_manager_create(server.wl_display);
|
||||
wlr_idle_create(server.wl_display);
|
||||
|
||||
server.compositor = wlr_compositor_create(server.wl_display,
|
||||
wlr_backend_get_renderer(server.backend));
|
||||
|
||||
// wlr_xdg_shell_v6_create(server.wl_display);
|
||||
wlr_idle_create(server.wl_display);
|
||||
if (!start_wb(&server)) {
|
||||
printf("Failed to start server\n");
|
||||
terminate_wb(&server);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
wl_display_run(server.wl_display);
|
||||
wl_display_destroy(server.wl_display);
|
||||
|
||||
terminate_wb(&server);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
19
src/meson.build
Normal file
19
src/meson.build
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
wb_src = files(
|
||||
'main.c',
|
||||
'output.c',
|
||||
'server.c',
|
||||
)
|
||||
|
||||
wb_dep = [
|
||||
wayland_server,
|
||||
wlroots,
|
||||
pixman,
|
||||
]
|
||||
|
||||
executable(
|
||||
'waybox',
|
||||
wb_src,
|
||||
include_directories: [inc_dir],
|
||||
dependencies: [wb_dep, wlr_protos],
|
||||
install: true,
|
||||
)
|
||||
74
src/output.c
Normal file
74
src/output.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#include <waybox/output.h>
|
||||
|
||||
void output_frame_notify(struct wl_listener *listener, void *data) {
|
||||
struct wb_output *output = wl_container_of(listener, output, frame);
|
||||
// struct wlr_backend *backend = output->server->backend;
|
||||
struct wlr_output *wlr_output = data;
|
||||
struct wlr_renderer *renderer = wlr_backend_get_renderer(
|
||||
wlr_output->backend);
|
||||
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
|
||||
wlr_output_make_current(wlr_output, NULL);
|
||||
wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height);
|
||||
|
||||
float color[4] = {0.4f, 0.4f, 0.4f, 1.0f};
|
||||
wlr_renderer_clear(renderer, color);
|
||||
|
||||
struct wl_resource *_surface;
|
||||
wl_resource_for_each(_surface, &output->server->compositor->surface_resources) {
|
||||
struct wlr_surface *surface = wlr_surface_from_resource(_surface);
|
||||
if (!wlr_surface_has_buffer(surface)) {
|
||||
continue;
|
||||
}
|
||||
struct wlr_box render_box = {
|
||||
.x = 20, .y = 20,
|
||||
.width = surface->current->width,
|
||||
.height = surface->current->height
|
||||
};
|
||||
float matrix[16];
|
||||
wlr_matrix_project_box(matrix, &render_box, surface->current->transform,
|
||||
0, wlr_output->transform_matrix);
|
||||
wlr_render_texture_with_matrix(renderer, surface->texture, matrix, 1.0f);
|
||||
wlr_surface_send_frame_done(surface, &now);
|
||||
}
|
||||
|
||||
wlr_output_swap_buffers(wlr_output, NULL, NULL);
|
||||
wlr_renderer_end(renderer);
|
||||
output->last_frame = now;
|
||||
}
|
||||
|
||||
void output_destroy_notify(struct wl_listener *listener, void *data) {
|
||||
struct wb_output *output = wl_container_of(listener, output, destroy);
|
||||
wl_list_remove(&output->link);
|
||||
wl_list_remove(&output->destroy.link);
|
||||
wl_list_remove(&output->frame.link);
|
||||
free(output);
|
||||
}
|
||||
|
||||
void new_output_notify(struct wl_listener *listener, void *data) {
|
||||
struct wb_server *server = wl_container_of(
|
||||
listener, server, new_output
|
||||
);
|
||||
struct wlr_output *wlr_output = data;
|
||||
|
||||
if (!wl_list_empty(&wlr_output->modes)) {
|
||||
struct wlr_output_mode *mode =
|
||||
wl_container_of(wlr_output->modes.prev, mode, link);
|
||||
wlr_output_set_mode(wlr_output, mode);
|
||||
|
||||
wlr_output_create_global(wlr_output);
|
||||
}
|
||||
|
||||
struct wb_output *output = calloc(1, sizeof(struct wb_output));
|
||||
clock_gettime(CLOCK_MONOTONIC, &output->last_frame);
|
||||
output->server = server;
|
||||
output->wlr_output = wlr_output;
|
||||
wl_list_insert(&server->outputs, &output->link);
|
||||
|
||||
output->destroy.notify = output_destroy_notify;
|
||||
wl_signal_add(&wlr_output->events.destroy, &output->destroy);
|
||||
output->frame.notify = output_frame_notify;
|
||||
wl_signal_add(&wlr_output->events.frame, &output->frame);
|
||||
}
|
||||
62
src/server.c
Normal file
62
src/server.c
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#include "waybox/server.h"
|
||||
|
||||
bool init_wb(struct wb_server* server) {
|
||||
|
||||
// create display
|
||||
server->wl_display = wl_display_create();
|
||||
assert(server->wl_display);
|
||||
|
||||
// create shared memory
|
||||
wl_display_init_shm(server->wl_display);
|
||||
|
||||
// event loop stuff
|
||||
server->wl_event_loop = wl_display_get_event_loop(server->wl_display);
|
||||
assert(server->wl_event_loop);
|
||||
|
||||
// create backend
|
||||
server->backend = wlr_backend_autocreate(server->wl_display, NULL);
|
||||
assert(server->backend);
|
||||
if(server->backend == NULL){
|
||||
printf("Failed to create backend\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start_wb(struct wb_server* server) {
|
||||
wl_list_init(&server->outputs);
|
||||
|
||||
server->new_output.notify = new_output_notify;
|
||||
wl_signal_add(&server->backend->events.new_output, &server->new_output);
|
||||
|
||||
const char *socket = wl_display_add_socket_auto(server->wl_display);
|
||||
assert(socket);
|
||||
|
||||
if (!wlr_backend_start(server->backend)) {
|
||||
fprintf(stderr, "Failed to start backend\n");
|
||||
wl_display_destroy(server->wl_display);
|
||||
return false;
|
||||
}
|
||||
|
||||
wlr_gamma_control_manager_create(server->wl_display);
|
||||
wlr_screenshooter_create(server->wl_display);
|
||||
wlr_primary_selection_device_manager_create(server->wl_display);
|
||||
wlr_idle_create(server->wl_display);
|
||||
|
||||
server->compositor = wlr_compositor_create(server->wl_display,
|
||||
wlr_backend_get_renderer(server->backend));
|
||||
|
||||
// wlr_xdg_shell_v6_create(server.wl_display);
|
||||
wlr_idle_create(server->wl_display);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool terminate_wb(struct wb_server* server) {
|
||||
wl_display_destroy(server->wl_display);
|
||||
|
||||
printf("Display destroyed.\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue