添加壁纸功能

This commit is contained in:
2025-08-10 02:07:57 +08:00
parent 751fce23c1
commit fc58edca2d
8 changed files with 241 additions and 21 deletions

View File

@@ -2,13 +2,13 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_image.h>
#include <xcb/xinerama.h>
#include <xcb/xinput.h>
#include <xcb/xproto.h>
@@ -30,7 +30,6 @@ bool has_other_wm_running(void) {
const xcb_setup_t *setup = xcb_get_setup(conn);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
screen = iter.data;
printf("screen number: %d\n", iter.rem);
uint32_t mask = XCB_CW_EVENT_MASK;
xcb_params_cw_t params = {
.event_mask = XCB_EVENT_MASK_LEAVE_WINDOW | XCB_EVENT_MASK_ENTER_WINDOW |
@@ -124,6 +123,44 @@ monitor_rect_t *detect_monitor_rect(void) {
return rect;
}
/**
* @brief 获取整个屏幕的宽高
* @returns 屏幕大小数据的指针,使用完后记得释放对应内存
*/
rect_size_t *get_screen_size(void) {
rect_size_t *size = ecalloc(1, sizeof(rect_size_t));
size->width = screen->width_in_pixels;
size->height = screen->height_in_pixels;
return size;
}
void set_wallpaper(uint32_t *wallpaper_data) {
uint32_t width = screen->width_in_pixels;
uint32_t height = screen->height_in_pixels;
uint8_t depth = screen->root_depth;
xcb_window_t root = screen->root;
uint32_t data_size = width * height * sizeof(uint32_t);
uint8_t *data = (uint8_t *)wallpaper_data;
xcb_image_t *xcb_img =
xcb_image_create_native(conn, width, height, XCB_IMAGE_FORMAT_Z_PIXMAP,
depth, NULL, data_size, data);
xcb_pixmap_t pixmap = xcb_generate_id(conn);
xcb_create_pixmap(conn, depth, pixmap, root, width, height);
xcb_gcontext_t gc = xcb_generate_id(conn);
xcb_create_gc(conn, gc, pixmap, 0, NULL);
xcb_image_put(conn, pixmap, gc, xcb_img, 0, 0, 0);
xcb_change_window_attributes_value_list_t value_list = {
.background_pixmap = pixmap,
};
xcb_change_window_attributes_aux(conn, root, XCB_CW_BACK_PIXMAP, &value_list);
xcb_clear_area(conn, 0, root, 0, 0, width, height);
xcb_image_destroy(xcb_img);
xcb_free_gc(conn, gc);
xcb_flush(conn);
}
static xcb_ewmh_connection_t *ewmh = NULL;
static bool ewmh_init_failed = false;
static xcb_window_t wm_check_window = XCB_NONE;