Compare commits
3 Commits
714a69f258
...
943d566cef
| Author | SHA1 | Date | |
|---|---|---|---|
|
943d566cef
|
|||
|
366e4a8f91
|
|||
|
b2041333c1
|
18
src/main.c
18
src/main.c
@@ -24,6 +24,7 @@ static void cleanup(void);
|
||||
|
||||
static monitor_t *monitors = NULL;
|
||||
static wallpaper_config_t *wallpaper_config = NULL;
|
||||
static GMainLoop *loop = NULL;
|
||||
|
||||
/* 调试函数,开发完成后删除 */
|
||||
static void print_monitor_list_info(monitor_t *monitor_list) {
|
||||
@@ -84,12 +85,10 @@ void init_monitors(void) {
|
||||
void init_wallpaper(void) {
|
||||
wallpaper_config = parse_wallpaper_config(wallpager_list);
|
||||
if (!wallpaper_config) return;
|
||||
if (wallpaper_interval) {
|
||||
g_timeout_add_seconds(wallpaper_interval, update_wallpaper,
|
||||
wallpaper_config);
|
||||
}
|
||||
|
||||
update_wallpaper(wallpaper_config);
|
||||
if (!wallpaper_interval) return;
|
||||
g_timeout_add_seconds(wallpaper_interval, update_wallpaper, wallpaper_config);
|
||||
}
|
||||
|
||||
gboolean update_wallpaper(gpointer user_data) {
|
||||
@@ -100,6 +99,10 @@ gboolean update_wallpaper(gpointer user_data) {
|
||||
set_wallpaper_by_index(index);
|
||||
uint32_t monitor_count = 0;
|
||||
for (monitor_t *m = monitors; m; m = m->next) monitor_count++;
|
||||
|
||||
/* 屏幕比壁纸多则不动态切换 */
|
||||
if (!monitor_count || monitor_count >= wc->count) return false;
|
||||
|
||||
if (monitor_count) {
|
||||
index += monitor_count;
|
||||
index %= wc->count;
|
||||
@@ -114,10 +117,13 @@ void set_wallpaper_by_index(uint32_t index) {
|
||||
rect_size_t *screen_size = get_screen_size();
|
||||
uint32_t *wallpaper = generate_wallpaper(
|
||||
wallpaper_config, monitors, index, screen_size->width, screen_size->height);
|
||||
|
||||
if (wallpaper) {
|
||||
set_wallpaper(wallpaper);
|
||||
free(wallpaper);
|
||||
}
|
||||
|
||||
free(screen_size);
|
||||
free(wallpaper);
|
||||
}
|
||||
|
||||
wallpaper_config_t *parse_wallpaper_config(const char **wallpapers) {
|
||||
@@ -194,7 +200,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
free_window_list(window_list);
|
||||
|
||||
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
|
||||
loop = g_main_loop_new(NULL, FALSE);
|
||||
g_main_loop_run(loop);
|
||||
|
||||
cleanup();
|
||||
|
||||
@@ -33,9 +33,8 @@ uint32_t *generate_wallpaper(const wallpaper_config_t *wallpaper_config,
|
||||
uint32_t monitor_index = 0;
|
||||
while (monitor) {
|
||||
uint32_t img_index = (index + monitor_index) % wallpaper_config->count;
|
||||
|
||||
Imlib_Image wallpaper_img =
|
||||
imlib_load_image(wallpaper_config->path_list[img_index]);
|
||||
const char *path = wallpaper_config->path_list[img_index];
|
||||
Imlib_Image wallpaper_img = imlib_load_image(path);
|
||||
if (!wallpaper_img) {
|
||||
monitor = monitor->next;
|
||||
monitor_index++;
|
||||
@@ -55,14 +54,11 @@ uint32_t *generate_wallpaper(const wallpaper_config_t *wallpaper_config,
|
||||
float height_ratio = (float)dst_height / img_height;
|
||||
float scale = MAX(width_ratio, height_ratio);
|
||||
|
||||
uint32_t scaled_width = img_width * scale;
|
||||
uint32_t scaled_height = img_height * scale;
|
||||
|
||||
/* 图片居中显示,计算原图绘制区域 */
|
||||
int src_x = (scaled_width - dst_width) / (2 * scale);
|
||||
int src_y = (scaled_height - dst_height) / (2 * scale);
|
||||
int src_width = dst_width / scale;
|
||||
int src_height = dst_height / scale;
|
||||
int src_x = (img_width - src_width) / 2;
|
||||
int src_y = (img_height - src_height) / 2;
|
||||
|
||||
/* 绘制图片到最终图像上 */
|
||||
imlib_context_set_image(final_image);
|
||||
@@ -84,7 +80,7 @@ uint32_t *generate_wallpaper(const wallpaper_config_t *wallpaper_config,
|
||||
|
||||
/* 复制数据到自己分配的内存中(原数据会在图像释放后无效) */
|
||||
size_t data_size = screen_width * screen_height * sizeof(uint32_t);
|
||||
uint32_t *result = malloc(data_size);
|
||||
uint32_t *result = ecalloc(1, data_size);
|
||||
if (result) memcpy(result, data, data_size);
|
||||
|
||||
/* 释放图像 */
|
||||
|
||||
@@ -99,6 +99,7 @@ static monitor_rect_t *detect_monitor_rect_by_xinerama(void) {
|
||||
}
|
||||
prev = m;
|
||||
}
|
||||
free(reply);
|
||||
|
||||
return list;
|
||||
}
|
||||
@@ -215,9 +216,9 @@ window_list_t *scan_window_list(void) {
|
||||
init_ewmh_extension();
|
||||
|
||||
xcb_query_tree_cookie_t cookie = xcb_query_tree(conn, screen->root);
|
||||
xcb_query_tree_reply_t *reply = xcb_query_tree_reply(conn, cookie, NULL);
|
||||
xcb_window_t *list = xcb_query_tree_children(reply);
|
||||
int len = xcb_query_tree_children_length(reply);
|
||||
xcb_query_tree_reply_t *tree_reply = xcb_query_tree_reply(conn, cookie, NULL);
|
||||
xcb_window_t *list = xcb_query_tree_children(tree_reply);
|
||||
int len = xcb_query_tree_children_length(tree_reply);
|
||||
|
||||
uint32_t count = 0;
|
||||
xcb_window_t temp_windoow_array[len];
|
||||
@@ -226,11 +227,11 @@ window_list_t *scan_window_list(void) {
|
||||
|
||||
xcb_get_window_attributes_cookie_t wa_cookie =
|
||||
xcb_get_window_attributes(conn, window);
|
||||
xcb_get_window_attributes_reply_t *reply =
|
||||
xcb_get_window_attributes_reply_t *attribute_reply =
|
||||
xcb_get_window_attributes_reply(conn, wa_cookie, NULL);
|
||||
uint8_t override_redirect = reply->override_redirect;
|
||||
uint8_t map_state = reply->map_state;
|
||||
free(reply);
|
||||
uint8_t override_redirect = attribute_reply->override_redirect;
|
||||
uint8_t map_state = attribute_reply->map_state;
|
||||
free(attribute_reply);
|
||||
if (override_redirect || map_state != XCB_MAP_STATE_VIEWABLE) continue;
|
||||
|
||||
xcb_window_t transient_for = XCB_NONE;
|
||||
@@ -242,6 +243,7 @@ window_list_t *scan_window_list(void) {
|
||||
temp_windoow_array[count] = window;
|
||||
count++;
|
||||
}
|
||||
free(tree_reply);
|
||||
|
||||
if (!count) return NULL;
|
||||
|
||||
@@ -271,6 +273,7 @@ void clean_xcb(void) {
|
||||
if (!ewmh_init_failed) {
|
||||
xcb_ewmh_connection_wipe(ewmh);
|
||||
}
|
||||
free(ewmh);
|
||||
ewmh_init_failed = false;
|
||||
ewmh = NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user