summaryrefslogtreecommitdiff
path: root/lib/river
diff options
context:
space:
mode:
Diffstat (limited to 'lib/river')
-rw-r--r--lib/river/include/astal-river.h59
-rw-r--r--lib/river/include/meson.build6
-rw-r--r--lib/river/include/river-private.h20
-rw-r--r--lib/river/include/wayland-source.h16
-rw-r--r--lib/river/meson.build18
-rw-r--r--lib/river/meson_options.txt13
-rw-r--r--lib/river/protocols/meson.build23
-rw-r--r--lib/river/protocols/river-control-unstable-v1.xml86
-rw-r--r--lib/river/protocols/river-status-unstable-v1.xml149
-rw-r--r--lib/river/src/astal-river.c51
-rw-r--r--lib/river/src/meson.build71
-rw-r--r--lib/river/src/river-output.c343
-rw-r--r--lib/river/src/river.c504
-rw-r--r--lib/river/src/wayland-source.c104
-rw-r--r--lib/river/version1
15 files changed, 1464 insertions, 0 deletions
diff --git a/lib/river/include/astal-river.h b/lib/river/include/astal-river.h
new file mode 100644
index 0000000..6bedd94
--- /dev/null
+++ b/lib/river/include/astal-river.h
@@ -0,0 +1,59 @@
+#ifndef ASTAL_RIVER_H
+#define ASTAL_RIVER_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define ASTAL_RIVER_TYPE_OUTPUT (astal_river_output_get_type())
+
+G_DECLARE_FINAL_TYPE(AstalRiverOutput, astal_river_output, ASTAL_RIVER, OUTPUT, GObject)
+
+guint astal_river_output_get_id(AstalRiverOutput *self);
+
+gchar *astal_river_output_get_name(AstalRiverOutput *self);
+
+gchar *astal_river_output_get_layout_name(AstalRiverOutput *self);
+
+gchar *astal_river_output_get_focused_view(AstalRiverOutput *self);
+
+guint astal_river_output_get_focused_tags(AstalRiverOutput *self);
+
+guint astal_river_output_get_urgent_tags(AstalRiverOutput *self);
+
+guint astal_river_output_get_occupied_tags(AstalRiverOutput *self);
+
+#define ASTAL_RIVER_TYPE_RIVER (astal_river_river_get_type())
+
+G_DECLARE_FINAL_TYPE(AstalRiverRiver, astal_river_river, ASTAL_RIVER, RIVER, GObject)
+
+AstalRiverRiver *astal_river_river_new();
+
+AstalRiverRiver *astal_river_river_get_default();
+AstalRiverRiver *astal_river_get_default();
+
+GList *astal_river_river_get_outputs(AstalRiverRiver *self);
+
+AstalRiverOutput *astal_river_river_get_output(AstalRiverRiver *self, gchar *name);
+
+gchar *astal_river_river_get_focused_view(AstalRiverRiver *self);
+
+gchar *astal_river_river_get_focused_output(AstalRiverRiver *self);
+
+gchar *astal_river_river_get_mode(AstalRiverRiver *self);
+
+/**
+ * AstalRiverCommandCallback:
+ * @success: a #gboolean indicating whether the command was executed successfully
+ * @msg: a string containing the result of the command
+ *
+ * A callback function that is called after a river command is run.
+ */
+typedef void (*AstalRiverCommandCallback)(gboolean success, const gchar *msg);
+
+void astal_river_river_run_command_async(AstalRiverRiver *self, gint length, const gchar **cmd,
+ AstalRiverCommandCallback callback);
+
+G_END_DECLS
+
+#endif // !ASTAL_RIVER_H
diff --git a/lib/river/include/meson.build b/lib/river/include/meson.build
new file mode 100644
index 0000000..4b08a89
--- /dev/null
+++ b/lib/river/include/meson.build
@@ -0,0 +1,6 @@
+astal_river_inc = include_directories('.')
+astal_river_headers = files(
+ 'astal-river.h',
+)
+
+install_headers('astal-river.h')
diff --git a/lib/river/include/river-private.h b/lib/river/include/river-private.h
new file mode 100644
index 0000000..14cd1c5
--- /dev/null
+++ b/lib/river/include/river-private.h
@@ -0,0 +1,20 @@
+#ifndef ASTAL_RIVER_OUTPUT_PRIVATE_H
+#define ASTAL_RIVER_OUTPUT_PRIVATE_H
+
+#include <wayland-client.h>
+
+#include "astal-river.h"
+#include "river-status-unstable-v1-client.h"
+
+G_BEGIN_DECLS
+
+AstalRiverOutput *astal_river_output_new(guint id, struct wl_output *wl_output,
+ struct zriver_status_manager_v1 *status_manager,
+ struct wl_display *wl_display);
+
+struct wl_output *astal_river_output_get_wl_output(AstalRiverOutput *self);
+void astal_river_output_set_focused_view(AstalRiverOutput *self, const gchar *focused_view);
+
+G_END_DECLS
+
+#endif // !ASTAL_RIVER_OUTPUT_PRIVATE_H
diff --git a/lib/river/include/wayland-source.h b/lib/river/include/wayland-source.h
new file mode 100644
index 0000000..b219589
--- /dev/null
+++ b/lib/river/include/wayland-source.h
@@ -0,0 +1,16 @@
+#ifndef __WAYLAND_SOURCE_H__
+#define __WAYLAND_SOURCE_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+typedef struct _WLSource WLSource;
+
+WLSource* wl_source_new();
+void wl_source_free(WLSource* self);
+struct wl_display* wl_source_get_display(WLSource* source);
+
+G_END_DECLS
+
+#endif /* __WAYLAND_SOURCE_H__ */
diff --git a/lib/river/meson.build b/lib/river/meson.build
new file mode 100644
index 0000000..aa5e23b
--- /dev/null
+++ b/lib/river/meson.build
@@ -0,0 +1,18 @@
+project(
+ 'astal_river',
+ 'c',
+ version: '0.1.0',
+ default_options: ['c_std=gnu11', 'warning_level=3', 'prefix=/usr'],
+)
+
+add_project_arguments(['-Wno-pedantic', '-Wno-unused-parameter'], language: 'c')
+
+version_split = meson.project_version().split('.')
+lib_so_version = version_split[0] + '.' + version_split[1]
+
+pkg_config = import('pkgconfig')
+gnome = import('gnome')
+
+subdir('protocols')
+subdir('include')
+subdir('src')
diff --git a/lib/river/meson_options.txt b/lib/river/meson_options.txt
new file mode 100644
index 0000000..97aa4e7
--- /dev/null
+++ b/lib/river/meson_options.txt
@@ -0,0 +1,13 @@
+option(
+ 'introspection',
+ type: 'boolean',
+ value: true,
+ description: 'Build gobject-introspection data',
+)
+
+option(
+ 'vapi',
+ type: 'boolean',
+ value: true,
+ description: 'Generate vapi data (needs vapigen & introspection option)',
+)
diff --git a/lib/river/protocols/meson.build b/lib/river/protocols/meson.build
new file mode 100644
index 0000000..ddd825f
--- /dev/null
+++ b/lib/river/protocols/meson.build
@@ -0,0 +1,23 @@
+wayland_scanner = find_program('wayland-scanner')
+
+protocols = ['river-status-unstable-v1.xml', 'river-control-unstable-v1.xml']
+
+gen_client_header = generator(
+ wayland_scanner,
+ output: ['@[email protected]'],
+ arguments: ['-c', 'client-header', '@INPUT@', '@BUILD_DIR@/@[email protected]'],
+)
+
+gen_private_code = generator(
+ wayland_scanner,
+ output: ['@[email protected]'],
+ arguments: ['-c', 'private-code', '@INPUT@', '@BUILD_DIR@/@[email protected]'],
+)
+
+client_protocol_srcs = []
+
+foreach protocol : protocols
+ client_header = gen_client_header.process(protocol)
+ code = gen_private_code.process(protocol)
+ client_protocol_srcs += [client_header, code]
+endforeach
diff --git a/lib/river/protocols/river-control-unstable-v1.xml b/lib/river/protocols/river-control-unstable-v1.xml
new file mode 100644
index 0000000..c431901
--- /dev/null
+++ b/lib/river/protocols/river-control-unstable-v1.xml
@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<protocol name="river_control_unstable_v1">
+ <copyright>
+ Copyright 2020 The River Developers
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ </copyright>
+
+ <interface name="zriver_control_v1" version="1">
+ <description summary="run compositor commands">
+ This interface allows clients to run compositor commands and receive a
+ success/failure response with output or a failure message respectively.
+
+ Each command is built up in a series of add_argument requests and
+ executed with a run_command request. The first argument is the command
+ to be run.
+
+ A complete list of commands should be made available in the man page of
+ the compositor.
+ </description>
+
+ <request name="destroy" type="destructor">
+ <description summary="destroy the river_control object">
+ This request indicates that the client will not use the
+ river_control object any more. Objects that have been created
+ through this instance are not affected.
+ </description>
+ </request>
+
+ <request name="add_argument">
+ <description summary="add an argument to the current command">
+ Arguments are stored by the server in the order they were sent until
+ the run_command request is made.
+ </description>
+ <arg name="argument" type="string" summary="the argument to add"/>
+ </request>
+
+ <request name="run_command">
+ <description summary="run the current command">
+ Execute the command built up using the add_argument request for the
+ given seat.
+ </description>
+ <arg name="seat" type="object" interface="wl_seat"/>
+ <arg name="callback" type="new_id" interface="zriver_command_callback_v1"
+ summary="callback object"/>
+ </request>
+ </interface>
+
+ <interface name="zriver_command_callback_v1" version="1">
+ <description summary="callback object">
+ This object is created by the run_command request. Exactly one of the
+ success or failure events will be sent. This object will be destroyed
+ by the compositor after one of the events is sent.
+ </description>
+
+ <event name="success" type="destructor">
+ <description summary="command successful">
+ Sent when the command has been successfully received and executed by
+ the compositor. Some commands may produce output, in which case the
+ output argument will be a non-empty string.
+ </description>
+ <arg name="output" type="string" summary="the output of the command"/>
+ </event>
+
+ <event name="failure" type="destructor">
+ <description summary="command failed">
+ Sent when the command could not be carried out. This could be due to
+ sending a non-existent command, no command, not enough arguments, too
+ many arguments, invalid arguments, etc.
+ </description>
+ <arg name="failure_message" type="string"
+ summary="a message explaining why failure occurred"/>
+ </event>
+ </interface>
+</protocol>
+
diff --git a/lib/river/protocols/river-status-unstable-v1.xml b/lib/river/protocols/river-status-unstable-v1.xml
new file mode 100644
index 0000000..09c52c1
--- /dev/null
+++ b/lib/river/protocols/river-status-unstable-v1.xml
@@ -0,0 +1,149 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<protocol name="river_status_unstable_v1">
+ <copyright>
+ Copyright 2020 The River Developers
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ </copyright>
+
+ <interface name="zriver_status_manager_v1" version="4">
+ <description summary="manage river status objects">
+ A global factory for objects that receive status information specific
+ to river. It could be used to implement, for example, a status bar.
+ </description>
+
+ <request name="destroy" type="destructor">
+ <description summary="destroy the river_status_manager object">
+ This request indicates that the client will not use the
+ river_status_manager object any more. Objects that have been created
+ through this instance are not affected.
+ </description>
+ </request>
+
+ <request name="get_river_output_status">
+ <description summary="create an output status object">
+ This creates a new river_output_status object for the given wl_output.
+ </description>
+ <arg name="id" type="new_id" interface="zriver_output_status_v1"/>
+ <arg name="output" type="object" interface="wl_output"/>
+ </request>
+
+ <request name="get_river_seat_status">
+ <description summary="create a seat status object">
+ This creates a new river_seat_status object for the given wl_seat.
+ </description>
+ <arg name="id" type="new_id" interface="zriver_seat_status_v1"/>
+ <arg name="seat" type="object" interface="wl_seat"/>
+ </request>
+ </interface>
+
+ <interface name="zriver_output_status_v1" version="4">
+ <description summary="track output tags and focus">
+ This interface allows clients to receive information about the current
+ windowing state of an output.
+ </description>
+
+ <request name="destroy" type="destructor">
+ <description summary="destroy the river_output_status object">
+ This request indicates that the client will not use the
+ river_output_status object any more.
+ </description>
+ </request>
+
+ <event name="focused_tags">
+ <description summary="focused tags of the output">
+ Sent once binding the interface and again whenever the tag focus of
+ the output changes.
+ </description>
+ <arg name="tags" type="uint" summary="32-bit bitfield"/>
+ </event>
+
+ <event name="view_tags">
+ <description summary="tag state of an output's views">
+ Sent once on binding the interface and again whenever the tag state
+ of the output changes.
+ </description>
+ <arg name="tags" type="array" summary="array of 32-bit bitfields"/>
+ </event>
+
+ <event name="urgent_tags" since="2">
+ <description summary="tags of the output with an urgent view">
+ Sent once on binding the interface and again whenever the set of
+ tags with at least one urgent view changes.
+ </description>
+ <arg name="tags" type="uint" summary="32-bit bitfield"/>
+ </event>
+
+ <event name="layout_name" since="4">
+ <description summary="name of the layout">
+ Sent once on binding the interface should a layout name exist and again
+ whenever the name changes.
+ </description>
+ <arg name="name" type="string" summary="layout name"/>
+ </event>
+
+ <event name="layout_name_clear" since="4">
+ <description summary="name of the layout">
+ Sent when the current layout name has been removed without a new one
+ being set, for example when the active layout generator disconnects.
+ </description>
+ </event>
+ </interface>
+
+ <interface name="zriver_seat_status_v1" version="3">
+ <description summary="track seat focus">
+ This interface allows clients to receive information about the current
+ focus of a seat. Note that (un)focused_output events will only be sent
+ if the client has bound the relevant wl_output globals.
+ </description>
+
+ <request name="destroy" type="destructor">
+ <description summary="destroy the river_seat_status object">
+ This request indicates that the client will not use the
+ river_seat_status object any more.
+ </description>
+ </request>
+
+ <event name="focused_output">
+ <description summary="the seat focused an output">
+ Sent on binding the interface and again whenever an output gains focus.
+ </description>
+ <arg name="output" type="object" interface="wl_output"/>
+ </event>
+
+ <event name="unfocused_output">
+ <description summary="the seat unfocused an output">
+ Sent whenever an output loses focus.
+ </description>
+ <arg name="output" type="object" interface="wl_output"/>
+ </event>
+
+ <event name="focused_view">
+ <description summary="information on the focused view">
+ Sent once on binding the interface and again whenever the focused
+ view or a property thereof changes. The title may be an empty string
+ if no view is focused or the focused view did not set a title.
+ </description>
+ <arg name="title" type="string" summary="title of the focused view"/>
+ </event>
+
+ <event name="mode" since="3">
+ <description summary="the active mode changed">
+ Sent once on binding the interface and again whenever a new mode
+ is entered (e.g. with riverctl enter-mode foobar).
+ </description>
+ <arg name="name" type="string" summary="name of the mode"/>
+ </event>
+ </interface>
+</protocol>
+
diff --git a/lib/river/src/astal-river.c b/lib/river/src/astal-river.c
new file mode 100644
index 0000000..37f34d5
--- /dev/null
+++ b/lib/river/src/astal-river.c
@@ -0,0 +1,51 @@
+#include <getopt.h>
+#include <json-glib/json-glib.h>
+#include <stdlib.h>
+
+#include "gio/gio.h"
+#include "astal-river.h"
+
+GMainLoop* loop;
+
+void print_json(AstalRiverRiver* river) {
+ JsonNode* json = json_gobject_serialize(G_OBJECT(river));
+
+ gchar* json_str = json_to_string(json, FALSE);
+ g_print("%s\n", json_str);
+ g_free(json);
+}
+
+int main(int argc, char** argv) {
+ gboolean daemon = FALSE;
+
+ int opt;
+ const char* optstring = "d";
+
+ static struct option long_options[] = {{"daemon", no_argument, NULL, 'd'}, {NULL, 0, NULL, 0}};
+
+ while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
+ switch (opt) {
+ case 'd':
+ daemon = TRUE;
+ break;
+ default:
+ g_print("Usage: %s [-d]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ GError* error = NULL;
+ AstalRiverRiver* river = g_initable_new(ASTAL_RIVER_TYPE_RIVER, NULL, &error, NULL);
+ if (error) {
+ g_critical("%s\n", error->message);
+ exit(EXIT_FAILURE);
+ }
+ if (daemon) {
+ loop = g_main_loop_new(NULL, FALSE);
+ g_signal_connect(river, "changed", G_CALLBACK(print_json), NULL);
+ g_main_loop_run(loop);
+ } else {
+ print_json(river);
+ g_object_unref(river);
+ }
+}
diff --git a/lib/river/src/meson.build b/lib/river/src/meson.build
new file mode 100644
index 0000000..b7ce20d
--- /dev/null
+++ b/lib/river/src/meson.build
@@ -0,0 +1,71 @@
+srcs = files(
+ 'river-output.c',
+ 'river.c',
+ 'wayland-source.c',
+)
+
+deps = [
+ dependency('gobject-2.0'),
+ dependency('gio-2.0'),
+ dependency('wayland-client'),
+ dependency('json-glib-1.0'),
+]
+
+astal_river_lib = library(
+ 'astal-river',
+ sources: srcs + client_protocol_srcs,
+ include_directories: astal_river_inc,
+ dependencies: deps,
+ version: meson.project_version(),
+ install: true,
+)
+
+libastal_river = declare_dependency(link_with: astal_river_lib, include_directories: astal_river_inc)
+
+executable(
+ 'astal-river',
+ files('astal-river.c'),
+ dependencies: [
+ dependency('gobject-2.0'),
+ dependency('gio-2.0'),
+ dependency('json-glib-1.0'),
+ libastal_river,
+ ],
+ install: true,
+)
+
+pkg_config_name = 'astal-river-' + lib_so_version
+
+if get_option('introspection')
+ gir = gnome.generate_gir(
+ astal_river_lib,
+ sources: srcs + astal_river_headers,
+ nsversion: '0.1',
+ namespace: 'AstalRiver',
+ symbol_prefix: 'astal_river',
+ identifier_prefix: 'AstalRiver',
+ includes: ['GObject-2.0', 'Gio-2.0'],
+ header: 'astal-river.h',
+ export_packages: pkg_config_name,
+ install: true,
+ )
+
+ if get_option('vapi')
+ gnome.generate_vapi(
+ pkg_config_name,
+ sources: [gir[0]],
+ packages: ['gobject-2.0', 'gio-2.0'],
+ install: true,
+ )
+ endif
+endif
+
+pkg_config.generate(
+ name: 'astal-river',
+ version: meson.project_version(),
+ libraries: [astal_river_lib],
+ filebase: pkg_config_name,
+ subdirs: 'astal',
+ description: 'astal riverentication module',
+ url: 'https://github.com/astal-sh/river',
+)
diff --git a/lib/river/src/river-output.c b/lib/river/src/river-output.c
new file mode 100644
index 0000000..6317db1
--- /dev/null
+++ b/lib/river/src/river-output.c
@@ -0,0 +1,343 @@
+#include <gio/gio.h>
+
+#include "river-private.h"
+#include "river-status-unstable-v1-client.h"
+
+struct _AstalRiverOutput {
+ GObject parent_instance;
+ guint focused_tags;
+ guint occupied_tags;
+ guint urgent_tags;
+ gchar* layout_name;
+ gchar* focused_view;
+ guint id;
+ gchar* name;
+};
+
+typedef struct {
+ struct zriver_status_manager_v1* river_status_manager;
+ struct zriver_output_status_v1* river_output_status;
+ struct wl_display* wl_display;
+ struct wl_output* wl_output;
+} AstalRiverOutputPrivate;
+
+G_DEFINE_FINAL_TYPE_WITH_PRIVATE(AstalRiverOutput, astal_river_output, G_TYPE_OBJECT);
+
+typedef enum {
+ ASTAL_RIVER_OUTPUT_PROP_FOCUSED_TAGS = 1,
+ ASTAL_RIVER_OUTPUT_PROP_OCCUPIED_TAGS,
+ ASTAL_RIVER_OUTPUT_PROP_URGENT_TAGS,
+ ASTAL_RIVER_OUTPUT_PROP_LAYOUT_NAME,
+ ASTAL_RIVER_OUTPUT_PROP_NAME,
+ ASTAL_RIVER_OUTPUT_PROP_FOCUSED_VIEW,
+ ASTAL_RIVER_OUTPUT_PROP_ID,
+ ASTAL_RIVER_OUTPUT_N_PROPERTIES
+} AstalRiverOutputProperties;
+
+typedef enum {
+ ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED,
+ ASTAL_RIVER_OUTPUT_N_SIGNALS
+} AstalRiverOutputSignals;
+
+static guint astal_river_output_signals[ASTAL_RIVER_OUTPUT_N_SIGNALS] = {
+ 0,
+};
+
+static GParamSpec* astal_river_output_properties[ASTAL_RIVER_OUTPUT_N_PROPERTIES] = {
+ NULL,
+};
+
+/**
+ * astal_river_output_get_nid
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: the id of the underlying wl_output object
+ */
+guint astal_river_output_get_id(AstalRiverOutput* self) { return self->id; }
+
+/**
+ * astal_river_output_get_name
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: (transfer none) (nullable): the name of the output
+ */
+gchar* astal_river_output_get_name(AstalRiverOutput* self) { return self->name; }
+
+/**
+ * astal_river_output_get_layout_name
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: (transfer none) (nullable): the currently used layout name of the output
+ */
+gchar* astal_river_output_get_layout_name(AstalRiverOutput* self) { return self->layout_name; }
+
+/**
+ * astal_river_output_get_focused_view
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: (transfer none) (nullable): the focused view on the output
+ */
+gchar* astal_river_output_get_focused_view(AstalRiverOutput* self) { return self->focused_view; }
+
+void astal_river_output_set_focused_view(AstalRiverOutput* self, const gchar* focused_view) {
+ g_free(self->focused_view);
+ self->focused_view = g_strdup(focused_view);
+ g_object_notify(G_OBJECT(self), "focused-view");
+ g_signal_emit(self, astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED], 0);
+}
+
+/**
+ * astal_river_output_get_focused_tags
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: the focused tags of the output
+ */
+guint astal_river_output_get_focused_tags(AstalRiverOutput* self) { return self->focused_tags; }
+
+/**
+ * astal_river_output_get_urgent_tags
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: the urgent tags of the output
+ */
+guint astal_river_output_get_urgent_tags(AstalRiverOutput* self) { return self->urgent_tags; }
+
+/**
+ * astal_river_output_get_occupied_tags
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: the occupied tags of the output
+ */
+guint astal_river_output_get_occupied_tags(AstalRiverOutput* self) { return self->occupied_tags; }
+
+struct wl_output* astal_river_output_get_wl_output(AstalRiverOutput* self) {
+ AstalRiverOutputPrivate* priv = astal_river_output_get_instance_private(self);
+ return priv->wl_output;
+}
+
+static void astal_river_output_get_property(GObject* object, guint property_id, GValue* value,
+ GParamSpec* pspec) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(object);
+
+ switch (property_id) {
+ case ASTAL_RIVER_OUTPUT_PROP_FOCUSED_TAGS:
+ g_value_set_uint(value, self->focused_tags);
+ break;
+ case ASTAL_RIVER_OUTPUT_PROP_OCCUPIED_TAGS:
+ g_value_set_uint(value, self->occupied_tags);
+ break;
+ case ASTAL_RIVER_OUTPUT_PROP_URGENT_TAGS:
+ g_value_set_uint(value, self->urgent_tags);
+ break;
+ case ASTAL_RIVER_OUTPUT_PROP_ID:
+ g_value_set_uint(value, self->id);
+ break;
+ case ASTAL_RIVER_OUTPUT_PROP_NAME:
+ g_value_set_string(value, self->name);
+ break;
+ case ASTAL_RIVER_OUTPUT_PROP_LAYOUT_NAME:
+ g_value_set_string(value, self->layout_name);
+ break;
+ case ASTAL_RIVER_OUTPUT_PROP_FOCUSED_VIEW:
+ g_value_set_string(value, self->focused_view);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+ break;
+ }
+}
+
+static void astal_river_output_set_property(GObject* object, guint property_id, const GValue* value,
+ GParamSpec* pspec) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(object);
+
+ switch (property_id) {
+ case ASTAL_RIVER_OUTPUT_PROP_ID:
+ self->id = g_value_get_uint(value);
+ g_object_notify(G_OBJECT(self), "id");
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+ break;
+ }
+ g_signal_emit(self, astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED], 0);
+}
+
+static void noop() {}
+
+static void astal_river_handle_focused_tags(void* data, struct zriver_output_status_v1* status,
+ uint32_t tags) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(data);
+ self->focused_tags = tags;
+ g_object_notify(G_OBJECT(self), "focused-tags");
+ g_signal_emit(self, astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED], 0);
+}
+
+static void astal_river_handle_urgent_tags(void* data, struct zriver_output_status_v1* status,
+ uint32_t tags) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(data);
+ self->urgent_tags = tags;
+ g_object_notify(G_OBJECT(self), "urgent-tags");
+ g_signal_emit(self, astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED], 0);
+}
+
+static void astal_river_handle_occupied_tags(void* data, struct zriver_output_status_v1* status,
+ struct wl_array* view_tags) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(data);
+ guint tags = 0;
+ guint* view;
+ wl_array_for_each(view, view_tags) { tags |= *view; }
+
+ self->occupied_tags = tags;
+ g_object_notify(G_OBJECT(self), "occupied-tags");
+ g_signal_emit(self, astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED], 0);
+}
+
+static void astal_river_handle_layout_name(void* data, struct zriver_output_status_v1* status,
+ const char* name) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(data);
+ g_free(self->layout_name);
+ self->layout_name = g_strdup(name);
+ g_object_notify(G_OBJECT(self), "layout-name");
+ g_signal_emit(self, astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED], 0);
+}
+
+static void astal_river_handle_layout_name_clear(void* data,
+ struct zriver_output_status_v1* status) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(data);
+ g_free(self->layout_name);
+ self->layout_name = NULL;
+ g_object_notify(G_OBJECT(self), "layout-name");
+ g_signal_emit(self, astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED], 0);
+}
+
+static void astal_river_wl_output_handle_name(void* data, struct wl_output* output,
+ const char* name) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(data);
+ g_free(self->name);
+ self->name = g_strdup(name);
+ g_object_notify(G_OBJECT(self), "name");
+ g_signal_emit(self, astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED], 0);
+}
+
+static const struct zriver_output_status_v1_listener output_status_listener = {
+ .focused_tags = astal_river_handle_focused_tags,
+ .view_tags = astal_river_handle_occupied_tags,
+ .urgent_tags = astal_river_handle_urgent_tags,
+ .layout_name = astal_river_handle_layout_name,
+ .layout_name_clear = astal_river_handle_layout_name_clear,
+};
+
+static const struct wl_output_listener wl_output_listener = {
+ .name = astal_river_wl_output_handle_name,
+ .geometry = noop,
+ .mode = noop,
+ .scale = noop,
+ .description = noop,
+ .done = noop,
+};
+
+static void astal_river_output_finalize(GObject* object) {
+ AstalRiverOutput* self = ASTAL_RIVER_OUTPUT(object);
+ AstalRiverOutputPrivate* priv = astal_river_output_get_instance_private(self);
+
+ zriver_output_status_v1_destroy(priv->river_output_status);
+ wl_output_destroy(priv->wl_output);
+
+ wl_display_roundtrip(priv->wl_display);
+
+ g_free(self->layout_name);
+ g_free(self->name);
+
+ G_OBJECT_CLASS(astal_river_output_parent_class)->finalize(object);
+}
+
+static void astal_river_output_init(AstalRiverOutput* self) {}
+
+AstalRiverOutput* astal_river_output_new(guint id, struct wl_output* wl_output,
+ struct zriver_status_manager_v1* status_manager,
+ struct wl_display* wl_display) {
+ AstalRiverOutput* self = g_object_new(ASTAL_RIVER_TYPE_OUTPUT, NULL);
+ AstalRiverOutputPrivate* priv = astal_river_output_get_instance_private(self);
+
+ self->id = id;
+ priv->wl_display = wl_display;
+ priv->wl_output = wl_output;
+ priv->river_status_manager = status_manager;
+
+ priv->river_output_status =
+ zriver_status_manager_v1_get_river_output_status(priv->river_status_manager, wl_output);
+
+ zriver_output_status_v1_add_listener(priv->river_output_status, &output_status_listener, self);
+
+ wl_output_add_listener(wl_output, &wl_output_listener, self);
+
+ wl_display_roundtrip(priv->wl_display);
+
+ return self;
+}
+
+static void astal_river_output_class_init(AstalRiverOutputClass* class) {
+ GObjectClass* object_class = G_OBJECT_CLASS(class);
+ object_class->get_property = astal_river_output_get_property;
+ object_class->set_property = astal_river_output_set_property;
+ object_class->finalize = astal_river_output_finalize;
+ /**
+ * AstalRiverOutput:focused-tags:
+ *
+ * The currently focused tags
+ */
+ astal_river_output_properties[ASTAL_RIVER_OUTPUT_PROP_FOCUSED_TAGS] = g_param_spec_uint(
+ "focused-tags", "focused-tags", "currently focused tags", 0, INT_MAX, 0, G_PARAM_READABLE);
+ /**
+ * AstalRiverOutput:occupied-tags:
+ *
+ * The currently occupied tags
+ */
+ astal_river_output_properties[ASTAL_RIVER_OUTPUT_PROP_OCCUPIED_TAGS] =
+ g_param_spec_uint("occupied-tags", "occupied-tags", "currently occupied tags", 0, INT_MAX,
+ 0, G_PARAM_READABLE);
+ /**
+ * AstalRiverOutput:urgent-tags:
+ *
+ * The currently tags marked as urgent
+ */
+ astal_river_output_properties[ASTAL_RIVER_OUTPUT_PROP_URGENT_TAGS] = g_param_spec_uint(
+ "urgent-tags", "urgent-tags", "currently urgent tags", 0, INT_MAX, 0, G_PARAM_READABLE);
+ /**
+ * AstalRiverOutput:id:
+ *
+ * The id of the underlying wl_output object
+ */
+ astal_river_output_properties[ASTAL_RIVER_OUTPUT_PROP_ID] =
+ g_param_spec_uint("id", "id", "id of the output object", 0, INT_MAX, 0, G_PARAM_READABLE);
+ /**
+ * AstalRiverOutput:layout-name:
+ *
+ * The name of active layout
+ */
+ astal_river_output_properties[ASTAL_RIVER_OUTPUT_PROP_LAYOUT_NAME] = g_param_spec_string(
+ "layout-name", "layout-name", "name of the current layout", NULL, G_PARAM_READABLE);
+ /**
+ * AstalRiverOutput:name:
+ *
+ * The name of this output
+ */
+ astal_river_output_properties[ASTAL_RIVER_OUTPUT_PROP_NAME] =
+ g_param_spec_string("name", "name", "name of the output", NULL, G_PARAM_READABLE);
+ /**
+ * AstalRiverOutput:focused-view:
+ *
+ * The name of currently focused view
+ */
+ astal_river_output_properties[ASTAL_RIVER_OUTPUT_PROP_FOCUSED_VIEW] =
+ g_param_spec_string("focused-view", "focused-view",
+ "name of last focused view on this output", NULL, G_PARAM_READABLE);
+
+ g_object_class_install_properties(object_class, ASTAL_RIVER_OUTPUT_N_PROPERTIES,
+ astal_river_output_properties);
+
+ astal_river_output_signals[ASTAL_RIVER_OUTPUT_SIGNAL_CHANGED] =
+ g_signal_new("changed", G_TYPE_FROM_CLASS(class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
+ G_TYPE_NONE, 0);
+}
diff --git a/lib/river/src/river.c b/lib/river/src/river.c
new file mode 100644
index 0000000..a735898
--- /dev/null
+++ b/lib/river/src/river.c
@@ -0,0 +1,504 @@
+#include <gio/gio.h>
+#include <json-glib/json-glib.h>
+#include <wayland-client-protocol.h>
+#include <wayland-client.h>
+
+#include "river-control-unstable-v1-client.h"
+#include "river-private.h"
+#include "river-status-unstable-v1-client.h"
+#include "wayland-source.h"
+
+struct _AstalRiverRiver {
+ GObject parent_instance;
+ GList* outputs;
+ gchar* focused_output;
+ gchar* focused_view;
+ gchar* mode;
+};
+
+typedef struct {
+ GHashTable* signal_ids;
+ gboolean init;
+ struct wl_registry* wl_registry;
+ struct wl_seat* seat;
+ struct wl_display* display;
+ WLSource* wl_source;
+ struct zriver_status_manager_v1* river_status_manager;
+ struct zriver_control_v1* river_control;
+ struct zriver_seat_status_v1* river_seat_status;
+} AstalRiverRiverPrivate;
+
+static JsonSerializableIface* serializable_iface = NULL;
+static void astal_river_river_initable_iface_init(GInitableIface* iface);
+static void astal_river_river_json_serializable_iface_init(JsonSerializableIface* g_iface);
+
+G_DEFINE_TYPE_WITH_CODE(AstalRiverRiver, astal_river_river, G_TYPE_OBJECT,
+ G_ADD_PRIVATE(AstalRiverRiver) G_IMPLEMENT_INTERFACE(
+ G_TYPE_INITABLE, astal_river_river_initable_iface_init)
+ G_IMPLEMENT_INTERFACE(JSON_TYPE_SERIALIZABLE,
+ astal_river_river_json_serializable_iface_init))
+
+typedef enum {
+ ASTAL_RIVER_RIVER_PROP_FOCUSED_OUTPUT = 1,
+ ASTAL_RIVER_RIVER_PROP_FOCUSED_VIEW,
+ ASTAL_RIVER_RIVER_PROP_MODE,
+ ASTAL_RIVER_RIVER_PROP_OUTPUTS,
+ ASTAL_RIVER_RIVER_N_PROPERTIES
+} AstalRiverRiverProperties;
+
+typedef enum {
+ ASTAL_RIVER_RIVER_SIGNAL_CHANGED,
+ ASTAL_RIVER_RIVER_SIGNAL_OUTPUT_ADDED,
+ ASTAL_RIVER_RIVER_SIGNAL_OUTPUT_REMOVED,
+ ASTAL_RIVER_RIVER_N_SIGNALS
+} AstalRiverRiverSignals;
+
+static guint astal_river_river_signals[ASTAL_RIVER_RIVER_N_SIGNALS] = {
+ 0,
+};
+static GParamSpec* astal_river_river_properties[ASTAL_RIVER_RIVER_N_PROPERTIES] = {
+ NULL,
+};
+
+static void reemit_changed(AstalRiverOutput* output, AstalRiverRiver* self) {
+ g_signal_emit(self, astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_CHANGED], 0);
+}
+
+static AstalRiverOutput* find_output_by_id(AstalRiverRiver* self, uint32_t id) {
+ GList* output = self->outputs;
+ while (output != NULL) {
+ AstalRiverOutput* river_output = output->data;
+ if (astal_river_output_get_id(river_output) == id) return river_output;
+ output = output->next;
+ }
+ return NULL;
+}
+
+static AstalRiverOutput* find_output_by_output(AstalRiverRiver* self, struct wl_output* wl_output) {
+ GList* output = self->outputs;
+ while (output != NULL) {
+ AstalRiverOutput* river_output = output->data;
+ if (astal_river_output_get_wl_output(river_output) == wl_output) return river_output;
+ output = output->next;
+ }
+ return NULL;
+}
+
+static AstalRiverOutput* find_output_by_name(AstalRiverRiver* self, gchar* name) {
+ GList* output = self->outputs;
+ while (output != NULL) {
+ AstalRiverOutput* river_output = output->data;
+ if (strcmp(astal_river_output_get_name(river_output), name) == 0) return river_output;
+ output = output->next;
+ }
+ return NULL;
+}
+
+/**
+ * astal_river_river_get_outputs
+ * @self: the AstalRiverRiver object
+ *
+ * Returns: (transfer none) (element-type AstalRiver.Output): a list of all outputs
+ *
+ */
+GList* astal_river_river_get_outputs(AstalRiverRiver* self) { return self->outputs; }
+
+/**
+ * astal_river_river_get_output
+ * @self: the AstalRiverRiver object
+ * @name: the name of the output
+ *
+ * Returns: (transfer none) (nullable): the output with the given name or null
+ */
+AstalRiverOutput* astal_river_river_get_output(AstalRiverRiver* self, gchar* name) {
+ return find_output_by_name(self, name);
+}
+
+/**
+ * astal_river_river_get_focused_view
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: (transfer none) (nullable): the currently focused view
+ */
+gchar* astal_river_river_get_focused_view(AstalRiverRiver* self) { return self->focused_view; }
+
+/**
+ * astal_river_river_get_focused_output
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: (transfer none) (nullable): the name of the currently focused output
+ */
+gchar* astal_river_river_get_focused_output(AstalRiverRiver* self) { return self->focused_output; }
+
+/**
+ * astal_river_river_get_mode
+ * @self: the AstalRiverOutput object
+ *
+ * Returns: (transfer none) (nullable): the currently active mode
+ */
+gchar* astal_river_river_get_mode(AstalRiverRiver* self) { return self->mode; }
+
+static void astal_river_river_get_property(GObject* object, guint property_id, GValue* value,
+ GParamSpec* pspec) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(object);
+
+ switch (property_id) {
+ case ASTAL_RIVER_RIVER_PROP_MODE:
+ g_value_set_string(value, self->mode);
+ break;
+ case ASTAL_RIVER_RIVER_PROP_FOCUSED_VIEW:
+ g_value_set_string(value, self->focused_view);
+ break;
+ case ASTAL_RIVER_RIVER_PROP_FOCUSED_OUTPUT:
+ g_value_set_string(value, self->focused_output);
+ break;
+ case ASTAL_RIVER_RIVER_PROP_OUTPUTS:
+ g_value_set_pointer(value, self->outputs);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
+ break;
+ }
+}
+
+static JsonNode* astal_river_river_serialize_property(JsonSerializable* serializable,
+ const gchar* name, const GValue* value,
+ GParamSpec* pspec) {
+ JsonNode* retval = NULL;
+ if (strcmp(name, "outputs") == 0) {
+ JsonArray* outputs = json_array_new();
+ retval = json_node_new(JSON_NODE_ARRAY);
+ GList* output = g_value_get_pointer(value);
+ while (output != NULL) {
+ AstalRiverOutput* river_output = output->data;
+ json_array_add_element(outputs, json_gobject_serialize(G_OBJECT(river_output)));
+ output = output->next;
+ }
+ json_node_take_array(retval, outputs);
+ } else
+ retval = serializable_iface->serialize_property(serializable, name, value, pspec);
+
+ return retval;
+}
+
+static void noop() {}
+
+static void river_seat_status_handle_focused_output(void* data,
+ struct zriver_seat_status_v1* seat_status,
+ struct wl_output* focused_output) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(data);
+ g_free(self->focused_output);
+ AstalRiverOutput* output = find_output_by_output(self, focused_output);
+ if (output == NULL) return;
+ self->focused_output = g_strdup(astal_river_output_get_name(output));
+ g_object_notify(G_OBJECT(self), "focused-output");
+ g_signal_emit(self, astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_CHANGED], 0);
+}
+
+static void river_seat_status_handle_focused_view(void* data,
+ struct zriver_seat_status_v1* seat_status,
+ const char* focused_view) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(data);
+ g_free(self->focused_view);
+ self->focused_view = g_strdup(focused_view);
+ AstalRiverOutput* output = find_output_by_name(self, self->focused_output);
+ astal_river_output_set_focused_view(output, focused_view);
+ g_object_notify(G_OBJECT(self), "focused-view");
+ g_signal_emit(self, astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_CHANGED], 0);
+}
+
+static void river_seat_status_handle_mode(void* data, struct zriver_seat_status_v1* seat_status,
+ const char* mode) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(data);
+ g_free(self->mode);
+ self->mode = g_strdup(mode);
+ g_object_notify(G_OBJECT(self), "mode");
+ g_signal_emit(self, astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_CHANGED], 0);
+}
+
+static const struct zriver_seat_status_v1_listener river_seat_status_listener = {
+ .focused_output = river_seat_status_handle_focused_output,
+ .unfocused_output = noop,
+ .focused_view = river_seat_status_handle_focused_view,
+ .mode = river_seat_status_handle_mode,
+};
+
+static void global_registry_handler(void* data, struct wl_registry* registry, uint32_t id,
+ const char* interface, uint32_t version) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(data);
+ AstalRiverRiverPrivate* priv = astal_river_river_get_instance_private(self);
+ if (strcmp(interface, wl_output_interface.name) == 0) {
+ if (priv->river_status_manager == NULL) return;
+ struct wl_output* wl_out = wl_registry_bind(registry, id, &wl_output_interface, 4);
+ AstalRiverOutput* output =
+ astal_river_output_new(id, wl_out, priv->river_status_manager, priv->display);
+
+ self->outputs = g_list_append(self->outputs, output);
+ g_object_notify(G_OBJECT(self), "outputs");
+ g_signal_emit(G_OBJECT(self),
+ astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_OUTPUT_ADDED], 0,
+ astal_river_output_get_name(output));
+ g_signal_emit(self, astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_CHANGED], 0);
+
+ guint signal_id = g_signal_connect(output, "changed", G_CALLBACK(reemit_changed), self);
+ g_hash_table_insert(priv->signal_ids, GUINT_TO_POINTER(id), GUINT_TO_POINTER(signal_id));
+ } else if (strcmp(interface, wl_seat_interface.name) == 0) {
+ priv->seat = wl_registry_bind(registry, id, &wl_seat_interface, 4);
+ } else if (strcmp(interface, zriver_status_manager_v1_interface.name) == 0) {
+ priv->river_status_manager =
+ wl_registry_bind(registry, id, &zriver_status_manager_v1_interface, 4);
+ } else if (strcmp(interface, zriver_control_v1_interface.name) == 0) {
+ priv->river_control = wl_registry_bind(registry, id, &zriver_control_v1_interface, 1);
+ }
+}
+
+static void astal_river_river_callback_success(void* data, struct zriver_command_callback_v1* cb,
+ const char* msg) {
+ AstalRiverCommandCallback callback = (AstalRiverCommandCallback)(data);
+ callback(TRUE, msg);
+}
+
+static void astal_river_river_callback_failure(void* data, struct zriver_command_callback_v1* cb,
+ const char* msg) {
+ AstalRiverCommandCallback callback = (AstalRiverCommandCallback)(data);
+ callback(FALSE, msg);
+}
+
+const struct zriver_command_callback_v1_listener cb_listener = {
+ .success = astal_river_river_callback_success, .failure = astal_river_river_callback_failure};
+
+/**
+ * astal_river_river_run_command_async:
+ * @self: the AstalRiverRiver object
+ * @length: the length of the cmd array
+ * @cmd: (array length=length): the command to execute
+ * @callback: (scope async) (nullable): the callback to invoke.
+ *
+ * Calls the given callback with the provided parameters.
+ */
+void astal_river_river_run_command_async(AstalRiverRiver* self, gint length, const gchar** cmd,
+ AstalRiverCommandCallback callback) {
+ AstalRiverRiverPrivate* priv = astal_river_river_get_instance_private(self);
+
+ for (gint i = 0; i < length; ++i) {
+ zriver_control_v1_add_argument(priv->river_control, cmd[i]);
+ }
+
+ struct zriver_command_callback_v1* cb =
+ zriver_control_v1_run_command(priv->river_control, priv->seat);
+ if (callback != NULL) zriver_command_callback_v1_add_listener(cb, &cb_listener, callback);
+}
+
+static void global_registry_remover(void* data, struct wl_registry* registry, uint32_t id) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(data);
+ AstalRiverRiverPrivate* priv = astal_river_river_get_instance_private(self);
+ AstalRiverOutput* output = find_output_by_id(self, id);
+ if (output != NULL) {
+ guint signal_id =
+ GPOINTER_TO_UINT(g_hash_table_lookup(priv->signal_ids, GUINT_TO_POINTER(id)));
+ g_hash_table_remove(priv->signal_ids, GUINT_TO_POINTER(id));
+ g_signal_handler_disconnect(output, signal_id);
+ g_signal_emit(G_OBJECT(self),
+ astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_OUTPUT_ADDED], 0,
+ astal_river_output_get_name(output));
+ self->outputs = g_list_remove(self->outputs, output);
+ g_object_notify(G_OBJECT(self), "outputs");
+ g_object_unref(output);
+ return;
+ }
+ g_signal_emit(self, astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_CHANGED], 0);
+}
+
+static const struct wl_registry_listener registry_listener = {global_registry_handler,
+ global_registry_remover};
+
+static void astal_river_river_json_serializable_iface_init(JsonSerializableIface* iface) {
+ iface->serialize_property = astal_river_river_serialize_property;
+ serializable_iface = g_type_default_interface_peek(JSON_TYPE_SERIALIZABLE);
+}
+
+static gboolean astal_river_river_initable_init(GInitable* initable, GCancellable* cancellable,
+ GError** error) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(initable);
+ AstalRiverRiverPrivate* priv = astal_river_river_get_instance_private(self);
+
+ if (priv->init) return TRUE;
+
+ priv->wl_source = wl_source_new(NULL, NULL);
+
+ if (priv->wl_source == NULL) {
+ g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "Can not connect to wayland display");
+ return FALSE;
+ }
+
+ priv->display = wl_source_get_display(priv->wl_source);
+
+ priv->wl_registry = wl_display_get_registry(priv->display);
+ wl_registry_add_listener(priv->wl_registry, &registry_listener, self);
+
+ wl_display_roundtrip(priv->display);
+
+ if (priv->river_status_manager == NULL) {
+ g_set_error_literal(error, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "Can not connect river status protocol");
+ return FALSE;
+ }
+
+ priv->river_seat_status =
+ zriver_status_manager_v1_get_river_seat_status(priv->river_status_manager, priv->seat);
+ zriver_seat_status_v1_add_listener(priv->river_seat_status, &river_seat_status_listener, self);
+
+ wl_display_roundtrip(priv->display);
+
+ priv->init = TRUE;
+ return TRUE;
+}
+
+static void astal_river_river_constructed(GObject* object) {
+ astal_river_river_initable_init(G_INITABLE(object), NULL, NULL);
+}
+
+static void astal_river_river_initable_iface_init(GInitableIface* iface) {
+ iface->init = astal_river_river_initable_init;
+}
+
+static void astal_river_river_init(AstalRiverRiver* self) {
+ AstalRiverRiverPrivate* priv = astal_river_river_get_instance_private(self);
+ self->outputs = NULL;
+ priv->init = FALSE;
+ priv->seat = NULL;
+ priv->display = NULL;
+ priv->river_status_manager = NULL;
+ priv->signal_ids = g_hash_table_new(g_direct_hash, g_direct_equal);
+}
+
+/**
+ * astal_river_river_new
+ *
+ * creates a new River object. It is recommended to use the get_default() method
+ * instead of this method.
+ *
+ * Returns: (nullable): a newly created connection to river
+ */
+AstalRiverRiver* astal_river_river_new() {
+ return g_initable_new(ASTAL_RIVER_TYPE_RIVER, NULL, NULL, NULL);
+}
+
+static void disconnect_signal(gpointer key, gpointer value, gpointer user_data) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(user_data);
+
+ AstalRiverOutput* output = find_output_by_id(self, GPOINTER_TO_UINT(key));
+ g_signal_handler_disconnect(output, GPOINTER_TO_UINT(value));
+}
+
+/**
+ * astal_river_river_get_default
+ *
+ * Returns: (nullable) (transfer none): gets the default River object.
+ */
+AstalRiverRiver* astal_river_river_get_default() {
+ static AstalRiverRiver* self = NULL;
+
+ if (self == NULL) self = astal_river_river_new();
+
+ return self;
+}
+
+/**
+ * astal_river_get_default
+ *
+ * Returns: (nullable) (transfer none): gets the default River object.
+ */
+AstalRiverRiver* astal_river_get_default() { return astal_river_river_get_default(); }
+
+static void astal_river_river_finalize(GObject* object) {
+ AstalRiverRiver* self = ASTAL_RIVER_RIVER(object);
+ AstalRiverRiverPrivate* priv = astal_river_river_get_instance_private(self);
+
+ g_hash_table_foreach(priv->signal_ids, disconnect_signal, self);
+ g_hash_table_destroy(priv->signal_ids);
+
+ if (priv->display != NULL) wl_display_roundtrip(priv->display);
+
+ g_clear_list(&self->outputs, g_object_unref);
+ self->outputs = NULL;
+
+ if (priv->wl_registry != NULL) wl_registry_destroy(priv->wl_registry);
+ if (priv->river_status_manager != NULL)
+ zriver_status_manager_v1_destroy(priv->river_status_manager);
+ if (priv->river_seat_status != NULL) zriver_seat_status_v1_destroy(priv->river_seat_status);
+ if (priv->seat != NULL) wl_seat_destroy(priv->seat);
+ if (priv->display != NULL) wl_display_flush(priv->display);
+
+ if (priv->wl_source != NULL) wl_source_free(priv->wl_source);
+
+ g_free(self->focused_view);
+ g_free(self->focused_output);
+ g_free(self->mode);
+
+ G_OBJECT_CLASS(astal_river_river_parent_class)->finalize(object);
+}
+
+static void astal_river_river_class_init(AstalRiverRiverClass* class) {
+ GObjectClass* object_class = G_OBJECT_CLASS(class);
+ object_class->get_property = astal_river_river_get_property;
+ object_class->finalize = astal_river_river_finalize;
+ object_class->constructed = astal_river_river_constructed;
+
+ /**
+ * AstalRiverRiver:mode:
+ *
+ * The currently active mode
+ */
+ astal_river_river_properties[ASTAL_RIVER_RIVER_PROP_MODE] =
+ g_param_spec_string("mode", "mode", "currently active mode", NULL, G_PARAM_READABLE);
+ /**
+ * AstalRiverRiver:focused-view:
+ *
+ * The name of the currently focused view
+ */
+ astal_river_river_properties[ASTAL_RIVER_RIVER_PROP_FOCUSED_VIEW] = g_param_spec_string(
+ "focused-view", "focused-view", "currently focused view", NULL, G_PARAM_READABLE);
+ /**
+ * AstalRiverRiver:focused-output:
+ *
+ * The name of the currently focused output
+ */
+ astal_river_river_properties[ASTAL_RIVER_RIVER_PROP_FOCUSED_OUTPUT] = g_param_spec_string(
+ "focused-output", "focused-output", "currently focused-output", NULL, G_PARAM_READABLE);
+ /**
+ * AstalRiverRiver:outputs: (type GList(AstalRiverOutput))
+ *
+ * A list of AstalRiverOutput objects
+ */
+ astal_river_river_properties[ASTAL_RIVER_RIVER_PROP_OUTPUTS] =
+ g_param_spec_pointer("outputs", "outputs", "a list of all outputs", G_PARAM_READABLE);
+
+ g_object_class_install_properties(object_class, ASTAL_RIVER_RIVER_N_PROPERTIES,
+ astal_river_river_properties);
+ /**
+ * AstalRiverRiver::output-added:
+ * @river: the object which received the signal.
+ * @output: the name of the added output
+ *
+ * This signal is emitted when a new output was connected
+ */
+ astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_OUTPUT_ADDED] =
+ g_signal_new("output-added", G_TYPE_FROM_CLASS(class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL,
+ NULL, G_TYPE_NONE, 1, G_TYPE_STRING);
+ /**
+ * AstalRiverRiver::output-removed:
+ * @river: the object which received the signal.
+ * @output: the name of the removed output
+ *
+ * This signal is emitted when a new output was disconnected
+ */
+ astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_OUTPUT_REMOVED] =
+ g_signal_new("output-removed", G_TYPE_FROM_CLASS(class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL,
+ NULL, G_TYPE_NONE, 1, G_TYPE_STRING);
+
+ astal_river_river_signals[ASTAL_RIVER_RIVER_SIGNAL_CHANGED] =
+ g_signal_new("changed", G_TYPE_FROM_CLASS(class), G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL,
+ G_TYPE_NONE, 0);
+}
diff --git a/lib/river/src/wayland-source.c b/lib/river/src/wayland-source.c
new file mode 100644
index 0000000..875c32c
--- /dev/null
+++ b/lib/river/src/wayland-source.c
@@ -0,0 +1,104 @@
+
+#include "wayland-source.h"
+
+#include <errno.h>
+#include <glib.h>
+#include <wayland-client.h>
+
+struct _WLSource {
+ GSource source;
+ struct wl_display *display;
+ gpointer fd;
+ int error;
+};
+
+static gboolean wl_source_prepare(GSource *source, gint *timeout) {
+ WLSource *self = (WLSource *)source;
+
+ *timeout = 0;
+ if (wl_display_prepare_read(self->display) != 0)
+ return TRUE;
+ else if (wl_display_flush(self->display) < 0) {
+ self->error = errno;
+ return TRUE;
+ }
+ *timeout = -1;
+ return FALSE;
+}
+
+static gboolean wl_source_check(GSource *source) {
+ WLSource *self = (WLSource *)source;
+
+ if (self->error > 0) return TRUE;
+
+ GIOCondition revents;
+ revents = g_source_query_unix_fd(source, self->fd);
+
+ if (revents & G_IO_IN) {
+ if (wl_display_read_events(self->display) < 0) self->error = errno;
+ } else
+ wl_display_cancel_read(self->display);
+
+ return revents > 0;
+}
+
+static gboolean wl_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
+ WLSource *self = (WLSource *)source;
+ GIOCondition revents;
+
+ revents = g_source_query_unix_fd(source, self->fd);
+ if ((self->error > 0) || (revents & (G_IO_ERR | G_IO_HUP))) {
+ errno = self->error;
+ self->error = 0;
+ if (callback != NULL) return callback(user_data);
+ return G_SOURCE_REMOVE;
+ }
+
+ if (wl_display_dispatch_pending(self->display) < 0) {
+ if (callback != NULL) return callback(user_data);
+ return G_SOURCE_REMOVE;
+ }
+
+ return G_SOURCE_CONTINUE;
+}
+
+static void wl_source_finalize(GSource *source) {
+ WLSource *self = (WLSource *)source;
+ wl_display_disconnect(self->display);
+}
+
+static GSourceFuncs wl_source_funcs = {
+ .prepare = wl_source_prepare,
+ .check = wl_source_check,
+ .dispatch = wl_source_dispatch,
+ .finalize = wl_source_finalize,
+};
+
+WLSource *wl_source_new() {
+ struct wl_display *display;
+ WLSource *self;
+ GSource *source;
+
+ display = wl_display_connect(NULL);
+ if (display == NULL) return NULL;
+
+ source = g_source_new(&wl_source_funcs, sizeof(WLSource));
+ self = (WLSource *)source;
+ self->display = display;
+
+ self->fd = g_source_add_unix_fd(source, wl_display_get_fd(self->display),
+ G_IO_IN | G_IO_ERR | G_IO_HUP);
+
+ g_source_attach(source, NULL);
+
+ return self;
+}
+
+void wl_source_free(WLSource *self) {
+ GSource *source = (GSource *)self;
+ g_return_if_fail(source != NULL);
+ g_source_destroy(source);
+ g_source_unref(source);
+}
+
+struct wl_display *wl_source_get_display(WLSource *self) { return self->display; }
diff --git a/lib/river/version b/lib/river/version
new file mode 100644
index 0000000..6e8bf73
--- /dev/null
+++ b/lib/river/version
@@ -0,0 +1 @@
+0.1.0