zzxyb / wsm

lychee wayland surface manager for pc and mobile devices
MIT License
3 stars 3 forks source link

remove notify dbus #8

Open zzxyb opened 4 months ago

zzxyb commented 4 months ago

Use LINUX Signals instead of dbus to notify another process that wsm is ready,maybe SIGUSR1 the return is as follows:

poll demo:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <poll.h>
#include <errno.h>
#include <string.h>

#define SOCKET_PATH "/run/user/1000/wayland-0"
#define TIMEOUT 5000  // 超时时间,单位为毫秒

int main() {
    int sockfd;
    struct sockaddr_un addr;
    struct pollfd pfd;
    int ret;

    // 创建 Unix 域套接字
    sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sockfd == -1) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    // 配置套接字地址
    memset(&addr, 0, sizeof(struct sockaddr_un));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);

    // 非阻塞模式
    int flags = fcntl(sockfd, F_GETFL, 0);
    fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

    // 尝试连接 Wayland 套接字
    ret = connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un));
    if (ret == -1 && errno != EINPROGRESS) {
        perror("connect");
        close(sockfd);
        exit(EXIT_FAILURE);
    }

    // 设置 poll 结构体
    pfd.fd = sockfd;
    pfd.events = POLLOUT;

    // 轮询 Wayland 套接字
    ret = poll(&pfd, 1, TIMEOUT);
    if (ret == -1) {
        perror("poll");
        close(sockfd);
        exit(EXIT_FAILURE);
    } else if (ret == 0) {
        printf("Timeout occurred! Wayland socket is not ready.\n");
        close(sockfd);
        exit(EXIT_FAILURE);
    } else {
        if (pfd.revents & POLLOUT) {
            printf("Wayland socket is ready for connection.\n");
        } else {
            printf("Wayland socket is not ready, unexpected event: %d\n", pfd.revents);
        }
    }

    // 关闭套接字
    close(sockfd);
    return 0;
}
lychee-yycy commented 3 months ago

LGTM