i3lock-color

The world's most popular non-default computer lockscreen.
Index Commits Files Refs README LICENSE
commit 59cdccb3e510c5d393cab9656b8a30efd37b45e6
parent 3edfb79f96254e93e5c98d54add290e789cfb60d
Author: Pandora <Pandora@techfo.xyz>
Date:   Fri,  8 Dec 2017 02:29:05 -0500

make render thread optional

theoretical potential security concerns; no use unless using bar and you care... I hammered pretty hard on my kb for a while to try and see if it's possible to configure it poorly and get the render thread to crash, but to no avail.

Diffstat:
Mi3lock.c | 28++++++++++++++++++----------
Mi3lock.h | 3+++
Munlock_indicator.c | 23++++++++++++++++++++++-
Munlock_indicator.h | 3++-
4 files changed, 45 insertions(+), 12 deletions(-)
diff --git a/i3lock.c b/i3lock.c
@@ -182,6 +182,9 @@ bool skip_repeated_empty_password = false;
 
 // for the rendering thread, so we can clean it up
 pthread_t draw_thread;
+// main thread still sometimes calls redraw()
+// allow you to disable. handy if you use bar with lots of crap.
+bool redraw_thread = false;
 
 #define BAR_VERT 0
 #define BAR_FLAT 1
@@ -1097,6 +1100,8 @@ int main(int argc, char *argv[]) {
         {"bar-periodic-step", required_argument, NULL, 0},
         {"bar-position", required_argument, NULL, 0},
 
+        {"redraw-thread", no_argument, NULL, 0},
+
         {NULL, no_argument, NULL, 0}};
 
     if ((pw = getpwuid(getuid())) == NULL)
@@ -1579,9 +1584,10 @@ int main(int argc, char *argv[]) {
                     if (sscanf(arg, "%31s", bar_expr) != 1) {
                         errx(1, "bar-position must be of the form [pos] with a max length of 31\n");
                     }
-                
                 }
-                
+                else if (strcmp(longopts[longoptind].name, "redraw-thread") == 0) {
+                    redraw_thread = true;
+                }
 
                 break;
             case 'f':
@@ -1816,15 +1822,17 @@ int main(int argc, char *argv[]) {
      * file descriptor becomes readable). */
     ev_invoke(main_loop, xcb_check, 0);
 
-// boy i sure hope this doesnt change in the future
-#define NANOSECONDS_IN_SECOND 1000000000
     if (show_clock || bar_enabled) {
-        struct timespec ts;
-        double s;
-        double ns = modf(refresh_rate, &s);
-        ts.tv_sec = (time_t) s;
-        ts.tv_nsec = ns * NANOSECONDS_IN_SECOND;
-        (void) pthread_create(&draw_thread, NULL, start_time_redraw_tick, (void*) &ts);
+        if (redraw_thread) {
+            struct timespec ts;
+            double s;
+            double ns = modf(refresh_rate, &s);
+            ts.tv_sec = (time_t) s;
+            ts.tv_nsec = ns * NANOSECONDS_IN_SECOND;
+            (void) pthread_create(&draw_thread, NULL, start_time_redraw_tick_pthread, (void*) &ts);
+        } else {
+            start_time_redraw_tick(main_loop);
+        }
     }
     ev_loop(main_loop, 0);
 
diff --git a/i3lock.h b/i3lock.h
@@ -1,6 +1,9 @@
 #ifndef _I3LOCK_H
 #define _I3LOCK_H
 
+// boy i sure hope this doesnt change in the future
+#define NANOSECONDS_IN_SECOND 1000000000
+
 /* This macro will only print debug output when started with --debug.
  * This is important because xautolock (for example) closes stdout/stderr by
  * default, so just printing something to stdout will lead to the data ending
diff --git a/unlock_indicator.c b/unlock_indicator.c
@@ -133,6 +133,9 @@ extern xcb_screen_t *screen;
  * Local variables.
  ******************************************************************************/
 
+/* time stuff */
+static struct ev_periodic *time_redraw_tick;
+
 /* Cache the screen’s visual, necessary for creating a Cairo context. */
 static xcb_visualtype_t *vistype;
 
@@ -969,7 +972,7 @@ void clear_indicator(void) {
     redraw_screen();
 }
 
-void* start_time_redraw_tick(void* arg) {
+void* start_time_redraw_tick_pthread(void* arg) {
     struct timespec *ts = (struct timespec *) arg;
     while(1) {
         nanosleep(ts, NULL);
@@ -977,3 +980,21 @@ void* start_time_redraw_tick(void* arg) {
     }
     return NULL;
 }
+
+static void time_redraw_cb(struct ev_loop *loop, ev_periodic *w, int revents) {
+    redraw_screen();
+}
+
+void start_time_redraw_tick(struct ev_loop* main_loop) {
+    if (time_redraw_tick) {
+        ev_periodic_set(time_redraw_tick, 0., refresh_rate, 0);
+        ev_periodic_again(main_loop, time_redraw_tick);
+    } else {
+        if (!(time_redraw_tick = calloc(sizeof(struct ev_periodic), 1))) {
+           return;
+        }
+        ev_periodic_init(time_redraw_tick, time_redraw_cb, 0., refresh_rate, 0);
+        ev_periodic_start(main_loop, time_redraw_tick);
+    }
+}
+
diff --git a/unlock_indicator.h b/unlock_indicator.h
@@ -51,5 +51,6 @@ void init_colors_once(void);
 void redraw_screen(void);
 void clear_indicator(void);
 void start_time_redraw_timeout(void);
-void* start_time_redraw_tick(void* arg);
+void* start_time_redraw_tick_pthread(void* arg);
+void start_time_redraw_tick(struct ev_loop* main_loop);
 #endif