1
0
Fork 0
mirror of https://github.com/Raymo111/i3lock-color.git synced 2025-02-17 15:55:52 -05:00

Refactor to share image drawing code

This commit is contained in:
Benoît Dardenne 2020-04-18 12:50:27 +02:00
parent 8bb468697c
commit 457ab2ae2b
3 changed files with 48 additions and 63 deletions

View file

@ -2195,21 +2195,7 @@ int main(int argc, char *argv[]) {
blur_image_surface(blur_img, blur_sigma); blur_image_surface(blur_img, blur_sigma);
if (img) { if (img) {
// Display image centered on all outputs. // Display image centered on all outputs.
if (centered) { draw_image(last_resolution, ctx);
draw_on_all_outputs(ctx);
} else if (tile) {
/* create a pattern and fill a rectangle as big as the screen */
cairo_pattern_t *pattern;
pattern = cairo_pattern_create_for_surface(img);
cairo_set_source(ctx, pattern);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
cairo_rectangle(ctx, 0, 0, last_resolution[0], last_resolution[1]);
cairo_fill(ctx);
cairo_pattern_destroy(pattern);
} else {
cairo_set_source_surface(ctx, img, 0, 0);
cairo_paint(ctx);
}
cairo_surface_destroy(img); cairo_surface_destroy(img);
img = NULL; img = NULL;
} }
@ -2223,7 +2209,7 @@ int main(int argc, char *argv[]) {
win = open_fullscreen_window(conn, screen, color); win = open_fullscreen_window(conn, screen, color);
xcb_pixmap_t pixmap = create_bg_pixmap(conn, win, last_resolution, color); xcb_pixmap_t pixmap = create_bg_pixmap(conn, win, last_resolution, color);
draw_image(last_resolution, pixmap); render_lock(last_resolution, pixmap);
xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[]){pixmap}); xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[]){pixmap});
xcb_free_pixmap(conn, pixmap); xcb_free_pixmap(conn, pixmap);

View file

@ -673,10 +673,9 @@ static void draw_elements(cairo_t *const ctx, DrawData const *const draw_data) {
} }
/* /*
* Draws global image with fill color onto a provided drawable with the given * Renders the lock screen on the provided drawable with the given resolution.
* resolution.
*/ */
void draw_image(uint32_t *resolution, xcb_drawable_t drawable) { void render_lock(uint32_t *resolution, xcb_drawable_t drawable) {
const double scaling_factor = get_dpi_value() / 96.0; const double scaling_factor = get_dpi_value() / 96.0;
int button_diameter_physical = ceil(scaling_factor * BUTTON_DIAMETER); int button_diameter_physical = ceil(scaling_factor * BUTTON_DIAMETER);
DEBUG("scaling_factor is %.f, physical diameter is %d px\n", DEBUG("scaling_factor is %.f, physical diameter is %d px\n",
@ -720,22 +719,7 @@ void draw_image(uint32_t *resolution, xcb_drawable_t drawable) {
cairo_set_source_surface(xcb_ctx, blur_img, 0, 0); cairo_set_source_surface(xcb_ctx, blur_img, 0, 0);
cairo_paint(xcb_ctx); cairo_paint(xcb_ctx);
} else { // img can no longer be non-NULL if blur_img is not null } else { // img can no longer be non-NULL if blur_img is not null
if (centered) draw_image(resolution, xcb_ctx);
draw_on_all_outputs(xcb_ctx);
}
else if (tile) {
/* create a pattern and fill a rectangle as big as the screen */
cairo_pattern_t *pattern;
pattern = cairo_pattern_create_for_surface(img);
cairo_set_source(xcb_ctx, pattern);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
cairo_fill(xcb_ctx);
cairo_pattern_destroy(pattern);
} else {
cairo_set_source_surface(xcb_ctx, img, 0, 0);
cairo_paint(xcb_ctx);
}
} }
} else { } else {
cairo_set_source_rgb(xcb_ctx, rgb16.red, rgb16.green, rgb16.blue); cairo_set_source_rgb(xcb_ctx, rgb16.red, rgb16.green, rgb16.blue);
@ -1068,7 +1052,8 @@ void draw_image(uint32_t *resolution, xcb_drawable_t drawable) {
cairo_destroy(xcb_ctx); cairo_destroy(xcb_ctx);
} }
void draw_on_all_outputs(cairo_t* xcb_ctx) { void draw_image(uint32_t* resolution, cairo_t* xcb_ctx) {
if (centered) {
double image_width = cairo_image_surface_get_width(img); double image_width = cairo_image_surface_get_width(img);
double image_height = cairo_image_surface_get_height(img); double image_height = cairo_image_surface_get_height(img);
@ -1101,16 +1086,30 @@ void draw_on_all_outputs(cairo_t* xcb_ctx) {
free(crtc); free(crtc);
free(output); free(output);
} }
} else if (tile) {
/* create a pattern and fill a rectangle as big as the screen */
cairo_pattern_t *pattern;
pattern = cairo_pattern_create_for_surface(img);
cairo_set_source(xcb_ctx, pattern);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
cairo_fill(xcb_ctx);
cairo_pattern_destroy(pattern);
} else {
cairo_set_source_surface(xcb_ctx, img, 0, 0);
cairo_paint(xcb_ctx);
}
} }
/* /*
* Calls draw_image on a new pixmap and swaps that with the current pixmap * Calls render_lock on a new pixmap and swaps that with the current pixmap
* *
*/ */
void redraw_screen(void) { void redraw_screen(void) {
DEBUG("redraw_screen(unlock_state = %d, auth_state = %d) @ [%lu]\n", unlock_state, auth_state, (unsigned long)time(NULL)); DEBUG("redraw_screen(unlock_state = %d, auth_state = %d) @ [%lu]\n", unlock_state, auth_state, (unsigned long)time(NULL));
xcb_pixmap_t pixmap = create_bg_pixmap(conn, win, last_resolution, color); xcb_pixmap_t pixmap = create_bg_pixmap(conn, win, last_resolution, color);
draw_image(last_resolution, pixmap); render_lock(last_resolution, pixmap);
xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){pixmap}); xcb_change_window_attributes(conn, win, XCB_CW_BACK_PIXMAP, (uint32_t[1]){pixmap});
xcb_clear_area(conn, 0, win, 0, 0, last_resolution[0], last_resolution[1]); xcb_clear_area(conn, 0, win, 0, 0, last_resolution[0], last_resolution[1]);
xcb_free_pixmap(conn, pixmap); xcb_free_pixmap(conn, pixmap);

View file

@ -38,8 +38,8 @@ typedef struct {
double bar_offset; double bar_offset;
} DrawData; } DrawData;
void draw_image(uint32_t* resolution, xcb_drawable_t drawable); void render_lock(uint32_t* resolution, xcb_drawable_t drawable);
void draw_on_all_outputs(cairo_t* xcb_ctx); void draw_image(uint32_t* resolution, cairo_t* xcb_ctx);
void init_colors_once(void); void init_colors_once(void);
void redraw_screen(void); void redraw_screen(void);
void clear_indicator(void); void clear_indicator(void);