Skip to content

Commit 046490f

Browse files
committed
Add --default-text
1 parent f9ce3f1 commit 046490f

File tree

7 files changed

+73
-50
lines changed

7 files changed

+73
-50
lines changed

completions/bash/swaylock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ _swaylock()
3434
--config
3535
--daemonize
3636
--debug
37+
--default-text
3738
--disable-caps-lock-text
3839
--font
3940
--font-size

completions/fish/swaylock.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ complete -c swaylock -l color -s c --description "Turn the scre
77
complete -c swaylock -l config -s C --description "Path to the config file."
88
complete -c swaylock -l daemonize -s f --description "Detach from the controlling terminal after locking."
99
complete -c swaylock -l debug -s d --description "Enable debugging output."
10+
complete -c swaylock -l default-text --description "Show text when no other (Caps Lock, attempt count, etc.) is shown."
1011
complete -c swaylock -l disable-caps-lock-text -s L --description "Disable the Caps Lock text."
1112
complete -c swaylock -l font --description "Sets the font of the text."
1213
complete -c swaylock -l font-size --description "Sets a fixed font size for the indicator text."

completions/zsh/_swaylock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _arguments -s \
1111
'(--config -C)'{--config,-C}'[Path to the config file]:filename:_files' \
1212
'(--daemonize -f)'{--daemonize,-f}'[Detach from the controlling terminal after locking]' \
1313
'(--debug -d)'{--debug,-d}'[Enable debugging output]' \
14+
'(--default-text)'--default-text'[Show text when no other (Caps Lock, attempt count, etc.) is shown]:text:' \
1415
'(--disable-caps-lock-text -L)'{--disable-caps-lock-text,-L}'[Disable the Caps Lock text]' \
1516
'(--font)'--font'[Sets the font of the text]:font:' \
1617
'(--font-size)'--font-size'[Sets a fixed font size for the indicator text]' \

include/swaylock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct swaylock_args {
6666
bool show_keyboard_layout;
6767
bool hide_keyboard_layout;
6868
bool show_failed_attempts;
69+
char *default_text;
6970
bool daemonize;
7071
int ready_fd;
7172
bool indicator_idle_visible;

main.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ enum line_mode {
484484
static int parse_options(int argc, char **argv, struct swaylock_state *state,
485485
enum line_mode *line_mode, char **config_path) {
486486
enum long_option_codes {
487-
LO_BS_HL_COLOR = 256,
487+
LO_DEFAULT_TEXT = 256,
488+
LO_BS_HL_COLOR,
488489
LO_CAPS_LOCK_BS_HL_COLOR,
489490
LO_CAPS_LOCK_KEY_HL_COLOR,
490491
LO_FONT,
@@ -541,6 +542,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
541542
{"hide-keyboard-layout", no_argument, NULL, 'K'},
542543
{"show-failed-attempts", no_argument, NULL, 'F'},
543544
{"version", no_argument, NULL, 'v'},
545+
{"default-text", required_argument, NULL, LO_DEFAULT_TEXT},
544546
{"bs-hl-color", required_argument, NULL, LO_BS_HL_COLOR},
545547
{"caps-lock-bs-hl-color", required_argument, NULL, LO_CAPS_LOCK_BS_HL_COLOR},
546548
{"caps-lock-key-hl-color", required_argument, NULL, LO_CAPS_LOCK_KEY_HL_COLOR},
@@ -616,6 +618,8 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
616618
"Disable the unlock indicator.\n"
617619
" -v, --version "
618620
"Show the version number and quit.\n"
621+
" --default-text <text> "
622+
"Show text when no other (Caps Lock, attempt count, etc.) is shown.\n"
619623
" --bs-hl-color <color> "
620624
"Sets the color of backspace highlight segments.\n"
621625
" --caps-lock-bs-hl-color <color> "
@@ -801,6 +805,12 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
801805
fprintf(stdout, "swaylock version " SWAYLOCK_VERSION "\n");
802806
exit(EXIT_SUCCESS);
803807
break;
808+
case LO_DEFAULT_TEXT:
809+
if (state) {
810+
free(state->args.default_text);
811+
state->args.default_text = strdup(optarg);
812+
}
813+
break;
804814
case LO_BS_HL_COLOR:
805815
if (state) {
806816
state->args.colors.bs_highlight = parse_color(optarg);
@@ -1138,6 +1148,7 @@ int main(int argc, char **argv) {
11381148
.show_keyboard_layout = false,
11391149
.hide_keyboard_layout = false,
11401150
.show_failed_attempts = false,
1151+
.default_text = NULL,
11411152
.indicator_idle_visible = false,
11421153
.ready_fd = -1,
11431154
};

render.c

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ void render_frame(struct swaylock_surface *surface) {
151151
layout_text = xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout);
152152
}
153153
}
154+
} else if (state->args.default_text) {
155+
text = state->args.default_text;
154156
}
155157

156158
// Compute the size of the buffer needed
@@ -245,27 +247,9 @@ void render_frame(struct swaylock_surface *surface) {
245247
set_color_for_state(cairo, state, &state->args.colors.ring);
246248
cairo_stroke(cairo);
247249

248-
// Draw a message
249-
configure_font_drawing(cairo, state, surface->subpixel, arc_radius);
250-
set_color_for_state(cairo, state, &state->args.colors.text);
251-
252-
if (text) {
253-
cairo_text_extents_t extents;
254-
cairo_font_extents_t fe;
255-
double x, y;
256-
cairo_text_extents(cairo, text, &extents);
257-
cairo_font_extents(cairo, &fe);
258-
x = (buffer_width / 2) -
259-
(extents.width / 2 + extents.x_bearing);
260-
y = (buffer_diameter / 2) +
261-
(fe.height / 2 - fe.descent);
262-
263-
cairo_move_to(cairo, x, y);
264-
cairo_show_text(cairo, text);
265-
cairo_close_path(cairo);
266-
cairo_new_sub_path(cairo);
267-
}
250+
}
268251

252+
if (draw_indicator) {
269253
// Typing indicator: Highlight random part on keypress
270254
if (state->input_state == INPUT_STATE_LETTER ||
271255
state->input_state == INPUT_STATE_BACKSPACE) {
@@ -311,37 +295,58 @@ void render_frame(struct swaylock_surface *surface) {
311295
cairo_arc(cairo, buffer_width / 2, buffer_diameter / 2,
312296
arc_radius + arc_thickness / 2, 0, 2 * M_PI);
313297
cairo_stroke(cairo);
298+
}
314299

315-
// display layout text separately
316-
if (layout_text) {
317-
cairo_text_extents_t extents;
318-
cairo_font_extents_t fe;
319-
double x, y;
320-
double box_padding = 4.0 * surface->scale;
321-
cairo_text_extents(cairo, layout_text, &extents);
322-
cairo_font_extents(cairo, &fe);
323-
// upper left coordinates for box
324-
x = (buffer_width / 2) - (extents.width / 2) - box_padding;
325-
y = buffer_diameter;
326-
327-
// background box
328-
cairo_rectangle(cairo, x, y,
329-
extents.width + 2.0 * box_padding,
330-
fe.height + 2.0 * box_padding);
331-
cairo_set_source_u32(cairo, state->args.colors.layout_background);
332-
cairo_fill_preserve(cairo);
333-
// border
334-
cairo_set_source_u32(cairo, state->args.colors.layout_border);
335-
cairo_stroke(cairo);
300+
// Draw a message
301+
configure_font_drawing(cairo, state, surface->subpixel, arc_radius);
302+
set_color_for_state(cairo, state, &state->args.colors.text);
303+
304+
if (text) {
305+
cairo_text_extents_t extents;
306+
cairo_font_extents_t fe;
307+
double x, y;
308+
cairo_text_extents(cairo, text, &extents);
309+
cairo_font_extents(cairo, &fe);
310+
x = (buffer_width / 2) -
311+
(extents.width / 2 + extents.x_bearing);
312+
y = (buffer_diameter / 2) +
313+
(fe.height / 2 - fe.descent);
314+
315+
cairo_move_to(cairo, x, y);
316+
cairo_show_text(cairo, text);
317+
cairo_close_path(cairo);
318+
cairo_new_sub_path(cairo);
319+
}
336320

337-
// take font extents and padding into account
338-
cairo_move_to(cairo,
339-
x - extents.x_bearing + box_padding,
340-
y + (fe.height - fe.descent) + box_padding);
341-
cairo_set_source_u32(cairo, state->args.colors.layout_text);
342-
cairo_show_text(cairo, layout_text);
343-
cairo_new_sub_path(cairo);
344-
}
321+
// display layout text separately
322+
if (layout_text) {
323+
cairo_text_extents_t extents;
324+
cairo_font_extents_t fe;
325+
double x, y;
326+
double box_padding = 4.0 * surface->scale;
327+
cairo_text_extents(cairo, layout_text, &extents);
328+
cairo_font_extents(cairo, &fe);
329+
// upper left coordinates for box
330+
x = (buffer_width / 2) - (extents.width / 2) - box_padding;
331+
y = buffer_diameter;
332+
333+
// background box
334+
cairo_rectangle(cairo, x, y,
335+
extents.width + 2.0 * box_padding,
336+
fe.height + 2.0 * box_padding);
337+
cairo_set_source_u32(cairo, state->args.colors.layout_background);
338+
cairo_fill_preserve(cairo);
339+
// border
340+
cairo_set_source_u32(cairo, state->args.colors.layout_border);
341+
cairo_stroke(cairo);
342+
343+
// take font extents and padding into account
344+
cairo_move_to(cairo,
345+
x - extents.x_bearing + box_padding,
346+
y + (fe.height - fe.descent) + box_padding);
347+
cairo_set_source_u32(cairo, state->args.colors.layout_text);
348+
cairo_show_text(cairo, layout_text);
349+
cairo_new_sub_path(cairo);
345350
}
346351

347352
// Send Wayland requests

swaylock.1.scd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ Locks your Wayland session.
8484
sets the background of the image to the given color. Defaults to white
8585
(FFFFFF).
8686

87+
*--default-text* <text>
88+
Show text when no other (Caps Lock, attempt count, etc.) is shown.
89+
8790
*--bs-hl-color* <rrggbb[aa]>
8891
Sets the color of backspace highlight segments.
8992

0 commit comments

Comments
 (0)