Scale the unlock indicator (for retina displays)

pull/1/head
Michael Stapelberg 2014-05-02 19:57:22 +02:00
parent e2dd1543e9
commit 6191590e5c
2 changed files with 35 additions and 8 deletions

View File

@ -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)

View File

@ -1,19 +1,21 @@
/*
* vim:ts=4:sw=4:expandtab
*
* © 2010-2012 Michael Stapelberg
* © 2010-2014 Michael Stapelberg
*
* See LICENSE for licensing information
*
*/
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <xcb/xcb.h>
#include <ev.h>
#include <cairo.h>
#include <cairo/cairo-xcb.h>
#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);
}