Skip to content

Commit

Permalink
Impl listener state helpers. Some other stuff.
Browse files Browse the repository at this point in the history
Added util functions.
Added listeners state helpers.
Added outputs state helpers.
Added explicit .clang-format.
  • Loading branch information
Luka Vilfan committed May 4, 2022
1 parent 448f9f1 commit 3abf364
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 29 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BasedOnStyle: Mozilla
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
IndentWidth: 4
57 changes: 56 additions & 1 deletion bonsai/config/output.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <wayland-server-core.h>
#include <wayland-util.h>
#include <wlr/types/wlr_output.h>

#include "bonsai/config/output.h"

Expand All @@ -10,7 +13,59 @@ bsi_outputs_init(struct bsi_outputs* bsi_outputs)
assert(bsi_outputs);

bsi_outputs->len = 0;
wl_list_init(&(bsi_outputs->outputs));
wl_list_init(&bsi_outputs->outputs);

return bsi_outputs;
}

void
bsi_outputs_add(struct bsi_outputs* bsi_outputs, struct bsi_output* bsi_output)
{
assert(bsi_outputs);
assert(bsi_output);

--bsi_outputs->len;
wl_list_insert(&bsi_outputs->outputs, &bsi_output->link);
}

void
bsi_outputs_remove(struct bsi_outputs* bsi_outputs,
struct bsi_output* bsi_output)
{
assert(bsi_outputs);
assert(bsi_output);

--bsi_outputs->len;
wl_list_remove(&bsi_output->link);
wl_list_remove(&bsi_output->destroy_listener.link);
wl_list_remove(&bsi_output->frame_listener.link);
free(bsi_output);
}

size_t
bsi_outputs_len(struct bsi_outputs* bsi_outputs)
{
return bsi_outputs->len;
}

void
bsi_output_add_destroy_listener(struct bsi_output* bsi_output,
wl_notify_func_t func)
{
assert(bsi_output);

bsi_output->destroy_listener.notify = func;
wl_signal_add(&bsi_output->wlr_output->events.destroy,
&bsi_output->destroy_listener);
}

void
bsi_output_add_frame_listener(struct bsi_output* bsi_output,
wl_notify_func_t func)
{
assert(bsi_output);

bsi_output->frame_listener.notify = func;
wl_signal_add(&bsi_output->wlr_output->events.frame,
&bsi_output->frame_listener);
}
22 changes: 22 additions & 0 deletions bonsai/config/signal.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include <assert.h>
#include <wayland-server-core.h>
#include <wayland-server.h>
#include <wayland-util.h>
#include <wlr/backend.h>

#include "bonsai/config/signal.h"
#include "bonsai/server.h"

struct bsi_listeners*
bsi_listeners_init(struct bsi_listeners* bsi_listeners,
struct bsi_server* bsi_server)
{
assert(bsi_listeners);

bsi_listeners->server = bsi_server;
bsi_listeners->active_listeners = 0;
return bsi_listeners;
Expand All @@ -16,14 +22,30 @@ void
bsi_listeners_add_new_output_notify(struct bsi_listeners* bsi_listeners,
wl_notify_func_t func)
{
assert(bsi_listeners);

bsi_listeners->active_listeners |= BSI_LISTENER_OUTPUT;
bsi_listeners->new_output_listener.notify = func;

struct bsi_server* server =
wl_container_of(bsi_listeners, server, bsi_listeners);

wl_signal_add(&server->wlr_backend->events.new_output,
&bsi_listeners->new_output_listener);
}

void
bsi_listeners_add_new_input_notify(struct bsi_listeners* bsi_listeners,
wl_notify_func_t func)
{
assert(bsi_listeners);

bsi_listeners->active_listeners |= BSI_LISTENER_INPUT;
bsi_listeners->new_input_listener.notify = func;

struct bsi_server* server =
wl_container_of(bsi_listeners, server, bsi_listeners);

wl_signal_add(&server->wlr_backend->events.new_input,
&bsi_listeners->new_input_listener);
}
24 changes: 7 additions & 17 deletions bonsai/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "bonsai/config/output.h"
#include "bonsai/config/signal.h"
#include "bonsai/server.h"
#include "bonsai/util.h"

static void
bsi_output_destroy_notify(struct wl_listener* listener, void* data)
Expand All @@ -26,11 +27,7 @@ bsi_output_destroy_notify(struct wl_listener* listener, void* data)
struct bsi_output* bsi_output =
wl_container_of(listener, bsi_output, destroy_listener);

wl_list_remove(&bsi_output->link);
wl_list_remove(&bsi_output->destroy_listener.link);
wl_list_remove(&bsi_output->frame_listener.link);

free(bsi_output);
bsi_outputs_remove(&bsi_output->server->bsi_outputs, bsi_output);
}

static void
Expand All @@ -46,8 +43,7 @@ bsi_output_frame_notify(struct wl_listener* listener, void* data)

wlr_scene_output_commit(wlr_scene_output);

struct timespec now;
timespec_get(&now, TIME_UTC);
struct timespec now = bsi_util_timespec_get();
wlr_scene_output_send_frame_done(wlr_scene_output, &now);
}

Expand All @@ -70,21 +66,17 @@ bsi_listeners_new_output_notify(struct wl_listener* listener, void* data)
return;
}

struct timespec now;
timespec_get(&now, TIME_UTC);
struct timespec now = bsi_util_timespec_get();

struct bsi_output* bsi_output = calloc(1, sizeof(struct bsi_output));
bsi_output->server = server;
bsi_output->wlr_output = wlr_output;
bsi_output->last_frame = now;
wl_list_insert(&server->bsi_outputs.outputs, &bsi_output->link);

bsi_output->destroy_listener.notify = bsi_output_destroy_notify;
wl_signal_add(&wlr_output->events.destroy, &bsi_output->destroy_listener);

bsi_output->frame_listener.notify = bsi_output_frame_notify;
wl_signal_add(&wlr_output->events.frame, &bsi_output->frame_listener);
bsi_output_add_destroy_listener(bsi_output, bsi_output_destroy_notify);
bsi_output_add_frame_listener(bsi_output, bsi_output_frame_notify);

bsi_outputs_add(&server->bsi_outputs, bsi_output);
wlr_output_layout_add_auto(server->wlr_output_layout, wlr_output);
}

Expand Down Expand Up @@ -137,8 +129,6 @@ main(void)

bsi_listeners_add_new_output_notify(&server.bsi_listeners,
bsi_listeners_new_output_notify);
wl_signal_add(&server.wlr_backend->events.new_output,
&server.bsi_listeners.new_output_listener);

wlr_log(WLR_DEBUG, "attached bsi_output_notify listeners");

Expand Down
2 changes: 2 additions & 0 deletions bonsai/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
bonsai_src = [
'main.c',

'util.c',

'config/output.c',
'config/signal.c'
]
Expand Down
11 changes: 11 additions & 0 deletions bonsai/util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <time.h>

#include "bonsai/util.h"

struct timespec
bsi_util_timespec_get()
{
struct timespec ts;
timespec_get(&ts, TIME_UTC);
return ts;
}
41 changes: 30 additions & 11 deletions include/bonsai/config/output.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#pragma once

#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <wayland-server-core.h>
#include <wayland-util.h>

/**
* @brief Holds all outputs the server knows about via signal listeners. The
* outputs list holds elements of type `struct bsi_output`
*
*/
struct bsi_outputs
{
size_t len;
struct wl_list outputs;
};

/**
* @brief Represents a single output.
*
Expand All @@ -22,16 +34,23 @@ struct bsi_output
struct wl_list link;
};

/**
* @brief Holds all outputs the server knows about via signal listeners. The
* outputs list holds elements of type `struct bsi_output`
*
*/
struct bsi_outputs
{
size_t len;
struct wl_list outputs;
};

struct bsi_outputs*
bsi_outputs_init(struct bsi_outputs* bsi_outputs);

void
bsi_outputs_add(struct bsi_outputs* bsi_outputs, struct bsi_output* bsi_output);

void
bsi_outputs_remove(struct bsi_outputs* bsi_outputs,
struct bsi_output* bsi_output);

size_t
bsi_outputs_len(struct bsi_outputs* bsi_outputs);

void
bsi_output_add_destroy_listener(struct bsi_output* bsi_output,
wl_notify_func_t func);

void
bsi_output_add_frame_listener(struct bsi_output* bsi_output,
wl_notify_func_t func);
4 changes: 4 additions & 0 deletions include/bonsai/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <time.h>

struct timespec
bsi_util_timespec_get();

0 comments on commit 3abf364

Please sign in to comment.