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

Added win32 API to the platform module #19

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_subdirectory("flux-foundation")
add_subdirectory("flux-io")
add_subdirectory("flux-logging")
add_subdirectory("flux-meta")
add_subdirectory("flux-platform")

#-----------------------------------------------------------------------------------------------------------------------
# Applications.
Expand Down
6 changes: 6 additions & 0 deletions flux-config/flux/config/features.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
# define FLUX_ALWAYS_INLINE /* nothing */
#endif

#if (!__has_cpp_attribute(__gnu__::__stdcall__) && !defined(__WINE__)) && defined(_MSC_VER)
# define FLUX_STDCALL __stdcall
#else
# define FLUX_STDCALL /* nothing */
#endif

#if __has_cpp_attribute(msvc::no_unique_address)
// MSVC implements [[no_unique_address]] as a silent no-op currently. If/when MSVC breaks its C++
// ABI, it will be changed to work as intended. However, MSVC implements [[msvc::no_unique_address]]
Expand Down
3 changes: 2 additions & 1 deletion flux-foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ flux_static_library(foundation
SOURCE
"flux/foundation/dummy.cpp"
LINK
flux::io)
flux::io
flux::platform)

# code: language="CMake" insertSpaces=true tabSize=4
7 changes: 7 additions & 0 deletions flux-platform/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
flux_interface_library(platform
COMMON
LINK
flux::config
flux::meta)

# code: language="CMake" insertSpaces=true tabSize=4
1 change: 1 addition & 0 deletions flux-platform/flux/platform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
101 changes: 101 additions & 0 deletions flux-platform/flux/platform/win32/api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#pragma once
#include <flux/config.hpp>

#include <cstddef>
#include <cstdint>

namespace flux::win32 {

// clang-format off
#if (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__))
[[__gnu__::__dllimport__]]
#endif
#if (__has_cpp_attribute(__gnu__::__stdcall__) && !defined(__WINE__))
[[__gnu__::__stdcall__]]
#endif
extern void* FLUX_STDCALL GetStdHandle(::std::uint_least32_t) noexcept
#if defined(FLUX_CLANG)
__asm__("GetStdHandle")
#endif
;

#if (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__))
[[__gnu__::__dllimport__]]
#endif
#if (__has_cpp_attribute(__gnu__::__stdcall__) && !defined(__WINE__))
[[__gnu__::__stdcall__]]
#endif
extern int FLUX_STDCALL GetConsoleMode(void*, ::std::uint_least32_t*) noexcept
#if defined(FLUX_CLANG)
__asm__("GetConsoleMode")
#endif
;

#if (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__))
[[__gnu__::__dllimport__]]
#endif
#if (__has_cpp_attribute(__gnu__::__stdcall__) && !defined(__WINE__))
[[__gnu__::__stdcall__]]
#endif
extern int FLUX_STDCALL SetConsoleMode(void*, ::std::uint_least32_t) noexcept
#if defined(FLUX_CLANG)
__asm__("SetConsoleMode")
#endif
;

#if (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__))
[[__gnu__::__dllimport__]]
#endif
#if (__has_cpp_attribute(__gnu__::__stdcall__) && !defined(__WINE__))
[[__gnu__::__stdcall__]]
#endif
#if __has_cpp_attribute(__gnu__::__malloc__)
[[__gnu__::__malloc__]]
#endif
extern void* FLUX_STDCALL HeapAlloc(void*, ::std::uint_least32_t, ::std::size_t) noexcept
#if defined(FLUX_CLANG)
__asm__("HeapAlloc")
#endif
;

#if (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__))
[[__gnu__::__dllimport__]]
#endif
#if (__has_cpp_attribute(__gnu__::__stdcall__) && !defined(__WINE__))
[[__gnu__::__stdcall__]]
#endif
extern void* FLUX_STDCALL HeapFree(void*, ::std::uint_least32_t, void*) noexcept
#if defined(FLUX_CLANG)
__asm__("HeapFree")
#endif
;

#if (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__))
[[__gnu__::__dllimport__]]
#endif
#if (__has_cpp_attribute(__gnu__::__stdcall__) && !defined(__WINE__))
[[__gnu__::__stdcall__]]
#endif
#if __has_cpp_attribute(__gnu__::__const__)
[[__gnu__::__const__]]
#endif
extern void* FLUX_STDCALL GetProcessHeap() noexcept
#if defined(FLUX_CLANG)
__asm__("GetProcessHeap")
#endif
;

#if (__has_cpp_attribute(__gnu__::__dllimport__) && !defined(__WINE__))
[[__gnu__::__dllimport__]]
#endif
#if (__has_cpp_attribute(__gnu__::__stdcall__) && !defined(__WINE__))
[[__gnu__::__stdcall__]]
#endif
extern void* FLUX_STDCALL HeapReAlloc(void*, ::std::uint_least32_t, void*, ::std::size_t) noexcept
#if defined(FLUX_CLANG)
__asm__("HeapReAlloc")
#endif
;
// clang-format on

} // namespace flux::win32
44 changes: 4 additions & 40 deletions flux-playground/playground.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,7 @@
#include <flux/logging.hpp>

#if defined(_WIN64)
// clang-format off
#if __has_cpp_attribute(gnu::dllimport) && !defined(__WINE__)
[[gnu::dllimport]]
#endif
#if __has_cpp_attribute(gnu::stdcall) && !defined(__WINE__)
[[gnu::stdcall]]
#endif
extern void* GetStdHandle(std::uint_least32_t) noexcept
#if defined(FLUX_CLANG)
__asm__("GetStdHandle")
#endif
;

#if __has_cpp_attribute(gnu::dllimport) && !defined(__WINE__)
[[gnu::dllimport]]
#endif
#if __has_cpp_attribute(gnu::stdcall) && !defined(__WINE__)
[[gnu::stdcall]]
#endif
extern int SetConsoleMode(void*, std::uint_least32_t) noexcept
#if defined(FLUX_CLANG)
__asm__("SetConsoleMode")
#endif
;

#if __has_cpp_attribute(gnu::dllimport) && !defined(__WINE__)
[[gnu::dllimport]]
#endif
#if __has_cpp_attribute(gnu::stdcall) && !defined(__WINE__)
[[gnu::stdcall]]
#endif
extern int GetConsoleMode(void*, std::uint_least32_t*) noexcept
#if defined(FLUX_CLANG)
__asm__("GetConsoleMode")
#endif
;
// clang-format on
# include <flux/platform/win32/api.hpp>
#endif

// Window dimensions
Expand Down Expand Up @@ -107,12 +71,12 @@ int main() {
constexpr auto enable_processed_output = static_cast<std::uint_least32_t>(0x0001);
constexpr auto enable_virtual_terminal_processing = static_cast<std::uint_least32_t>(0x0004);

auto* handle = GetStdHandle(std_output_handle);
auto* handle = flux::win32::GetStdHandle(std_output_handle);
std::uint_least32_t console_mode = 0;
GetConsoleMode(handle, &console_mode);
flux::win32::GetConsoleMode(handle, &console_mode);
console_mode |= enable_processed_output;
console_mode |= enable_virtual_terminal_processing;
SetConsoleMode(handle, console_mode);
flux::win32::SetConsoleMode(handle, console_mode);
#endif

glfwInit();
Expand Down