Skip to content

Commit

Permalink
Added battery to separate line
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthmrao committed Feb 25, 2024
1 parent 1f170bc commit fa2a2c5
Showing 1 changed file with 54 additions and 32 deletions.
86 changes: 54 additions & 32 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ static void set_color_for_state(cairo_t *cairo, struct swaylock_state *state,
}
}

static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr) {
static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, char **bstr) {
static char dbuf[256];
static char tbuf[256];
static char bbuf[256];

// Use user's locale for strftime calls
char *prevloc = setlocale(LC_TIME, NULL);
Expand All @@ -63,30 +64,11 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr)

if (surface->state->args.datestr[0]) {
// Create a temporary buffer to hold the modified date string
char tempbuf[256];

// Write battery percentage as '100% | '
if (surface->state->args.display_battery && surface->state->args.clock) {
// Write battery percentage as '100% | '
snprintf(tempbuf, sizeof(tempbuf), "%s | ", surface->state->args.battery_str);
// Write date
strftime(tempbuf + strlen(tempbuf), sizeof(tempbuf) - strlen(tempbuf), surface->state->args.datestr, tm);
} else if (surface->state->args.display_battery) {
// Write battery percentage
snprintf(tempbuf, sizeof(tempbuf), "%s", surface->state->args.battery_str);
} else {
// Write date
strftime(tempbuf, sizeof(tempbuf), surface->state->args.datestr, tm);
}

// Copy the modified date string to dbuf
strncpy(dbuf, tempbuf, sizeof(dbuf));
// Assign dbuf to *dstr
*dstr = dbuf;
} else if (surface->state->args.display_battery) {
char tempbuf[256];
// Write battery percentage
snprintf(tempbuf, sizeof(tempbuf), "%s", surface->state->args.battery_str);

// Write the date
strftime(tempbuf, sizeof(tempbuf), surface->state->args.datestr, tm);

// Copy the modified date string to dbuf
strncpy(dbuf, tempbuf, sizeof(dbuf));
// Assign dbuf to *dstr
Expand All @@ -95,6 +77,13 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr)
*dstr = NULL;
}

if (surface->state->args.display_battery) {
// Write battery percentage
snprintf(bbuf, sizeof(bbuf), "%s", surface->state->args.battery_str);
*bstr = bbuf;
} else {
*bstr = NULL;
}

// Set it back, so we don't break stuff
setlocale(LC_TIME, prevloc);
Expand Down Expand Up @@ -283,6 +272,7 @@ void render_frame(struct swaylock_surface *surface) {
char *text = NULL;
char *text_l1 = NULL;
char *text_l2 = NULL;
char *text_l3 = NULL;
const char *layout_text = NULL;
double font_size;
char attempts[4]; // like i3lock: count no more than 999
Expand Down Expand Up @@ -320,7 +310,7 @@ void render_frame(struct swaylock_surface *surface) {
text = attempts;
}
} else if (state->args.clock || state->args.display_battery) {
subtext(surface, &text_l1, &text_l2);
subtext(surface, &text_l1, &text_l2, &text_l3);
}

xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap);
Expand All @@ -340,14 +330,22 @@ void render_frame(struct swaylock_surface *surface) {
break;
default:
if (state->args.clock || state->args.display_battery)
subtext(surface, &text_l1, &text_l2);
subtext(surface, &text_l1, &text_l2, &text_l3);
break;
}

if (text_l1 && !text_l2)
if (text_l1 && !text_l2 && !text_l3) {
text = text_l1;
if (text_l2 && !text_l1)
if (text_l2 && !text_l1 && !text_l3)
text = text_l2;
if (text_l3 && !text_l1 && !text_l2)
text = text_l3;
}

if (text_l1 && text_l3 && !text_l2) {
text_l2 = text_l3;
text_l3 = NULL;
}

if (text) {
cairo_text_extents_t extents;
Expand Down Expand Up @@ -375,13 +373,13 @@ void render_frame(struct swaylock_surface *surface) {

/* Top */

cairo_set_font_size(cairo, arc_radius / 4.0f);
cairo_set_font_size(cairo, arc_radius / 3.0f);
cairo_text_extents(cairo, text_l1, &extents_l1);
cairo_font_extents(cairo, &fe_l1);
x_l1 = (buffer_width / 2) -
(extents_l1.width / 2 + extents_l1.x_bearing);
y_l1 = (buffer_diameter / 2) +
(fe_l1.height / 2 - fe_l1.descent) - arc_radius / 8.0f;
(fe_l1.height / 2 - fe_l1.descent) - arc_radius / 10.0f;

cairo_move_to(cairo, x_l1, y_l1);
cairo_show_text(cairo, text_l1);
Expand All @@ -390,13 +388,13 @@ void render_frame(struct swaylock_surface *surface) {

/* Bottom */

cairo_set_font_size(cairo, arc_radius / 6.5f);
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) -
(extents_l2.width / 2 + extents_l2.x_bearing);
y_l2 = (buffer_diameter / 2) +
(fe_l2.height / 2 - fe_l2.descent) + arc_radius / 5.0f;
(fe_l2.height / 2 - fe_l2.descent) + arc_radius / 4.0f;

cairo_move_to(cairo, x_l2, y_l2);
cairo_show_text(cairo, text_l2);
Expand All @@ -410,6 +408,30 @@ void render_frame(struct swaylock_surface *surface) {


cairo_set_font_size(cairo, font_size);

if (text_l3) {
cairo_text_extents_t extents_l3;
cairo_font_extents_t fe_l3;
double x_l3, y_l3;

/* Even more Bottom */

cairo_set_font_size(cairo, arc_radius / 6.0f);
cairo_text_extents(cairo, text_l3, &extents_l3);
cairo_font_extents(cairo, &fe_l3);
x_l3 = (buffer_width / 2) -
(extents_l3.width / 2 + extents_l3.x_bearing);
y_l3 = (buffer_diameter / 2) +
(fe_l3.height / 2 - fe_l3.descent) + arc_radius / 2.0f;

cairo_move_to(cairo, x_l3, y_l3);
cairo_show_text(cairo, text_l3);
cairo_close_path(cairo);
cairo_new_sub_path(cairo);

if (new_width < extents_l3.width)
new_width = extents_l3.width;
}
}

// Typing indicator: Highlight random part on keypress
Expand Down

0 comments on commit fa2a2c5

Please sign in to comment.