implement ext-idle-notify-v1
This commit is contained in:
278
src/riverguile.c
278
src/riverguile.c
@@ -19,168 +19,80 @@
|
||||
#endif
|
||||
|
||||
#include "river-layout-v3.h"
|
||||
#include "ext-idle-notify-v1.h"
|
||||
|
||||
#include "types.h"
|
||||
#include "call-layout-demand-handler.h"
|
||||
#include "call-user-command-handler.h"
|
||||
#include "load-script.h"
|
||||
#include "riverguile.h"
|
||||
#include "output.h"
|
||||
#include "seat.h"
|
||||
|
||||
bool loop = true;
|
||||
int ret = EXIT_SUCCESS;
|
||||
struct Context context = {
|
||||
/* Handlers are initially NULL instead of SCM_EOL because I am not sure whether
|
||||
* scm_null_p() is safe to use outside of a guile context and if yes, whether
|
||||
* that is intended or will disappear in the future. Special care must be taken
|
||||
* to never call any scm_* function on these while they are NULL.
|
||||
*/
|
||||
.layout_demand_handler = NULL,
|
||||
.user_command_handler = NULL,
|
||||
|
||||
jmp_buf skip_main_loop;
|
||||
|
||||
struct wl_display *wl_display = NULL;
|
||||
struct wl_registry *wl_registry = NULL;
|
||||
struct wl_callback *sync_callback = NULL;
|
||||
struct river_layout_manager_v3 *layout_manager = NULL;
|
||||
|
||||
struct wl_list outputs;
|
||||
|
||||
/* Handlers are initially NULL instead of SCM_EOL because I am not sure whether
|
||||
* scm_null_p() is safe to use outside of a guile context and if yes, whether
|
||||
* that is intended or will disappear in the future. Special care must be taken
|
||||
* to never call any scm_* function on these while they are NULL.
|
||||
*/
|
||||
SCM layout_demand_handler = NULL;
|
||||
SCM user_command_handler = NULL;
|
||||
|
||||
static void layout_handle_layout_demand (void *data, struct river_layout_v3 *river_layout_v3,
|
||||
uint32_t view_count, uint32_t width, uint32_t height, uint32_t tags, uint32_t serial)
|
||||
{
|
||||
struct Output *output = (struct Output *)data;
|
||||
|
||||
struct Call_layout_demand_handler_parameters params = {
|
||||
.view_count = view_count,
|
||||
.width = width,
|
||||
.height = height,
|
||||
.serial = serial,
|
||||
.tags = tags,
|
||||
.output = output,
|
||||
};
|
||||
|
||||
void *res = scm_with_guile(call_layout_demand_handler, (void *)¶ms);
|
||||
if ( res != NULL )
|
||||
{
|
||||
fputs(res, stderr);
|
||||
fputs("INFO: This error is not fatal.\n", stderr);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO allow setting layout name from user installed layout demand handler
|
||||
river_layout_v3_commit(output->layout, "(🐦)", serial);
|
||||
}
|
||||
|
||||
static void layout_handle_user_command (void *data,
|
||||
struct river_layout_v3 *river_layout, const char *command)
|
||||
{
|
||||
if ( user_command_handler == NULL )
|
||||
return;
|
||||
|
||||
struct Output *output = (struct Output *)data;
|
||||
|
||||
struct Call_user_command_parameters params = {
|
||||
.cmd = command,
|
||||
.output = output,
|
||||
};
|
||||
|
||||
void *res = scm_with_guile(call_user_command_handler, (void *)¶ms);
|
||||
if ( res != NULL )
|
||||
{
|
||||
fputs(res, stderr);
|
||||
fputs("INFO: This error is not fatal.\n", stderr);
|
||||
}
|
||||
}
|
||||
|
||||
static void layout_handle_user_command_tags (void *data,
|
||||
struct river_layout_v3 *river_layout, uint32_t tags)
|
||||
{
|
||||
struct Output *output = (struct Output *)data;
|
||||
output->user_command_tags = tags;
|
||||
}
|
||||
|
||||
static void layout_handle_namespace_in_use (void *data, struct river_layout_v3 *river_layout_v3)
|
||||
{
|
||||
fputs("ERROR: Namespace already in use.\n", stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
loop = false;
|
||||
}
|
||||
|
||||
static const struct river_layout_v3_listener layout_listener = {
|
||||
.namespace_in_use = layout_handle_namespace_in_use,
|
||||
.layout_demand = layout_handle_layout_demand,
|
||||
.user_command = layout_handle_user_command,
|
||||
.user_command_tags = layout_handle_user_command_tags,
|
||||
.loop = true,
|
||||
.ret = EXIT_SUCCESS,
|
||||
};
|
||||
|
||||
static void output_configure (struct Output *output)
|
||||
{
|
||||
if ( layout_manager == NULL )
|
||||
return;
|
||||
if (output->configured)
|
||||
return;
|
||||
|
||||
output->layout = river_layout_manager_v3_get_layout(
|
||||
layout_manager, output->wl_output, "riverguile"
|
||||
);
|
||||
river_layout_v3_add_listener(output->layout, &layout_listener, output);
|
||||
output->configured = true;
|
||||
}
|
||||
|
||||
static struct Output *output_create (struct wl_output *wl_output)
|
||||
{
|
||||
struct Output *output = calloc(1, sizeof(struct Output));
|
||||
if ( output == NULL )
|
||||
{
|
||||
fprintf(stderr, "ERROR: calloc: %s\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
output->wl_output = wl_output;
|
||||
output_configure(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
static void output_destroy (struct Output *output)
|
||||
{
|
||||
if ( output->layout != NULL )
|
||||
river_layout_v3_destroy(output->layout);
|
||||
assert(output->wl_output != NULL);
|
||||
wl_output_destroy(output->wl_output);
|
||||
free(output);
|
||||
}
|
||||
jmp_buf skip_main_loop;
|
||||
|
||||
static void registry_handle_global (void *data, struct wl_registry *registry,
|
||||
uint32_t name, const char *interface, uint32_t version)
|
||||
{
|
||||
if ( strcmp(interface, river_layout_manager_v3_interface.name) == 0 )
|
||||
{
|
||||
layout_manager = wl_registry_bind(
|
||||
context.layout_manager = wl_registry_bind(
|
||||
registry, name, &river_layout_manager_v3_interface, 2
|
||||
);
|
||||
}
|
||||
else if ( strcmp(interface, ext_idle_notifier_v1_interface.name) == 0 )
|
||||
{
|
||||
context.idle_notifier = wl_registry_bind(
|
||||
registry, name, &ext_idle_notifier_v1_interface, 1
|
||||
);
|
||||
}
|
||||
else if ( strcmp(interface, wl_output_interface.name) == 0 )
|
||||
{
|
||||
struct wl_output *wl_output = wl_registry_bind(
|
||||
registry, name, &wl_output_interface, 1
|
||||
);
|
||||
struct Output *output = output_create(wl_output);
|
||||
struct Output *output = output_create(wl_output, name);
|
||||
if ( output == NULL )
|
||||
{
|
||||
wl_output_destroy(wl_output);
|
||||
ret = EXIT_FAILURE;
|
||||
loop = false;
|
||||
context.ret = EXIT_FAILURE;
|
||||
context.loop = false;
|
||||
return;
|
||||
}
|
||||
wl_list_insert(&outputs, &output->link);
|
||||
wl_list_insert(&context.outputs, &output->link);
|
||||
}
|
||||
else if ( strcmp(interface, wl_seat_interface.name) == 0 )
|
||||
{
|
||||
struct wl_seat *wl_seat = wl_registry_bind(
|
||||
registry, name, &wl_seat_interface, 1
|
||||
);
|
||||
struct Seat *seat = seat_create(wl_seat, name);
|
||||
if ( seat == NULL )
|
||||
{
|
||||
wl_seat_destroy(wl_seat);
|
||||
context.ret = EXIT_FAILURE;
|
||||
context.loop = false;
|
||||
return;
|
||||
}
|
||||
wl_list_insert(&context.seats, &seat->link);
|
||||
}
|
||||
}
|
||||
|
||||
static void registry_handle_global_remove (void *data, struct wl_registry *registry,
|
||||
uint32_t name)
|
||||
{
|
||||
struct Output *output, *tmp;
|
||||
wl_list_for_each_safe(output, tmp, &outputs, link)
|
||||
struct Output *output, *tmp_o;
|
||||
wl_list_for_each_safe(output, tmp_o, &context.outputs, link)
|
||||
{
|
||||
if ( output->name != name )
|
||||
continue;
|
||||
@@ -188,6 +100,16 @@ static void registry_handle_global_remove (void *data, struct wl_registry *regis
|
||||
output_destroy(output);
|
||||
return;
|
||||
}
|
||||
|
||||
struct Seat *seat, *tmp_s;
|
||||
wl_list_for_each_safe(seat, tmp_s, &context.seats, link)
|
||||
{
|
||||
if ( seat->name != name )
|
||||
continue;
|
||||
wl_list_remove(&seat->link);
|
||||
seat_destroy(seat);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct wl_registry_listener registry_listener = {
|
||||
@@ -198,22 +120,41 @@ static const struct wl_registry_listener registry_listener = {
|
||||
static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint32_t other_data)
|
||||
{
|
||||
wl_callback_destroy(wl_callback);
|
||||
sync_callback = NULL;
|
||||
context.sync_callback = NULL;
|
||||
|
||||
if ( layout_manager == NULL )
|
||||
if ( context.layout_manager == NULL )
|
||||
{
|
||||
fputs("ERROR: Wayland server does not support river-layout-v3.\n", stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
loop = false;
|
||||
context.ret = EXIT_FAILURE;
|
||||
context.loop = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( context.idle_notifier == NULL )
|
||||
{
|
||||
if ( wl_list_length(&context.unconfigured_idles) > 0 )
|
||||
{
|
||||
fputs("ERROR: Wayland server does not support ext-idle-notify-v1.\n", stderr);
|
||||
fputs("INFO: This error is not fatal.\n", stderr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
struct Seat *seat;
|
||||
wl_list_for_each(seat, &context.seats, link)
|
||||
break;
|
||||
|
||||
struct Idle *idle, *tmp_i;
|
||||
wl_list_for_each_safe(idle, tmp_i, &context.unconfigured_idles, link)
|
||||
idle_configure(idle, seat);
|
||||
}
|
||||
|
||||
/* If outputs were registered before the river_layout_manager is
|
||||
* available, they won't have a river_layout, so we need to create
|
||||
* those here.
|
||||
*/
|
||||
struct Output *output;
|
||||
wl_list_for_each(output, &outputs, link)
|
||||
wl_list_for_each(output, &context.outputs, link)
|
||||
output_configure(output);
|
||||
}
|
||||
|
||||
@@ -224,7 +165,7 @@ static const struct wl_callback_listener sync_callback_listener = {
|
||||
static void handle_interrupt (int signum)
|
||||
{
|
||||
fputs("Killed 💀\n", stderr);
|
||||
loop = false;
|
||||
context.loop = false;
|
||||
longjmp(skip_main_loop, 1);
|
||||
}
|
||||
|
||||
@@ -353,13 +294,16 @@ int main(int argc, char *argv[])
|
||||
signal(SIGSEGV, handle_error);
|
||||
signal(SIGFPE, handle_error);
|
||||
signal(SIGINT, handle_interrupt);
|
||||
wl_list_init(&outputs);
|
||||
|
||||
wl_list_init(&context.outputs);
|
||||
wl_list_init(&context.seats);
|
||||
wl_list_init(&context.unconfigured_idles);
|
||||
|
||||
// TODO use argv[1] if present
|
||||
char *path = get_script_path();
|
||||
if ( path == NULL )
|
||||
{
|
||||
ret = EXIT_FAILURE;
|
||||
context.ret = EXIT_FAILURE;
|
||||
goto early_exit;
|
||||
}
|
||||
|
||||
@@ -367,8 +311,8 @@ int main(int argc, char *argv[])
|
||||
if ( res != NULL )
|
||||
{
|
||||
fputs((char *)res, stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
loop = false;
|
||||
context.ret = EXIT_FAILURE;
|
||||
context.loop = false;
|
||||
goto early_exit;
|
||||
}
|
||||
|
||||
@@ -381,44 +325,62 @@ int main(int argc, char *argv[])
|
||||
if ( display_name == NULL )
|
||||
{
|
||||
fputs("ERROR: WAYLAND_DISPLAY is not set.\n", stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
context.ret = EXIT_FAILURE;
|
||||
goto early_exit;
|
||||
}
|
||||
|
||||
wl_display = wl_display_connect(display_name);
|
||||
if ( wl_display == NULL )
|
||||
context.wl_display = wl_display_connect(display_name);
|
||||
if ( context.wl_display == NULL )
|
||||
{
|
||||
fputs("ERROR: Can not connect to wayland display.\n", stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
context.ret = EXIT_FAILURE;
|
||||
goto early_exit;
|
||||
}
|
||||
|
||||
wl_registry = wl_display_get_registry(wl_display);
|
||||
wl_registry_add_listener(wl_registry, ®istry_listener, NULL);
|
||||
context.wl_registry = wl_display_get_registry(context.wl_display);
|
||||
wl_registry_add_listener(context.wl_registry, ®istry_listener, NULL);
|
||||
|
||||
sync_callback = wl_display_sync(wl_display);
|
||||
wl_callback_add_listener(sync_callback, &sync_callback_listener, NULL);
|
||||
context.sync_callback = wl_display_sync(context.wl_display);
|
||||
wl_callback_add_listener(context.sync_callback, &sync_callback_listener, NULL);
|
||||
|
||||
if ( setjmp(skip_main_loop) == 0 )
|
||||
while ( loop && wl_display_dispatch(wl_display) > 0 );
|
||||
while ( context.loop && wl_display_dispatch(context.wl_display) > 0 );
|
||||
|
||||
struct Output *output, *tmp;
|
||||
wl_list_for_each_safe(output, tmp, &outputs, link)
|
||||
struct Idle *idle, *tmp_i;
|
||||
wl_list_for_each_safe(idle, tmp_i, &context.unconfigured_idles, link)
|
||||
{
|
||||
wl_list_remove(&idle->link);
|
||||
idle_destroy(idle);
|
||||
}
|
||||
|
||||
struct Output *output, *tmp_o;
|
||||
wl_list_for_each_safe(output, tmp_o, &context.outputs, link)
|
||||
{
|
||||
wl_list_remove(&output->link);
|
||||
output_destroy(output);
|
||||
}
|
||||
|
||||
if ( sync_callback != NULL )
|
||||
wl_callback_destroy(sync_callback);
|
||||
if ( wl_registry != NULL )
|
||||
wl_registry_destroy(wl_registry);
|
||||
wl_display_disconnect(wl_display);
|
||||
struct Seat *seat, *tmp_s;
|
||||
wl_list_for_each_safe(seat, tmp_s, &context.seats, link)
|
||||
{
|
||||
wl_list_remove(&seat->link);
|
||||
seat_destroy(seat);
|
||||
}
|
||||
|
||||
if ( context.idle_notifier != NULL )
|
||||
ext_idle_notifier_v1_destroy(context.idle_notifier);
|
||||
if ( context.layout_manager != NULL )
|
||||
river_layout_manager_v3_destroy(context.layout_manager);
|
||||
if ( context.sync_callback != NULL )
|
||||
wl_callback_destroy(context.sync_callback);
|
||||
if ( context.wl_registry != NULL )
|
||||
wl_registry_destroy(context.wl_registry);
|
||||
wl_display_disconnect(context.wl_display);
|
||||
|
||||
early_exit:
|
||||
if ( path != NULL )
|
||||
free(path);
|
||||
|
||||
return ret;
|
||||
return context.ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user