Skip to content

Commit

Permalink
introduce clockNow()
Browse files Browse the repository at this point in the history
this will be useful for converting some of the nanosleep calls into
scrotSleepFor().
  • Loading branch information
N-R-K committed May 27, 2023
1 parent 6772c8d commit 646f713
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
19 changes: 17 additions & 2 deletions src/scrot.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,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);

Expand Down Expand Up @@ -351,6 +351,21 @@ 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.
*/
struct timespec clockNow(void)
{
struct timespec ret;
#if defined(__linux__)
clock_gettime(CLOCK_BOOTTIME, &ret);
#else
clock_gettime(CLOCK_MONOTONIC, &ret);
#endif
return ret;
}

/* scrotWaitUntil: clock_nanosleep with a simpler interface and no EINTR nagging
*/
static void scrotWaitUntil(const struct timespec *time)
Expand All @@ -360,7 +375,7 @@ static void scrotWaitUntil(const struct timespec *time)
*/
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
Expand Down
3 changes: 3 additions & 0 deletions src/scrot.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include <time.h>

#include <X11/Xlib.h>
#include <Imlib2.h>

Expand All @@ -39,5 +41,6 @@ extern Screen *scr;
Window scrotGetWindow(Display *, Window, int, int);
int scrotGetGeometry(Window, int *, int *, int *, int *);
void scrotNiceClip(int *, int *, int *, int *);
struct timespec clockNow(void);
void scrotDoDelay(void);
void scrotGrabMousePointer(const Imlib_Image, const int, const int);
2 changes: 1 addition & 1 deletion src/scrot_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ Imlib_Image scrotSelectionSelectMode(void)
return NULL;

if (!opt.delaySelection) {
clock_gettime(CONTINUOUS_CLOCK, &opt.delayStart);
opt.delayStart = clockNow();
scrotDoDelay();
}

Expand Down
12 changes: 0 additions & 12 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#pragma once

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

/* On Linux, CLOCK_MONOTONIC does not progress while the system is suspended,
* and an alternative non-standard clock which does not suffer from this problem
* called CLOCK_BOOTTIME is available. Scrot's CONTINUOUS_CLOCK has the exact
* same semantics as CLOCK_MONOTONIC, only it avoids this bug.
*/
#if defined(__linux__)
#define CONTINUOUS_CLOCK CLOCK_BOOTTIME
#else
#define CONTINUOUS_CLOCK CLOCK_MONOTONIC
#endif

#ifdef DEBUG
#define scrotAssert(X) do { \
if (!(X)) { \
Expand Down

0 comments on commit 646f713

Please sign in to comment.