-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revise scrotWaitUntil interface #323
Merged
N-R-K
merged 4 commits into
resurrecting-open-source-projects:master
from
N-R-K:waitFor
May 31, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ static size_t scrotHaveFileExtension(const char *, char **); | |
static Imlib_Image scrotGrabFocused(void); | ||
static void applyFilterIfRequired(void); | ||
static Imlib_Image scrotGrabAutoselect(void); | ||
static void scrotWaitUntil(const struct timespec *); | ||
static long miliToNanoSec(int); | ||
static Imlib_Image scrotGrabShotMulti(void); | ||
static Imlib_Image scrotGrabShotMonitor(void); | ||
static Imlib_Image scrotGrabStackWindows(void); | ||
|
@@ -108,7 +108,7 @@ int main(int argc, char *argv[]) | |
struct tm *tm; | ||
|
||
/* Get the time ASAP to reduce the timing error in case --delay is used. */ | ||
clock_gettime(CONTINUOUS_CLOCK, &opt.delayStart); | ||
opt.delayStart = clockNow(); | ||
|
||
atexit(uninitXAndImlib); | ||
|
||
|
@@ -145,7 +145,7 @@ int main(int argc, char *argv[]) | |
* screenshot-taking above or it will skew the timing. | ||
*/ | ||
clock_gettime(CLOCK_REALTIME, &timeStamp); | ||
if (timeStamp.tv_nsec >= 500*1000*1000) { | ||
if (timeStamp.tv_nsec >= miliToNanoSec(500)) { | ||
/* Round the timestamp to the nearest second. */ | ||
timeStamp.tv_sec++; | ||
} | ||
|
@@ -335,38 +335,67 @@ void scrotDoDelay(void) | |
dprintf(STDERR_FILENO, "Taking shot in "); | ||
for (int i = opt.delay; i > 0; i--) { | ||
dprintf(STDERR_FILENO, "%d.. ", i); | ||
opt.delayStart.tv_sec++; | ||
scrotWaitUntil(&opt.delayStart); | ||
opt.delayStart = scrotSleepFor(opt.delayStart, 1000); | ||
} | ||
dprintf(STDERR_FILENO, "0.\n"); | ||
} else { | ||
opt.delayStart.tv_sec += opt.delay; | ||
scrotWaitUntil(&opt.delayStart); | ||
scrotSleepFor(opt.delayStart, opt.delay * 1000); | ||
} | ||
} | ||
|
||
/* scrotWaitUntil: clock_nanosleep with a simpler interface and no EINTR nagging | ||
static long miliToNanoSec(int ms) | ||
{ | ||
return ms * 1000L * 1000L; | ||
} | ||
|
||
/* clockNow() has the exact same semantics as CLOCK_MONOTONIC. Except that on | ||
* Linux, CLOCK_MONOTONIC does not progress while the system is suspended, so | ||
* the non-standard CLOCK_BOOTTIME is used instead to avoid this bug. | ||
*/ | ||
static void scrotWaitUntil(const struct timespec *time) | ||
struct timespec clockNow(void) | ||
{ | ||
/* OpenBSD and OS X lack clock_nanosleep(), so we use a trivial algorithm to | ||
* correct for drift and call nanosleep() everywhere. | ||
*/ | ||
struct timespec ret; | ||
#if defined(__linux__) | ||
clock_gettime(CLOCK_BOOTTIME, &ret); | ||
#else | ||
clock_gettime(CLOCK_MONOTONIC, &ret); | ||
#endif | ||
return ret; | ||
} | ||
|
||
/* OpenBSD and OS X lack clock_nanosleep(), so we call nanosleep() and use a | ||
* trivial algorithm to correct for drift. The end timespec is returned for | ||
* callers that want it. EINTR is also dealt with. | ||
*/ | ||
struct timespec scrotSleepFor(struct timespec start, int ms) | ||
{ | ||
scrotAssert(ms >= 0); | ||
struct timespec end = { | ||
.tv_sec = start.tv_sec + (ms / 1000), | ||
.tv_nsec = start.tv_nsec + miliToNanoSec(ms % 1000), | ||
}; | ||
if (end.tv_nsec >= miliToNanoSec(1000)) { | ||
++end.tv_sec; | ||
end.tv_nsec -= miliToNanoSec(1000); | ||
} | ||
|
||
struct timespec tmp; | ||
do { | ||
clock_gettime(CONTINUOUS_CLOCK, &tmp); | ||
tmp = clockNow(); | ||
|
||
/* XXX: Use timespecsub(). OS X doesn't have that BSD macro, and libbsd | ||
* doesn't support OS X save for an unmaintained fork. libobsd supports | ||
* OS X but doesn't have the macro yet. | ||
*/ | ||
tmp.tv_sec = time->tv_sec - tmp.tv_sec; | ||
tmp.tv_nsec = time->tv_nsec - tmp.tv_nsec; | ||
tmp.tv_sec = end.tv_sec - tmp.tv_sec; | ||
tmp.tv_nsec = end.tv_nsec - tmp.tv_nsec; | ||
if (tmp.tv_nsec < 0) { | ||
tmp.tv_sec--; | ||
tmp.tv_nsec += 1000000000L; | ||
--tmp.tv_sec; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The webkit style guide doesn't say anything about preference toward pre vs post increments. And Ctrl+F "++" reveals many examples with both being used. |
||
tmp.tv_nsec += miliToNanoSec(1000); | ||
} | ||
} while (nanosleep(&tmp, NULL) == -1 && errno == EINTR); | ||
} while (nanosleep(&tmp, NULL) < 0 && errno == EINTR); | ||
|
||
return end; | ||
} | ||
|
||
/* Clip rectangle nicely */ | ||
|
@@ -429,8 +458,7 @@ int scrotGetGeometry(Window target, int *rx, int *ry, int *rw, int *rh) | |
/* HACK: there doesn't seem to be any way to figure out whether the | ||
* raise request was accepted or rejected. so just sleep a bit to | ||
* give the WM some time to update. */ | ||
struct timespec t = { .tv_nsec = 160 * 1000L * 1000L }; | ||
while (nanosleep(&t, &t) < 0 && errno == EINTR); | ||
scrotSleepFor(clockNow(), 160); | ||
} | ||
} | ||
stat = XGetWindowAttributes(disp, target, &attr); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't think the
CONTINUOUS_CLOCK
needed to be "public" anymore - since you can just useclockNow()
instead. So theifdef
can be done internally forclockNow
.(Though the main reason I added this function was to work around the awkwardness of
clock_gettime
's outparam, e.gscrotSleepFor(clockNow(), 64)
is much easier to use.)