From 39733d69d8923e4b66b67f089e64ba0b4dc90101 Mon Sep 17 00:00:00 2001 From: c2h4ie Date: Sun, 17 Nov 2024 21:26:28 +0100 Subject: [PATCH] add hypr switch --- hyprSwitch.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 hyprSwitch.c diff --git a/hyprSwitch.c b/hyprSwitch.c new file mode 100644 index 0000000..8bb7e04 --- /dev/null +++ b/hyprSwitch.c @@ -0,0 +1,80 @@ +#include +#include + +#define PREV_WORKSPACE_FILE "/tmp/hypr_previous_workspace" + +int get_current_workspace() { + FILE* fp = popen("hyprctl activeworkspace -j | jq '.id'", "r"); + if (!fp) { + perror("Failed to run hyprctl command"); + return -1; + } + + int current_workspace; + if (fscanf(fp, "%d", ¤t_workspace) != 1) { + fprintf(stderr, "Error reading current workspace ID\n"); + pclose(fp); + return -1; + } + + pclose(fp); + return current_workspace; +} + +void change_workspace(int workspace) { + char command[64]; + snprintf(command, sizeof(command), "hyprctl dispatch workspace %d", workspace); + system(command); +} + +int get_previous_workspace() { + FILE* file = fopen(PREV_WORKSPACE_FILE, "r"); + if (!file) + return -1; + + int prev_workspace; + if (fscanf(file, "%d", &prev_workspace) != 1) { + fclose(file); + return -1; + } + + fclose(file); + return prev_workspace; +} + +void save_previous_workspace(int workspace) { + FILE* file = fopen(PREV_WORKSPACE_FILE, "w"); + if (!file) { + perror("Failed to open previous workspace file for writing"); + return; + } + + fprintf(file, "%d", workspace); + fclose(file); +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + int target_workspace = atoi(argv[1]); + int current_workspace = get_current_workspace(); + if (current_workspace == -1) + return 1; + + int previous_workspace = get_previous_workspace(); + if (previous_workspace == -1) + previous_workspace = current_workspace; + + if (target_workspace == current_workspace) { + change_workspace(previous_workspace); + } else { + change_workspace(target_workspace); + } + + save_previous_workspace(current_workspace); + + return 0; +}