diff --git a/oguri.c b/oguri.c index bcf7697..3ae6d48 100644 --- a/oguri.c +++ b/oguri.c @@ -27,6 +27,7 @@ #include "animation.h" #include "config.h" #include "output.h" +#include "oguri_ipc.h" // // Signal handler @@ -147,19 +148,41 @@ static void oguri_ipc_handle_command( } FILE * ipc_config = fdopen(config_fd, "r"); - int loaded = load_config(oguri, ipc_config, "ipc"); - if (loaded == -1) { - // TODO: Expose the error messages instead of writing them to oguri's - // stderr, where they will go nowhere. - if (write(client, "Invalid configuration\n", 23) < 0) { - fprintf(stderr, "Error replying to ipc command\n"); - } - } - - // TODO: If there was an error reading the config, we might have partially - // applied it. We're going to reconfig so that nothing gets out of sync - // internally, but this should be fixed in the config handlers. - oguri_reconfigure(oguri); + enum oguri_ipc_command cmd = OGURI_NOT_IPC_COMMAND; + cmd = fgetc(ipc_config); + if (cmd == OGURI_NOT_IPC_COMMAND) + { + fprintf(stderr, "Did not receive ipc command\n"); + } + else + { + switch (cmd) + { + case OGURI_IPC_CONFIGURE: + int loaded = load_config(oguri, ipc_config, "ipc"); + if (loaded == -1) { + // TODO: Expose the error messages instead of writing them to oguri's + // stderr, where they will go nowhere. + if (write(client, "Invalid configuration\n", 23) < 0) { + fprintf(stderr, "Error replying to ipc command\n"); + } + } + + // TODO: If there was an error reading the config, we might have partially + // applied it. We're going to reconfig so that nothing gets out of sync + // internally, but this should be fixed in the config handlers. + oguri_reconfigure(oguri, false); + break; + case OGURI_IPC_RELOAD_IMAGES: + oguri_reconfigure(oguri, true); + break; + default: + fprintf(stderr, "Unknown ipc command: %d\n", cmd); + break; + } + } + // close FILE, if we didn't do that before + if (fileno(ipc_config) >= 0) fclose(ipc_config); close(client); oguri->events[OGURI_IPC_CLIENT_EVENT].fd = -1; @@ -179,7 +202,7 @@ static void oguri_ipc_handle_command( // // This is not particularly efficient, but it's extremely simple which makes it // unlikely to introduce bugs. We also don't have that many outputs. -void oguri_reconfigure(struct oguri_state * oguri) { +void oguri_reconfigure(struct oguri_state * oguri, bool reload_cache) { // Return all outputs to the idle list. struct oguri_animation * anim; wl_list_for_each(anim, &oguri->animations, link) { @@ -220,7 +243,7 @@ void oguri_reconfigure(struct oguri_state * oguri) { } } - if (!found_anim) { + if (!found_anim || reload_cache) { // No animation exists, so make one. Note that this may still // fail, in which case this output will become idle. // TODO: It would be better to get any possible failures out of @@ -342,7 +365,7 @@ int main(int argc, char * argv[]) { return 1; } - oguri_reconfigure(&oguri); + oguri_reconfigure(&oguri, false); oguri.run = true; while (oguri.run) { diff --git a/oguri.h b/oguri.h index 2e74962..d493474 100644 --- a/oguri.h +++ b/oguri.h @@ -38,6 +38,6 @@ struct oguri_state { struct wl_list animations; // oguri_animation::link }; -void oguri_reconfigure(struct oguri_state * oguri); +void oguri_reconfigure(struct oguri_state * oguri, bool reload_cache); #endif diff --git a/oguri_ipc.h b/oguri_ipc.h new file mode 100644 index 0000000..b0e6179 --- /dev/null +++ b/oguri_ipc.h @@ -0,0 +1,10 @@ +#ifndef OGURI_IPC_H +#define OGURI_IPC_H + +enum oguri_ipc_command { + OGURI_NOT_IPC_COMMAND = 0, + OGURI_IPC_CONFIGURE, + OGURI_IPC_RELOAD_IMAGES, +}; + +#endif diff --git a/ogurictl.c b/ogurictl.c index 264692d..347a454 100644 --- a/ogurictl.c +++ b/ogurictl.c @@ -8,17 +8,21 @@ #include #include #include +#include "oguri_ipc.h" static const char usage[] = - "Usage: ogurictl output NAME []\n" + "Usage: ogurictl COMMAND\n" " ogurictl [--help] [--version]\n" "\n" - "Output options:\n" - " --anchor Sides to which the image should be anchored\n" - " --filter Scaling filter to apply to the image\n" - " --image Path to the image to show on this output\n" - " --scaling-mode Method used to fit the image to the output\n" + "Available commands:\n" + " output NAME []\n" + " --anchor Sides to which the image should be anchored\n" + " --filter Scaling filter to apply to the image\n" + " --image Path to the image to show on this output\n" + " --scaling-mode Method used to fit the image to the output\n" + "\n" + " reload\n" "\n" "General options:\n" " -V, --version Show the version of oguri\n" @@ -91,6 +95,11 @@ int handle_output(int argc, char * argv[], char ** buffer, unsigned long * buffe return 0; } +static enum oguri_ipc_command parse_subcommand(char * subcommand) { + if (strcmp(subcommand, "output") == 0) return OGURI_IPC_CONFIGURE; + if (strcmp(subcommand, "reload") == 0) return OGURI_IPC_RELOAD_IMAGES; + return OGURI_NOT_IPC_COMMAND; +} int main(int argc, char * argv[]) { int opt_char, opt_index = -1; @@ -118,13 +127,19 @@ int main(int argc, char * argv[]) { // Since getopt state is global, we can just continue working in a // command-specific function. int subcommand_return = 0; - if (strcmp(subcommand, "output") == 0) { - subcommand_return = handle_output(argc, argv, &buffer, &buffer_size); - } - else { - fprintf(stderr, "Unknown command '%s'\n\n%s", subcommand, usage); - free(buffer); - return 1; + char * cmd = calloc(1, sizeof(char)); + cmd [0] = parse_subcommand(subcommand); + + switch (cmd[0]) { + case OGURI_IPC_CONFIGURE: + subcommand_return = handle_output(argc, argv, &buffer, &buffer_size); + break; + case OGURI_IPC_RELOAD_IMAGES: + break; + default: + fprintf(stderr, "Unknown command '%s'\n\n%s", subcommand, usage); + free(buffer); + return 1; } if (subcommand_return != 0) { @@ -166,8 +181,12 @@ int main(int argc, char * argv[]) { return 1; } + if (send(sock_fd, cmd, 1, 0) == -1) { + perror("Unable to send command type to oguri"); + goto close_err; + } if (send(sock_fd, buffer, strnlen(buffer, buffer_size - 1), 0) == -1) { - perror("Unable to send command to oguri"); + perror("Unable to send command data to oguri"); goto close_err; } diff --git a/output.c b/output.c index 04e9e55..082c866 100644 --- a/output.c +++ b/output.c @@ -212,7 +212,7 @@ struct oguri_output * oguri_output_create( wl_display_roundtrip(oguri->display); wl_list_insert(oguri->idle_outputs.prev, &output->link); - oguri_reconfigure(oguri); + oguri_reconfigure(oguri, false); return output; }