Skip to content

Commit

Permalink
Handle new views in frontend
Browse files Browse the repository at this point in the history
Add view callbacks and use them to initialize views in the lisp code.

The view lists added to the state object might not stick around and
were added for illustrative purposes
  • Loading branch information
sdilts committed Oct 28, 2023
1 parent c512af2 commit 7f878c0
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 44 deletions.
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
16 changes: 16 additions & 0 deletions heart/include/hrt/hrt_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#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;
Expand All @@ -10,9 +14,21 @@ struct hrt_view {
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;
};

/**
Expand Down
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;
}
12 changes: 9 additions & 3 deletions heart/src/xdg_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ 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);
}

static struct hrt_view *create_view_from_xdg_surface(struct wlr_xdg_surface *xdg_surface) {
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;

view->map.notify = handle_xdg_toplevel_map;
wl_signal_add(&xdg_surface->events.map, &view->map);
Expand Down Expand Up @@ -72,8 +75,11 @@ void handle_new_xdg_surface(struct wl_listener *listener, void *data) {
// 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);
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:
hrt_view_init(view, &server->scene->tree);
// hrt_view_init(view, &server->scene->tree);

server->view_callbacks->new_view(view);
}
68 changes: 41 additions & 27 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,29 +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-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)))

(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))
(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
10 changes: 9 additions & 1 deletion lisp/main.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@
(setf ,@(loop for pair in sets
append (list (car pair) `(cffi:callback ,(cadr pair))))))))

(defun init-view-callbacks (view-callbacks)
(init-callback-struct view-callbacks (:struct hrt-view-callbacks)
(new-view handle-new-view-event)
(view-destroyed handle-view-destroyed-event)))

(defun run-server ()
(disable-fpu-exceptions)
(hrt:load-foreign-libraries)
(log-init :level :trace)
(cffi:with-foreign-objects ((output-callbacks '(:struct hrt-output-callbacks))
(seat-callbacks '(:struct hrt-seat-callbacks))
(view-callbacks '(:struct hrt-view-callbacks))
(server '(:struct hrt-server)))
(init-callback-struct output-callbacks (:struct hrt-output-callbacks)
(output-added output-callback)
Expand All @@ -46,9 +52,11 @@
(button-event cursor-callback)
(wheel-event cursor-callback)
(keyboard-keypress-event keyboard-callback))
(init-view-callbacks view-callbacks)

(setf (mahogany-state-server *compositor-state*) server)
(log-string :debug "Initialized mahogany state")
(hrt-server-init server output-callbacks seat-callbacks 3)
(hrt-server-init server output-callbacks seat-callbacks view-callbacks 3)
(log-string :debug "Initialized heart state")
(unwind-protect
(hrt-server-start server)
Expand Down
16 changes: 15 additions & 1 deletion lisp/state.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
:accessor mahogany-state-key-state)
(keybindings :type list
:initform nil
:reader mahogany-state-keybindings)))
:reader mahogany-state-keybindings)
(views :type list
:initform nil
:reader mahogany-state-views)))

;; (defmethod initialize-instance :after ((object mahogany-state) &key &allow-other-keys))

Expand All @@ -31,3 +34,14 @@
(setf (slot-value state 'keybindings) kmaps)
(unless (key-state-active-p (mahogany-state-key-state state))
(server-keystate-reset state)))

(defun mahogany-state-view-add (state view)
(declare (type mahogany-state state))
(push view (slot-value state 'views))
(log-string :trace "Views: ~S" (slot-value state 'views)))

(defun mahogany-state-view-remove (state view)
(declare (type mahogany-state state))
(with-slots (views) state
(setf views (remove view views :test #'cffi:pointer-eq))
(log-string :trace "Views: ~S" views)))
12 changes: 12 additions & 0 deletions lisp/view.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(in-package #:mahogany)

(cffi:defcallback handle-new-view-event :void
((view (:pointer (:struct hrt-view))))
(log-string :trace "New view callback called!")
(hrt-view-init view (hrt-server-scene-tree (mahogany-state-server *compositor-state*)))
(mahogany-state-view-add *compositor-state* view))

(cffi:defcallback handle-view-destroyed-event :void
((view (:pointer (:struct hrt-view))))
(log-string :trace "View destroyed callback called!")
(mahogany-state-view-remove *compositor-state* view))
1 change: 1 addition & 0 deletions mahogany.asd
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
(:file "frame" :depends-on ("tree-interface"))
(:file "view" :depends-on ("tree-interface"))))
(:file "state" :depends-on ("package"))
(:file "view" :depends-on ("package" "bindings"))
(:file "input" :depends-on ("state" "keyboard"))
(:file "globals" :depends-on ("state" "system"))
(:file "main" :depends-on ("bindings" "keyboard" "input" "package"))))
Expand Down

0 comments on commit 7f878c0

Please sign in to comment.