summaryrefslogtreecommitdiff
path: root/render.c
diff options
context:
space:
mode:
authorDominik Bendle <fubenkyou@protonmail.ch>2019-02-05 16:12:54 +0100
committerDominik Bendle <fubenkyou@protonmail.ch>2019-02-06 01:42:49 +0100
commita052a56bc2c476870259f22aca5791c553bf582b (patch)
tree04f862b0ee2bdadc73ab934f6e10719d724ac22c /render.c
parent1db5c780378a9e7241c96917f90ada1433998b4d (diff)
Display xkb layout while typing
Displays the current keyboard layout below the unlock indicator if more than one xkb layout is configured or the show-keyboard-layout option is given. The text is displayed with a background box. Adds commandline options for text color, background color and border color of the box as well.
Diffstat (limited to 'render.c')
-rw-r--r--render.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/render.c b/render.c
index cf6de2a..a209f21 100644
--- a/render.c
+++ b/render.c
@@ -86,6 +86,7 @@ void render_frame(struct swaylock_surface *surface) {
// Draw a message
char *text = NULL;
+ const char *layout_text = NULL;
char attempts[4]; // like i3lock: count no more than 999
set_color_for_state(cairo, state, &state->args.colors.text);
cairo_select_font_face(cairo, state->args.font,
@@ -116,6 +117,20 @@ void render_frame(struct swaylock_surface *surface) {
text = attempts;
}
}
+
+ xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap);
+ if (state->args.show_keyboard_layout || num_layout > 1) {
+ xkb_layout_index_t curr_layout = 0;
+
+ // advance to the first active layout (if any)
+ while (curr_layout < num_layout &&
+ xkb_state_layout_index_is_active(state->xkb.state,
+ curr_layout, XKB_STATE_LAYOUT_EFFECTIVE) != 1) {
+ ++curr_layout;
+ }
+ // will handle invalid index if none are active
+ layout_text = xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout);
+ }
break;
default:
break;
@@ -185,6 +200,39 @@ void render_frame(struct swaylock_surface *surface) {
cairo_arc(cairo, buffer_width / 2, buffer_height / 2,
arc_radius + arc_thickness / 2, 0, 2 * M_PI);
cairo_stroke(cairo);
+
+ // display layout text seperately
+ if (layout_text) {
+ cairo_text_extents_t extents;
+ cairo_font_extents_t fe;
+ double x, y;
+ double box_padding = 4.0 * surface->scale;
+ cairo_text_extents(cairo, layout_text, &extents);
+ cairo_font_extents(cairo, &fe);
+ // upper left coordinates for box
+ x = (buffer_width / 2) - (extents.width / 2) - box_padding;
+ y = (buffer_height / 2) + arc_radius + arc_thickness/2 +
+ box_padding; // use box_padding also as gap to indicator
+
+ // background box
+ cairo_rectangle(cairo, x, y,
+ extents.width + 2.0 * box_padding,
+ fe.height + 2.0 * box_padding);
+ cairo_set_source_u32(cairo, state->args.colors.layout_background);
+ cairo_fill_preserve(cairo);
+ // border
+ cairo_set_source_u32(cairo, state->args.colors.layout_border);
+ cairo_stroke(cairo);
+ cairo_new_sub_path(cairo);
+
+ // take font extents and padding into account
+ cairo_move_to(cairo,
+ x - extents.x_bearing + box_padding,
+ y + (fe.height - fe.descent) + box_padding);
+ cairo_set_source_u32(cairo, state->args.colors.layout_text);
+ cairo_show_text(cairo, layout_text);
+ cairo_new_sub_path(cairo);
+ }
}
wl_surface_set_buffer_scale(surface->surface, surface->scale);