Skip to content
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

replace usleep() with nanosleep() in msleep(), add a missing default case in getkey() #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions rlutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
}
#else
#include <stdio.h> // for getch() / printf()

#include <string.h> // for strlen()
RLUTIL_INLINE void locate(int x, int y); // Forward declare for C to avoid warnings
#endif // __cplusplus
Expand All @@ -61,7 +62,8 @@
#define kbhit _kbhit
#else
#include <termios.h> // for getch() and kbhit()
#include <unistd.h> // for getch(), kbhit() and (u)sleep()
#include <unistd.h> // for getch() and kbhit()
#include <time.h> // for nanosleep()
#include <sys/ioctl.h> // for getkey()
#include <sys/types.h> // for kbhit()
#include <sys/time.h> // for kbhit()
Expand Down Expand Up @@ -392,6 +394,7 @@ RLUTIL_INLINE int getkey(void) {
case 'B': return KEY_DOWN;
case 'C': return KEY_RIGHT;
case 'D': return KEY_LEFT;
default: return -1;
}
} else return KEY_ESCAPE;
}
Expand Down Expand Up @@ -637,9 +640,14 @@ RLUTIL_INLINE void msleep(unsigned int ms) {
#ifdef _WIN32
Sleep(ms);
#else
// usleep argument must be under 1 000 000
if (ms > 1000) sleep(ms/1000000);
usleep((ms % 1000000) * 1000);
struct timespec ts;

ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;

if(nanosleep(&ts, NULL) < 0) {
perror("sleep failed");
}
#endif
}

Expand Down