Skip to content

Commit

Permalink
Add username and lock symbol to indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
loserMcloser committed May 31, 2021
1 parent 5cb9579 commit 797175c
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/swaylock.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ struct swaylock_args {
bool clock;
char *timestr;
char *datestr;
bool lock_symbol;
bool user;
uint32_t fade_in;
bool password_submit_on_touch;
uint32_t password_grace_period;
Expand Down Expand Up @@ -104,6 +106,7 @@ struct swaylock_state {
struct swaylock_password password;
struct swaylock_xkb xkb;
enum auth_state auth_state;
char *username;
bool indicator_dirty;
int render_randnum;
int failed_attempts;
Expand Down
37 changes: 35 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
#include <fcntl.h>
#include <getopt.h>
#include <poll.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <wayland-client.h>
Expand Down Expand Up @@ -103,7 +105,7 @@ static const char *parse_screen_pos_pair(const char *str, char delim,
}

static const char *parse_constant(const char *str1, const char *str2) {
size_t len = strlen(str2);
size_t len = strlen(str2);
if (strncmp(str1, str2, len) == 0) {
return str1 + len;
} else {
Expand Down Expand Up @@ -956,6 +958,8 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
LO_CLOCK,
LO_TIMESTR,
LO_DATESTR,
LO_LOCK_SYMBOL,
LO_USER,
LO_FADE_IN,
LO_SUBMIT_ON_TOUCH,
LO_GRACE,
Expand Down Expand Up @@ -1031,6 +1035,8 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
{"clock", no_argument, NULL, LO_CLOCK},
{"timestr", required_argument, NULL, LO_TIMESTR},
{"datestr", required_argument, NULL, LO_DATESTR},
{"lock-symbol", no_argument, NULL, LO_LOCK_SYMBOL},
{"user", no_argument, NULL, LO_USER},
{"fade-in", required_argument, NULL, LO_FADE_IN},
{"submit-on-touch", no_argument, NULL, LO_SUBMIT_ON_TOUCH},
{"grace", required_argument, NULL, LO_GRACE},
Expand Down Expand Up @@ -1094,6 +1100,10 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
"The format string for the time. Defaults to '%T'.\n"
" --datestr <format> "
"The format string for the date. Defaults to '%a, %x'.\n"
" --lock-symbol "
"Show a lock symbol. (Requires Font Awesome 5 Free.)\n"
" --user "
"Show name of locked user.\n"
" -v, --version "
"Show the version number and quit.\n"
" --bs-hl-color <color> "
Expand Down Expand Up @@ -1574,6 +1584,16 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state,
state->args.datestr = strdup(optarg);
}
break;
case LO_LOCK_SYMBOL:
if (state) {
state->args.lock_symbol = true;
}
break;
case LO_USER:
if (state) {
state->args.user = true;
}
break;
case LO_FADE_IN:
if (state) {
state->args.fade_in = parse_seconds(optarg);
Expand Down Expand Up @@ -1717,11 +1737,19 @@ static void timer_render(void *data) {
loop_add_timer(state->eventloop, 1000, timer_render, state);
}

static void get_username(char **ustr) {
uid_t uid = geteuid();
struct passwd *pw = getpwuid(uid);
if (pw)
*ustr = pw->pw_name;
else
*ustr = NULL;
}

int main(int argc, char **argv) {
swaylock_log_init(LOG_ERROR);
initialize_pw_backend(argc, argv);
srand(time(NULL));

enum line_mode line_mode = LM_LINE;
state.failed_attempts = 0;
state.indicator_dirty = false;
Expand Down Expand Up @@ -1751,6 +1779,8 @@ int main(int argc, char **argv) {
.clock = false,
.timestr = strdup("%T"),
.datestr = strdup("%a, %x"),
.lock_symbol = false,
.user = false,
.password_grace_period = 0,
};
wl_list_init(&state.images);
Expand Down Expand Up @@ -1795,6 +1825,9 @@ int main(int argc, char **argv) {
state.auth_state = AUTH_STATE_GRACE;
}

if (state.args.user)
get_username(&(state.username));

#ifdef __linux__
// Most non-linux platforms require root to mlock()
if (mlock(state.password.buffer, sizeof(state.password.buffer)) != 0) {
Expand Down
52 changes: 50 additions & 2 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ void render_background_fade_prepare(struct swaylock_surface *surface, struct poo
wl_surface_commit(surface->surface);
}

void render_lock_symbol(cairo_t *cairo, char *lock_symbol, int buffer_width, int buffer_diameter, double line_y) {
cairo_text_extents_t lock_extents;
cairo_font_extents_t lock_fe;
double lock_x, lock_y;
cairo_select_font_face(cairo, "Font Awesome 5 Free",
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_text_extents(cairo, lock_symbol, &lock_extents);
cairo_font_extents(cairo, &lock_fe);
lock_x = (buffer_width / 2) - (lock_extents.width / 2 + lock_extents.x_bearing);
if (buffer_diameter > 0) {
lock_y = (buffer_diameter / 2) + (lock_fe.height / 2 - lock_fe.descent) - lock_fe.height / 10;
} else
lock_y = line_y - lock_fe.height * 1.25f;
cairo_move_to(cairo, lock_x, lock_y);
cairo_show_text(cairo, lock_symbol);
}

void render_frame(struct swaylock_surface *surface) {
struct swaylock_state *state = surface->state;

Expand Down Expand Up @@ -227,6 +244,7 @@ void render_frame(struct swaylock_surface *surface) {
char *text = NULL;
char *text_l1 = NULL;
char *text_l2 = NULL;
char lock_symbol[4] = "";
const char *layout_text = NULL;
double font_size;
char attempts[4]; // like i3lock: count no more than 999
Expand All @@ -242,6 +260,7 @@ void render_frame(struct swaylock_surface *surface) {
switch (state->auth_state) {
case AUTH_STATE_VALIDATING:
text = "verifying";
snprintf(lock_symbol, 4, "");
break;
case AUTH_STATE_INVALID:
text = "wrong";
Expand All @@ -265,6 +284,8 @@ void render_frame(struct swaylock_surface *surface) {
}
} else if (state->args.clock) {
timetext(surface, &text_l1, &text_l2);
} else if (state->args.user) {
text = state->username;
}

xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap);
Expand All @@ -285,6 +306,8 @@ void render_frame(struct swaylock_surface *surface) {
default:
if (state->args.clock)
timetext(surface, &text_l1, &text_l2);
else if (state->args.user)
text = state->username;
break;
}

Expand All @@ -304,6 +327,12 @@ void render_frame(struct swaylock_surface *surface) {
y = (buffer_diameter / 2) +
(fe.height / 2 - fe.descent);

if (state->args.lock_symbol) {
render_lock_symbol(cairo, lock_symbol, buffer_width, 0, y);
cairo_select_font_face(cairo, state->args.font,
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
}

cairo_move_to(cairo, x, y);
cairo_show_text(cairo, text);
cairo_close_path(cairo);
Expand Down Expand Up @@ -333,7 +362,8 @@ void render_frame(struct swaylock_surface *surface) {

/* Bottom */

cairo_set_font_size(cairo, arc_radius / 6.0f);
if (! (state->args.font_size > 0))
cairo_set_font_size(cairo, arc_radius / 6.0f);
cairo_text_extents(cairo, text_l2, &extents_l2);
cairo_font_extents(cairo, &fe_l2);
x_l2 = (buffer_width / 2) -
Expand All @@ -343,6 +373,18 @@ void render_frame(struct swaylock_surface *surface) {

cairo_move_to(cairo, x_l2, y_l2);
cairo_show_text(cairo, text_l2);
cairo_close_path(cairo);
cairo_new_sub_path(cairo);
cairo_set_font_size(cairo, font_size);

/* Lock symbol */

if (state->args.lock_symbol) {
render_lock_symbol(cairo, lock_symbol, buffer_width, 0, y_l1);
cairo_select_font_face(cairo, state->args.font,
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
}

cairo_close_path(cairo);
cairo_new_sub_path(cairo);

Expand All @@ -351,8 +393,14 @@ void render_frame(struct swaylock_surface *surface) {
if (new_width < extents_l2.width)
new_width = extents_l2.width;


} else if (state->args.lock_symbol) {
cairo_set_font_size(cairo, arc_radius / 1.5f);
render_lock_symbol(cairo, lock_symbol, buffer_width, buffer_diameter, 0.0f);
cairo_select_font_face(cairo, state->args.font,
CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cairo, font_size);
cairo_close_path(cairo);
cairo_new_sub_path(cairo);
}

// Typing indicator: Highlight random part on keypress
Expand Down
6 changes: 6 additions & 0 deletions swaylock.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ Locks your Wayland session.
*--datestr* <format>
The date format for the indicator clock. Defaults to '%a, %x'.

*--lock-symbol*
Show a lock symbol in the indicator. (Requires Font Awesome 5 Free.)

*--user*
Show username in the indicator.

*-i, --image* [[<output>]:]<path>
Display the given image, optionally only on the given output. Use -c to set
a background color. If the path potentially contains a ':', prefix it with another
Expand Down

0 comments on commit 797175c

Please sign in to comment.