add hypr switch

This commit is contained in:
c2h4ie 2024-11-17 21:26:28 +01:00
parent 5c9bdcd1b5
commit 39733d69d8
Signed by untrusted user who does not match committer: etenie
GPG Key ID: 02B622C4DE405C2B
1 changed files with 80 additions and 0 deletions

80
hyprSwitch.c Normal file
View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#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", &current_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 <workspace_number>\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;
}