调整壁纸绘制方案
This commit is contained in:
67
src/main.c
67
src/main.c
@@ -25,7 +25,8 @@ static void init_monitors(void);
|
||||
static char **get_monitor_tags(uint32_t monitor_count, uint32_t monitor_index);
|
||||
static void init_wallpaper(void);
|
||||
static gboolean update_wallpaper(gpointer user_data);
|
||||
static void set_wallpaper_by_index(uint32_t index, wallpaper_config_t *config);
|
||||
static void set_wallpaper_by_index(uint32_t index, wallpaper_config_t *config,
|
||||
cairo_t *cr);
|
||||
static wallpaper_config_t *parse_wallpaper_config(void);
|
||||
static void free_wallpaper_config(wallpaper_config_t *wallpaper_config);
|
||||
static void init_color_set(void);
|
||||
@@ -70,6 +71,7 @@ static void cleanup(void);
|
||||
|
||||
static monitor_t *monitors = NULL;
|
||||
static monitor_t *current_monitor = NULL;
|
||||
static cairo_t *wallpaper_cr = NULL;
|
||||
static wallpaper_config_t *wallpaper_config = NULL;
|
||||
static color_set_t *color_set = NULL;
|
||||
static GMainLoop *loop = NULL;
|
||||
@@ -253,6 +255,28 @@ void init_wallpaper(void) {
|
||||
wallpaper_config = parse_wallpaper_config();
|
||||
if (!wallpaper_config) return;
|
||||
|
||||
bar_cairo_params_t *p = prepare_wallpaper_surface_params();
|
||||
rect_size_t *screen_size = get_screen_size();
|
||||
if (wallpaper_cr) cairo_destroy(wallpaper_cr);
|
||||
|
||||
cairo_surface_t *surface = cairo_xcb_surface_create(
|
||||
p->conn, p->window, p->visual, screen_size->width, screen_size->height);
|
||||
free(p);
|
||||
free(screen_size);
|
||||
|
||||
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) {
|
||||
cairo_surface_destroy(surface);
|
||||
return;
|
||||
}
|
||||
|
||||
cairo_t *cr = cairo_create(surface);
|
||||
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
|
||||
cairo_destroy(cr);
|
||||
return;
|
||||
}
|
||||
wallpaper_cr = cr;
|
||||
cairo_surface_destroy(surface);
|
||||
|
||||
update_wallpaper(wallpaper_config);
|
||||
if (!wallpaper_interval) return;
|
||||
g_timeout_add_seconds(wallpaper_interval, update_wallpaper, wallpaper_config);
|
||||
@@ -260,10 +284,10 @@ void init_wallpaper(void) {
|
||||
|
||||
gboolean update_wallpaper(gpointer user_data) {
|
||||
wallpaper_config_t *wc = user_data;
|
||||
if (!wc) return false;
|
||||
if (!wc || !wallpaper_cr) return false;
|
||||
|
||||
static uint32_t index = 0;
|
||||
set_wallpaper_by_index(index, wc);
|
||||
set_wallpaper_by_index(index, wc, wallpaper_cr);
|
||||
uint32_t monitor_count = 0;
|
||||
for (monitor_t *m = monitors; m; m = m->next) monitor_count++;
|
||||
|
||||
@@ -278,22 +302,29 @@ gboolean update_wallpaper(gpointer user_data) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void set_wallpaper_by_index(uint32_t index, wallpaper_config_t *config) {
|
||||
if (!config) return;
|
||||
|
||||
void set_wallpaper_by_index(uint32_t index, wallpaper_config_t *config,
|
||||
cairo_t *cr) {
|
||||
rect_size_t *screen_size = get_screen_size();
|
||||
uint32_t *wallpaper = generate_wallpaper(
|
||||
config, monitors, index, screen_size->width, screen_size->height);
|
||||
|
||||
if (wallpaper) {
|
||||
double start = get_time();
|
||||
set_wallpaper(wallpaper);
|
||||
double end = get_time();
|
||||
free(wallpaper);
|
||||
logger("set wallpaper cost: %fs\n", end - start);
|
||||
}
|
||||
|
||||
uint32_t width = screen_size->width;
|
||||
uint32_t height = screen_size->height;
|
||||
free(screen_size);
|
||||
|
||||
uint32_t *wallpaper =
|
||||
generate_wallpaper(config, monitors, index, width, height);
|
||||
|
||||
if (!wallpaper) return;
|
||||
|
||||
double start = get_time();
|
||||
uint8_t *data = (uint8_t *)wallpaper;
|
||||
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
|
||||
cairo_surface_t *s = cairo_image_surface_create_for_data(
|
||||
data, CAIRO_FORMAT_ARGB32, width, height, stride);
|
||||
cairo_set_source_surface(cr, s, 0, 0);
|
||||
cairo_paint(cr);
|
||||
refresh_root_window();
|
||||
free(wallpaper);
|
||||
double end = get_time();
|
||||
logger("set wallpaper cost: %fs\n", end - start);
|
||||
}
|
||||
|
||||
wallpaper_config_t *parse_wallpaper_config(void) {
|
||||
@@ -926,7 +957,9 @@ void cleanup(void) {
|
||||
clean_pango_context();
|
||||
|
||||
free_wallpaper_config(wallpaper_config);
|
||||
cairo_destroy(wallpaper_cr);
|
||||
wallpaper_config = NULL;
|
||||
wallpaper_cr = NULL;
|
||||
|
||||
free(color_set);
|
||||
color_set = NULL;
|
||||
|
||||
Reference in New Issue
Block a user