From 040c13e2b9b59895368930cd74d0943d43418383 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Fri, 12 Apr 2024 11:22:31 +0200 Subject: Sync with upstream 2.15 and update the documentation --- unlock_indicator.c | 94 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 31 deletions(-) (limited to 'unlock_indicator.c') diff --git a/unlock_indicator.c b/unlock_indicator.c index 6f6fbf7..7d620fd 100644 --- a/unlock_indicator.c +++ b/unlock_indicator.c @@ -1,6 +1,6 @@ /* * This file is part of i3lock-extended - * Copyright (C) 2020-2023 Simeon Simeonov + * Copyright (C) 2020-2024 Simeon Simeonov * i3lock-extended is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -90,6 +90,8 @@ extern bool unlock_indicator; /* List of pressed modifiers, or NULL if none are pressed. */ extern char *modifier_string; +/* Name of the current keyboard layout or NULL if not initialized. */ +char *layout_string = NULL; /* A Cairo surface containing the specified image (-i), if any. */ extern cairo_surface_t *img; @@ -101,6 +103,8 @@ extern char color[7]; /* Whether the failed attempts should be displayed. */ extern bool show_failed_attempts; +/* Whether keyboard layout should be displayed. */ +extern bool show_keyboard_layout; /* Number of failed unlock attempts. */ extern int failed_attempts; @@ -126,6 +130,53 @@ static xcb_visualtype_t *vistype; unlock_state_t unlock_state; auth_state_t auth_state; +static void string_append(char **string_ptr, const char *appended) { + char *tmp = NULL; + if (*string_ptr == NULL) { + if (asprintf(&tmp, "%s", appended) != -1) { + *string_ptr = tmp; + } + } else if (asprintf(&tmp, "%s, %s", *string_ptr, appended) != -1) { + free(*string_ptr); + *string_ptr = tmp; + } +} + +static void display_button_text( + cairo_t *ctx, const char *text, double y_offset, bool use_dark_text) { + cairo_text_extents_t extents; + double x, y; + + cairo_text_extents(ctx, text, &extents); + x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing); + y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing) + y_offset; + + cairo_move_to(ctx, x, y); + if (use_dark_text) { + cairo_set_source_rgb(ctx, 0., 0., 0.); + } else { + cairo_set_source_rgb(ctx, 1., 1., 1.); + } + cairo_show_text(ctx, text); + cairo_close_path(ctx); +} + +static void update_layout_string() { + if (layout_string) { + free(layout_string); + layout_string = NULL; + } + xkb_layout_index_t num_layouts = xkb_keymap_num_layouts(xkb_keymap); + for (xkb_layout_index_t i = 0; i < num_layouts; ++i) { + if (xkb_state_layout_index_is_active(xkb_state, i, XKB_STATE_LAYOUT_EFFECTIVE)) { + const char *name = xkb_keymap_layout_get_name(xkb_keymap, i); + if (name) { + string_append(&layout_string, name); + } + } + } +} + /* 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) { @@ -152,15 +203,7 @@ static void check_modifier_keys(void) { * leak state about the password. */ continue; } - - 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; - } + string_append(&modifier_string, mod_name); } } @@ -249,6 +292,8 @@ void draw_image(xcb_pixmap_t bg_pixmap, uint32_t *resolution) { } cairo_fill_preserve(ctx); + bool use_dark_text = true; + switch (auth_state) { case STATE_AUTH_VERIFY: case STATE_AUTH_LOCK: @@ -265,6 +310,7 @@ void draw_image(xcb_pixmap_t bg_pixmap, uint32_t *resolution) { } cairo_set_source_rgb(ctx, 51.0 / 255, 125.0 / 255, 0); + use_dark_text = false; break; } cairo_stroke(ctx); @@ -321,31 +367,16 @@ void draw_image(xcb_pixmap_t bg_pixmap, uint32_t *resolution) { } if (text) { - cairo_text_extents_t extents; - double x, y; - - cairo_text_extents(ctx, text, &extents); - x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing); - y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing); - - cairo_move_to(ctx, x, y); - cairo_show_text(ctx, text); - cairo_close_path(ctx); + display_button_text(ctx, text, 0., use_dark_text); } if (modifier_string != NULL) { - cairo_text_extents_t extents; - double x, y; - cairo_set_font_size(ctx, 14.0); - - cairo_text_extents(ctx, modifier_string, &extents); - x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing); - y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing) + 28.0; - - cairo_move_to(ctx, x, y); - cairo_show_text(ctx, modifier_string); - cairo_close_path(ctx); + display_button_text(ctx, modifier_string, 28., use_dark_text); + } + if (show_keyboard_layout && layout_string != NULL) { + cairo_set_font_size(ctx, 14.0); + display_button_text(ctx, layout_string, -28., use_dark_text); } /* After the user pressed any valid key or the backspace key, we @@ -480,6 +511,7 @@ void redraw_screen(void) { modifier_string = NULL; } check_modifier_keys(); + update_layout_string(); if (bg_pixmap == XCB_NONE) { DEBUG("allocating pixmap for %d x %d px\n", last_resolution[0], last_resolution[1]); -- cgit v1.3