From 927a9e57b6662360b9b47c19efd07be528db7ac0 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sat, 24 Feb 2024 17:04:50 -0500 Subject: [PATCH 01/19] Clarify pam --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index a3758e42..f3860e38 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,10 @@ Run these commands: ninja -C build sudo ninja -C build install +On systems with PAM, copy pam/swaylock to /etc/pam.d/swaylock + + sudo cp pam/swaylock /etc/pam.d/swaylock + On systems without PAM, you need to suid the swaylock binary: sudo chmod a+s /usr/local/bin/swaylock From 9ce590e2ac398c73dcf3fd6a5f4c294a3c5e3fe3 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 00:23:41 -0500 Subject: [PATCH 02/19] Battery works. Need to clean up. --- .idea/.gitignore | 8 + .idea/inspectionProfiles/Project_Default.xml | 145 ++++++++++++++++++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/swaylock-effects.iml | 8 + .idea/vcs.xml | 6 + include/swaylock.h | 6 +- main.c | 75 ++++++++- render.c | 36 +++-- 10 files changed, 287 insertions(+), 18 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/swaylock-effects.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..527a0dfe --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,145 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..450fc790 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..69ccdedb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/swaylock-effects.iml b/.idea/swaylock-effects.iml new file mode 100644 index 00000000..d0876a78 --- /dev/null +++ b/.idea/swaylock-effects.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/include/swaylock.h b/include/swaylock.h index daa6f754..eb7d8704 100644 --- a/include/swaylock.h +++ b/include/swaylock.h @@ -82,6 +82,8 @@ struct swaylock_args { bool password_grace_no_mouse; bool password_grace_no_touch; + bool battery; + char *text_cleared; char *text_caps_lock; char *text_verifying; @@ -169,8 +171,8 @@ void swaylock_handle_key(struct swaylock_state *state, void swaylock_handle_mouse(struct swaylock_state *state); void swaylock_handle_touch(struct swaylock_state *state); void render_frame_background(struct swaylock_surface *surface, bool commit); -void render_background_fade(struct swaylock_surface *surface, uint32_t time); -void render_frame(struct swaylock_surface *surface); +void render_background_fade(struct swaylock_surface *surface, uint32_t time, char *battery_str); +void render_frame(struct swaylock_surface *surface, char *battery_str); void damage_surface(struct swaylock_surface *surface); void damage_state(struct swaylock_state *state); void clear_password_buffer(struct swaylock_password *pw); diff --git a/main.c b/main.c index 9e055176..d2250df3 100644 --- a/main.c +++ b/main.c @@ -30,6 +30,8 @@ #include "wlr-screencopy-unstable-v1-client-protocol.h" #include "ext-session-lock-v1-client-protocol.h" +char battery_str[5]; + // returns a positive integer in milliseconds static uint32_t parse_seconds(const char *seconds) { char *endptr; @@ -339,7 +341,7 @@ static void initially_render_surface(struct swaylock_surface *surface) { if (!surface->state->ext_session_lock_v1) { render_frame_background(surface, true); - render_frame(surface); + render_frame(surface, battery_str); } } @@ -386,7 +388,7 @@ static void ext_session_lock_surface_v1_handle_configure(void *data, render_frame_background(surface, false); ext_session_lock_surface_v1_ack_configure(lock_surface, serial); wl_surface_commit(surface->surface); - render_frame(surface); + render_frame(surface, battery_str); } static const struct ext_session_lock_surface_v1_listener ext_session_lock_surface_v1_listener = { @@ -397,6 +399,37 @@ static const struct wl_callback_listener surface_frame_listener; static void surface_frame_handle_done(void *data, struct wl_callback *callback, uint32_t time) { + + FILE *file; + char buffer[128]; + int battery_percentage; + + // Open the file containing battery information + file = fopen("/sys/class/power_supply/BAT0/capacity", "r"); + if (file == NULL) { + perror("Error opening file"); +// return 1; + } + + // Read battery percentage from the file + if (fgets(buffer, sizeof(buffer), file) == NULL) { + perror("Error reading file"); + fclose(file); +// return 1; + } + + // Convert the read value to an integer + battery_percentage = atoi(buffer); + + // Close the file + fclose(file); + + // Print the battery percentage + snprintf(battery_str, sizeof(battery_str), "%d%%", battery_percentage); + + // Print battery percentage to console + printf("Battery: %s\n", battery_str); + struct swaylock_surface *surface = data; wl_callback_destroy(callback); @@ -410,11 +443,11 @@ static void surface_frame_handle_done(void *data, struct wl_callback *callback, surface->dirty = false; if (!fade_is_complete(&surface->fade)) { - render_background_fade(surface, time); + render_background_fade(surface, time, battery_str); surface->dirty = true; } - render_frame(surface); + render_frame(surface, battery_str); } } @@ -1812,6 +1845,34 @@ void log_init(int argc, char **argv) { } int main(int argc, char **argv) { + FILE *file; + char buffer[128]; + int battery_percentage; + + // Open the file containing battery information + file = fopen("/sys/class/power_supply/BAT0/capacity", "r"); + if (file == NULL) { + perror("Error opening file"); + return 1; + } + + // Read battery percentage from the file + if (fgets(buffer, sizeof(buffer), file) == NULL) { + perror("Error reading file"); + fclose(file); + return 1; + } + + // Convert the read value to an integer + battery_percentage = atoi(buffer); + + // Close the file + fclose(file); + + // Print the battery percentage + snprintf(battery_str, sizeof(battery_str), "%d%%", battery_percentage); + + log_init(argc, argv); initialize_pw_backend(argc, argv); srand(time(NULL)); @@ -1823,7 +1884,7 @@ int main(int argc, char **argv) { .mode = BACKGROUND_MODE_FILL, .font = strdup("sans-serif"), .font_size = 0, - .radius = 75, + .radius = 100, .thickness = 10, .indicator_x_position = 0, .indicator_y_position = 0, @@ -1844,10 +1905,12 @@ int main(int argc, char **argv) { .indicator = false, .clock = false, .timestr = strdup("%T"), - .datestr = strdup("%a, %x"), + .datestr = strdup("%x"), .allow_fade = true, .password_grace_period = 0, + .battery = true, + .text_cleared = strdup("Cleared"), .text_caps_lock = strdup("Caps Lock"), .text_verifying = strdup("Verifying"), diff --git a/render.c b/render.c index f92b3c1f..f2f4bcc1 100644 --- a/render.c +++ b/render.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include "cairo.h" #include "background-image.h" @@ -40,7 +42,7 @@ static void set_color_for_state(cairo_t *cairo, struct swaylock_state *state, } } -static void timetext(struct swaylock_surface *surface, char **tstr, char **dstr) { +static void timetext(struct swaylock_surface *surface, char **tstr, char **dstr, char *battery_str) { static char dbuf[256]; static char tbuf[256]; @@ -59,12 +61,26 @@ static void timetext(struct swaylock_surface *surface, char **tstr, char **dstr) } if (surface->state->args.datestr[0]) { - strftime(dbuf, sizeof(dbuf), surface->state->args.datestr, tm); - *dstr = dbuf; + // Create a temporary buffer to hold the modified date string + char tempbuf[256]; + + // Write battery percentage as '100% | ' + if (surface->state->args.battery) { + snprintf(tempbuf, sizeof(tempbuf), "%s | ", battery_str); + } + + // Write date + strftime(tempbuf + strlen(tempbuf), sizeof(tempbuf) - strlen(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 { - *dstr = NULL; + *dstr = NULL; } + // Set it back, so we don't break stuff setlocale(LC_TIME, prevloc); } @@ -127,7 +143,7 @@ void render_frame_background(struct swaylock_surface *surface, bool commit) { } } -void render_background_fade(struct swaylock_surface *surface, uint32_t time) { +void render_background_fade(struct swaylock_surface *surface, uint32_t time, char *battery_str) { if (fade_is_complete(&surface->fade)) { return; } @@ -135,10 +151,10 @@ void render_background_fade(struct swaylock_surface *surface, uint32_t time) { fade_update(&surface->fade, time); render_frame_background(surface, true); - render_frame(surface); + render_frame(surface, battery_str); } -void render_frame(struct swaylock_surface *surface) { +void render_frame(struct swaylock_surface *surface, char *battery_str) { struct swaylock_state *state = surface->state; int arc_radius = state->args.radius * surface->scale; @@ -289,7 +305,7 @@ void render_frame(struct swaylock_surface *surface) { text = attempts; } } else if (state->args.clock) { - timetext(surface, &text_l1, &text_l2); + timetext(surface, &text_l1, &text_l2, battery_str); } xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap); @@ -309,7 +325,7 @@ void render_frame(struct swaylock_surface *surface) { break; default: if (state->args.clock) - timetext(surface, &text_l1, &text_l2); + timetext(surface, &text_l1, &text_l2, battery_str); break; } @@ -478,7 +494,7 @@ void render_frame(struct swaylock_surface *surface) { destroy_buffer(buffer); surface->indicator_width = new_width; surface->indicator_height = new_height; - render_frame(surface); + render_frame(surface, battery_str); return; } From e65fff238b88f0cb626d0bf7cd01aeab1b2ff2e1 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 00:32:46 -0500 Subject: [PATCH 03/19] Made battery and clock separate options. --- main.c | 12 ++++++++---- render.c | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index d2250df3..1ab5f8b3 100644 --- a/main.c +++ b/main.c @@ -427,9 +427,6 @@ static void surface_frame_handle_done(void *data, struct wl_callback *callback, // Print the battery percentage snprintf(battery_str, sizeof(battery_str), "%d%%", battery_percentage); - // Print battery percentage to console - printf("Battery: %s\n", battery_str); - struct swaylock_surface *surface = data; wl_callback_destroy(callback); @@ -1008,6 +1005,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, LO_TIME_EFFECTS, LO_INDICATOR, LO_CLOCK, + LO_BATTERY, LO_TIMESTR, LO_DATESTR, LO_FADE_IN, @@ -1088,6 +1086,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, {"time-effects", no_argument, NULL, LO_TIME_EFFECTS}, {"indicator", no_argument, NULL, LO_INDICATOR}, {"clock", no_argument, NULL, LO_CLOCK}, + {"battery", no_argument, NULL, LO_BATTERY}, {"timestr", required_argument, NULL, LO_TIMESTR}, {"datestr", required_argument, NULL, LO_DATESTR}, {"fade-in", required_argument, NULL, LO_FADE_IN}, @@ -1652,6 +1651,11 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, state->args.clock = true; } break; + case LO_BATTERY: + if (state) { + state->args.battery = true; + } + break; case LO_TIMESTR: if (state) { free(state->args.timestr); @@ -1909,7 +1913,7 @@ int main(int argc, char **argv) { .allow_fade = true, .password_grace_period = 0, - .battery = true, + .battery = false, .text_cleared = strdup("Cleared"), .text_caps_lock = strdup("Caps Lock"), diff --git a/render.c b/render.c index f2f4bcc1..d6cd7f1e 100644 --- a/render.c +++ b/render.c @@ -65,13 +65,19 @@ static void timetext(struct swaylock_surface *surface, char **tstr, char **dstr, char tempbuf[256]; // Write battery percentage as '100% | ' - if (surface->state->args.battery) { + if (surface->state->args.battery && surface->state->args.clock) { + // Write battery percentage as '100% | ' snprintf(tempbuf, sizeof(tempbuf), "%s | ", battery_str); + // Write date + strftime(tempbuf + strlen(tempbuf), sizeof(tempbuf) - strlen(tempbuf), surface->state->args.datestr, tm); + } else if (surface->state->args.battery) { + // Write battery percentage + snprintf(tempbuf, sizeof(tempbuf), "%s", battery_str); + } else { + // Write date + strftime(tempbuf, sizeof(tempbuf), surface->state->args.datestr, tm); } - // Write date - strftime(tempbuf + strlen(tempbuf), sizeof(tempbuf) - strlen(tempbuf), surface->state->args.datestr, tm); - // Copy the modified date string to dbuf strncpy(dbuf, tempbuf, sizeof(dbuf)); // Assign dbuf to *dstr @@ -304,7 +310,7 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { snprintf(attempts, sizeof(attempts), "%d", state->failed_attempts); text = attempts; } - } else if (state->args.clock) { + } else if (state->args.clock || state->args.battery) { timetext(surface, &text_l1, &text_l2, battery_str); } @@ -324,7 +330,7 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { } break; default: - if (state->args.clock) + if (state->args.clock || state->args.battery) timetext(surface, &text_l1, &text_l2, battery_str); break; } From 7c973b777c9e11057b9d5b8fdf35ddbb04fa6c45 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 00:42:00 -0500 Subject: [PATCH 04/19] Cleaned. --- render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/render.c b/render.c index d6cd7f1e..573bd9a3 100644 --- a/render.c +++ b/render.c @@ -283,7 +283,7 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { if (state->args.font_size > 0) { font_size = state->args.font_size; } else { - font_size = arc_radius / 3.0f; + font_size = arc_radius / 4.0f; } cairo_set_font_size(cairo, font_size); switch (state->auth_state) { From eef9f19060ae4aff3e022c6a17749e38dbf4faa5 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 01:20:24 -0500 Subject: [PATCH 05/19] Only update battery when needed. --- main.c | 83 ++++++++++++++++++++------------------------------------ render.c | 2 +- 2 files changed, 30 insertions(+), 55 deletions(-) diff --git a/main.c b/main.c index 1ab5f8b3..3e13c5fb 100644 --- a/main.c +++ b/main.c @@ -31,6 +31,30 @@ #include "ext-session-lock-v1-client-protocol.h" char battery_str[5]; +bool battery = false; + +void update_battery() { + FILE *file; + char buffer[128]; + int battery_percentage; + + // Open the file containing battery information + file = fopen("/sys/class/power_supply/BAT0/capacity", "r"); + + // Read battery percentage from the file + if (fgets(buffer, sizeof(buffer), file) == NULL) { + fclose(file); + } + + // Convert the read value to an integer + battery_percentage = atoi(buffer); + + // Close the file + fclose(file); + + // Print the battery percentage + snprintf(battery_str, sizeof(battery_str), "%d%%", battery_percentage); +} // returns a positive integer in milliseconds static uint32_t parse_seconds(const char *seconds) { @@ -400,32 +424,9 @@ static const struct wl_callback_listener surface_frame_listener; static void surface_frame_handle_done(void *data, struct wl_callback *callback, uint32_t time) { - FILE *file; - char buffer[128]; - int battery_percentage; - - // Open the file containing battery information - file = fopen("/sys/class/power_supply/BAT0/capacity", "r"); - if (file == NULL) { - perror("Error opening file"); -// return 1; - } - - // Read battery percentage from the file - if (fgets(buffer, sizeof(buffer), file) == NULL) { - perror("Error reading file"); - fclose(file); -// return 1; - } - - // Convert the read value to an integer - battery_percentage = atoi(buffer); - - // Close the file - fclose(file); - - // Print the battery percentage - snprintf(battery_str, sizeof(battery_str), "%d%%", battery_percentage); + if (battery) { + update_battery(); + } struct swaylock_surface *surface = data; @@ -1654,7 +1655,9 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, case LO_BATTERY: if (state) { state->args.battery = true; + battery = true; } + update_battery(); break; case LO_TIMESTR: if (state) { @@ -1849,34 +1852,6 @@ void log_init(int argc, char **argv) { } int main(int argc, char **argv) { - FILE *file; - char buffer[128]; - int battery_percentage; - - // Open the file containing battery information - file = fopen("/sys/class/power_supply/BAT0/capacity", "r"); - if (file == NULL) { - perror("Error opening file"); - return 1; - } - - // Read battery percentage from the file - if (fgets(buffer, sizeof(buffer), file) == NULL) { - perror("Error reading file"); - fclose(file); - return 1; - } - - // Convert the read value to an integer - battery_percentage = atoi(buffer); - - // Close the file - fclose(file); - - // Print the battery percentage - snprintf(battery_str, sizeof(battery_str), "%d%%", battery_percentage); - - log_init(argc, argv); initialize_pw_backend(argc, argv); srand(time(NULL)); diff --git a/render.c b/render.c index 573bd9a3..d6cd7f1e 100644 --- a/render.c +++ b/render.c @@ -283,7 +283,7 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { if (state->args.font_size > 0) { font_size = state->args.font_size; } else { - font_size = arc_radius / 4.0f; + font_size = arc_radius / 3.0f; } cairo_set_font_size(cairo, font_size); switch (state->auth_state) { From ade1241360b3884ce4846cf9a9c1247c85eec06e Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 01:21:32 -0500 Subject: [PATCH 06/19] Refactor timetext function to subtext. --- render.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/render.c b/render.c index d6cd7f1e..39d7cb36 100644 --- a/render.c +++ b/render.c @@ -42,7 +42,7 @@ static void set_color_for_state(cairo_t *cairo, struct swaylock_state *state, } } -static void timetext(struct swaylock_surface *surface, char **tstr, char **dstr, char *battery_str) { +static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, char *battery_str) { static char dbuf[256]; static char tbuf[256]; @@ -311,7 +311,7 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { text = attempts; } } else if (state->args.clock || state->args.battery) { - timetext(surface, &text_l1, &text_l2, battery_str); + subtext(surface, &text_l1, &text_l2, battery_str); } xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap); @@ -331,7 +331,7 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { break; default: if (state->args.clock || state->args.battery) - timetext(surface, &text_l1, &text_l2, battery_str); + subtext(surface, &text_l1, &text_l2, battery_str); break; } From 4fb870ab5bcc0ce11edb2aa062bf4c9c00fc840e Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 01:24:22 -0500 Subject: [PATCH 07/19] Still write battery if no date format specified --- render.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/render.c b/render.c index 39d7cb36..3bc54b2d 100644 --- a/render.c +++ b/render.c @@ -53,6 +53,7 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, time_t t = time(NULL); struct tm *tm = localtime(&t); + // Write time if (surface->state->args.timestr[0]) { strftime(tbuf, sizeof(tbuf), surface->state->args.timestr, tm); *tstr = tbuf; @@ -82,8 +83,16 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, strncpy(dbuf, tempbuf, sizeof(dbuf)); // Assign dbuf to *dstr *dstr = dbuf; + } else if (surface->state->args.battery) { + char tempbuf[256]; + // Write battery percentage + snprintf(tempbuf, sizeof(tempbuf), "%s", battery_str); + // Copy the modified date string to dbuf + strncpy(dbuf, tempbuf, sizeof(dbuf)); + // Assign dbuf to *dstr + *dstr = dbuf; } else { - *dstr = NULL; + *dstr = NULL; } From c9eee2646a2f9421803ae30169090d91485c0b12 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 01:39:37 -0500 Subject: [PATCH 08/19] Help --- README.md | 7 ++++++- main.c | 6 ++++-- render.c | 7 ++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f3860e38..6895fe5f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ -# swaylock-effects +# swaylock-effects-battery + +Swaylock-effects-battery is a fork of [swaylock-effects](https://github.com/jirutka/swaylock-effects) +which adds a battery percentage display feature. Swaylock-effects is a fork of [swaylock](https://github.com/swaywm/swaylock) which adds built-in screenshots and image manipulation effects like blurring. @@ -15,6 +18,7 @@ which is no longer maintained. swaylock \ --screenshots \ + --battery \ --clock \ --indicator \ --indicator-radius 100 \ @@ -33,6 +37,7 @@ which is no longer maintained. The main new features compared to upstream swaylock are: +* `--battery` to display the battery percentage * `--screenshots` to use screenshots instead of an image on disk or a color * `--clock` to show date/time in the indicator * Use `--indicator` to make the indicator always active diff --git a/main.c b/main.c index 3e13c5fb..28b6cbdf 100644 --- a/main.c +++ b/main.c @@ -1147,6 +1147,8 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, "Disable the unlock indicator.\n" " --indicator " "Always show the indicator.\n" + " --battery " + "Show the battery percentage.\n" " --clock " "Show time and date.\n" " --timestr " @@ -1864,7 +1866,7 @@ int main(int argc, char **argv) { .font = strdup("sans-serif"), .font_size = 0, .radius = 100, - .thickness = 10, + .thickness = 9, .indicator_x_position = 0, .indicator_y_position = 0, .override_indicator_x_position = false, @@ -1884,7 +1886,7 @@ int main(int argc, char **argv) { .indicator = false, .clock = false, .timestr = strdup("%T"), - .datestr = strdup("%x"), + .datestr = strdup("%a, %x"), .allow_fade = true, .password_grace_period = 0, diff --git a/render.c b/render.c index 3bc54b2d..5d35d21f 100644 --- a/render.c +++ b/render.c @@ -375,12 +375,13 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { /* Top */ + cairo_set_font_size(cairo, arc_radius / 4.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 / 10.0f; + (fe_l1.height / 2 - fe_l1.descent) - arc_radius / 8.0f; cairo_move_to(cairo, x_l1, y_l1); cairo_show_text(cairo, text_l1); @@ -389,13 +390,13 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { /* Bottom */ - cairo_set_font_size(cairo, arc_radius / 6.0f); + cairo_set_font_size(cairo, arc_radius / 6.5f); 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 / 3.5f; + (fe_l2.height / 2 - fe_l2.descent) + arc_radius / 5.0f; cairo_move_to(cairo, x_l2, y_l2); cairo_show_text(cairo, text_l2); From c7e6ffd2acbfda1d73046c0aec21ca2906cd4ab2 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 01:40:42 -0500 Subject: [PATCH 09/19] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6895fe5f..fd81f649 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ which is no longer maintained. swaylock \ --screenshots \ - --battery \ + --battery \ --clock \ --indicator \ --indicator-radius 100 \ From 1f170bce6a3b040304a1de42127779f1730b2924 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 13:55:51 -0500 Subject: [PATCH 10/19] Added battery_str to state --- .idea/modules.xml | 1 + .idea/swaylock-effects2.iml | 2 ++ .vscode/settings.json | 5 +++++ include/swaylock.h | 7 ++++--- main.c | 33 +++++++++++++++++++-------------- render.c | 30 +++++++++++++++--------------- 6 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 .idea/swaylock-effects2.iml create mode 100644 .vscode/settings.json diff --git a/.idea/modules.xml b/.idea/modules.xml index 69ccdedb..a6bbb61e 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/.idea/swaylock-effects2.iml b/.idea/swaylock-effects2.iml new file mode 100644 index 00000000..4c942354 --- /dev/null +++ b/.idea/swaylock-effects2.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..5d36ab40 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "ext-session-lock-v1-client-protocol.h": "c" + } +} \ No newline at end of file diff --git a/include/swaylock.h b/include/swaylock.h index eb7d8704..7238e95f 100644 --- a/include/swaylock.h +++ b/include/swaylock.h @@ -82,7 +82,8 @@ struct swaylock_args { bool password_grace_no_mouse; bool password_grace_no_touch; - bool battery; + bool display_battery; + char *battery_str; char *text_cleared; char *text_caps_lock; @@ -171,8 +172,8 @@ void swaylock_handle_key(struct swaylock_state *state, void swaylock_handle_mouse(struct swaylock_state *state); void swaylock_handle_touch(struct swaylock_state *state); void render_frame_background(struct swaylock_surface *surface, bool commit); -void render_background_fade(struct swaylock_surface *surface, uint32_t time, char *battery_str); -void render_frame(struct swaylock_surface *surface, char *battery_str); +void render_background_fade(struct swaylock_surface *surface, uint32_t time); +void render_frame(struct swaylock_surface *surface); void damage_surface(struct swaylock_surface *surface); void damage_state(struct swaylock_state *state); void clear_password_buffer(struct swaylock_password *pw); diff --git a/main.c b/main.c index 28b6cbdf..681ff79f 100644 --- a/main.c +++ b/main.c @@ -30,11 +30,13 @@ #include "wlr-screencopy-unstable-v1-client-protocol.h" #include "ext-session-lock-v1-client-protocol.h" -char battery_str[5]; -bool battery = false; +#define MAX_BATTERY_STR_LEN 5 + +static struct swaylock_state state; void update_battery() { FILE *file; + char *battery_str = (char *)malloc((MAX_BATTERY_STR_LEN + 1) * sizeof(char)); char buffer[128]; int battery_percentage; @@ -53,7 +55,12 @@ void update_battery() { fclose(file); // Print the battery percentage - snprintf(battery_str, sizeof(battery_str), "%d%%", battery_percentage); + snprintf(battery_str, MAX_BATTERY_STR_LEN, "%d%%", battery_percentage); + + // Set battery_str to the battery percentage + state.args.battery_str = battery_str; + + // strcpy(state.args.battery_str, battery_str); } // returns a positive integer in milliseconds @@ -365,7 +372,7 @@ static void initially_render_surface(struct swaylock_surface *surface) { if (!surface->state->ext_session_lock_v1) { render_frame_background(surface, true); - render_frame(surface, battery_str); + render_frame(surface); } } @@ -412,7 +419,7 @@ static void ext_session_lock_surface_v1_handle_configure(void *data, render_frame_background(surface, false); ext_session_lock_surface_v1_ack_configure(lock_surface, serial); wl_surface_commit(surface->surface); - render_frame(surface, battery_str); + render_frame(surface); } static const struct ext_session_lock_surface_v1_listener ext_session_lock_surface_v1_listener = { @@ -424,9 +431,9 @@ static const struct wl_callback_listener surface_frame_listener; static void surface_frame_handle_done(void *data, struct wl_callback *callback, uint32_t time) { - if (battery) { + if (state.args.display_battery) { update_battery(); - } + }; struct swaylock_surface *surface = data; @@ -441,11 +448,11 @@ static void surface_frame_handle_done(void *data, struct wl_callback *callback, surface->dirty = false; if (!fade_is_complete(&surface->fade)) { - render_background_fade(surface, time, battery_str); + render_background_fade(surface, time); surface->dirty = true; } - render_frame(surface, battery_str); + render_frame(surface); } } @@ -1656,8 +1663,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, break; case LO_BATTERY: if (state) { - state->args.battery = true; - battery = true; + state->args.display_battery = true; } update_battery(); break; @@ -1783,8 +1789,6 @@ static int load_config(char *path, struct swaylock_state *state, return 0; } -static struct swaylock_state state; - static void display_in(int fd, short mask, void *data) { if (wl_display_dispatch(state.display) == -1) { state.run_display = false; @@ -1890,7 +1894,8 @@ int main(int argc, char **argv) { .allow_fade = true, .password_grace_period = 0, - .battery = false, + .display_battery = false, + .battery_str = NULL, .text_cleared = strdup("Cleared"), .text_caps_lock = strdup("Caps Lock"), diff --git a/render.c b/render.c index 5d35d21f..25ae79a5 100644 --- a/render.c +++ b/render.c @@ -42,7 +42,7 @@ static void set_color_for_state(cairo_t *cairo, struct swaylock_state *state, } } -static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, char *battery_str) { +static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr) { static char dbuf[256]; static char tbuf[256]; @@ -66,14 +66,14 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, char tempbuf[256]; // Write battery percentage as '100% | ' - if (surface->state->args.battery && surface->state->args.clock) { + if (surface->state->args.display_battery && surface->state->args.clock) { // Write battery percentage as '100% | ' - snprintf(tempbuf, sizeof(tempbuf), "%s | ", battery_str); + 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.battery) { + } else if (surface->state->args.display_battery) { // Write battery percentage - snprintf(tempbuf, sizeof(tempbuf), "%s", battery_str); + snprintf(tempbuf, sizeof(tempbuf), "%s", surface->state->args.battery_str); } else { // Write date strftime(tempbuf, sizeof(tempbuf), surface->state->args.datestr, tm); @@ -83,10 +83,10 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, strncpy(dbuf, tempbuf, sizeof(dbuf)); // Assign dbuf to *dstr *dstr = dbuf; - } else if (surface->state->args.battery) { + } else if (surface->state->args.display_battery) { char tempbuf[256]; // Write battery percentage - snprintf(tempbuf, sizeof(tempbuf), "%s", battery_str); + snprintf(tempbuf, sizeof(tempbuf), "%s", surface->state->args.battery_str); // Copy the modified date string to dbuf strncpy(dbuf, tempbuf, sizeof(dbuf)); // Assign dbuf to *dstr @@ -158,7 +158,7 @@ void render_frame_background(struct swaylock_surface *surface, bool commit) { } } -void render_background_fade(struct swaylock_surface *surface, uint32_t time, char *battery_str) { +void render_background_fade(struct swaylock_surface *surface, uint32_t time) { if (fade_is_complete(&surface->fade)) { return; } @@ -166,10 +166,10 @@ void render_background_fade(struct swaylock_surface *surface, uint32_t time, cha fade_update(&surface->fade, time); render_frame_background(surface, true); - render_frame(surface, battery_str); + render_frame(surface); } -void render_frame(struct swaylock_surface *surface, char *battery_str) { +void render_frame(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; int arc_radius = state->args.radius * surface->scale; @@ -319,8 +319,8 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { snprintf(attempts, sizeof(attempts), "%d", state->failed_attempts); text = attempts; } - } else if (state->args.clock || state->args.battery) { - subtext(surface, &text_l1, &text_l2, battery_str); + } else if (state->args.clock || state->args.display_battery) { + subtext(surface, &text_l1, &text_l2); } xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap); @@ -339,8 +339,8 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { } break; default: - if (state->args.clock || state->args.battery) - subtext(surface, &text_l1, &text_l2, battery_str); + if (state->args.clock || state->args.display_battery) + subtext(surface, &text_l1, &text_l2); break; } @@ -510,7 +510,7 @@ void render_frame(struct swaylock_surface *surface, char *battery_str) { destroy_buffer(buffer); surface->indicator_width = new_width; surface->indicator_height = new_height; - render_frame(surface, battery_str); + render_frame(surface); return; } From fa2a2c5e5f422dfd0a8f9276a8db023e5297955e Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 14:14:56 -0500 Subject: [PATCH 11/19] Added battery to separate line --- render.c | 86 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/render.c b/render.c index 25ae79a5..49bab0a1 100644 --- a/render.c +++ b/render.c @@ -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); @@ -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 @@ -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); @@ -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 @@ -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); @@ -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; @@ -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); @@ -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); @@ -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 From 9a40cbbed68b83941391cbb580af7793ca18a7a2 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 14:21:25 -0500 Subject: [PATCH 12/19] Made battery interact better with other layers. --- render.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/render.c b/render.c index 49bab0a1..5cd65f95 100644 --- a/render.c +++ b/render.c @@ -55,14 +55,14 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, struct tm *tm = localtime(&t); // Write time - if (surface->state->args.timestr[0]) { + if (surface->state->args.clock && surface->state->args.timestr[0]) { strftime(tbuf, sizeof(tbuf), surface->state->args.timestr, tm); *tstr = tbuf; } else { *tstr = NULL; } - if (surface->state->args.datestr[0]) { + if (surface->state->args.clock && surface->state->args.datestr[0]) { // Create a temporary buffer to hold the modified date string char tempbuf[256]; @@ -336,9 +336,11 @@ void render_frame(struct swaylock_surface *surface) { if (text_l1 && !text_l2 && !text_l3) { text = text_l1; - if (text_l2 && !text_l1 && !text_l3) + } + if (text_l2 && !text_l1 && !text_l3) { text = text_l2; - if (text_l3 && !text_l1 && !text_l2) + } + if (text_l3 && !text_l1 && !text_l2) { text = text_l3; } From 3ed8fbe6e79141b910186b5933b9ff7a16b23274 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 14:26:47 -0500 Subject: [PATCH 13/19] Simplify subtext function. --- render.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/render.c b/render.c index 5cd65f95..98c3f010 100644 --- a/render.c +++ b/render.c @@ -54,7 +54,6 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, time_t t = time(NULL); struct tm *tm = localtime(&t); - // Write time if (surface->state->args.clock && surface->state->args.timestr[0]) { strftime(tbuf, sizeof(tbuf), surface->state->args.timestr, tm); *tstr = tbuf; @@ -63,15 +62,7 @@ static void subtext(struct swaylock_surface *surface, char **tstr, char **dstr, } if (surface->state->args.clock && surface->state->args.datestr[0]) { - // Create a temporary buffer to hold the modified date string - char tempbuf[256]; - - // 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 + strftime(dbuf, sizeof(dbuf), surface->state->args.datestr, tm); *dstr = dbuf; } else { *dstr = NULL; From 626176e8a200731542015c62f0f116e78da80dea Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sun, 25 Feb 2024 18:01:58 -0500 Subject: [PATCH 14/19] Changed battery display size --- render.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/render.c b/render.c index 98c3f010..0919ec23 100644 --- a/render.c +++ b/render.c @@ -381,7 +381,7 @@ void render_frame(struct swaylock_surface *surface) { /* Bottom */ - cairo_set_font_size(cairo, arc_radius / 6.0f); + cairo_set_font_size(cairo, arc_radius / 5.5f); cairo_text_extents(cairo, text_l2, &extents_l2); cairo_font_extents(cairo, &fe_l2); x_l2 = (buffer_width / 2) - @@ -409,13 +409,13 @@ void render_frame(struct swaylock_surface *surface) { /* Even more Bottom */ - cairo_set_font_size(cairo, arc_radius / 6.0f); + cairo_set_font_size(cairo, arc_radius / 5.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; + (fe_l3.height / 2 - fe_l3.descent) + arc_radius / 1.9f; cairo_move_to(cairo, x_l3, y_l3); cairo_show_text(cairo, text_l3); From cbabee5d646d06d8834ba59faafbe0137b84e9e3 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Tue, 5 Mar 2024 13:02:07 -0500 Subject: [PATCH 15/19] Now outputs the output of a battery script instead of directly reading the battery --- main.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 107 insertions(+), 11 deletions(-) diff --git a/main.c b/main.c index 681ff79f..4fb0b928 100644 --- a/main.c +++ b/main.c @@ -31,29 +31,125 @@ #include "ext-session-lock-v1-client-protocol.h" #define MAX_BATTERY_STR_LEN 5 +#define MAX_OUTPUT_SIZE 1024 static struct swaylock_state state; +void update_script() { + FILE *pipe; + char path[1035]; + char output[MAX_OUTPUT_SIZE]; + char *battery_str = (char *)malloc((MAX_OUTPUT_SIZE + 1) * sizeof(char)); + + pipe = popen("~/Programming/battery_script.sh", "r"); + if (pipe == NULL) { + fprintf(stderr, "Failed to run battery script.\n"); + return; + } + + memset(output, 0, sizeof(output)); + + while (fgets(path, sizeof(path), pipe) != NULL) { + path[strcspn(path, "\r\n")] = '\0'; + strcat(output, path); + } + + pclose(pipe); + + printf("%s\n", output); + + // Print size of output + printf("Size of output: %lu\n", strlen(output)); + + // Print size of battery_str + printf("Size of battery_str: %lu\n", sizeof(state.args.battery_str)); + + snprintf(battery_str, MAX_OUTPUT_SIZE, "%s", output); + + state.args.battery_str = battery_str; + + // strcpy(state.args.battery_str, output); +} + +// void update_battery() { +// FILE *pipe; +// char buffer[128]; +// char *battery_str = (char *)malloc((MAX_BATTERY_STR_LEN + 1) * sizeof(char)); + +// // Run the battery script and read its output +// pipe = popen("~/Programming/battery_script.sh", "r"); + +// if (pipe == NULL) { +// fprintf(stderr, "Failed to run battery script.\n"); +// strcpy(state.args.battery_str, "ERROR"); // Set battery string to "ERROR" +// return; +// } + +// // Read battery percentage from the output of the script +// if (fgets(buffer, sizeof(buffer), pipe) == NULL) { +// pclose(pipe); +// strcpy(state.args.battery_str, "ERROR"); // Set battery string to "ERROR" +// return; +// } + +// // Close the pipe +// pclose(pipe); + +// // Remove newline character +// size_t len = strlen(buffer); +// if (len > 0 && buffer[len-1] == '\n') +// buffer[len-1] = '\0'; + +// // Copy the battery percentage to battery_str +// strncpy(battery_str, buffer, MAX_BATTERY_STR_LEN); + +// // Assuming state.args.battery_str is a global variable +// // Set battery_str to the battery string +// strncpy(state.args.battery_str, battery_str, MAX_BATTERY_STR_LEN); + +// // Free the allocated memory +// free(battery_str); +// } + + void update_battery() { - FILE *file; + FILE *pipe; char *battery_str = (char *)malloc((MAX_BATTERY_STR_LEN + 1) * sizeof(char)); char buffer[128]; int battery_percentage; - // Open the file containing battery information - file = fopen("/sys/class/power_supply/BAT0/capacity", "r"); + // // Open the file containing battery information + // file = fopen("/sys/class/power_supply/BAT0/capacity", "r"); + + // Run the battery script and read its output + pipe = popen("~/Programming/battery_script.sh", "r"); + + // // Read battery percentage from the file + // if (fgets(buffer, sizeof(buffer), file) == NULL) { + // fclose(file); + // } + + if (pipe == NULL) { + fprintf(stderr, "Failed to run battery script.\n"); + strcpy(state.args.battery_str, "ERROR"); // Set battery string to "ERROR" + return; + } - // Read battery percentage from the file - if (fgets(buffer, sizeof(buffer), file) == NULL) { - fclose(file); + // Read battery percentage from the output of the script + if (fgets(buffer, sizeof(buffer), pipe) == NULL) { + pclose(pipe); + strcpy(state.args.battery_str, "ERROR"); // Set battery string to "ERROR" + return; } + // Close the file + // fclose(file); + + pclose(pipe); + // Convert the read value to an integer battery_percentage = atoi(buffer); - // Close the file - fclose(file); - // Print the battery percentage snprintf(battery_str, MAX_BATTERY_STR_LEN, "%d%%", battery_percentage); @@ -432,7 +528,7 @@ static void surface_frame_handle_done(void *data, struct wl_callback *callback, uint32_t time) { if (state.args.display_battery) { - update_battery(); + update_script(); }; struct swaylock_surface *surface = data; @@ -1665,7 +1761,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, if (state) { state->args.display_battery = true; } - update_battery(); + update_script(); break; case LO_TIMESTR: if (state) { From f96b66f55d0c6a34bfc2428804aabad4dfbb1284 Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Tue, 5 Mar 2024 13:05:13 -0500 Subject: [PATCH 16/19] Add script --- main.c | 13 ++----------- scripts/battery_script.sh | 1 + 2 files changed, 3 insertions(+), 11 deletions(-) create mode 100755 scripts/battery_script.sh diff --git a/main.c b/main.c index 4fb0b928..7dd103f1 100644 --- a/main.c +++ b/main.c @@ -41,7 +41,8 @@ void update_script() { char output[MAX_OUTPUT_SIZE]; char *battery_str = (char *)malloc((MAX_OUTPUT_SIZE + 1) * sizeof(char)); - pipe = popen("~/Programming/battery_script.sh", "r"); + pipe = popen("~/Programming/swaylock-effects/scripts/battery_script.sh", "r"); + if (pipe == NULL) { fprintf(stderr, "Failed to run battery script.\n"); return; @@ -56,19 +57,9 @@ void update_script() { pclose(pipe); - printf("%s\n", output); - - // Print size of output - printf("Size of output: %lu\n", strlen(output)); - - // Print size of battery_str - printf("Size of battery_str: %lu\n", sizeof(state.args.battery_str)); - snprintf(battery_str, MAX_OUTPUT_SIZE, "%s", output); state.args.battery_str = battery_str; - - // strcpy(state.args.battery_str, output); } // void update_battery() { diff --git a/scripts/battery_script.sh b/scripts/battery_script.sh new file mode 100755 index 00000000..d05cc53c --- /dev/null +++ b/scripts/battery_script.sh @@ -0,0 +1 @@ +echo $(cat /sys/class/power_supply/BAT0/capacity)% From cbba048d4a93a28755f489d4fc1c47bd864e055c Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Fri, 8 Mar 2024 12:26:07 -0500 Subject: [PATCH 17/19] Can now run any input script and display the output with --battery 'script_path'. Will soon change all instances of battery to script. --- README.md | 2 +- include/swaylock.h | 1 + main.c | 94 ++------------------------------------- scripts/battery_script.sh | 2 +- 4 files changed, 7 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index fd81f649..7d6b9c2a 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ which is no longer maintained. swaylock \ --screenshots \ - --battery \ + --battery ~/path_to_script \ --clock \ --indicator \ --indicator-radius 100 \ diff --git a/include/swaylock.h b/include/swaylock.h index 7238e95f..80f2955b 100644 --- a/include/swaylock.h +++ b/include/swaylock.h @@ -84,6 +84,7 @@ struct swaylock_args { bool display_battery; char *battery_str; + char *battery_path; char *text_cleared; char *text_caps_lock; diff --git a/main.c b/main.c index 7dd103f1..fde84f3e 100644 --- a/main.c +++ b/main.c @@ -41,7 +41,7 @@ void update_script() { char output[MAX_OUTPUT_SIZE]; char *battery_str = (char *)malloc((MAX_OUTPUT_SIZE + 1) * sizeof(char)); - pipe = popen("~/Programming/swaylock-effects/scripts/battery_script.sh", "r"); + pipe = popen(state.args.battery_path, "r"); if (pipe == NULL) { fprintf(stderr, "Failed to run battery script.\n"); @@ -62,94 +62,6 @@ void update_script() { state.args.battery_str = battery_str; } -// void update_battery() { -// FILE *pipe; -// char buffer[128]; -// char *battery_str = (char *)malloc((MAX_BATTERY_STR_LEN + 1) * sizeof(char)); - -// // Run the battery script and read its output -// pipe = popen("~/Programming/battery_script.sh", "r"); - -// if (pipe == NULL) { -// fprintf(stderr, "Failed to run battery script.\n"); -// strcpy(state.args.battery_str, "ERROR"); // Set battery string to "ERROR" -// return; -// } - -// // Read battery percentage from the output of the script -// if (fgets(buffer, sizeof(buffer), pipe) == NULL) { -// pclose(pipe); -// strcpy(state.args.battery_str, "ERROR"); // Set battery string to "ERROR" -// return; -// } - -// // Close the pipe -// pclose(pipe); - -// // Remove newline character -// size_t len = strlen(buffer); -// if (len > 0 && buffer[len-1] == '\n') -// buffer[len-1] = '\0'; - -// // Copy the battery percentage to battery_str -// strncpy(battery_str, buffer, MAX_BATTERY_STR_LEN); - -// // Assuming state.args.battery_str is a global variable -// // Set battery_str to the battery string -// strncpy(state.args.battery_str, battery_str, MAX_BATTERY_STR_LEN); - -// // Free the allocated memory -// free(battery_str); -// } - - -void update_battery() { - FILE *pipe; - char *battery_str = (char *)malloc((MAX_BATTERY_STR_LEN + 1) * sizeof(char)); - char buffer[128]; - int battery_percentage; - - // // Open the file containing battery information - // file = fopen("/sys/class/power_supply/BAT0/capacity", "r"); - - // Run the battery script and read its output - pipe = popen("~/Programming/battery_script.sh", "r"); - - // // Read battery percentage from the file - // if (fgets(buffer, sizeof(buffer), file) == NULL) { - // fclose(file); - // } - - if (pipe == NULL) { - fprintf(stderr, "Failed to run battery script.\n"); - strcpy(state.args.battery_str, "ERROR"); // Set battery string to "ERROR" - return; - } - - // Read battery percentage from the output of the script - if (fgets(buffer, sizeof(buffer), pipe) == NULL) { - pclose(pipe); - strcpy(state.args.battery_str, "ERROR"); // Set battery string to "ERROR" - return; - } - - // Close the file - // fclose(file); - - pclose(pipe); - - // Convert the read value to an integer - battery_percentage = atoi(buffer); - - // Print the battery percentage - snprintf(battery_str, MAX_BATTERY_STR_LEN, "%d%%", battery_percentage); - - // Set battery_str to the battery percentage - state.args.battery_str = battery_str; - - // strcpy(state.args.battery_str, battery_str); -} - // returns a positive integer in milliseconds static uint32_t parse_seconds(const char *seconds) { char *endptr; @@ -1181,7 +1093,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, {"time-effects", no_argument, NULL, LO_TIME_EFFECTS}, {"indicator", no_argument, NULL, LO_INDICATOR}, {"clock", no_argument, NULL, LO_CLOCK}, - {"battery", no_argument, NULL, LO_BATTERY}, + {"battery", required_argument, NULL, LO_BATTERY}, {"timestr", required_argument, NULL, LO_TIMESTR}, {"datestr", required_argument, NULL, LO_DATESTR}, {"fade-in", required_argument, NULL, LO_FADE_IN}, @@ -1750,6 +1662,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, break; case LO_BATTERY: if (state) { + state->args.battery_path = strdup(optarg); state->args.display_battery = true; } update_script(); @@ -1983,6 +1896,7 @@ int main(int argc, char **argv) { .display_battery = false, .battery_str = NULL, + .battery_path = NULL, .text_cleared = strdup("Cleared"), .text_caps_lock = strdup("Caps Lock"), diff --git a/scripts/battery_script.sh b/scripts/battery_script.sh index d05cc53c..d080b74c 100755 --- a/scripts/battery_script.sh +++ b/scripts/battery_script.sh @@ -1 +1 @@ -echo $(cat /sys/class/power_supply/BAT0/capacity)% +echo $(cat /sys/class/power_supply/BAT0/capacity)% \ No newline at end of file From 09891aa17b5f22f1e6dfa9e1cc456a3230aec9ea Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Fri, 8 Mar 2024 14:02:20 -0500 Subject: [PATCH 18/19] Replaced all instances of battery with script. --- README.md | 10 +++++----- include/swaylock.h | 6 +++--- main.c | 33 ++++++++++++++++----------------- render.c | 10 +++++----- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 7d6b9c2a..8ef86ed6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# swaylock-effects-battery +# swaylock-effects-script -Swaylock-effects-battery is a fork of [swaylock-effects](https://github.com/jirutka/swaylock-effects) -which adds a battery percentage display feature. +Swaylock-effects-script is a fork of [swaylock-effects](https://github.com/jirutka/swaylock-effects) +which adds a script output display feature. Swaylock-effects is a fork of [swaylock](https://github.com/swaywm/swaylock) which adds built-in screenshots and image manipulation effects like blurring. @@ -18,7 +18,7 @@ which is no longer maintained. swaylock \ --screenshots \ - --battery ~/path_to_script \ + --script ~/path_to_script \ --clock \ --indicator \ --indicator-radius 100 \ @@ -37,7 +37,7 @@ which is no longer maintained. The main new features compared to upstream swaylock are: -* `--battery` to display the battery percentage +* `--script` to display the script output * `--screenshots` to use screenshots instead of an image on disk or a color * `--clock` to show date/time in the indicator * Use `--indicator` to make the indicator always active diff --git a/include/swaylock.h b/include/swaylock.h index 80f2955b..1a0e399d 100644 --- a/include/swaylock.h +++ b/include/swaylock.h @@ -82,9 +82,9 @@ struct swaylock_args { bool password_grace_no_mouse; bool password_grace_no_touch; - bool display_battery; - char *battery_str; - char *battery_path; + bool display_script; + char *script_str; + char *script_path; char *text_cleared; char *text_caps_lock; diff --git a/main.c b/main.c index fde84f3e..a6f19969 100644 --- a/main.c +++ b/main.c @@ -30,7 +30,6 @@ #include "wlr-screencopy-unstable-v1-client-protocol.h" #include "ext-session-lock-v1-client-protocol.h" -#define MAX_BATTERY_STR_LEN 5 #define MAX_OUTPUT_SIZE 1024 static struct swaylock_state state; @@ -39,12 +38,12 @@ void update_script() { FILE *pipe; char path[1035]; char output[MAX_OUTPUT_SIZE]; - char *battery_str = (char *)malloc((MAX_OUTPUT_SIZE + 1) * sizeof(char)); + char *script_str = (char *)malloc((MAX_OUTPUT_SIZE + 1) * sizeof(char)); - pipe = popen(state.args.battery_path, "r"); + pipe = popen(state.args.script_path, "r"); if (pipe == NULL) { - fprintf(stderr, "Failed to run battery script.\n"); + fprintf(stderr, "Failed to run script.\n"); return; } @@ -57,9 +56,9 @@ void update_script() { pclose(pipe); - snprintf(battery_str, MAX_OUTPUT_SIZE, "%s", output); + snprintf(script_str, MAX_OUTPUT_SIZE, "%s", output); - state.args.battery_str = battery_str; + state.args.script_str = script_str; } // returns a positive integer in milliseconds @@ -430,7 +429,7 @@ static const struct wl_callback_listener surface_frame_listener; static void surface_frame_handle_done(void *data, struct wl_callback *callback, uint32_t time) { - if (state.args.display_battery) { + if (state.args.display_script) { update_script(); }; @@ -1012,7 +1011,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, LO_TIME_EFFECTS, LO_INDICATOR, LO_CLOCK, - LO_BATTERY, + LO_SCRIPT, LO_TIMESTR, LO_DATESTR, LO_FADE_IN, @@ -1093,7 +1092,7 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, {"time-effects", no_argument, NULL, LO_TIME_EFFECTS}, {"indicator", no_argument, NULL, LO_INDICATOR}, {"clock", no_argument, NULL, LO_CLOCK}, - {"battery", required_argument, NULL, LO_BATTERY}, + {"script", required_argument, NULL, LO_SCRIPT}, {"timestr", required_argument, NULL, LO_TIMESTR}, {"datestr", required_argument, NULL, LO_DATESTR}, {"fade-in", required_argument, NULL, LO_FADE_IN}, @@ -1153,8 +1152,8 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, "Disable the unlock indicator.\n" " --indicator " "Always show the indicator.\n" - " --battery " - "Show the battery percentage.\n" + " --script " + "Show the script output.\n" " --clock " "Show time and date.\n" " --timestr " @@ -1660,10 +1659,10 @@ static int parse_options(int argc, char **argv, struct swaylock_state *state, state->args.clock = true; } break; - case LO_BATTERY: + case LO_SCRIPT: if (state) { - state->args.battery_path = strdup(optarg); - state->args.display_battery = true; + state->args.script_path = strdup(optarg); + state->args.display_script = true; } update_script(); break; @@ -1894,9 +1893,9 @@ int main(int argc, char **argv) { .allow_fade = true, .password_grace_period = 0, - .display_battery = false, - .battery_str = NULL, - .battery_path = NULL, + .display_script = false, + .script_str = NULL, + .script_path = NULL, .text_cleared = strdup("Cleared"), .text_caps_lock = strdup("Caps Lock"), diff --git a/render.c b/render.c index 0919ec23..19711236 100644 --- a/render.c +++ b/render.c @@ -68,9 +68,9 @@ 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); + if (surface->state->args.display_script) { + // Write script output + snprintf(bbuf, sizeof(bbuf), "%s", surface->state->args.script_str); *bstr = bbuf; } else { *bstr = NULL; @@ -300,7 +300,7 @@ void render_frame(struct swaylock_surface *surface) { snprintf(attempts, sizeof(attempts), "%d", state->failed_attempts); text = attempts; } - } else if (state->args.clock || state->args.display_battery) { + } else if (state->args.clock || state->args.display_script) { subtext(surface, &text_l1, &text_l2, &text_l3); } @@ -320,7 +320,7 @@ void render_frame(struct swaylock_surface *surface) { } break; default: - if (state->args.clock || state->args.display_battery) + if (state->args.clock || state->args.display_script) subtext(surface, &text_l1, &text_l2, &text_l3); break; } From 86381d84b759cf5cbba717d2025460a79a80fffc Mon Sep 17 00:00:00 2001 From: Sidharth Rao Date: Sat, 2 Nov 2024 00:19:20 -0400 Subject: [PATCH 19/19] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8ef86ed6..3aab8cd1 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,8 @@ which adds built-in screenshots and image manipulation effects like blurring. It's inspired by [i3lock-color](https://github.com/PandorasFox/i3lock-color), although the feature sets aren't perfectly overlapping. -This repository ([jirutka/swaylock-effects](https://github.com/jirutka/swaylock-effects)) +This repository ([sidharthmrao/swaylock-effects-script](https://github.com/sidharthmrao/swaylock-effects-script)) +is a fork of ([jirutka/swaylock-effects](https://github.com/jirutka/swaylock-effects)) which is a fork of [mortie/swaylock-effects](https://github.com/mortie/swaylock-effects) which is no longer maintained. @@ -18,7 +19,7 @@ which is no longer maintained. swaylock \ --screenshots \ - --script ~/path_to_script \ + --script `path_to_script` \ --clock \ --indicator \ --indicator-radius 100 \