new API
and code cleanup
This commit is contained in:
83
src/call-layout-demand-handler.c
Normal file
83
src/call-layout-demand-handler.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <assert.h>
|
||||
#include <libguile.h>
|
||||
|
||||
#include "river-layout-v3.h"
|
||||
|
||||
#include "call-layout-demand-handler.h"
|
||||
|
||||
extern SCM layout_demand_handler;
|
||||
|
||||
static void *call_layout_demand_handler_inner (void *data)
|
||||
{
|
||||
struct Call_layout_demand_handler_parameters *params = (struct Call_layout_demand_handler_parameters *)data;
|
||||
return scm_call_5(
|
||||
layout_demand_handler,
|
||||
scm_from_uint32(params->view_count),
|
||||
scm_from_uint32(params->width),
|
||||
scm_from_uint32(params->height),
|
||||
scm_from_uint32(params->tags),
|
||||
scm_from_uint32(params->output->name)
|
||||
);
|
||||
}
|
||||
|
||||
void *call_layout_demand_handler (void *data)
|
||||
{
|
||||
struct Call_layout_demand_handler_parameters *params = (struct Call_layout_demand_handler_parameters *)data;
|
||||
|
||||
assert(layout_demand_handler != NULL);
|
||||
assert(scm_is_true(scm_procedure_p(layout_demand_handler)) == 1);
|
||||
|
||||
/* Continuation barrier causes stack unwind on exceptions (i.e. errors
|
||||
* in the user defined layout demand handler) to stop here. Otherwise
|
||||
* the entire stack created by scm_with_guile() would be unwound. This
|
||||
* makes responding to exceptions nicer.
|
||||
*/
|
||||
SCM call_result = scm_c_with_continuation_barrier(
|
||||
call_layout_demand_handler_inner, data
|
||||
);
|
||||
|
||||
if ( call_result == NULL )
|
||||
return (void *)"ERROR: An exception occured while calling the layout demand handler.\n";
|
||||
|
||||
/* Check integrity of data before firing off any Wayland requests. */
|
||||
if ( scm_is_false(scm_list_p(call_result)) == 1 )
|
||||
return (void *)"ERROR: layout-demand-handler did not return a list.\n";
|
||||
if ( params->view_count != scm_to_uint32(scm_length(call_result)) )
|
||||
return (void *)"ERROR: Length of list returned by layout-demand-handler does not match view count.\n";
|
||||
for (uint32_t i = 0; i < params->view_count; i++)
|
||||
{
|
||||
SCM elem = scm_list_ref(call_result, scm_from_uint32(i));
|
||||
if ( scm_is_false(scm_list_p(elem)) == 1 )
|
||||
return (void *)"ERROR: View dimensions list is not a list.\n";
|
||||
if ( scm_to_uint32(scm_length(elem)) != 4 )
|
||||
return (void *)"ERROR: View dimensions list length does not equal four (x, y, width, height).\n";
|
||||
for (uint8_t j = 0; j < 4; j++)
|
||||
{
|
||||
SCM list_elem = scm_list_ref(elem, scm_from_int(j));
|
||||
if ( scm_is_false(scm_number_p(list_elem)) == 1 )
|
||||
return (void *)"ERROR: Encountered non-numerical view dimensions.\n";
|
||||
}
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < params->view_count; i++)
|
||||
{
|
||||
SCM elem = scm_list_ref(call_result, scm_from_uint32(i));
|
||||
uint32_t x = scm_to_uint32(scm_inexact_to_exact(
|
||||
scm_round_number(scm_list_ref(elem, scm_from_int(0)))));
|
||||
uint32_t y = scm_to_uint32(scm_inexact_to_exact(
|
||||
scm_round_number(scm_list_ref(elem, scm_from_int(1)))));
|
||||
uint32_t w = scm_to_uint32(scm_inexact_to_exact(
|
||||
scm_round_number(scm_list_ref(elem, scm_from_int(2)))));
|
||||
uint32_t h = scm_to_uint32(scm_inexact_to_exact(
|
||||
scm_round_number(scm_list_ref(elem, scm_from_int(3)))));
|
||||
|
||||
river_layout_v3_push_view_dimensions(
|
||||
params->output->layout,
|
||||
x, y, w, h,
|
||||
params->serial
|
||||
);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
16
src/call-layout-demand-handler.h
Normal file
16
src/call-layout-demand-handler.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef RIVERGUILE_CALL_LAYOUT_DEMAND_HANDLER_H
|
||||
#define RIVERGUILE_CALL_LAYOUT_DEMAND_HANDLER_H
|
||||
|
||||
#include <libguile.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
struct Call_layout_demand_handler_parameters
|
||||
{
|
||||
uint32_t view_count, width, height, serial, tags;
|
||||
struct Output *output;
|
||||
};
|
||||
|
||||
void *call_layout_demand_handler (void *data);
|
||||
|
||||
#endif
|
||||
38
src/call-user-command-handler.c
Normal file
38
src/call-user-command-handler.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <assert.h>
|
||||
#include <libguile.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "call-user-command-handler.h"
|
||||
|
||||
extern SCM user_command_handler;
|
||||
|
||||
static void *call_user_command_handler_inner (void *data)
|
||||
{
|
||||
struct Call_user_command_parameters *params = (struct Call_user_command_parameters *)data;
|
||||
return scm_call_3(
|
||||
user_command_handler,
|
||||
scm_from_utf8_string(params->cmd),
|
||||
scm_from_uint32(params->output->user_command_tags),
|
||||
scm_from_uint32(params->output->name)
|
||||
);
|
||||
}
|
||||
|
||||
void *call_user_command_handler (void *data)
|
||||
{
|
||||
assert(user_command_handler != NULL);
|
||||
assert(scm_is_true(scm_procedure_p(user_command_handler)) == 1);
|
||||
|
||||
/* Continuation barrier causes stack unwind on exceptions (i.e. errors
|
||||
* in the user defined user-command handler) to stop here. Otherwise
|
||||
* the entire stack created by scm_with_guile() would be unwound. This
|
||||
* makes responding to exceptions nicer.
|
||||
*/
|
||||
SCM call_result = scm_c_with_continuation_barrier(
|
||||
call_user_command_handler_inner, data
|
||||
);
|
||||
|
||||
if ( call_result == NULL )
|
||||
return (void *)"ERROR: An exception occured while calling the user-command handler.\n";
|
||||
|
||||
return NULL;
|
||||
}
|
||||
14
src/call-user-command-handler.h
Normal file
14
src/call-user-command-handler.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef RIVERGUILE_CALL_USER_COMMAND_HANDLER_H
|
||||
#define RIVERGUILE_CALL_USER_COMMAND_HANDLER_H
|
||||
|
||||
#include "types.h"
|
||||
|
||||
struct Call_user_command_parameters
|
||||
{
|
||||
struct Output *output;
|
||||
const char *cmd;
|
||||
};
|
||||
|
||||
void *call_user_command_handler (void *data);
|
||||
|
||||
#endif
|
||||
104
src/load-script.c
Normal file
104
src/load-script.c
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <libguile.h>
|
||||
|
||||
/**
|
||||
* ISO C forbids casting a function pointer to a void pointer because on some
|
||||
* architectures they have different sizes. However scm_c_define_gsubr() wants
|
||||
* a function pointer as a void pointer, which trips -Wpedantic. Compiling C
|
||||
* without pedantic errors is not a reasonable option in my opinion, so instead
|
||||
* we'll have to resort to this hack.
|
||||
*/
|
||||
#define scm_c_define_gsubr_fix(NAME, REQ, OPT, RST, FN) \
|
||||
{ const long int ptr = (long int)FN; scm_c_define_gsubr(NAME, REQ, OPT, RST, (void *)ptr); }
|
||||
|
||||
extern SCM layout_demand_handler;
|
||||
extern SCM user_command_handler;
|
||||
|
||||
static SCM install_handler (SCM key, SCM proc)
|
||||
{
|
||||
if ( scm_is_false(scm_symbol_p(key)) == 1 )
|
||||
{
|
||||
scm_error_scm(
|
||||
scm_from_utf8_symbol("wrong-type-arg"),
|
||||
scm_from_utf8_string("install-handler"),
|
||||
scm_from_utf8_string("First argument must be a symbol."),
|
||||
SCM_BOOL_F,
|
||||
scm_list_1(key)
|
||||
);
|
||||
return SCM_UNSPECIFIED;
|
||||
}
|
||||
|
||||
// TODO check if the procedure has the right amount of arguments.
|
||||
if ( scm_is_false(scm_procedure_p(proc)) == 1 )
|
||||
{
|
||||
scm_error_scm(
|
||||
scm_from_utf8_symbol("wrong-type-arg"),
|
||||
scm_from_utf8_string("install-handler"),
|
||||
scm_from_utf8_string("Second argument must be a procedure."),
|
||||
SCM_BOOL_F,
|
||||
scm_list_1(proc)
|
||||
);
|
||||
return SCM_UNSPECIFIED;
|
||||
}
|
||||
|
||||
if (scm_is_eq(scm_from_utf8_symbol("layout-demand"), key))
|
||||
layout_demand_handler = proc;
|
||||
else if (scm_is_eq(scm_from_utf8_symbol("user-command"), key))
|
||||
user_command_handler = proc;
|
||||
else
|
||||
{
|
||||
scm_error_scm(
|
||||
// TODO should this be 'misc-error instead?
|
||||
scm_from_utf8_symbol("out-of-range"),
|
||||
scm_from_utf8_string("install-handler"),
|
||||
scm_from_utf8_string("Unknown key: ~A"),
|
||||
scm_list_1(key),
|
||||
scm_list_1(key)
|
||||
);
|
||||
return SCM_UNSPECIFIED;
|
||||
}
|
||||
|
||||
return SCM_BOOL_T;
|
||||
}
|
||||
|
||||
static void *load_script_inner (void *data)
|
||||
{
|
||||
const char *path = (char *)data;
|
||||
|
||||
/* scm_primitive_load_path() searches guiles load-path when encountering
|
||||
* a relative path. That should never happen here though.
|
||||
*/
|
||||
assert(path[0] == '/');
|
||||
|
||||
return scm_primitive_load_path(scm_from_utf8_string(path));
|
||||
}
|
||||
|
||||
void *load_script (void *data)
|
||||
{
|
||||
/* Note: All guile objects are garbage collected. */
|
||||
|
||||
scm_c_define_gsubr_fix("install-handler", 2, 0, 0, install_handler);
|
||||
|
||||
/* Continuation barrier causes stack unwind on exceptions to stop here.
|
||||
* Otherwise the entire stack created by scm_with_guile() would be
|
||||
* unwound. This makes responding to exceptions nicer.
|
||||
*/
|
||||
SCM call_result = scm_c_with_continuation_barrier(
|
||||
load_script_inner, data
|
||||
);
|
||||
|
||||
if ( call_result == NULL )
|
||||
return (void *)"ERROR: Fatal error while loading layout script.\n";
|
||||
|
||||
if ( layout_demand_handler == NULL )
|
||||
return (void *)"ERROR: No layout demand handler installed.\n";
|
||||
|
||||
/* Checked in the installer functions. */
|
||||
assert(scm_is_true(scm_procedure_p(layout_demand_handler)) == 1);
|
||||
if ( user_command_handler != NULL )
|
||||
assert(scm_is_true(scm_procedure_p(user_command_handler)) == 1);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
6
src/load-script.h
Normal file
6
src/load-script.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef RIVERGUILE_LOAD_SCRIPT_H_INCLUDED
|
||||
#define RIVERGUILE_LOAD_SCRIPT_H_INCLUDED
|
||||
|
||||
void *load_script (void *data);
|
||||
|
||||
#endif
|
||||
424
src/riverguile.c
Normal file
424
src/riverguile.c
Normal file
@@ -0,0 +1,424 @@
|
||||
#include <ctype.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <setjmp.h>
|
||||
#include <wayland-client.h>
|
||||
#include <libguile.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <features.h>
|
||||
#ifdef __GLIBC__
|
||||
#include<execinfo.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "river-layout-v3.h"
|
||||
|
||||
#include "types.h"
|
||||
#include "call-layout-demand-handler.h"
|
||||
#include "call-user-command-handler.h"
|
||||
#include "load-script.h"
|
||||
|
||||
bool loop = true;
|
||||
int ret = EXIT_SUCCESS;
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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(
|
||||
registry, name, &river_layout_manager_v3_interface, 2
|
||||
);
|
||||
}
|
||||
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);
|
||||
if ( output == NULL )
|
||||
{
|
||||
wl_output_destroy(wl_output);
|
||||
ret = EXIT_FAILURE;
|
||||
loop = false;
|
||||
return;
|
||||
}
|
||||
wl_list_insert(&outputs, &output->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)
|
||||
{
|
||||
if ( output->name != name )
|
||||
continue;
|
||||
wl_list_remove(&output->link);
|
||||
output_destroy(output);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct wl_registry_listener registry_listener = {
|
||||
.global = registry_handle_global,
|
||||
.global_remove = registry_handle_global_remove,
|
||||
};
|
||||
|
||||
static void sync_handle_done (void *data, struct wl_callback *wl_callback, uint32_t other_data)
|
||||
{
|
||||
wl_callback_destroy(wl_callback);
|
||||
sync_callback = NULL;
|
||||
|
||||
if ( layout_manager == NULL )
|
||||
{
|
||||
fputs("ERROR: Wayland server does not support river-layout-v3.\n", stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
loop = false;
|
||||
return;
|
||||
}
|
||||
|
||||
/* 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)
|
||||
output_configure(output);
|
||||
}
|
||||
|
||||
static const struct wl_callback_listener sync_callback_listener = {
|
||||
.done = sync_handle_done,
|
||||
};
|
||||
|
||||
static void handle_interrupt (int signum)
|
||||
{
|
||||
fputs("Killed 💀\n", stderr);
|
||||
loop = false;
|
||||
longjmp(skip_main_loop, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercept error signals (like SIGSEGV and SIGFPE) so that we can try to
|
||||
* print a fancy error message and a backtracke before letting the system kill us.
|
||||
*/
|
||||
static void handle_error (int signum)
|
||||
{
|
||||
const char *msg =
|
||||
"\n"
|
||||
"┌──────────────────────────────────────────┐\n"
|
||||
"│ │\n"
|
||||
"│ riverguile has crashed. │\n"
|
||||
"│ │\n"
|
||||
"│ This is most likely a bug, so please │\n"
|
||||
"│ report this to the mailing list. │\n"
|
||||
"│ │\n"
|
||||
"│ ~leon_plickat/public-inbox@lists.sr.ht │\n"
|
||||
"│ │\n"
|
||||
"└──────────────────────────────────────────┘\n"
|
||||
"\n";
|
||||
fputs(msg, stderr);
|
||||
|
||||
/* Set up the default handlers to deal with the rest. We do this before
|
||||
* attempting to get a backtrace, because sometimes that could also
|
||||
* cause a SEGFAULT and we don't want a funny signal loop to happen.
|
||||
*/
|
||||
signal(signum, SIG_DFL);
|
||||
|
||||
#ifdef __linux__
|
||||
#ifdef __GLIBC__
|
||||
fputs("Attempting to get backtrace:\n", stderr);
|
||||
void *buffer[255];
|
||||
const int calls = backtrace(buffer, sizeof(buffer) / sizeof(void *));
|
||||
backtrace_symbols_fd(buffer, calls, fileno(stderr));
|
||||
fputs("\n", stderr);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Easiest way of calling the default signal handler. */
|
||||
kill(getpid(), signum);
|
||||
}
|
||||
|
||||
|
||||
static char *formatted_buffer (const char *fmt, ...)
|
||||
{
|
||||
/* Determine length of formatted text. */
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
/* +1 for NULL. */
|
||||
unsigned long len = (unsigned long)vsnprintf(NULL, 0, fmt, args) + 1;
|
||||
va_end(args);
|
||||
|
||||
char *buffer = calloc(1, len);
|
||||
if ( buffer == NULL )
|
||||
{
|
||||
fprintf(stderr, "ERROR: calloc: %s\n", strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Finally write formatted text into buffer. */
|
||||
va_start(args, fmt);
|
||||
vsnprintf(buffer, len, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static char *get_script_path (void)
|
||||
{
|
||||
struct {
|
||||
const char *fmt;
|
||||
bool needs_env;
|
||||
const char *env;
|
||||
} paths[] = {
|
||||
{ .fmt = "layout.scm", .needs_env = false, .env = NULL },
|
||||
{ .fmt = "%s/riverguile/layout.scm", .needs_env = true, .env = getenv("XDG_CONFIG_HOME") },
|
||||
{ .fmt = "%s/river/layout.scm", .needs_env = true, .env = getenv("XDG_CONFIG_HOME") },
|
||||
{ .fmt = "%s/.config/riverguile/layout.scm", .needs_env = true, .env = getenv("HOME") },
|
||||
{ .fmt = "%s/.config/river/layout.scm", .needs_env = true, .env = getenv("HOME") },
|
||||
{ .fmt = "/etc/riverguile/layout.scm", .needs_env = false, .env = NULL }
|
||||
};
|
||||
|
||||
char *path = NULL;
|
||||
for (size_t i = 0; i < (sizeof(paths) / sizeof(paths[0])); i++)
|
||||
{
|
||||
if ( path != NULL )
|
||||
{
|
||||
free(path);
|
||||
path = NULL;
|
||||
}
|
||||
|
||||
/* Path needs env var, but it is not set. */
|
||||
if ( paths[i].needs_env && paths[i].env == NULL )
|
||||
continue;
|
||||
|
||||
path = formatted_buffer(paths[i].fmt, paths[i].env);
|
||||
if ( path == NULL )
|
||||
return NULL;
|
||||
|
||||
/* Does the path exist? */
|
||||
if ( access(path, F_OK) != 0 )
|
||||
continue;
|
||||
|
||||
/* Is it readable? */
|
||||
if ( access(path, R_OK) != 0 )
|
||||
{
|
||||
fprintf(stderr, "ERROR: Script '%s' exists, but is not readable.\n", path);
|
||||
free(path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
if ( path != NULL )
|
||||
free(path);
|
||||
|
||||
fputs("ERROR: No layout script found.\n", stderr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
signal(SIGSEGV, handle_error);
|
||||
signal(SIGFPE, handle_error);
|
||||
signal(SIGINT, handle_interrupt);
|
||||
wl_list_init(&outputs);
|
||||
|
||||
// TODO use argv[1] if present
|
||||
char *path = get_script_path();
|
||||
if ( path == NULL )
|
||||
{
|
||||
ret = EXIT_FAILURE;
|
||||
goto early_exit;
|
||||
}
|
||||
|
||||
void *res = scm_with_guile(load_script, (void *)path);
|
||||
if ( res != NULL )
|
||||
{
|
||||
fputs((char *)res, stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
loop = false;
|
||||
goto early_exit;
|
||||
}
|
||||
|
||||
/* We query the display name here instead of letting wl_display_connect()
|
||||
* figure it out itself, because libwayland (for legacy reasons) falls
|
||||
* back to using "wayland-0" when $WAYLAND_DISPLAY is not set, which is
|
||||
* generally not desirable.
|
||||
*/
|
||||
const char *display_name = getenv("WAYLAND_DISPLAY");
|
||||
if ( display_name == NULL )
|
||||
{
|
||||
fputs("ERROR: WAYLAND_DISPLAY is not set.\n", stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
goto early_exit;
|
||||
}
|
||||
|
||||
wl_display = wl_display_connect(display_name);
|
||||
if ( wl_display == NULL )
|
||||
{
|
||||
fputs("ERROR: Can not connect to wayland display.\n", stderr);
|
||||
ret = EXIT_FAILURE;
|
||||
goto early_exit;
|
||||
}
|
||||
|
||||
wl_registry = wl_display_get_registry(wl_display);
|
||||
wl_registry_add_listener(wl_registry, ®istry_listener, NULL);
|
||||
|
||||
sync_callback = wl_display_sync(wl_display);
|
||||
wl_callback_add_listener(sync_callback, &sync_callback_listener, NULL);
|
||||
|
||||
if ( setjmp(skip_main_loop) == 0 )
|
||||
while ( loop && wl_display_dispatch(wl_display) > 0 );
|
||||
|
||||
struct Output *output, *tmp;
|
||||
wl_list_for_each_safe(output, tmp, &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);
|
||||
|
||||
early_exit:
|
||||
if ( path != NULL )
|
||||
free(path);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
25
src/types.h
Normal file
25
src/types.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef RIVERGUILE_TYPES_H_INCLUDED
|
||||
#define RIVERGUILE_TYPES_H_INCLUDED
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <wayland-client.h>
|
||||
|
||||
#include "river-layout-v3.h"
|
||||
|
||||
struct Output
|
||||
{
|
||||
struct wl_list link;
|
||||
struct wl_output *wl_output;
|
||||
struct river_layout_v3 *layout;
|
||||
uint32_t name;
|
||||
bool configured;
|
||||
|
||||
/* Tags for the next user command. Due to backwards compatability, the
|
||||
* layout protocol sends us the currently active tags for a user command
|
||||
* in a separate event before the actual user command. This event is
|
||||
* guaranteed to be received.
|
||||
*/
|
||||
uint32_t user_command_tags;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user