Skip to content

Instantly share code, notes, and snippets.

@dequis
Created July 20, 2022 10:09
Show Gist options
  • Save dequis/fe411d9c50567a26d0134f0bfb2c2433 to your computer and use it in GitHub Desktop.
Save dequis/fe411d9c50567a26d0134f0bfb2c2433 to your computer and use it in GitHub Desktop.
sleep(4) frizz reduction spray for zoom
/*
sleep(4) frizz reduction spray for zoom
because this thing decides to freeze for a long time every time i plug/unplug
my usb hub, or when any cable is loose. seems to be 4 seconds multiplied by
number of plug/unplug events received
caveats:
- your video will still be frozen as usual, re-enable video
- this might increase the risk of your devices not showing up after it
unfreezes (hopefully the first longer sleep takes care of that)
- no manual entry for sleep in section 4
to compile:
$ gcc -fPIC -Wall -ldl -shared ldpreload.c -o ldpreload.so
to use:
$ LD_PRELOAD=$PWD/ldpreload.so zoom
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
/* seconds since the last sleep(4) call in which shorter sleeps are applied */
#define GRACE_PERIOD_S 4
/* the first sleep is longer, which seems needed to get devices back */
#define FIRST_LONG_SLEEP_MS 2000
/* each sleep within the grace period is shorter, to avoid accumulating */
#define SHORTER_SLEEPS_MS 1000
static int (* sleep_real)(wchar_t) = NULL;
static __attribute__((__constructor__)) void init(void) {
sleep_real = dlsym(RTLD_NEXT, "sleep");
}
unsigned int sleep(unsigned int seconds) {
static time_t last_long_sleep = 0;
if (seconds == 4) {
time_t now = time(NULL);
if (now - last_long_sleep < 3) {
/* subsequent sleeps within 3 seconds are shorter each */
usleep(SHORTER_SLEEPS_MS * 1000);
} else {
/* the first sleep is longer,
* which appears to be needed for getting devices back */
usleep(FIRST_LONG_SLEEP_MS * 1000);
}
last_long_sleep = now;
return 0;
}
return sleep_real(seconds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment