Skip to content

Commit

Permalink
make render thread optional
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PandorasFox committed Dec 8, 2017
1 parent 3edfb79 commit 59cdccb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
28 changes: 18 additions & 10 deletions i3lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions i3lock.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 22 additions & 1 deletion unlock_indicator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -969,11 +972,29 @@ 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);
redraw_screen();
}
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);
}
}

3 changes: 2 additions & 1 deletion unlock_indicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 59cdccb

Please sign in to comment.