Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lightsd: get the build system and time_monotonic to work on Windows
This is the first step towards Windows portability.
- Loading branch information
Showing
23 changed files
with
325 additions
and
60 deletions.
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
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 |
---|---|---|
@@ -1,8 +1,12 @@ | ||
FUNCTION(ADD_ALL_SUBDIRECTORIES) | ||
FILE(GLOB SUBDIRECTORIES "*") | ||
FOREACH (ENTRY ${SUBDIRECTORIES}) | ||
IF (IS_DIRECTORY ${ENTRY} AND EXISTS "${ENTRY}/CMakeLists.txt") | ||
ADD_SUBDIRECTORY(${ENTRY}) | ||
IF ( | ||
IS_DIRECTORY "${ENTRY}" AND | ||
NOT IS_SYMLINK "${ENTRY}" AND | ||
EXISTS "${ENTRY}/CMakeLists.txt" | ||
) | ||
ADD_SUBDIRECTORY("${ENTRY}") | ||
ENDIF () | ||
ENDFOREACH () | ||
ENDFUNCTION() |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <stdlib.h> | ||
|
||
#if @BIG_ENDIAN_SYSTEM@ | ||
# define htobe16(x) (x) | ||
# define htole16(x) _byteswap_ushort(x) | ||
# define be16toh(x) (x) | ||
# define le16toh(x) _byteswap_ushort(x) | ||
|
||
# define htobe32(x) (x) | ||
# define htole32(x) _byteswap_ulong(x) | ||
# define be32toh(x) (x) | ||
# define le32toh(x) _byteswap_ulong(x) | ||
|
||
# define htobe64(x) (x) | ||
# define htole64(x) _byteswap_uint64(x) | ||
# define be64toh(x) (x) | ||
# define le64toh(x) _byteswap_uint64(x) | ||
#else | ||
# define htobe16(x) _byteswap_ushort(x) | ||
# define htole16(x) (x) | ||
# define be16toh(x) _byteswap_ushort(x) | ||
# define le16toh(x) (x) | ||
|
||
# define htobe32(x) _byteswap_ulong(x) | ||
# define htole32(x) (x) | ||
# define be32toh(x) _byteswap_ulong(x) | ||
# define le32toh(x) (x) | ||
|
||
# define htobe64(x) _byteswap_uint64(x) | ||
# define htole64(x) (x) | ||
# define be64toh(x) _byteswap_uint64(x) | ||
# define le64toh(x) (x) | ||
#endif |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) 2017, Louis Opter <louis@opter.org> | ||
// | ||
// This file is part of lighstd. | ||
// | ||
// lighstd is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// lighstd is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with lighstd. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
// See: | ||
// | ||
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx | ||
// https://msdn.microsoft.com/en-us/library/dd743626(v=vs.85).aspx | ||
|
||
#include <Windows.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
|
||
#include "core/time_monotonic.h" | ||
#include "core/lightsd.h" | ||
|
||
enum { TIMER_RESOLUTION_MS = 1 }; | ||
|
||
static UINT | ||
_lgtd_set_timer_resolution(UINT resolution_ms) | ||
{ | ||
TIMECAPS tc; | ||
if (timeGetDevCaps(&tc, sizeof(tc)) != TIMERR_NOERROR) { | ||
fprintf(stderr, "lightsd: multimedia timers unavailable."); | ||
exit(1); | ||
} | ||
|
||
UINT resolution = LGTD_MAX(tc.wPeriodMin, resolution_ms); | ||
resolution = LGTD_MIN(resolution, tc.wPeriodMax); | ||
timeBeginPeriod(resolution); | ||
return resolution; | ||
} | ||
|
||
static void | ||
_lgtd_reset_timer_resolution(UINT resolution_ms) | ||
{ | ||
if (timeEndPeriod(resolution_ms) != TIMERR_NOERROR) { | ||
fprintf(stderr, "lightsd: can't reset timer resolution."); | ||
} | ||
} | ||
|
||
void | ||
lgtd_sleep_monotonic_msecs(int msecs) | ||
{ | ||
UINT actual = _lgtd_set_timer_resolution(TIMER_RESOLUTION_MS); | ||
Sleep(msecs); | ||
_lgtd_reset_timer_resolution(actual); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2017, Louis Opter <louis@opter.org> | ||
// | ||
// This file is part of lighstd. | ||
// | ||
// lighstd is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// lighstd is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with lighstd. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
// This is pretty much "Using QPC in native code" from: | ||
// | ||
// https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx | ||
|
||
#include <Windows.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
#include "core/time_monotonic.h" | ||
|
||
lgtd_time_mono_t | ||
lgtd_time_monotonic_msecs(void) | ||
{ | ||
static LARGE_INTEGER frequency = { .QuadPart = 0 }; // ticks per second | ||
if (frequency.QuadPart == 0 | ||
&& QueryPerformanceFrequency(&frequency) == false) { | ||
LPSTR msg = NULL; | ||
DWORD msg_len = FormatMessageA( | ||
FORMAT_MESSAGE_ALLOCATE_BUFFER | ||
| FORMAT_MESSAGE_FROM_SYSTEM | ||
| FORMAT_MESSAGE_IGNORE_INSERTS, | ||
NULL, | ||
GetLastError(), | ||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | ||
// By default the message is stored in the buffer pointed by | ||
// lpMsgBuf, but we use FORMAT_MESSAGE_ALLOCATE_BUFFER to have | ||
// the buffer allocated for us. | ||
// So instead you need to give the function a pointer on your | ||
// pointer instead of your buffer and do an ugly cast: | ||
(LPSTR)&msg, | ||
0, | ||
NULL | ||
); | ||
if (!msg_len) { | ||
msg = "unknown error"; | ||
} | ||
fprintf(stderr, "lightsd: QPC timer unavailable: %s.\r\n", msg); | ||
exit(1); | ||
} | ||
|
||
LARGE_INTEGER time; | ||
QueryPerformanceCounter(&time); | ||
// Multiply the number of ticks by 1000 first to get a final value in ms: | ||
time.QuadPart *= 1000; | ||
// Then (the order is important to avoid loosing precision) divide by the | ||
// number of ticks per second: | ||
time.QuadPart /= frequency.QuadPart; | ||
return time.QuadPart; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.