[PATCH kanshi] ctl: status command
Export this patch
---
I think this feature might be a useful. This is the first piece of C
code I wrote in a few years, so I hope I didn't do any too bad mistakes.
ctl.c | 25 +++++++++++++++++++++++--
ipc.c | 15 +++++++++++++++
2 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/ctl.c b/ctl.c
index e5ca870..3e1ebad 100644
--- a/ctl.c
+++ b/ctl.c
@@ -14,7 +14,25 @@ static void usage(void) {
"\n"
"Commands:\n"
" reload Reload the configuration file\n"
- " switch <profile> Switch to another profile\n");
+ " switch <profile> Switch to another profile\n"
+ " status Show current profile\n");
+}
+
+static long handle_status_call_done(VarlinkConnection *connection, const char *error,
+ VarlinkObject *parameters, uint64_t flags, void *userdata) {
+ if (error != NULL) {
+ fprintf(stderr, "Error: could not get current profile\n");
+ exit(EXIT_FAILURE);
+ }
+
+ const char *word;
+ if (varlink_object_get_string(parameters, "profile", &word) < 0) {
+ fprintf(stderr, "Error: could not get current profile\n");
+ exit(EXIT_FAILURE);
+ } else {
+ fprintf(stderr, "Current profile: %s\n", word);
+ return varlink_connection_close(connection);
+ }
}
static long handle_call_done(VarlinkConnection *connection, const char *error,
@@ -105,7 +123,10 @@ int main(int argc, char *argv[]) {
ret = varlink_connection_call(connection,
"fr.emersion.kanshi.Switch", params, 0, handle_call_done, NULL);
varlink_object_unref(params);
- } else {
+ } else if (strcmp(command, "status") == 0) {
+ ret = varlink_connection_call(connection,
+ "fr.emersion.kanshi.Status", NULL, 0, handle_status_call_done, NULL);
+ } else {
fprintf(stderr, "invalid command: %s\n", argv[1]);
usage();
return EXIT_FAILURE;
diff --git a/ipc.c b/ipc.c
index fe61f63..9db008d 100644
--- a/ipc.c
+++ b/ipc.c
@@ -37,6 +37,19 @@ static long handle_reload(VarlinkService *service, VarlinkCall *call,
return 0;
}
+static long handle_status(VarlinkService *service, VarlinkCall *call,
+ VarlinkObject *parameters, uint64_t flags, void *userdata) {
+ struct kanshi_state *state = userdata;
+
+ VarlinkObject *res_parameters;
+ varlink_object_new(&res_parameters);
+ varlink_object_set_string(res_parameters, "profile", state->current_profile->name);
+
+ varlink_call_reply(call, res_parameters, 0);
+
+ return 0;
+}
+
static long handle_switch(VarlinkService *service, VarlinkCall *call,
VarlinkObject *parameters, uint64_t flags, void *userdata) {
struct kanshi_state *state = userdata;
@@ -103,12 +116,14 @@ int kanshi_init_ipc(struct kanshi_state *state, int listen_fd) {
const char *interface = "interface fr.emersion.kanshi\n"
"method Reload() -> ()\n"
+ "method Status() -> (profile: string)\n"
"method Switch(profile: string) -> ()\n"
"error ProfileNotFound()\n"
"error ProfileNotMatched()\n"
"error ProfileNotApplied()\n";
long result = varlink_service_add_interface(service, interface,
+ "Status", handle_status, state,
"Reload", handle_reload, state,
"Switch", handle_switch, state,
NULL);
--
2.46.0
kanshi/patches/.build.yml: SUCCESS in 17s
[ctl: status command][0] from [Adam Chovanec][1]
[0]: https://lists.sr.ht/~emersion/public-inbox/patches/54255
[1]: mailto:git@adamchovanec.cz
✓ #1292129 SUCCESS kanshi/patches/.build.yml https://builds.sr.ht/~emersion/job/1292129