Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize views in front end code #54

Merged
merged 2 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions heart/example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@
#include <hrt/hrt_server.h>
#include <hrt/hrt_output.h>
#include <hrt/hrt_input.h>
#include <hrt/hrt_view.h>

void cursor_callback(struct hrt_seat *seat) {
static void cursor_callback(struct hrt_seat *seat) {
puts("Cursor callback called");
}

void output_callback(struct hrt_output *output) {
static void output_callback(struct hrt_output *output) {
puts("Output callback called");
}

static void new_view_callback(struct hrt_view *view) {
puts("New view callback called!");
}

static void view_destroy_callback(struct hrt_view *view) {
puts("View destroy callback called");
}

static bool showNormalCursor = true;
bool keyboard_callback(struct hrt_seat *seat, struct hrt_keypress_info *info) {
static bool keyboard_callback(struct hrt_seat *seat, struct hrt_keypress_info *info) {
puts("Keyboard callback called");
printf("Modifiers: %d\n", info->modifiers);
printf("Keys pressed:");
Expand All @@ -42,17 +51,22 @@ static const struct hrt_output_callbacks output_callbacks = {
};

static const struct hrt_seat_callbacks seat_callbacks = {
.button_event = &cursor_callback,
.wheel_event = &cursor_callback,
.keyboard_keypress_event = &keyboard_callback,
.button_event = &cursor_callback,
.wheel_event = &cursor_callback,
.keyboard_keypress_event = &keyboard_callback,
};

static const struct hrt_view_callbacks view_callbacks = {
.new_view = &new_view_callback,
.view_destroyed = &view_destroy_callback,
};

int main(int argc, char *argv[]) {
wlr_log_init(WLR_DEBUG, NULL);

struct hrt_server server;

if(!hrt_server_init(&server, &output_callbacks, &seat_callbacks, WLR_DEBUG)) {
if(!hrt_server_init(&server, &output_callbacks, &seat_callbacks, &view_callbacks, WLR_DEBUG)) {
return 1;
}

Expand Down
10 changes: 8 additions & 2 deletions heart/include/hrt/hrt_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,21 @@ struct hrt_server {
struct wl_listener new_xdg_surface;

const struct hrt_output_callbacks *output_callback;
const struct hrt_view_callbacks *view_callbacks;
};

bool hrt_server_init(struct hrt_server *server, const struct hrt_output_callbacks *output_callbacks,
const struct hrt_seat_callbacks *seat_callbacks, enum wlr_log_importance log_level);
bool hrt_server_init(struct hrt_server *server,
const struct hrt_output_callbacks *output_callbacks,
const struct hrt_seat_callbacks *seat_callbacks,
const struct hrt_view_callbacks *view_callbacks,
enum wlr_log_importance log_level);

bool hrt_server_start(struct hrt_server *server);

void hrt_server_stop(struct hrt_server *server);

void hrt_server_finish(struct hrt_server *server);

struct wlr_scene_tree *hrt_server_scene_tree(struct hrt_server *server);

#endif
37 changes: 37 additions & 0 deletions heart/include/hrt/hrt_view.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
#include <stdint.h>
#include <wayland-server-core.h>
#include "hrt/hrt_server.h"

struct hrt_view;

typedef void (*view_destroy_handler)(struct hrt_view *view);

struct hrt_view {
struct wlr_xdg_surface *xdg_surface;
struct wlr_xdg_toplevel *xdg_toplevel;
/*
Contains the tree with the xdg surface tree
plus decorations and that sort of thing.
*/
struct wlr_scene_tree *scene_tree;

// internal state:
struct wl_listener map;
struct wl_listener unmap;
struct wl_listener destroy;
view_destroy_handler destroy_handler;
};

struct hrt_view_callbacks {
/**
* A new view has been created. Must call `hrt_view_init` for the
* view to be displayed.
**/
void (*new_view)(struct hrt_view *view);
view_destroy_handler view_destroyed;
};

/**
* Fully initialize the view and place it in the given scene tree.
**/
void hrt_view_init(struct hrt_view *view, struct wlr_scene_tree *tree);

/**
* Request that this view be the given size. Returns the associated configure serial.
**/
uint32_t hrt_view_set_size(struct hrt_view *view, int width, int height);

/**
* Sets the view to the given coordinates relative to its parent.
**/
void hrt_view_set_relative(struct hrt_view *view, int x, int y);
9 changes: 5 additions & 4 deletions heart/src/meson.build
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
hrt_source_files += files(
'server.c',
'output.c',
'cursor.c',
'input.c',
'seat.c',
'keyboard.c',
'cursor.c',
'output.c',
'seat.c',
'server.c',
'view.c',
'xdg_shell.c',
)
13 changes: 11 additions & 2 deletions heart/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
#include <hrt/hrt_output.h>
#include <hrt/hrt_input.h>

bool hrt_server_init(struct hrt_server *server, const struct hrt_output_callbacks *output_callbacks,
const struct hrt_seat_callbacks *seat_callbacks, enum wlr_log_importance log_level) {
bool hrt_server_init(struct hrt_server *server,
const struct hrt_output_callbacks *output_callbacks,
const struct hrt_seat_callbacks *seat_callbacks,
const struct hrt_view_callbacks *view_callbacks,
enum wlr_log_importance log_level) {
wlr_log_init(log_level, NULL);
server->wl_display = wl_display_create();
server->backend = wlr_backend_autocreate(server->wl_display);
Expand Down Expand Up @@ -47,6 +50,8 @@ bool hrt_server_init(struct hrt_server *server, const struct hrt_output_callback

server->output_layout = wlr_output_layout_create();

server->view_callbacks = view_callbacks;

server->xdg_shell = wlr_xdg_shell_create(server->wl_display, 3);
server->new_xdg_surface.notify = handle_new_xdg_surface;
wl_signal_add(&server->xdg_shell->events.new_surface, &server->new_xdg_surface);
Expand Down Expand Up @@ -101,3 +106,7 @@ void hrt_server_finish(struct hrt_server *server) {
wl_display_destroy_clients(server->wl_display);
wl_display_destroy(server->wl_display);
}

struct wlr_scene_tree *hrt_server_scene_tree(struct hrt_server *server) {
return &server->scene->tree;
}
22 changes: 22 additions & 0 deletions heart/src/view.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdint.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/types/wlr_scene.h>

#include "hrt/hrt_view.h"

void hrt_view_init(struct hrt_view *view, struct wlr_scene_tree *tree) {
view->scene_tree = wlr_scene_tree_create(tree);

struct wlr_scene_tree *xdg_tree =
wlr_scene_xdg_surface_create(view->scene_tree, view->xdg_toplevel->base);
xdg_tree->node.data = view;
view->xdg_surface->data = xdg_tree;
}

uint32_t hrt_view_set_size(struct hrt_view *view, int width, int height) {
return wlr_xdg_toplevel_set_size(view->xdg_toplevel, width, height);
}

void hrt_view_set_relative(struct hrt_view *view, int x, int y) {
wlr_scene_node_set_position(&view->scene_tree->node, x, y);
}
33 changes: 21 additions & 12 deletions heart/src/xdg_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,23 @@ static void handle_xdg_toplevel_destroy(struct wl_listener *listener,
wlr_log(WLR_DEBUG, "XDG Toplevel Destroyed!");
struct hrt_view *view = wl_container_of(listener, view, destroy);

view->destroy_handler(view);

wl_list_remove(&view->map.link);
wl_list_remove(&view->unmap.link);
wl_list_remove(&view->destroy.link);

free(view);
}

struct hrt_view *initialize_view(struct wlr_xdg_surface *xdg_surface, struct wlr_scene_tree *tree) {
static struct hrt_view *create_view_from_xdg_surface(struct wlr_xdg_surface *xdg_surface, view_destroy_handler destroy_handler) {
// This method can only deal with toplevel xdg_surfaces:
assert(xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL);
struct hrt_view *view = calloc(1, sizeof(struct hrt_view));
view->xdg_toplevel = xdg_surface->toplevel;
view->xdg_surface = xdg_surface;
view->destroy_handler = destroy_handler;

// Add the view to the scene tree (we should probab
view->scene_tree = wlr_scene_xdg_surface_create(tree, view->xdg_toplevel->base);
view->scene_tree->node.data = view;
xdg_surface->data = view->scene_tree;

// Listen to events:
view->map.notify = handle_xdg_toplevel_map;
wl_signal_add(&xdg_surface->events.map, &view->map);
view->unmap.notify = handle_xdg_toplevel_unmap;
Expand All @@ -54,23 +52,34 @@ struct hrt_view *initialize_view(struct wlr_xdg_surface *xdg_surface, struct wlr
return view;
}


void handle_new_xdg_surface(struct wl_listener *listener, void *data) {
wlr_log(WLR_DEBUG, "New XDG Surface recieved");
struct hrt_server *server = wl_container_of(listener, server, new_xdg_surface);
struct wlr_xdg_surface *xdg_surface = data;

if(xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) {
// We the front end doesn't need to know about popups; wlroots handles it for us.
// The front end doesn't need to know about popups; wlroots handles it for us.
// we do need to set some internal data so that they can be rendered though.
struct wlr_xdg_surface *parent = wlr_xdg_surface_from_wlr_surface(xdg_surface->popup->parent);
struct wlr_scene_tree *parent_tree = parent->data;
xdg_surface->data = wlr_scene_xdg_surface_create(
parent_tree, xdg_surface);
// The parent view might not have been initizlized properly. In that case, it
// isn't being displayed, so we just ignore it:
if (parent_tree) {
xdg_surface->data = wlr_scene_xdg_surface_create(parent_tree, xdg_surface);
} else {
wlr_log(WLR_ERROR, "Encountered XDG Popup without properly configured parent");
}
return;
}

// Initialization occurs in two steps so the consumer can place the view where it needs to go;
// in order to create a scene tree node, it must have a parent.
// We don't have it until the callback.
struct hrt_view *view = create_view_from_xdg_surface(xdg_surface,
server->view_callbacks->view_destroyed);
// At some point, we will want the front end to call this, as it should decide what node
// of the scene graph the view gets added to:
initialize_view(xdg_surface, &server->scene->tree);
// hrt_view_init(view, &server->scene->tree);

server->view_callbacks->new_view(view);
}
50 changes: 41 additions & 9 deletions lisp/bindings/hrt-bindings.lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
(cl:in-package #:hrt)

;; next section imported from file build/include/hrt/hrt_view.h

(cffi:defcstruct hrt-view)

(cffi:defctype view-destroy-handler :pointer #| function ptr void (struct hrt_view *) |#)

(cffi:defcstruct hrt-view
(xdg-surface :pointer #| (:struct wlr-xdg-surface) |# )
(xdg-toplevel :pointer #| (:struct wlr-xdg-toplevel) |# )
(scene-tree :pointer #| (:struct wlr-scene-tree) |# )
(map (:struct wl-listener))
(unmap (:struct wl-listener))
(destroy (:struct wl-listener))
(destroy-handler view-destroy-handler))

(cffi:defcstruct hrt-view-callbacks
(new-view :pointer #| function ptr void (struct hrt_view *) |#)
(view-destroyed view-destroy-handler))

(cffi:defcfun ("hrt_view_init" hrt-view-init) :void
"Fully initialize the view and place it in the given scene tree."
(view (:pointer (:struct hrt-view)))
(tree :pointer #| (:struct wlr-scene-tree) |# ))

(cffi:defcfun ("hrt_view_set_size" hrt-view-set-size) :uint32
"Request that this view be the given size. Returns the associated configure serial."
(view (:pointer (:struct hrt-view)))
(width :int)
(height :int))

(cffi:defcfun ("hrt_view_set_relative" hrt-view-set-relative) :void
"Sets the view to the given coordinates relative to its parent."
(view (:pointer (:struct hrt-view)))
(x :int)
(y :int))

;; next section imported from file build/include/hrt/hrt_input.h

(cffi:defcstruct hrt-server)
Expand Down Expand Up @@ -100,12 +136,14 @@ See themes section of man xcursor(3) to find where to find valid cursor names."
(seat (:struct hrt-seat))
(xdg-shell :pointer #| (:struct wlr-xdg-shell) |# )
(new-xdg-surface (:struct wl-listener))
(output-callback (:pointer (:struct hrt-output-callbacks))))
(output-callback (:pointer (:struct hrt-output-callbacks)))
(view-callbacks (:pointer (:struct hrt-view-callbacks))))

(cffi:defcfun ("hrt_server_init" hrt-server-init) :bool
(server (:pointer (:struct hrt-server)))
(output-callbacks (:pointer (:struct hrt-output-callbacks)))
(seat-callbacks (:pointer (:struct hrt-seat-callbacks)))
(view-callbacks (:pointer (:struct hrt-view-callbacks)))
(log-level :int #| enum wlr-log-importance |#))

(cffi:defcfun ("hrt_server_start" hrt-server-start) :bool
Expand All @@ -117,11 +155,5 @@ See themes section of man xcursor(3) to find where to find valid cursor names."
(cffi:defcfun ("hrt_server_finish" hrt-server-finish) :void
(server (:pointer (:struct hrt-server))))

;; next section imported from file build/include/hrt/hrt_view.h

(cffi:defcstruct hrt-view
(xdg-toplevel :pointer #| (:struct wlr-xdg-toplevel) |# )
(scene-tree :pointer #| (:struct wlr-scene-tree) |# )
(map (:struct wl-listener))
(unmap (:struct wl-listener))
(destroy (:struct wl-listener)))
(cffi:defcfun ("hrt_server_scene_tree" hrt-server-scene-tree) :pointer #| (:struct wlr-scene-tree) |#
(server (:pointer (:struct hrt-server))))
2 changes: 1 addition & 1 deletion lisp/bindings/hrt-bindings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ arguments:
- "-DWLR_USE_UNSTABLE"
- "-Iheart/include"
files:
- build/include/hrt/hrt_view.h
- build/include/hrt/hrt_input.h
- build/include/hrt/hrt_output.h
- build/include/hrt/hrt_server.h
- build/include/hrt/hrt_view.h
pointer-expansion:
include:
match: "hrt.*"
6 changes: 6 additions & 0 deletions lisp/bindings/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
(:nicknames #:hrt)
(:export #:hrt-output-callbacks
#:hrt-seat-callbacks
#:hrt-view-callbacks
#:new-view
#:hrt-view
#:hrt-view-init
#:view-destroyed
#:hrt-seat
#:hrt-output
#:hrt-keypress-info
#:output-added
#:output-removed
#:button-event #:wheel-event #:keyboard-keypress-event
#:hrt-server
#:hrt-server-scene-tree
#:hrt-server-init
#:hrt-server-start
#:hrt-server-stop
Expand Down
Loading
Loading