feat: use socket instead of hyprctl
This commit is contained in:
@@ -1,9 +1,72 @@
|
|||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <rlgl.h>
|
#include <rlgl.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define WIDTH 1920
|
#define WIDTH 1920
|
||||||
#define HEIGHT 1080
|
#define HEIGHT 1080
|
||||||
|
|
||||||
|
char *hyprland_socket_path;
|
||||||
|
|
||||||
|
Vector2 hyprland_get_cursor_position()
|
||||||
|
{
|
||||||
|
Vector2 pos = {0};
|
||||||
|
|
||||||
|
int sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if (sock_fd == -1)
|
||||||
|
{
|
||||||
|
perror("socket");
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_un addr = {0};
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
strncpy(addr.sun_path, hyprland_socket_path, sizeof(addr.sun_path) - 1);
|
||||||
|
|
||||||
|
if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
|
||||||
|
{
|
||||||
|
printf("Using hyprland socket: %s\n", hyprland_socket_path);
|
||||||
|
perror("connect");
|
||||||
|
close(sock_fd);
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *message = "/cursorpos";
|
||||||
|
if (send(sock_fd, message, strlen(message), 0) == -1)
|
||||||
|
{
|
||||||
|
perror("send");
|
||||||
|
close(sock_fd);
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buffer[256] = {0};
|
||||||
|
ssize_t bytes_received = recv(sock_fd, buffer, sizeof(buffer) - 1, 0);
|
||||||
|
if (bytes_received == -1)
|
||||||
|
{
|
||||||
|
perror("recv");
|
||||||
|
close(sock_fd);
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
// format is x, y so parse here
|
||||||
|
buffer[bytes_received] = '\0';
|
||||||
|
|
||||||
|
if (sscanf(buffer, "%f, %f", &pos.x, &pos.y) != 2)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to parse cursor position\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
close(sock_fd);
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
Texture2D load_texture_from_file(const char *file_path)
|
Texture2D load_texture_from_file(const char *file_path)
|
||||||
{
|
{
|
||||||
Texture2D texture = LoadTexture(file_path);
|
Texture2D texture = LoadTexture(file_path);
|
||||||
@@ -16,10 +79,13 @@ Texture2D load_texture_from_file(const char *file_path)
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
InitWindow(WIDTH, HEIGHT, "wallpaper");
|
InitWindow(WIDTH, HEIGHT, "pihkaal-wallpaper");
|
||||||
SetWindowState(FLAG_WINDOW_RESIZABLE);
|
SetWindowState(FLAG_WINDOW_RESIZABLE);
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
|
// initialize hyprland socket path
|
||||||
|
hyprland_socket_path = strdup(TextFormat("%s/hypr/%s/.socket.sock", getenv("XDG_RUNTIME_DIR"), getenv("HYPRLAND_INSTANCE_SIGNATURE")));
|
||||||
|
|
||||||
// initialize textures
|
// initialize textures
|
||||||
Texture2D distanceFieldTex = load_texture_from_file("resources/textures/background_distance_field.png");
|
Texture2D distanceFieldTex = load_texture_from_file("resources/textures/background_distance_field.png");
|
||||||
Texture2D backgroundTex = load_texture_from_file("resources/textures/background_transparent.png");
|
Texture2D backgroundTex = load_texture_from_file("resources/textures/background_transparent.png");
|
||||||
@@ -60,8 +126,8 @@ int main(void)
|
|||||||
SetShaderValue(shader, resolutionLoc, &resolution, SHADER_UNIFORM_VEC2);
|
SetShaderValue(shader, resolutionLoc, &resolution, SHADER_UNIFORM_VEC2);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 mousePos = GetMousePosition();
|
Vector2 hyprCursor = hyprland_get_cursor_position();
|
||||||
mousePos.y = screenHeight - mousePos.y;
|
Vector2 mousePos = {hyprCursor.x, (float)screenHeight - hyprCursor.y};
|
||||||
SetShaderValue(shader, mousePosLoc, &mousePos, SHADER_UNIFORM_VEC2);
|
SetShaderValue(shader, mousePosLoc, &mousePos, SHADER_UNIFORM_VEC2);
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
@@ -87,5 +153,7 @@ int main(void)
|
|||||||
UnloadShader(shader);
|
UnloadShader(shader);
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
|
||||||
|
free(hyprland_socket_path);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user