From: Michael Stapelberg Date: Fri, 2 May 2014 17:57:22 +0000 (+0200) Subject: Scale the unlock indicator (for retina displays) X-Git-Tag: 2.6~5 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=6191590e5c207803df211b74a714daf92c69c28f;p=i3%2Fi3lock Scale the unlock indicator (for retina displays) --- diff --git a/Makefile b/Makefile index 2633bef..ea3b89d 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ CFLAGS += $(shell pkg-config --cflags cairo xcb-dpms xcb-xinerama xcb-atom xkbco LIBS += $(shell pkg-config --libs cairo xcb-dpms xcb-xinerama xcb-atom xcb-image xkbcommon xkbfile x11 x11-xcb) LIBS += -lpam LIBS += -lev +LIBS += -lm FILES:=$(wildcard *.c) FILES:=$(FILES:.c=.o) diff --git a/unlock_indicator.c b/unlock_indicator.c index daaeeb1..ac7db09 100644 --- a/unlock_indicator.c +++ b/unlock_indicator.c @@ -1,19 +1,21 @@ /* * vim:ts=4:sw=4:expandtab * - * © 2010-2012 Michael Stapelberg + * © 2010-2014 Michael Stapelberg * * See LICENSE for licensing information * */ #include #include +#include #include #include #include #include #include +#include "i3lock.h" #include "xcb.h" #include "unlock_indicator.h" #include "xinerama.h" @@ -27,6 +29,8 @@ * Variables defined in i3lock.c. ******************************************************************************/ +extern bool debug_mode; + /* The current position in the input buffer. Useful to determine if any * characters of the password have already been entered or not. */ int input_position; @@ -48,6 +52,13 @@ extern bool tile; /* The background color to use (in hex). */ extern char color[7]; +/******************************************************************************* + * Variables defined in xcb.c. + ******************************************************************************/ + +/* The root screen, to determine the DPI. */ +extern xcb_screen_t *screen; + /******************************************************************************* * Local variables. ******************************************************************************/ @@ -60,6 +71,17 @@ static xcb_visualtype_t *vistype; unlock_state_t unlock_state; pam_state_t pam_state; +/* + * Returns the scaling factor of the current screen. E.g., on a 227 DPI MacBook + * Pro 13" Retina screen, the scaling factor is 227/96 = 2.36. + * + */ +static double scaling_factor(void) { + const int dpi = (double)screen->height_in_pixels * 25.4 / + (double)screen->height_in_millimeters; + return (dpi / 96.0); +} + /* * Draws global image with fill color onto a pixmap with the given * resolution and returns it. @@ -67,6 +89,9 @@ pam_state_t pam_state; */ xcb_pixmap_t draw_image(uint32_t *resolution) { xcb_pixmap_t bg_pixmap = XCB_NONE; + int button_diameter_physical = ceil(scaling_factor() * BUTTON_DIAMETER); + DEBUG("scaling_factor is %.f, physical diameter is %d px\n", + scaling_factor(), button_diameter_physical); if (!vistype) vistype = get_root_visual_type(screen); @@ -74,7 +99,7 @@ xcb_pixmap_t draw_image(uint32_t *resolution) { /* Initialize cairo: Create one in-memory surface to render the unlock * indicator on, create one XCB surface to actually draw (one or more, * depending on the amount of screens) unlock indicators on. */ - cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, BUTTON_DIAMETER, BUTTON_DIAMETER); + cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, button_diameter_physical, button_diameter_physical); cairo_t *ctx = cairo_create(output); cairo_surface_t *xcb_output = cairo_xcb_surface_create(conn, bg_pixmap, vistype, resolution[0], resolution[1]); @@ -107,6 +132,7 @@ xcb_pixmap_t draw_image(uint32_t *resolution) { } if (unlock_state >= STATE_KEY_PRESSED && unlock_indicator) { + cairo_scale(ctx, scaling_factor(), scaling_factor()); /* Draw a (centered) circle with transparent background. */ cairo_set_line_width(ctx, 10.0); cairo_arc(ctx, @@ -231,20 +257,20 @@ xcb_pixmap_t draw_image(uint32_t *resolution) { if (xr_screens > 0) { /* Composite the unlock indicator in the middle of each screen. */ for (int screen = 0; screen < xr_screens; screen++) { - int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (BUTTON_DIAMETER / 2))); - int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (BUTTON_DIAMETER / 2))); + int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (button_diameter_physical / 2))); + int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (button_diameter_physical / 2))); cairo_set_source_surface(xcb_ctx, output, x, y); - cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER); + cairo_rectangle(xcb_ctx, x, y, button_diameter_physical, button_diameter_physical); cairo_fill(xcb_ctx); } } else { /* We have no information about the screen sizes/positions, so we just * place the unlock indicator in the middle of the X root window and * hope for the best. */ - int x = (last_resolution[0] / 2) - (BUTTON_DIAMETER / 2); - int y = (last_resolution[1] / 2) - (BUTTON_DIAMETER / 2); + int x = (last_resolution[0] / 2) - (button_diameter_physical / 2); + int y = (last_resolution[1] / 2) - (button_diameter_physical / 2); cairo_set_source_surface(xcb_ctx, output, x, y); - cairo_rectangle(xcb_ctx, x, y, BUTTON_DIAMETER, BUTTON_DIAMETER); + cairo_rectangle(xcb_ctx, x, y, button_diameter_physical, button_diameter_physical); cairo_fill(xcb_ctx); }