mirror of
https://github.com/DreamMaoMao/maomaowm.git
synced 2026-05-03 06:46:38 -04:00
Merge f86ec6876a into b9c6a2c196
This commit is contained in:
commit
87313d7464
7 changed files with 1641 additions and 2 deletions
|
|
@ -78,6 +78,21 @@ no_border_when_single=0
|
||||||
axis_bind_apply_timeout=100
|
axis_bind_apply_timeout=100
|
||||||
focus_on_activate=1
|
focus_on_activate=1
|
||||||
idleinhibit_ignore_visible=0
|
idleinhibit_ignore_visible=0
|
||||||
|
# Restore and save session state across compositor restarts.
|
||||||
|
# Disabled by default to preserve Mango's current behavior.
|
||||||
|
# Minimized restore is currently unsupported.
|
||||||
|
# Restore expects target outputs to already exist before clients map.
|
||||||
|
# Mango only restores from a trusted session file owned by the current user
|
||||||
|
# and not writable by group or others.
|
||||||
|
session_restore=0
|
||||||
|
# Optional relaunch override used when Mango cannot recover a suitable launcher.
|
||||||
|
# Format: session_launch=app_id|command
|
||||||
|
# or: session_launch=app_id|title|command
|
||||||
|
# Mango prefers exact Mango-owned spawn commands, then normalized desktop/Flatpak
|
||||||
|
# launchers, then raw process argv before consulting session_launch.
|
||||||
|
# session_launch=foot|foot
|
||||||
|
# session_launch=foot|gamma|foot -a foot -T gamma -e sh -lc "sleep 600"
|
||||||
|
# session_launch=org.kde.dolphin|dolphin .
|
||||||
sloppyfocus=1
|
sloppyfocus=1
|
||||||
warpcursor=1
|
warpcursor=1
|
||||||
focus_cross_monitor=0
|
focus_cross_monitor=0
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,7 @@ endif
|
||||||
executable('mango',
|
executable('mango',
|
||||||
'src/mango.c',
|
'src/mango.c',
|
||||||
'src/common/util.c',
|
'src/common/util.c',
|
||||||
|
'src/session/session.c',
|
||||||
'src/ext-protocol/wlr_ext_workspace_v1.c',
|
'src/ext-protocol/wlr_ext_workspace_v1.c',
|
||||||
wayland_sources,
|
wayland_sources,
|
||||||
dependencies : [
|
dependencies : [
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,12 @@ typedef struct {
|
||||||
char *value;
|
char *value;
|
||||||
} ConfigEnv;
|
} ConfigEnv;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char app_id[256];
|
||||||
|
char title[512];
|
||||||
|
char command[1024];
|
||||||
|
} ConfigSessionLaunchRule;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char *id;
|
const char *id;
|
||||||
const char *title;
|
const char *title;
|
||||||
|
|
@ -251,6 +257,7 @@ typedef struct {
|
||||||
|
|
||||||
uint32_t axis_bind_apply_timeout;
|
uint32_t axis_bind_apply_timeout;
|
||||||
uint32_t focus_on_activate;
|
uint32_t focus_on_activate;
|
||||||
|
int32_t session_restore;
|
||||||
int32_t idleinhibit_ignore_visible;
|
int32_t idleinhibit_ignore_visible;
|
||||||
int32_t sloppyfocus;
|
int32_t sloppyfocus;
|
||||||
int32_t warpcursor;
|
int32_t warpcursor;
|
||||||
|
|
@ -352,6 +359,8 @@ typedef struct {
|
||||||
|
|
||||||
char **exec_once;
|
char **exec_once;
|
||||||
int32_t exec_once_count;
|
int32_t exec_once_count;
|
||||||
|
ConfigSessionLaunchRule *session_launch_rules;
|
||||||
|
int32_t session_launch_rules_count;
|
||||||
|
|
||||||
char *cursor_theme;
|
char *cursor_theme;
|
||||||
uint32_t cursor_size;
|
uint32_t cursor_size;
|
||||||
|
|
@ -1426,6 +1435,71 @@ bool parse_option(Config *config, char *key, char *value) {
|
||||||
config->allow_shortcuts_inhibit = atoi(value);
|
config->allow_shortcuts_inhibit = atoi(value);
|
||||||
} else if (strcmp(key, "allow_lock_transparent") == 0) {
|
} else if (strcmp(key, "allow_lock_transparent") == 0) {
|
||||||
config->allow_lock_transparent = atoi(value);
|
config->allow_lock_transparent = atoi(value);
|
||||||
|
} else if (strcmp(key, "session_restore") == 0) {
|
||||||
|
config->session_restore = atoi(value);
|
||||||
|
} else if (strcmp(key, "session_launch") == 0) {
|
||||||
|
ConfigSessionLaunchRule rule = {0};
|
||||||
|
ConfigSessionLaunchRule *new_rules = NULL;
|
||||||
|
char *first_sep = strchr(value, '|');
|
||||||
|
char *second_sep = NULL;
|
||||||
|
size_t app_len, title_len = 0, cmd_len;
|
||||||
|
|
||||||
|
if (!first_sep) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"\033[1m\033[31m[ERROR]:\033[33m Invalid session_launch "
|
||||||
|
"format. Expected app_id|command or "
|
||||||
|
"app_id|title|command\033[0m\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
second_sep = strchr(first_sep + 1, '|');
|
||||||
|
app_len = (size_t)(first_sep - value);
|
||||||
|
if (second_sep) {
|
||||||
|
title_len = (size_t)(second_sep - (first_sep + 1));
|
||||||
|
cmd_len = strlen(second_sep + 1);
|
||||||
|
} else {
|
||||||
|
cmd_len = strlen(first_sep + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (app_len == 0 || cmd_len == 0 || app_len >= sizeof(rule.app_id) ||
|
||||||
|
cmd_len >= sizeof(rule.command) ||
|
||||||
|
title_len >= sizeof(rule.title)) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"\033[1m\033[31m[ERROR]:\033[33m Invalid session_launch "
|
||||||
|
"entry length\033[0m\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(rule.app_id, value, app_len);
|
||||||
|
rule.app_id[app_len] = '\0';
|
||||||
|
if (second_sep) {
|
||||||
|
memcpy(rule.title, first_sep + 1, title_len);
|
||||||
|
rule.title[title_len] = '\0';
|
||||||
|
memcpy(rule.command, second_sep + 1, cmd_len + 1);
|
||||||
|
} else {
|
||||||
|
memcpy(rule.command, first_sep + 1, cmd_len + 1);
|
||||||
|
}
|
||||||
|
trim_whitespace(rule.app_id);
|
||||||
|
trim_whitespace(rule.title);
|
||||||
|
trim_whitespace(rule.command);
|
||||||
|
if (rule.app_id[0] == '\0' || rule.command[0] == '\0') {
|
||||||
|
fprintf(stderr,
|
||||||
|
"\033[1m\033[31m[ERROR]:\033[33m session_launch requires "
|
||||||
|
"both app_id and command\033[0m\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_rules = realloc(config->session_launch_rules,
|
||||||
|
sizeof(*config->session_launch_rules) *
|
||||||
|
(config->session_launch_rules_count + 1));
|
||||||
|
if (!new_rules) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"\033[1m\033[31m[ERROR]:\033[33m Failed to allocate "
|
||||||
|
"session_launch rules\033[0m\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
config->session_launch_rules = new_rules;
|
||||||
|
config->session_launch_rules[config->session_launch_rules_count++] = rule;
|
||||||
} else if (strcmp(key, "no_border_when_single") == 0) {
|
} else if (strcmp(key, "no_border_when_single") == 0) {
|
||||||
config->no_border_when_single = atoi(value);
|
config->no_border_when_single = atoi(value);
|
||||||
} else if (strcmp(key, "no_radius_when_single") == 0) {
|
} else if (strcmp(key, "no_radius_when_single") == 0) {
|
||||||
|
|
@ -2826,6 +2900,14 @@ void free_circle_layout(Config *config) {
|
||||||
config->circle_layout_count = 0; // 重置计数
|
config->circle_layout_count = 0; // 重置计数
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_session_launch_rules(Config *config) {
|
||||||
|
if (config->session_launch_rules) {
|
||||||
|
free(config->session_launch_rules);
|
||||||
|
config->session_launch_rules = NULL;
|
||||||
|
}
|
||||||
|
config->session_launch_rules_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void free_baked_points(void) {
|
void free_baked_points(void) {
|
||||||
if (baked_points_move) {
|
if (baked_points_move) {
|
||||||
free(baked_points_move);
|
free(baked_points_move);
|
||||||
|
|
@ -3096,6 +3178,7 @@ void free_config(void) {
|
||||||
|
|
||||||
// 释放 circle_layout
|
// 释放 circle_layout
|
||||||
free_circle_layout(&config);
|
free_circle_layout(&config);
|
||||||
|
free_session_launch_rules(&config);
|
||||||
|
|
||||||
// 释放动画资源
|
// 释放动画资源
|
||||||
free_baked_points();
|
free_baked_points();
|
||||||
|
|
@ -3171,6 +3254,7 @@ void override_config(void) {
|
||||||
config.axis_bind_apply_timeout =
|
config.axis_bind_apply_timeout =
|
||||||
CLAMP_INT(config.axis_bind_apply_timeout, 0, 1000);
|
CLAMP_INT(config.axis_bind_apply_timeout, 0, 1000);
|
||||||
config.focus_on_activate = CLAMP_INT(config.focus_on_activate, 0, 1);
|
config.focus_on_activate = CLAMP_INT(config.focus_on_activate, 0, 1);
|
||||||
|
config.session_restore = CLAMP_INT(config.session_restore, 0, 1);
|
||||||
config.idleinhibit_ignore_visible =
|
config.idleinhibit_ignore_visible =
|
||||||
CLAMP_INT(config.idleinhibit_ignore_visible, 0, 1);
|
CLAMP_INT(config.idleinhibit_ignore_visible, 0, 1);
|
||||||
config.sloppyfocus = CLAMP_INT(config.sloppyfocus, 0, 1);
|
config.sloppyfocus = CLAMP_INT(config.sloppyfocus, 0, 1);
|
||||||
|
|
@ -3275,6 +3359,9 @@ void set_value_default() {
|
||||||
|
|
||||||
config.axis_bind_apply_timeout = 100;
|
config.axis_bind_apply_timeout = 100;
|
||||||
config.focus_on_activate = 1;
|
config.focus_on_activate = 1;
|
||||||
|
config.session_restore = 0;
|
||||||
|
config.session_launch_rules = NULL;
|
||||||
|
config.session_launch_rules_count = 0;
|
||||||
config.new_is_master = 1;
|
config.new_is_master = 1;
|
||||||
config.default_mfact = 0.55f;
|
config.default_mfact = 0.55f;
|
||||||
config.default_nmaster = 1;
|
config.default_nmaster = 1;
|
||||||
|
|
@ -3511,6 +3598,8 @@ bool parse_config(void) {
|
||||||
config.exec_count = 0;
|
config.exec_count = 0;
|
||||||
config.exec_once = NULL;
|
config.exec_once = NULL;
|
||||||
config.exec_once_count = 0;
|
config.exec_once_count = 0;
|
||||||
|
config.session_launch_rules = NULL;
|
||||||
|
config.session_launch_rules_count = 0;
|
||||||
config.scroller_proportion_preset = NULL;
|
config.scroller_proportion_preset = NULL;
|
||||||
config.scroller_proportion_preset_count = 0;
|
config.scroller_proportion_preset_count = 0;
|
||||||
config.circle_layout = NULL;
|
config.circle_layout = NULL;
|
||||||
|
|
|
||||||
|
|
@ -836,10 +836,13 @@ int32_t centerwin(const Arg *arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t spawn_shell(const Arg *arg) {
|
int32_t spawn_shell(const Arg *arg) {
|
||||||
|
pid_t pid;
|
||||||
|
|
||||||
if (!arg->v)
|
if (!arg->v)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (fork() == 0) {
|
pid = fork();
|
||||||
|
if (pid == 0) {
|
||||||
// 1. 忽略可能导致 coredump 的信号
|
// 1. 忽略可能导致 coredump 的信号
|
||||||
signal(SIGSEGV, SIG_IGN);
|
signal(SIGSEGV, SIG_IGN);
|
||||||
signal(SIGABRT, SIG_IGN);
|
signal(SIGABRT, SIG_IGN);
|
||||||
|
|
@ -859,14 +862,18 @@ int32_t spawn_shell(const Arg *arg) {
|
||||||
arg->v, strerror(errno));
|
arg->v, strerror(errno));
|
||||||
_exit(EXIT_FAILURE);
|
_exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
if (pid > 0)
|
||||||
|
mango_session_track_spawned_command(pid, arg->v);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t spawn(const Arg *arg) {
|
int32_t spawn(const Arg *arg) {
|
||||||
|
pid_t pid;
|
||||||
if (!arg->v)
|
if (!arg->v)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (fork() == 0) {
|
pid = fork();
|
||||||
|
if (pid == 0) {
|
||||||
// 1. 忽略可能导致 coredump 的信号
|
// 1. 忽略可能导致 coredump 的信号
|
||||||
signal(SIGSEGV, SIG_IGN);
|
signal(SIGSEGV, SIG_IGN);
|
||||||
signal(SIGABRT, SIG_IGN);
|
signal(SIGABRT, SIG_IGN);
|
||||||
|
|
@ -891,6 +898,8 @@ int32_t spawn(const Arg *arg) {
|
||||||
wordfree(&p); // 释放 wordexp 分配的内存
|
wordfree(&p); // 释放 wordexp 分配的内存
|
||||||
_exit(EXIT_FAILURE);
|
_exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
if (pid > 0)
|
||||||
|
mango_session_track_spawned_command(pid, arg->v);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
967
src/mango.c
967
src/mango.c
File diff suppressed because it is too large
Load diff
519
src/session/session.c
Normal file
519
src/session/session.c
Normal file
|
|
@ -0,0 +1,519 @@
|
||||||
|
#include "session.h"
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "../common/util.h"
|
||||||
|
|
||||||
|
extern int32_t mango_session_is_config_enabled(void);
|
||||||
|
extern int32_t mango_session_write_snapshot(FILE *out);
|
||||||
|
extern const char *mango_session_client_appid(Client *c);
|
||||||
|
extern const char *mango_session_client_title(Client *c);
|
||||||
|
extern const char *mango_session_client_monitor(Client *c);
|
||||||
|
extern const char *mango_session_lookup_launch_command(const char *app_id,
|
||||||
|
const char *title);
|
||||||
|
extern void mango_session_remember_client_launch_command(Client *c,
|
||||||
|
const char *command);
|
||||||
|
extern void mango_session_spawn_command(const char *command);
|
||||||
|
extern void mango_session_apply_restore_entry(Client *c,
|
||||||
|
const SessionRestoreEntry *entry);
|
||||||
|
extern void mango_session_spawn_tracker_init(void);
|
||||||
|
extern void mango_session_spawn_tracker_shutdown(void);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SessionRestoreEntry entry;
|
||||||
|
bool used;
|
||||||
|
} PendingSessionEntry;
|
||||||
|
|
||||||
|
static PendingSessionEntry *pending_entries;
|
||||||
|
static size_t pending_count;
|
||||||
|
static bool restore_started;
|
||||||
|
|
||||||
|
static bool mkdir_p(const char *dir) {
|
||||||
|
char tmp[PATH_MAX];
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
if (!dir || dir[0] == '\0')
|
||||||
|
return false;
|
||||||
|
|
||||||
|
len = strlen(dir);
|
||||||
|
if (len >= sizeof(tmp))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
memcpy(tmp, dir, len + 1);
|
||||||
|
|
||||||
|
for (char *p = tmp + 1; *p != '\0'; ++p) {
|
||||||
|
if (*p != '/')
|
||||||
|
continue;
|
||||||
|
*p = '\0';
|
||||||
|
if (mkdir(tmp, 0755) < 0 && errno != EEXIST)
|
||||||
|
return false;
|
||||||
|
*p = '/';
|
||||||
|
}
|
||||||
|
|
||||||
|
return mkdir(tmp, 0755) == 0 || errno == EEXIST;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *session_data_dir(void) {
|
||||||
|
const char *xdg_data = getenv("XDG_DATA_HOME");
|
||||||
|
if (xdg_data && xdg_data[0] != '\0')
|
||||||
|
return string_printf("%s/mango", xdg_data);
|
||||||
|
|
||||||
|
const char *home = getenv("HOME");
|
||||||
|
if (!home || home[0] == '\0')
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return string_printf("%s/.local/share/mango", home);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *session_file_path(void) {
|
||||||
|
char *dir = session_data_dir();
|
||||||
|
char *path;
|
||||||
|
if (!dir)
|
||||||
|
return NULL;
|
||||||
|
path = string_printf("%s/session.json", dir);
|
||||||
|
free(dir);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool session_file_is_trusted(const char *path) {
|
||||||
|
struct stat st;
|
||||||
|
uid_t uid = getuid();
|
||||||
|
|
||||||
|
if (!path || stat(path, &st) != 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!S_ISREG(st.st_mode))
|
||||||
|
return false;
|
||||||
|
if (st.st_uid != uid)
|
||||||
|
return false;
|
||||||
|
if ((st.st_mode & (S_IWGRP | S_IWOTH)) != 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void free_pending_entries(void) {
|
||||||
|
free(pending_entries);
|
||||||
|
pending_entries = NULL;
|
||||||
|
pending_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *skip_ws(const char *p) {
|
||||||
|
while (p && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t'))
|
||||||
|
++p;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *find_matching_brace(const char *start) {
|
||||||
|
int depth = 0;
|
||||||
|
bool in_string = false;
|
||||||
|
bool escaped = false;
|
||||||
|
|
||||||
|
for (const char *p = start; p && *p != '\0'; ++p) {
|
||||||
|
if (in_string) {
|
||||||
|
if (escaped) {
|
||||||
|
escaped = false;
|
||||||
|
} else if (*p == '\\') {
|
||||||
|
escaped = true;
|
||||||
|
} else if (*p == '"') {
|
||||||
|
in_string = false;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*p == '"') {
|
||||||
|
in_string = true;
|
||||||
|
} else if (*p == '{') {
|
||||||
|
depth++;
|
||||||
|
} else if (*p == '}') {
|
||||||
|
depth--;
|
||||||
|
if (depth == 0)
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool parse_json_string_value(const char *value, char *dest, size_t dest_size) {
|
||||||
|
size_t i = 0;
|
||||||
|
const char *p = skip_ws(value);
|
||||||
|
|
||||||
|
if (!p || *p != '"' || dest_size == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
++p;
|
||||||
|
while (*p != '\0' && *p != '"' && i + 1 < dest_size) {
|
||||||
|
if (*p == '\\') {
|
||||||
|
++p;
|
||||||
|
if (*p == '\0')
|
||||||
|
break;
|
||||||
|
switch (*p) {
|
||||||
|
case 'n':
|
||||||
|
dest[i++] = '\n';
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
dest[i++] = '\r';
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
dest[i++] = '\t';
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
case '"':
|
||||||
|
case '/':
|
||||||
|
dest[i++] = *p;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
dest[i++] = *p;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++p;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
dest[i++] = *p++;
|
||||||
|
}
|
||||||
|
|
||||||
|
dest[i] = '\0';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool extract_json_string(const char *obj, const char *key, char *dest,
|
||||||
|
size_t dest_size) {
|
||||||
|
const char *p = strstr(obj, key);
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
p = strchr(p, ':');
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
return parse_json_string_value(p + 1, dest, dest_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool extract_json_int(const char *obj, const char *key, int32_t *out) {
|
||||||
|
char *end = NULL;
|
||||||
|
const char *p = strstr(obj, key);
|
||||||
|
long value;
|
||||||
|
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
p = strchr(p, ':');
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
p = skip_ws(p + 1);
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
value = strtol(p, &end, 10);
|
||||||
|
if (end == p)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
*out = (int32_t)value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool extract_json_object_range(const char *obj, const char *key,
|
||||||
|
const char **start, const char **end) {
|
||||||
|
const char *p = strstr(obj, key);
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
p = strchr(p, ':');
|
||||||
|
if (!p)
|
||||||
|
return false;
|
||||||
|
p = skip_ws(p + 1);
|
||||||
|
if (!p || *p != '{')
|
||||||
|
return false;
|
||||||
|
*start = p;
|
||||||
|
*end = find_matching_brace(p);
|
||||||
|
return *end != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool parse_geom_object(const char *obj, const char *key, SessionRect *geom) {
|
||||||
|
const char *start = NULL, *end = NULL;
|
||||||
|
char *sub = NULL;
|
||||||
|
bool ok;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
if (!extract_json_object_range(obj, key, &start, &end))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
len = (size_t)(end - start + 1);
|
||||||
|
sub = ecalloc(len + 1, 1);
|
||||||
|
memcpy(sub, start, len);
|
||||||
|
|
||||||
|
ok = extract_json_int(sub, "\"x\"", &geom->x) &&
|
||||||
|
extract_json_int(sub, "\"y\"", &geom->y) &&
|
||||||
|
extract_json_int(sub, "\"width\"", &geom->width) &&
|
||||||
|
extract_json_int(sub, "\"height\"", &geom->height);
|
||||||
|
|
||||||
|
free(sub);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool parse_session_entry(const char *obj, SessionRestoreEntry *entry) {
|
||||||
|
int32_t tags = 0;
|
||||||
|
|
||||||
|
memset(entry, 0, sizeof(*entry));
|
||||||
|
|
||||||
|
if (!extract_json_string(obj, "\"app_id\"", entry->app_id,
|
||||||
|
sizeof(entry->app_id)))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
extract_json_string(obj, "\"title\"", entry->title, sizeof(entry->title));
|
||||||
|
extract_json_string(obj, "\"monitor\"", entry->monitor,
|
||||||
|
sizeof(entry->monitor));
|
||||||
|
extract_json_string(obj, "\"launch_command\"", entry->launch_command,
|
||||||
|
sizeof(entry->launch_command));
|
||||||
|
extract_json_int(obj, "\"pid\"", &entry->pid);
|
||||||
|
extract_json_int(obj, "\"tags\"", &tags);
|
||||||
|
entry->tags = (uint32_t)tags;
|
||||||
|
extract_json_int(obj, "\"is_floating\"", &entry->is_floating);
|
||||||
|
extract_json_int(obj, "\"is_fullscreen\"", &entry->is_fullscreen);
|
||||||
|
extract_json_int(obj, "\"is_minimized\"", &entry->is_minimized);
|
||||||
|
parse_geom_object(obj, "\"geom\"", &entry->geom);
|
||||||
|
parse_geom_object(obj, "\"float_geom\"", &entry->float_geom);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool load_pending_entries(void) {
|
||||||
|
char *path = session_file_path();
|
||||||
|
char *contents = NULL;
|
||||||
|
FILE *in = NULL;
|
||||||
|
const char *cursor;
|
||||||
|
bool loaded = false;
|
||||||
|
|
||||||
|
free_pending_entries();
|
||||||
|
if (!path)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (access(path, F_OK) != 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (!session_file_is_trusted(path)) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"mango session: refusing to restore from untrusted session file: %s\n",
|
||||||
|
path);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
in = fopen(path, "r");
|
||||||
|
if (!in)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (fseek(in, 0, SEEK_END) != 0)
|
||||||
|
goto cleanup;
|
||||||
|
long size = ftell(in);
|
||||||
|
if (size < 0 || fseek(in, 0, SEEK_SET) != 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
contents = ecalloc((size_t)size + 1, 1);
|
||||||
|
if (fread(contents, 1, (size_t)size, in) != (size_t)size)
|
||||||
|
goto cleanup;
|
||||||
|
contents[size] = '\0';
|
||||||
|
|
||||||
|
cursor = contents;
|
||||||
|
while ((cursor = strchr(cursor, '{')) != NULL) {
|
||||||
|
const char *end = find_matching_brace(cursor);
|
||||||
|
SessionRestoreEntry entry;
|
||||||
|
char *obj;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
if (!end)
|
||||||
|
break;
|
||||||
|
|
||||||
|
len = (size_t)(end - cursor + 1);
|
||||||
|
obj = ecalloc(len + 1, 1);
|
||||||
|
memcpy(obj, cursor, len);
|
||||||
|
|
||||||
|
if (parse_session_entry(obj, &entry)) {
|
||||||
|
PendingSessionEntry *new_entries = realloc(
|
||||||
|
pending_entries, sizeof(*pending_entries) * (pending_count + 1));
|
||||||
|
if (!new_entries) {
|
||||||
|
free(obj);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
pending_entries = new_entries;
|
||||||
|
pending_entries[pending_count].entry = entry;
|
||||||
|
pending_entries[pending_count].used = false;
|
||||||
|
pending_count++;
|
||||||
|
loaded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(obj);
|
||||||
|
cursor = end + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (in)
|
||||||
|
fclose(in);
|
||||||
|
free(contents);
|
||||||
|
free(path);
|
||||||
|
return loaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PendingSessionEntry *find_pending_match(const char *appid, const char *title,
|
||||||
|
const char *monitor) {
|
||||||
|
PendingSessionEntry *fallback = NULL;
|
||||||
|
PendingSessionEntry *monitor_match = NULL;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < pending_count; ++i) {
|
||||||
|
PendingSessionEntry *candidate = &pending_entries[i];
|
||||||
|
if (candidate->used)
|
||||||
|
continue;
|
||||||
|
if (strcmp(candidate->entry.app_id, appid) != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (title && title[0] != '\0' &&
|
||||||
|
strcmp(candidate->entry.title, title) == 0) {
|
||||||
|
if (monitor && monitor[0] != '\0' &&
|
||||||
|
strcmp(candidate->entry.monitor, monitor) == 0)
|
||||||
|
return candidate;
|
||||||
|
if (!fallback)
|
||||||
|
fallback = candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!monitor_match && monitor && monitor[0] != '\0' &&
|
||||||
|
strcmp(candidate->entry.monitor, monitor) == 0) {
|
||||||
|
monitor_match = candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fallback)
|
||||||
|
fallback = candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
return monitor_match ? monitor_match : fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool session_resolve_launch_command(SessionRestoreEntry *entry) {
|
||||||
|
const char *mapped_command;
|
||||||
|
|
||||||
|
if (!entry || entry->app_id[0] == '\0')
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* Explicit user mapping wins over best-effort persisted launch data. */
|
||||||
|
mapped_command = mango_session_lookup_launch_command(entry->app_id,
|
||||||
|
entry->title);
|
||||||
|
if (mapped_command && mapped_command[0] != '\0') {
|
||||||
|
strncpy(entry->launch_command, mapped_command,
|
||||||
|
sizeof(entry->launch_command) - 1);
|
||||||
|
entry->launch_command[sizeof(entry->launch_command) - 1] = '\0';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return entry->launch_command[0] != '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static void session_spawn_restore_entries(void) {
|
||||||
|
for (size_t i = 0; i < pending_count; ++i) {
|
||||||
|
SessionRestoreEntry *entry = &pending_entries[i].entry;
|
||||||
|
|
||||||
|
if (!session_resolve_launch_command(entry))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
mango_session_spawn_command(entry->launch_command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void session_init(void) { mango_session_spawn_tracker_init(); }
|
||||||
|
|
||||||
|
void session_shutdown(void) {
|
||||||
|
free_pending_entries();
|
||||||
|
restore_started = false;
|
||||||
|
mango_session_spawn_tracker_shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
void session_maybe_restore_startup(void) {
|
||||||
|
if (!session_is_enabled() || restore_started)
|
||||||
|
return;
|
||||||
|
|
||||||
|
restore_started = true;
|
||||||
|
if (!load_pending_entries())
|
||||||
|
return;
|
||||||
|
|
||||||
|
session_spawn_restore_entries();
|
||||||
|
}
|
||||||
|
|
||||||
|
void session_handle_client_mapped(Client *c) {
|
||||||
|
PendingSessionEntry *match;
|
||||||
|
const char *appid;
|
||||||
|
const char *title;
|
||||||
|
const char *monitor;
|
||||||
|
|
||||||
|
if (!restore_started || pending_count == 0 || !c)
|
||||||
|
return;
|
||||||
|
|
||||||
|
appid = mango_session_client_appid(c);
|
||||||
|
if (!appid || appid[0] == '\0' || strcmp(appid, "broken") == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
title = mango_session_client_title(c);
|
||||||
|
monitor = mango_session_client_monitor(c);
|
||||||
|
match = find_pending_match(appid, title, monitor);
|
||||||
|
if (!match)
|
||||||
|
return;
|
||||||
|
|
||||||
|
mango_session_remember_client_launch_command(c, match->entry.launch_command);
|
||||||
|
mango_session_apply_restore_entry(c, &match->entry);
|
||||||
|
match->used = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void session_handle_client_destroyed(Client *c) {
|
||||||
|
(void)c;
|
||||||
|
}
|
||||||
|
|
||||||
|
void session_save_now(bool is_final_shutdown) {
|
||||||
|
FILE *out = NULL;
|
||||||
|
char *dir = NULL, *tmp_path = NULL, *final_path = NULL;
|
||||||
|
int32_t count = 0;
|
||||||
|
|
||||||
|
if (!session_is_enabled())
|
||||||
|
return;
|
||||||
|
|
||||||
|
dir = session_data_dir();
|
||||||
|
if (!dir || !mkdir_p(dir))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
tmp_path = string_printf("%s/session.json.tmp", dir);
|
||||||
|
final_path = string_printf("%s/session.json", dir);
|
||||||
|
if (!tmp_path || !final_path)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
out = fopen(tmp_path, "w");
|
||||||
|
if (!out)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
count = mango_session_write_snapshot(out);
|
||||||
|
if (fclose(out) != 0) {
|
||||||
|
out = NULL;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
out = NULL;
|
||||||
|
|
||||||
|
if (count <= 0 && is_final_shutdown) {
|
||||||
|
unlink(tmp_path);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rename(tmp_path, final_path) != 0)
|
||||||
|
unlink(tmp_path);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
if (out)
|
||||||
|
fclose(out);
|
||||||
|
free(dir);
|
||||||
|
free(tmp_path);
|
||||||
|
free(final_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool session_is_enabled(void) { return mango_session_is_config_enabled() != 0; }
|
||||||
|
|
||||||
|
bool session_is_restorable_client(Client *c) {
|
||||||
|
(void)c;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
39
src/session/session.h
Normal file
39
src/session/session.h
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
#ifndef SESSION_H
|
||||||
|
#define SESSION_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct Client Client;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int32_t x;
|
||||||
|
int32_t y;
|
||||||
|
int32_t width;
|
||||||
|
int32_t height;
|
||||||
|
} SessionRect;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char app_id[256];
|
||||||
|
char title[512];
|
||||||
|
char monitor[128];
|
||||||
|
char launch_command[1024];
|
||||||
|
int32_t pid;
|
||||||
|
uint32_t tags;
|
||||||
|
int32_t is_floating;
|
||||||
|
int32_t is_fullscreen;
|
||||||
|
int32_t is_minimized;
|
||||||
|
SessionRect geom;
|
||||||
|
SessionRect float_geom;
|
||||||
|
} SessionRestoreEntry;
|
||||||
|
|
||||||
|
void session_init(void);
|
||||||
|
void session_shutdown(void);
|
||||||
|
void session_maybe_restore_startup(void);
|
||||||
|
void session_handle_client_mapped(Client *c);
|
||||||
|
void session_handle_client_destroyed(Client *c);
|
||||||
|
void session_save_now(bool is_final_shutdown);
|
||||||
|
bool session_is_enabled(void);
|
||||||
|
bool session_is_restorable_client(Client *c);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue