检测显示器信息

This commit is contained in:
2025-08-03 01:25:14 +08:00
parent 82456e77c9
commit 80f909ce22
8 changed files with 186 additions and 2 deletions

70
src/xcb/xcb.c Normal file
View File

@@ -0,0 +1,70 @@
#include "xcb.h"
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/xinerama.h>
#include "utils.h"
#include "xcb-private.h"
xcb_connection_t *conn = NULL;
bool has_other_wm_running(void) {
conn = xcb_connect(NULL, NULL);
if (!conn || xcb_connection_has_error(conn)) {
die("connection:");
}
const xcb_setup_t *setup = xcb_get_setup(conn);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
xcb_screen_t *screen = iter.data;
uint32_t mask = XCB_CW_EVENT_MASK;
xcb_params_cw_t params = {
.event_mask = XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_ENTER_WINDOW |
XCB_EVENT_MASK_POINTER_MOTION |
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY,
};
xcb_void_cookie_t cookie =
xcb_aux_change_window_attributes(conn, screen->root, mask, &params);
xcb_generic_error_t *error = xcb_request_check(conn, cookie);
if (error) {
xcb_disconnect(conn);
return true;
}
return false;
}
monitor_rect_t *detect_monitor_rect(void) {
monitor_rect_t *list, *prev;
xcb_xinerama_query_screens_cookie_t cookie = xcb_xinerama_query_screens(conn);
xcb_xinerama_query_screens_reply_t *reply =
xcb_xinerama_query_screens_reply(conn, cookie, NULL);
xcb_xinerama_screen_info_t *screen_info =
xcb_xinerama_query_screens_screen_info(reply);
if (screen_info == NULL) {
die("no monitor finded");
}
int count = xcb_xinerama_query_screens_screen_info_length(reply);
for (int i = 0; i < count; i++) {
monitor_rect_t *m = ecalloc(1, sizeof(monitor_rect_t));
xcb_xinerama_screen_info_t info = screen_info[i];
m->x = info.x_org;
m->y = info.y_org;
m->width = info.width;
m->height = info.height;
m->next = NULL;
if (i == 0) {
list = m;
} else {
prev->next = m;
}
prev = m;
}
return list;
}