<commit_before>#include <stdlib.h>
#include <stdbool.h>
#include <strings.h>
#include <string.h>
#include "config.h"
#include "stringop.h"
#include "container.h"
#include "workspace.h"
#include "focus.h"
#include "layout.h"
#include "input_state.h"
#include "log.h"

#define ASSERT_NONNULL(PTR) \
	sway_assert (PTR, #PTR "must be non-null")

static swayc_t *new_swayc(enum swayc_types type) {
	swayc_t *c = calloc(1, sizeof(swayc_t));
	c->handle = -1;
	c->gaps = -1;
	c->layout = L_NONE;
	c->type = type;
	if (type != C_VIEW) {
		c->children = create_list();
	}
	return c;
}

static void free_swayc(swayc_t *cont) {
	if (!ASSERT_NONNULL(cont)) {
		return;
	}
	if (cont->children) {
		// remove children until there are no more, free_swayc calls
		// remove_child, which removes child from this container
		while (cont->children->length) {
			free_swayc(cont->children->items[0]);
		}
		list_free(cont->children);
	}
	if (cont->floating) {
		while (cont->floating->length) {
			free_swayc(cont->floating->items[0]);
		}
		list_free(cont->floating);
	}
	if (cont->parent) {
		remove_child(cont);
	}
	if (cont->name) {
		free(cont->name);
	}
	if (cont->class) {
		free(cont->class);
	}
	if (cont->app_id) {
		free(cont->app_id);
	}
	free(cont);
}

// New containers

swayc_t *new_output(wlc_handle handle) {
	const struct wlc_size *size = wlc_output_get_resolution(handle);
	const char *name = wlc_output_get_name(handle);
	// Find current outputs to see if this already exists
	{
		int i, len = root_container.children->length;
		for (i = 0; i < len; ++i) {
			swayc_t *op = root_container.children->items[i];
			const char *op_name = op->name;
			if (op_name && name && strcmp(op_name, name) == 0) {
				sway_log(L_DEBUG, "restoring output %lu:%s", handle, op_name);
				return op;
			}
		}
	}

	sway_log(L_DEBUG, "New output %lu:%s", handle, name);

	struct output_config *oc = NULL;
	int i;
	for (i = 0; i < config->output_configs->length; ++i) {
		oc = config->output_configs->items[i];
		if (strcasecmp(name, oc->name) == 0) {
			sway_log(L_DEBUG, "Matched output config for %s", name);
			break;
		}
		if (strcasecmp("*", oc->name) == 0) {
			sway_log(L_DEBUG, "Matched wildcard output config for %s", name);
			break;
		}
	}

	if (oc && !oc->enabled) {
		return NULL;
	}

	swayc_t *output = new_swayc(C_OUTPUT);
	output->handle = handle;
	output->name = name ? strdup(name) : NULL;
	output->width = size->w;
	output->height = size->h;

	apply_output_config(oc, output);
	add_child(&root_container, output);

	// Create workspace
	char *ws_name = NULL;
	if (name) {
		for (i = 0; i < config->workspace_outputs->length; ++i) {
			struct workspace_output *wso = config->workspace_outputs->items[i];
			if (strcasecmp(wso->output, name) == 0) {
				sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output);
				// Check if any other workspaces are using this name
				if (workspace_by_name(wso->workspace)) {
					sway_log(L_DEBUG, "But it's already taken");
					break;
				}
				sway_log(L_DEBUG, "So we're going to use it");<commit_msg>Fix using last output config if none matched<commit_after>#include <stdlib.h>
#include <stdbool.h>
#include <strings.h>
#include <string.h>
#include "config.h"
#include "stringop.h"
#include "container.h"
#include "workspace.h"
#include "focus.h"
#include "layout.h"
#include "input_state.h"
#include "log.h"

#define ASSERT_NONNULL(PTR) \
	sway_assert (PTR, #PTR "must be non-null")

static swayc_t *new_swayc(enum swayc_types type) {
	swayc_t *c = calloc(1, sizeof(swayc_t));
	c->handle = -1;
	c->gaps = -1;
	c->layout = L_NONE;
	c->type = type;
	if (type != C_VIEW) {
		c->children = create_list();
	}
	return c;
}

static void free_swayc(swayc_t *cont) {
	if (!ASSERT_NONNULL(cont)) {
		return;
	}
	if (cont->children) {
		// remove children until there are no more, free_swayc calls
		// remove_child, which removes child from this container
		while (cont->children->length) {
			free_swayc(cont->children->items[0]);
		}
		list_free(cont->children);
	}
	if (cont->floating) {
		while (cont->floating->length) {
			free_swayc(cont->floating->items[0]);
		}
		list_free(cont->floating);
	}
	if (cont->parent) {
		remove_child(cont);
	}
	if (cont->name) {
		free(cont->name);
	}
	if (cont->class) {
		free(cont->class);
	}
	if (cont->app_id) {
		free(cont->app_id);
	}
	free(cont);
}

// New containers

swayc_t *new_output(wlc_handle handle) {
	const struct wlc_size *size = wlc_output_get_resolution(handle);
	const char *name = wlc_output_get_name(handle);
	// Find current outputs to see if this already exists
	{
		int i, len = root_container.children->length;
		for (i = 0; i < len; ++i) {
			swayc_t *op = root_container.children->items[i];
			const char *op_name = op->name;
			if (op_name && name && strcmp(op_name, name) == 0) {
				sway_log(L_DEBUG, "restoring output %lu:%s", handle, op_name);
				return op;
			}
		}
	}

	sway_log(L_DEBUG, "New output %lu:%s", handle, name);

	struct output_config *oc = NULL;
	int i;
	for (i = 0; i < config->output_configs->length; ++i) {
		struct output_config *cur = config->output_configs->items[i];
		if (strcasecmp(name, cur->name) == 0) {
			sway_log(L_DEBUG, "Matched output config for %s", name);
			oc = cur;
			break;
		}
		if (strcasecmp("*", cur->name) == 0) {
			sway_log(L_DEBUG, "Matched wildcard output config for %s", name);
			oc = cur;
			break;
		}
	}

	if (oc && !oc->enabled) {
		return NULL;
	}

	swayc_t *output = new_swayc(C_OUTPUT);
	output->handle = handle;
	output->name = name ? strdup(name) : NULL;
	output->width = size->w;
	output->height = size->h;

	apply_output_config(oc, output);
	add_child(&root_container, output);

	// Create workspace
	char *ws_name = NULL;
	if (name) {
		for (i = 0; i < config->workspace_outputs->length; ++i) {
			struct workspace_output *wso = config->workspace_outputs->items[i];
			if (strcasecmp(wso->output, name) == 0) {
				sway_log(L_DEBUG, "Matched workspace to output: %s for %s", wso->workspace, wso->output);
				// Check if any other workspaces are using this name
				if (workspace_by_name(wso->workspace)) {
					sway_log(L_DEBUG, "But it's already taken");
					break;
				}
				sway_log(L_DEBUG, "So we're going to use it");
{'fraction_non_alphanumeric': 0.163989720526823, 'fraction_numerical': 0.0038548024413748794, 'mean_word_length': 3.9108832807570977, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4071729', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2436, 'n_words': 572}