Ejemplo n.º 1
0
Archivo: bar.c Proyecto: SirCmpwn/sway
struct cmd_results *cmd_bar(int argc, char **argv) {
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) {
		return error;
	}

	if (config->reading && strcmp("{", argv[0]) != 0) {
		return cmd_results_new(CMD_INVALID, "bar",
				"Expected '{' at start of bar config definition.");
	}

	if (!config->reading) {
		if (argc > 1) {
			if (strcasecmp("mode", argv[0]) == 0) {
				return bar_cmd_mode(argc-1, argv + 1);
			}

			if (strcasecmp("hidden_state", argv[0]) == 0) {
				return bar_cmd_hidden_state(argc-1, argv + 1);
			}
		}

		return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file.");
	}

	// Create new bar with default values
	struct bar_config *bar = default_bar_config();

	// set bar id
	int i;
	for (i = 0; i < config->bars->length; ++i) {
		if (bar == config->bars->items[i]) {
			const int len = 5 + numlen(i); // "bar-" + i + \0
			bar->id = malloc(len * sizeof(char));
			snprintf(bar->id, len, "bar-%d", i);
			break;
		}
	}

	// Set current bar
	config->current_bar = bar;
	sway_log(L_DEBUG, "Configuring bar %s", bar->id);
	return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL);
}
Ejemplo n.º 2
0
struct cmd_results *cmd_bar(int argc, char **argv) {
	struct cmd_results *error = NULL;
	if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 2))) {
		return error;
	}

	bool spawn = false;
	struct bar_config *bar = NULL;
	if (strcmp(argv[0], "id") != 0 && is_subcommand(argv[1])) {
		for (int i = 0; i < config->bars->length; ++i) {
			struct bar_config *item = config->bars->items[i];
			if (strcmp(item->id, argv[0]) == 0) {
				sway_log(SWAY_DEBUG, "Selecting bar: %s", argv[0]);
				bar = item;
				break;
			}
		}
		if (!bar) {
			spawn = !config->reading;
			sway_log(SWAY_DEBUG, "Creating bar: %s", argv[0]);
			bar = default_bar_config();
			if (!bar) {
				return cmd_results_new(CMD_FAILURE,
						"Unable to allocate bar state");
			}

			bar->id = strdup(argv[0]);
		}
		config->current_bar = bar;
		++argv; --argc;
	}

	if (!config->current_bar && config->reading) {
		// Create new bar with default values
		struct bar_config *bar = default_bar_config();
		if (!bar) {
			return cmd_results_new(CMD_FAILURE,
					"Unable to allocate bar state");
		}

		// set bar id
		const int len = snprintf(NULL, 0, "bar-%d", config->bars->length - 1) + 1;
		bar->id = malloc(len * sizeof(char));
		if (bar->id) {
			snprintf(bar->id, len, "bar-%d", config->bars->length - 1);
		} else {
			return cmd_results_new(CMD_FAILURE, "Unable to allocate bar ID");
		}

		// Set current bar
		config->current_bar = bar;
		sway_log(SWAY_DEBUG, "Creating bar %s", bar->id);
	}

	if (find_handler(argv[0], bar_config_handlers,
				sizeof(bar_config_handlers))) {
		if (config->reading) {
			return config_subcommand(argv, argc, bar_config_handlers,
					sizeof(bar_config_handlers));
		} else if (spawn) {
			for (int i = config->bars->length - 1; i >= 0; i--) {
				struct bar_config *bar = config->bars->items[i];
				if (bar == config->current_bar) {
					list_del(config->bars, i);
					free_bar_config(bar);
					break;
				}
			}
		}
		return cmd_results_new(CMD_INVALID,
				"Can only be used in the config file.");
	}

	struct cmd_results *res =
		config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
	if (!config->reading) {
		if (spawn) {
			load_swaybar(config->current_bar);
		}
		config->current_bar = NULL;
	}
	return res;
}