Compare commits

...

6 Commits

13 changed files with 332 additions and 47 deletions

View File

@@ -2,6 +2,7 @@
#define ZDWM_ACTION_H
#include <stdint.h>
#include <zdwm/types.h>
#if defined(__cplusplus)
extern "C" {
@@ -12,16 +13,36 @@ typedef union zdwm_action_arg_t {
int32_t i;
uint32_t ui;
const char *str;
const void *ptr;
} zdwm_action_arg_t;
typedef struct zdwm_action_ctx_t zdwm_action_ctx_t;
typedef struct zdwm_runtime_t zdwm_runtime_t;
typedef void
zdwm_action_fn(const zdwm_action_ctx_t *ctx, const zdwm_action_arg_t *arg);
struct zdwm_action_ctx_t {
typedef struct zdwm_action_api_t {
void (*spawn)(const char *command);
};
void (*quit)(zdwm_runtime_t *runtime, bool restart);
void (*raise_or_run)(
zdwm_runtime_t *runtime,
const char *class_name,
const char *command
);
void (*toggle_fullscreen)(zdwm_runtime_t *runtime);
void (*toggle_maximize)(zdwm_runtime_t *runtime);
void (*toggle_floating)(zdwm_runtime_t *runtime);
void (*toggle_sticky)(zdwm_runtime_t *runtime);
void (*switch_workspace)(
zdwm_runtime_t *runtime,
zdwm_workspace_id_t workspace_id
);
void (*cycle_layout)(zdwm_runtime_t *runtime, int32_t delta);
void (*cycle_current_output)(zdwm_runtime_t *runtime, int32_t delta);
} zdwm_action_api_t;
typedef void zdwm_action_fn(
zdwm_runtime_t *runtime,
const zdwm_action_api_t *ctx,
const zdwm_action_arg_t *arg
);
#if defined(__cplusplus)
}

View File

@@ -395,7 +395,7 @@ void window_grab_keys(
for (size_t i = 0; i < count; ++i) {
auto key = &keys[i];
auto keycodes = xcb_key_symbols_get_keycode(key_symbols, keys->keysym);
auto keycodes = xcb_key_symbols_get_keycode(key_symbols, key->keysym);
if (!keycodes) continue;
auto modifiers = get_xcb_modifier(key->modifiers);

View File

@@ -20,7 +20,7 @@ void window_list_reset(window_list_t *window_list) {
}
void window_list_cleanup(window_list_t *window_list) {
p_delete(window_list->windows);
p_delete(&window_list->windows);
window_list->count = 0;
window_list->capacity = 0;
}

View File

@@ -3,13 +3,46 @@
#include <zdwm/action.h>
#include <zdwm/types.h>
#include "base/log.h"
#include "base/macros.h"
static constexpr char launcher[] =
"rofi -show combi -modes combi -combi-modes window,drun,run,ssh,windowcd";
static void spawn(const zdwm_action_ctx_t *ctx, const zdwm_action_arg_t *arg) {
ctx->spawn(arg->str);
typedef struct raise_or_run_arg_t {
const char *class_name;
const char *command;
} raise_or_run_arg_t;
static raise_or_run_arg_t terminal = {"XTerm", "xterm"};
static raise_or_run_arg_t editor = {"Emacs", "emacsclient -a '' -r -n"};
static raise_or_run_arg_t browser = {"firefox", "firefox-bin"};
static raise_or_run_arg_t chrome = {"Google-chrome", "google-chrome-stable"};
static void spawn(
zdwm_runtime_t *runtime,
const zdwm_action_api_t *api,
const zdwm_action_arg_t *arg
) {
api->spawn(arg->str);
}
static void quit(
zdwm_runtime_t *runtime,
const zdwm_action_api_t *api,
const zdwm_action_arg_t *arg
) {
logger("quit: restart: %s\n", arg->b ? "true" : "false");
api->quit(runtime, arg->b);
}
static void raise_or_run(
zdwm_runtime_t *runtime,
const zdwm_action_api_t *api,
const zdwm_action_arg_t *arg
) {
auto args = (raise_or_run_arg_t *)arg->ptr;
api->raise_or_run(runtime, args->class_name, args->command);
}
bool config_defaults_build(
@@ -71,10 +104,20 @@ bool config_defaults_build(
auto default_mode = api->add_mode(builder, "default");
#define BIND(mode, key, fn, arg) \
api->bind(builder, mode, key, fn, (zdwm_action_arg_t)arg)
#define Alt "Mod1"
#define Super "Mod4"
#define BIND(MODE, KEY, FN, ARG) \
api->bind(builder, MODE, KEY, FN, (zdwm_action_arg_t)ARG)
BIND(default_mode, "Mod4+r", spawn, {.str = launcher});
BIND(default_mode, Super "+r", spawn, {.str = launcher});
BIND(default_mode, Super "+Shift+q", quit, {.b = false});
BIND(default_mode, Super "+Control+r", quit, {.b = true});
BIND(default_mode, Super "+Return", spawn, {.str = terminal.command});
BIND(default_mode, Alt "+Control+r", raise_or_run, {.ptr = &terminal});
BIND(default_mode, Super "+e", raise_or_run, {.ptr = &editor});
BIND(default_mode, Super "+q", raise_or_run, {.ptr = &browser});
BIND(default_mode, Super "+a", raise_or_run, {.ptr = &chrome});
#undef BIND

View File

@@ -1,11 +1,18 @@
#include "core/action.h"
#include <paths.h>
#include <stddef.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "base/log.h"
#include "base/macros.h"
#include "core/command.h"
#include "core/command_buffer.h"
#include "core/state.h"
#include "core/types.h"
#include "core/window.h"
void spawn(const char *command) {
if (fork() == 0) {
@@ -21,3 +28,63 @@ void spawn(const char *command) {
wait(0);
}
void quit(runtime_t *runtime, bool restart) {
runtime->will_restart = restart;
runtime->running = false;
}
static const window_t *
get_next_window_by_class(state_t *state, const char *class_name) {
auto output = state_output_at(state, state->current_output_index);
auto workspace = state_workspace_get(state, output->current_workspace_id);
size_t start_index = 0;
if (!window_id_invalid(workspace->focused_window_id)) {
for (size_t i = 0; i < state_window_count(state); ++i) {
if (state_window_at(state, i)->id == workspace->focused_window_id) {
start_index = i;
break;
}
}
}
for (size_t i = start_index + 1; i < state_window_count(state); ++i) {
auto window = state_window_at(state, i);
if (strcmp(window->class_name, class_name) == 0) {
return window;
}
}
for (size_t i = 0; i < MIN(start_index + 1, state_window_count(state)); ++i) {
auto window = state_window_at(state, i);
if (strcmp(window->class_name, class_name) == 0) {
return window;
}
}
return nullptr;
}
void raise_or_run(
zdwm_runtime_t *runtime,
const char *class_name,
const char *command
) {
auto window = get_next_window_by_class(&runtime->state, class_name);
if (!window) {
spawn(command);
return;
}
command_t switch_workspace_cmd = {
.type = ZDWM_COMMAND_SWITCH_WORKSPACE,
.as.switch_workspace.workspace = window->workspace_id,
};
command_buffer_push(&runtime->command_buffer, &switch_workspace_cmd);
command_t focus_cmd = {
.type = ZDWM_COMMAND_FOCUS_WINDOW,
.as.focus.window = window->id,
};
command_buffer_push(&runtime->command_buffer, &focus_cmd);
}

View File

@@ -1,3 +1,11 @@
#pragma once
#include "core/runtime.h"
void spawn(const char *command);
void quit(runtime_t *runtime, bool restart);
void raise_or_run(
zdwm_runtime_t *runtime,
const char *class_name,
const char *command
);

View File

@@ -11,6 +11,11 @@ typedef enum command_type_t {
ZDWM_COMMAND_WITHDRAW_WINDOW,
ZDWM_COMMAND_CONFIGURE_WINDOW,
ZDWM_COMMAND_CHANGE_WINDOW_STATE,
ZDWM_COMMAND_WINDOW_SET_FLOATING,
ZDWM_COMMAND_WINDOW_SET_STICKY,
ZDWM_COMMAND_WINDOW_SET_MINIMIZED,
ZDWM_COMMAND_WINDOW_SET_MAXIMIZED,
ZDWM_COMMAND_WINDOW_SET_FULLSCREEN,
ZDWM_COMMAND_SWITCH_WORKSPACE,
} command_type_t;
@@ -21,7 +26,6 @@ typedef struct manage_window_command_t {
} manage_window_command_t;
typedef struct switch_workspace_command_t {
output_id_t output;
workspace_id_t workspace;
} switch_workspace_command_t;
@@ -31,6 +35,11 @@ typedef struct window_state_change_command_t {
window_state_request_action_t action;
} window_state_change_command_t;
typedef struct window_bool_state_t {
window_id_t window;
bool state;
} window_bool_state_t;
/**
* @brief 非 owning 的命令值对象
* @details
@@ -50,6 +59,11 @@ typedef struct command_t {
only_window_data_t withdraw;
configure_data_t configure;
window_state_change_command_t state_change;
window_bool_state_t floating;
window_bool_state_t sticky;
window_bool_state_t minimized;
window_bool_state_t maximized;
window_bool_state_t fullscreen;
switch_workspace_command_t switch_workspace;
} as;
} command_t;

View File

@@ -18,11 +18,10 @@
#include "core/window.h"
#include "core/wm_desc.h"
static void route_key_press(
binding_table_t *binding_table,
const zdwm_action_ctx_t *action_ctx,
const key_press_event_t *e
) {
static void
route_key_press(const policy_context_t *ctx, const key_press_event_t *e) {
auto binding_table = ctx->bind_table;
size_t count = 0;
auto bindings = binding_table_get_current_bindings(binding_table, &count);
if (!bindings) return;
@@ -30,7 +29,7 @@ static void route_key_press(
for (size_t i = 0; i < count; ++i) {
auto binding = &bindings[i];
if (binding->modifiers == e->modifiers && binding->keysym == e->keysym) {
binding->fn(action_ctx, &binding->arg);
binding->fn(ctx->runtime, &ctx->action_api, &binding->arg);
}
}
}
@@ -101,23 +100,19 @@ static void route_map_request(
}
command_buffer_push(out, &manage_window_cmd);
if (have_rule_match && action.switch_to_workspace) {
if (!have_rule_match || !action.switch_to_workspace) return;
workspace_id_t workspace_id = manage_window_cmd.as.manage_window.workspace;
const workspace_t *workspace = state_workspace_get(state, workspace_id);
if (!workspace) return;
if (workspace) {
command_t switch_workspace_cmd = {
.type = ZDWM_COMMAND_SWITCH_WORKSPACE,
.as.switch_workspace = {
.output = workspace->output_id,
.workspace = workspace_id,
},
.as.switch_workspace.workspace = workspace_id,
};
command_buffer_push(out, &switch_workspace_cmd);
}
}
}
static void route_window_remove(
state_t *state,
@@ -184,10 +179,11 @@ void policy_route_event(
auto state = ctx->state;
switch (event->type) {
case ZDWM_EVENT_KEY_PRESS:
route_key_press(ctx->bind_table, &ctx->action_ctx, &event->as.key_press);
route_key_press(ctx, &event->as.key_press);
break;
case ZDWM_EVENT_POINTER_ENTER:
route_pointer_enter(state, event->as.pointer_enter.window, out);
break;
case ZDWM_EVENT_WINDOW_MAP_REQUEST:
route_map_request(state, ctx->rules, &event->as.window_map_request, out);
break;
@@ -422,6 +418,60 @@ configure_window(state_t *state, const configure_data_t *data, plan_t *plan) {
plan_push_effect(plan, &configure_effect);
}
static void fullscreen_window(
const policy_context_t *ctx,
window_id_t window_id,
bool value,
plan_t *plan
);
static void maximize_window(
const policy_context_t *ctx,
window_id_t window_id,
bool value,
plan_t *plan
);
static void add_window_floating_effect(
const policy_context_t *ctx,
const window_t *window,
plan_t *plan
) {
effect_t configure_effect = {
.type = ZDWM_EFFECT_CONFIGURE_WINDOW,
.as.configure = {
.window = window->id,
.border_width = ctx->border->width,
.x = window->frame_rect.x,
.y = window->frame_rect.y,
.width = window->frame_rect.width - 2 * (int32_t)ctx->border->width,
.height = window->frame_rect.height - 2 * (int32_t)ctx->border->width,
.changed_fields = ZDWM_CONFIGURE_FIELD_X | ZDWM_CONFIGURE_FIELD_Y |
ZDWM_CONFIGURE_FIELD_WIDTH |
ZDWM_CONFIGURE_FIELD_HEIGHT |
ZDWM_CONFIGURE_FIELD_BORDER_WIDTH
}
};
plan_push_effect(plan, &configure_effect);
}
static void floating_window(
const policy_context_t *ctx,
window_id_t window_id,
bool value,
plan_t *plan
) {
auto window = state_window_get(ctx->state, window_id);
if (!window) return;
bool old_floating = window->floating;
state_window_set_floating(ctx->state, window_id, value);
if (old_floating == window->floating) return;
if (window->floating) add_window_floating_effect(ctx, window, plan);
plan->need_relayout = true;
}
static void fullscreen_window(
const policy_context_t *ctx,
window_id_t window_id,
@@ -441,11 +491,13 @@ static void fullscreen_window(
state_window_set_border_width(state, window_id, 0);
if (window->floating) {
state_window_set_float_rect(state, window_id, window->frame_rect);
add_window_floating_effect(ctx, window, plan);
}
} else {
state_window_set_border_width(state, window_id, ctx->border->width);
if (window->floating) {
state_window_set_frame_rect(state, window_id, window->float_rect);
add_window_floating_effect(ctx, window, plan);
}
}
@@ -474,11 +526,13 @@ static void maximize_window(
state_window_set_border_width(state, window_id, 0);
if (window->floating) {
state_window_set_float_rect(state, window_id, window->frame_rect);
add_window_floating_effect(ctx, window, plan);
}
} else {
state_window_set_border_width(state, window_id, ctx->border->width);
if (window->floating) {
state_window_set_frame_rect(state, window_id, window->float_rect);
add_window_floating_effect(ctx, window, plan);
}
}
@@ -589,13 +643,58 @@ static void change_window_state(
}
}
static void switch_workspace(
state_t *state,
const switch_workspace_command_t *command,
static void command_window_set_floating(
const policy_context_t *ctx,
const window_bool_state_t *data,
plan_t *plan
) {
auto output_id = command->output;
auto workspace_id = command->workspace;
floating_window(ctx, data->window, data->state, plan);
}
static void command_window_set_sticky(
const policy_context_t *ctx,
const window_bool_state_t *data,
plan_t *plan
) {
auto window = state_window_get(ctx->state, data->window);
if (!window || window->sticky == data->state) return;
if (!window->floating) {
}
state_window_set_sticky(ctx->state, data->window, data->state);
if (data->state) return;
}
static void command_window_set_minimized(
const policy_context_t *ctx,
const window_bool_state_t *data,
plan_t *plan
) {
minimize_window(ctx, data->window, data->state, plan);
}
static void command_window_set_maximized(
const policy_context_t *ctx,
const window_bool_state_t *data,
plan_t *plan
) {
maximize_window(ctx, data->window, data->state, plan);
}
static void command_window_set_fullscreen(
const policy_context_t *ctx,
const window_bool_state_t *data,
plan_t *plan
) {
fullscreen_window(ctx, data->window, data->state, plan);
}
static void
switch_workspace(state_t *state, workspace_id_t workspace_id, plan_t *plan) {
auto workspace = state_workspace_get(state, workspace_id);
if (!workspace) return;
auto output_id = workspace->output_id;
workspace_id_t old_workspace = ZDWM_WORKSPACE_ID_INVALID;
if (state_output_set_current_workspace(
@@ -641,8 +740,23 @@ void policy_apply_command(
case ZDWM_COMMAND_CHANGE_WINDOW_STATE:
change_window_state(ctx, &cmd->as.state_change, plan);
break;
case ZDWM_COMMAND_WINDOW_SET_FLOATING:
command_window_set_floating(ctx, &cmd->as.floating, plan);
break;
case ZDWM_COMMAND_WINDOW_SET_STICKY:
command_window_set_sticky(ctx, &cmd->as.sticky, plan);
break;
case ZDWM_COMMAND_WINDOW_SET_MINIMIZED:
command_window_set_minimized(ctx, &cmd->as.minimized, plan);
break;
case ZDWM_COMMAND_WINDOW_SET_MAXIMIZED:
command_window_set_maximized(ctx, &cmd->as.maximized, plan);
break;
case ZDWM_COMMAND_WINDOW_SET_FULLSCREEN:
command_window_set_fullscreen(ctx, &cmd->as.fullscreen, plan);
break;
case ZDWM_COMMAND_SWITCH_WORKSPACE:
switch_workspace(state, &cmd->as.switch_workspace, plan);
switch_workspace(state, cmd->as.switch_workspace.workspace, plan);
break;
}
}

View File

@@ -17,7 +17,8 @@ typedef struct policy_context_t {
const rules_t *rules;
const border_config_t *border;
const layout_registry_t *layouts;
const zdwm_action_ctx_t action_ctx;
const zdwm_action_api_t action_api;
zdwm_runtime_t *runtime;
} policy_context_t;
/**

View File

@@ -5,9 +5,9 @@
#include <stdint.h>
#include <zdwm/layout.h>
#include "action.h"
#include "base/array.h"
#include "base/memory.h"
#include "core/action.h"
#include "core/backend.h"
#include "core/binding.h"
#include "core/command_buffer.h"
@@ -270,8 +270,11 @@ void runtime_run(runtime_t *runtime) {
.rules = &runtime->rules,
.border = &runtime->border,
.layouts = &runtime->layouts,
.action_ctx = {
.runtime = runtime,
.action_api = {
.spawn = spawn,
.quit = quit,
.raise_or_run = raise_or_run,
},
};

View File

@@ -26,7 +26,7 @@ typedef struct runtime_init_desc_t {
binding_table_t *binding_table;
} runtime_init_desc_t;
typedef struct runtime_t {
typedef struct zdwm_runtime_t {
bool running;
bool will_restart;

View File

@@ -51,6 +51,8 @@ void window_set_minimized(window_t *window, bool minimized) {
void window_set_floating(window_t *window, bool floating) {
if (!floating && (window->fixed_size || window->sticky)) return;
if (floating == window->floating) return;
window->floating = floating;
if (floating) window->float_rect = window->frame_rect;
}

View File

@@ -1,3 +1,6 @@
#include <errno.h>
#include <unistd.h>
#include "base/log.h"
#include "config/runtime_config.h"
#include "core/backend.h"
@@ -32,7 +35,11 @@ static void bootstrap(runtime_t *runtime) {
}
};
int main(void) {
char **cmd_argv = nullptr;
int main(int argc, char *argv[]) {
cmd_argv = argv;
runtime_t runtime = {0};
bootstrap(&runtime);
@@ -40,5 +47,10 @@ int main(void) {
runtime_run(&runtime);
runtime_shutdown(&runtime);
if (runtime.will_restart) {
execvp(cmd_argv[0], cmd_argv);
fatal("execv() failed: %s", strerror(errno));
}
return 0;
}