misc/hyprSwitch.c

81 lines
1.7 KiB
C

#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;
}