implement ext-idle-notify-v1

This commit is contained in:
Leon Henrik Plickat
2024-01-06 21:25:26 +01:00
parent 25fcd37930
commit 2ccbfb2283
16 changed files with 682 additions and 187 deletions

View File

@@ -1,6 +1,11 @@
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <libguile.h>
#include <string.h>
#include "riverguile.h"
#include "seat.h"
/**
* ISO C forbids casting a function pointer to a void pointer because on some
@@ -12,8 +17,35 @@
#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 uint32_t extract_ms_from_idle_key (SCM key)
{
SCM key_split = scm_string_split(
scm_symbol_to_string(key),
scm_to_char_set(scm_from_utf8_string(":"))
);
if ( scm_to_uint32(scm_length(key_split)) != 2 )
goto error;
SCM maybe_number = scm_string_to_number(scm_cadr(key_split), scm_from_int(10));
if ( scm_is_false(maybe_number) == 1 )
goto error;
int32_t ms = scm_to_int32(maybe_number);
if ( ms < 0 )
goto error;
return (uint32_t)ms * 1000;
error:
scm_error_scm(
scm_from_utf8_symbol("wrong-type-arg"),
scm_from_utf8_string("install-handler"),
scm_from_utf8_string("First argument: Excpected 'idle:<positive integer>."),
SCM_BOOL_F,
scm_list_1(key)
);
return 0;
}
static SCM install_handler (SCM key, SCM proc)
{
@@ -42,10 +74,33 @@ static SCM install_handler (SCM key, SCM 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;
if ( scm_is_eq(scm_from_utf8_symbol("layout-demand"), key) == 1 )
context.layout_demand_handler = proc;
else if ( scm_is_eq(scm_from_utf8_symbol("user-command"), key) == 1)
context.user_command_handler = proc;
else if ( scm_is_true(scm_string_prefix_p(scm_from_utf8_string("idle:"), scm_symbol_to_string(key),
scm_from_int(0), scm_from_int(5),
scm_from_int(0), scm_string_length(scm_symbol_to_string(key)))) == 1 )
{
uint32_t ms = extract_ms_from_idle_key(key);
struct Idle *idle = calloc(1, sizeof(struct Idle));
if ( idle == NULL )
{
scm_error_scm(
scm_from_utf8_symbol("memory-allocation-error"),
scm_from_utf8_string("install-handler"),
SCM_BOOL_F,
SCM_BOOL_F,
SCM_BOOL_F
);
return SCM_UNSPECIFIED;
}
idle->ms = ms;
idle->handler = proc;
wl_list_insert(&context.unconfigured_idles, &idle->link);
}
else
{
scm_error_scm(
@@ -91,13 +146,13 @@ void *load_script (void *data)
if ( call_result == NULL )
return (void *)"ERROR: Fatal error while loading layout script.\n";
if ( layout_demand_handler == NULL )
if ( context.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);
assert(scm_is_true(scm_procedure_p(context.layout_demand_handler)) == 1);
if ( context.user_command_handler != NULL )
assert(scm_is_true(scm_procedure_p(context.user_command_handler)) == 1);
return NULL;
}