Display modifier key warning before unlocking, too

Info about mod keys such as Caps Lock is now displayed
during typing, not only on failed attempt.

Signed-off-by: Janczar Kurek <janczar.kurek@student.uj.edu.pl>
pull/317/head
Janczar Kurek 2019-05-31 22:17:42 +02:00 committed by Michael Stapelberg
parent 5169138779
commit deaf8b9f0f
2 changed files with 52 additions and 38 deletions

View File

@ -83,9 +83,9 @@ int failed_attempts = 0;
bool show_failed_attempts = false;
bool retry_verification = false;
static struct xkb_state *xkb_state;
struct xkb_state *xkb_state;
static struct xkb_context *xkb_context;
static struct xkb_keymap *xkb_keymap;
struct xkb_keymap *xkb_keymap;
static struct xkb_compose_table *xkb_compose_table;
static struct xkb_compose_state *xkb_compose_state;
static uint8_t xkb_base_event;
@ -308,41 +308,6 @@ static void input_done(void) {
if (debug_mode)
fprintf(stderr, "Authentication failure\n");
/* Get state of Caps and Num lock modifiers, to be displayed in
* STATE_AUTH_WRONG state */
xkb_mod_index_t idx, num_mods;
const char *mod_name;
num_mods = xkb_keymap_num_mods(xkb_keymap);
for (idx = 0; idx < num_mods; idx++) {
if (!xkb_state_mod_index_is_active(xkb_state, idx, XKB_STATE_MODS_EFFECTIVE))
continue;
mod_name = xkb_keymap_mod_get_name(xkb_keymap, idx);
if (mod_name == NULL)
continue;
/* Replace certain xkb names with nicer, human-readable ones. */
if (strcmp(mod_name, XKB_MOD_NAME_CAPS) == 0)
mod_name = "Caps Lock";
else if (strcmp(mod_name, XKB_MOD_NAME_ALT) == 0)
mod_name = "Alt";
else if (strcmp(mod_name, XKB_MOD_NAME_NUM) == 0)
mod_name = "Num Lock";
else if (strcmp(mod_name, XKB_MOD_NAME_LOGO) == 0)
mod_name = "Super";
char *tmp;
if (modifier_string == NULL) {
if (asprintf(&tmp, "%s", mod_name) != -1)
modifier_string = tmp;
} else if (asprintf(&tmp, "%s, %s", modifier_string, mod_name) != -1) {
free(modifier_string);
modifier_string = tmp;
}
}
auth_state = STATE_AUTH_WRONG;
failed_attempts += 1;
clear_input();

View File

@ -6,12 +6,14 @@
* See LICENSE for licensing information
*
*/
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <xcb/xcb.h>
#include <xkbcommon/xkbcommon.h>
#include <ev.h>
#include <cairo.h>
#include <cairo/cairo-xcb.h>
@ -62,6 +64,9 @@ extern bool show_failed_attempts;
/* Number of failed unlock attempts. */
extern int failed_attempts;
extern struct xkb_keymap *xkb_keymap;
extern struct xkb_state *xkb_state;
/*******************************************************************************
* Variables defined in xcb.c.
******************************************************************************/
@ -81,6 +86,43 @@ static xcb_visualtype_t *vistype;
unlock_state_t unlock_state;
auth_state_t auth_state;
/* check_modifier_keys describes the currently active modifiers (Caps Lock, Alt,
Num Lock or Super) in the modifier_string variable. */
static void check_modifier_keys(void) {
xkb_mod_index_t idx, num_mods;
const char *mod_name;
num_mods = xkb_keymap_num_mods(xkb_keymap);
for (idx = 0; idx < num_mods; idx++) {
if (!xkb_state_mod_index_is_active(xkb_state, idx, XKB_STATE_MODS_EFFECTIVE))
continue;
mod_name = xkb_keymap_mod_get_name(xkb_keymap, idx);
if (mod_name == NULL)
continue;
/* Replace certain xkb names with nicer, human-readable ones. */
if (strcmp(mod_name, XKB_MOD_NAME_CAPS) == 0)
mod_name = "Caps Lock";
else if (strcmp(mod_name, XKB_MOD_NAME_ALT) == 0)
mod_name = "Alt";
else if (strcmp(mod_name, XKB_MOD_NAME_NUM) == 0)
mod_name = "Num Lock";
else if (strcmp(mod_name, XKB_MOD_NAME_LOGO) == 0)
mod_name = "Super";
char *tmp;
if (modifier_string == NULL) {
if (asprintf(&tmp, "%s", mod_name) != -1)
modifier_string = tmp;
} else if (asprintf(&tmp, "%s, %s", modifier_string, mod_name) != -1) {
free(modifier_string);
modifier_string = tmp;
}
}
}
/*
* Draws global image with fill color onto a pixmap with the given
* resolution and returns it.
@ -250,7 +292,7 @@ void draw_image(xcb_pixmap_t bg_pixmap, uint32_t *resolution) {
cairo_close_path(ctx);
}
if (auth_state == STATE_AUTH_WRONG && (modifier_string != NULL)) {
if (modifier_string != NULL) {
cairo_text_extents_t extents;
double x, y;
@ -351,6 +393,13 @@ void free_bg_pixmap(void) {
*/
void redraw_screen(void) {
DEBUG("redraw_screen(unlock_state = %d, auth_state = %d)\n", unlock_state, auth_state);
if (modifier_string) {
free(modifier_string);
modifier_string = NULL;
}
check_modifier_keys();
if (bg_pixmap == XCB_NONE) {
DEBUG("allocating pixmap for %d x %d px\n", last_resolution[0], last_resolution[1]);
bg_pixmap = create_bg_pixmap(conn, screen, last_resolution, color);