Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new directive include_one #8099

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions include/sway/commands.h
Expand Up @@ -144,6 +144,7 @@ sway_cmd cmd_fullscreen;
sway_cmd cmd_gaps;
sway_cmd cmd_hide_edge_borders;
sway_cmd cmd_include;
sway_cmd cmd_include_one;
sway_cmd cmd_inhibit_idle;
sway_cmd cmd_input;
sway_cmd cmd_seat;
Expand Down
5 changes: 4 additions & 1 deletion include/sway/config.h
Expand Up @@ -614,7 +614,10 @@ bool load_main_config(const char *path, bool is_active, bool validating);
* Loads an included config. Can only be used after load_main_config.
*/
void load_include_configs(const char *path, struct sway_config *config,
struct swaynag_instance *swaynag);
struct swaynag_instance *swaynag, bool flag);

void load_include_one_config(const char *paths, struct sway_config *config,
struct swaynag_instance *swaynag);

/**
* Reads the config from the given FILE.
Expand Down
1 change: 1 addition & 0 deletions sway/commands.c
Expand Up @@ -103,6 +103,7 @@ static const struct cmd_handler handlers[] = {
static const struct cmd_handler config_handlers[] = {
{ "default_orientation", cmd_default_orientation },
{ "include", cmd_include },
{ "include_one", cmd_include_one },
{ "primary_selection", cmd_primary_selection },
{ "swaybg_command", cmd_swaybg_command },
{ "swaynag_command", cmd_swaynag_command },
Expand Down
2 changes: 1 addition & 1 deletion sway/commands/include.c
Expand Up @@ -8,7 +8,7 @@ struct cmd_results *cmd_include(int argc, char **argv) {
}

// We don't care if the included config(s) fails to load.
load_include_configs(argv[0], config, &config->swaynag_config_errors);
load_include_configs(argv[0], config, &config->swaynag_config_errors, false);

return cmd_results_new(CMD_SUCCESS, NULL);
}
14 changes: 14 additions & 0 deletions sway/commands/include_one.c
@@ -0,0 +1,14 @@
#include "sway/commands.h"
#include "sway/config.h"

struct cmd_results *cmd_include_one(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "include_one", EXPECTED_EQUAL_TO, 1))) {
return error;
}

// We don't care if the included config(s) fails to load.
load_include_one_config(argv[0], config, &config->swaynag_config_errors);

return cmd_results_new(CMD_SUCCESS, NULL);
}
43 changes: 40 additions & 3 deletions sway/config.c
Expand Up @@ -549,8 +549,22 @@ bool load_main_config(const char *file, bool is_active, bool validating) {
return success;
}

static char *extract_filename(const char *path) {
char *filename = strrchr(path, '/');
if (filename == NULL) {
return NULL;
}
return filename;
}

static bool same_file(const char *path1, const char *path2) {
const char *file1 = extract_filename(path1);
const char *file2 = extract_filename(path2);
return (strcmp(file1, file2) == 0);
}

static bool load_include_config(const char *path, const char *parent_dir,
struct sway_config *config, struct swaynag_instance *swaynag) {
struct sway_config *config, struct swaynag_instance *swaynag, bool flag) {
// save parent config
const char *parent_config = config->current_config_path;

Expand Down Expand Up @@ -588,6 +602,14 @@ static bool load_include_config(const char *path, const char *parent_dir,
free(real_path);
return false;
}
// check if function is called from include_one
if (flag == true && same_file(real_path, old_path) == true) {
sway_log(SWAY_DEBUG,
"%s already included once, won't be included again.",
real_path);
free(real_path);
return false;
}
}

config->current_config_path = real_path;
Expand All @@ -607,7 +629,7 @@ static bool load_include_config(const char *path, const char *parent_dir,
}

void load_include_configs(const char *path, struct sway_config *config,
struct swaynag_instance *swaynag) {
struct swaynag_instance *swaynag, bool flag) {
char *wd = getcwd(NULL, 0);
char *parent_path = strdup(config->current_config_path);
const char *parent_dir = dirname(parent_path);
Expand All @@ -622,7 +644,7 @@ void load_include_configs(const char *path, struct sway_config *config,
char **w = p.we_wordv;
size_t i;
for (i = 0; i < p.we_wordc; ++i) {
load_include_config(w[i], parent_dir, config, swaynag);
load_include_config(w[i], parent_dir, config, swaynag, flag);
}
wordfree(&p);
}
Expand All @@ -636,6 +658,21 @@ void load_include_configs(const char *path, struct sway_config *config,
free(wd);
}


void load_include_one_config(const char *paths, struct sway_config *config,
struct swaynag_instance *swaynag) {
char *p = strdup(paths);
char *path = strtok(p, " ");
while (path != NULL) {
load_include_configs(path, config, swaynag, true);
path = strtok(NULL, " ");
}
goto cleanup;

cleanup:
free(p);
}

void run_deferred_commands(void) {
if (!config->cmd_queue->length) {
return;
Expand Down
1 change: 1 addition & 0 deletions sway/meson.build
Expand Up @@ -74,6 +74,7 @@ sway_sources = files(
'commands/max_render_time.c',
'commands/opacity.c',
'commands/include.c',
'commands/include_one.c',
'commands/input.c',
'commands/layout.c',
'commands/mode.c',
Expand Down