2023-10-03 20:37:00 +02:00
|
|
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "COSOperator.h"
|
|
|
|
|
|
|
|
#ifdef _IRR_WINDOWS_API_
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2024-02-25 22:09:38 +01:00
|
|
|
#include <cstring>
|
2023-10-03 20:37:00 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#ifndef _IRR_ANDROID_PLATFORM_
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef _IRR_OSX_PLATFORM_
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
|
|
|
|
#include <SDL_clipboard.h>
|
|
|
|
#include <SDL_version.h>
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
|
|
|
#include "CIrrDeviceLinux.h"
|
|
|
|
#endif
|
|
|
|
#if defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "fast_atof.h"
|
|
|
|
|
|
|
|
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
|
|
|
|
static const bool sdl_supports_primary_selection = [] {
|
|
|
|
#if SDL_VERSION_ATLEAST(2, 25, 0)
|
|
|
|
SDL_version linked_version;
|
|
|
|
SDL_GetVersion(&linked_version);
|
2024-03-20 19:35:52 +01:00
|
|
|
return (linked_version.major == 2 && linked_version.minor >= 25) || linked_version.major > 2;
|
2023-10-03 20:37:00 +02:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
|
|
|
|
#if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
|
|
|
// constructor linux
|
2024-03-20 19:35:52 +01:00
|
|
|
COSOperator::COSOperator(const core::stringc &osVersion, CIrrDeviceLinux *device) :
|
|
|
|
OperatingSystem(osVersion), IrrDeviceLinux(device)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// constructor
|
2024-03-20 19:35:52 +01:00
|
|
|
COSOperator::COSOperator(const core::stringc &osVersion) :
|
|
|
|
OperatingSystem(osVersion)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
#ifdef _DEBUG
|
2023-10-03 20:37:00 +02:00
|
|
|
setDebugName("COSOperator");
|
2024-03-20 19:35:52 +01:00
|
|
|
#endif
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
COSOperator::~COSOperator()
|
|
|
|
{
|
|
|
|
#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
|
|
|
|
SDL_free(ClipboardSelectionText);
|
|
|
|
SDL_free(PrimarySelectionText);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns the current operating system version as string.
|
2024-03-20 19:35:52 +01:00
|
|
|
const core::stringc &COSOperator::getOperatingSystemVersion() const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
return OperatingSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! copies text to the clipboard
|
|
|
|
void COSOperator::copyToClipboard(const c8 *text) const
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (strlen(text) == 0)
|
2023-10-03 20:37:00 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
|
|
|
|
SDL_SetClipboardText(text);
|
|
|
|
|
|
|
|
#elif defined(_IRR_WINDOWS_API_)
|
|
|
|
if (!OpenClipboard(NULL) || text == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
EmptyClipboard();
|
|
|
|
|
|
|
|
core::stringw tempbuffer;
|
2023-10-05 17:55:55 +02:00
|
|
|
core::utf8ToWString(tempbuffer, text);
|
2023-10-03 20:37:00 +02:00
|
|
|
const u32 size = (tempbuffer.size() + 1) * sizeof(wchar_t);
|
|
|
|
|
|
|
|
HGLOBAL clipbuffer;
|
2024-03-20 19:35:52 +01:00
|
|
|
void *buffer;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
clipbuffer = GlobalAlloc(GMEM_MOVEABLE, size);
|
|
|
|
buffer = GlobalLock(clipbuffer);
|
|
|
|
|
|
|
|
memcpy(buffer, tempbuffer.c_str(), size);
|
|
|
|
|
|
|
|
GlobalUnlock(clipbuffer);
|
|
|
|
SetClipboardData(CF_UNICODETEXT, clipbuffer);
|
|
|
|
CloseClipboard();
|
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
|
2024-03-20 19:35:52 +01:00
|
|
|
NSString *str = nil;
|
|
|
|
NSPasteboard *board = nil;
|
|
|
|
|
|
|
|
if ((text != NULL) && (strlen(text) > 0)) {
|
|
|
|
str = [NSString stringWithCString:text encoding:NSUTF8StringEncoding];
|
|
|
|
board = [NSPasteboard generalPasteboard];
|
|
|
|
[board declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:NSApp];
|
|
|
|
[board setString:str forType:NSStringPboardType];
|
|
|
|
}
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IrrDeviceLinux)
|
|
|
|
IrrDeviceLinux->copyToClipboard(text);
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//! copies text to the primary selection
|
|
|
|
void COSOperator::copyToPrimarySelection(const c8 *text) const
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (strlen(text) == 0)
|
2023-10-03 20:37:00 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
|
|
|
|
#if SDL_VERSION_ATLEAST(2, 25, 0)
|
|
|
|
if (sdl_supports_primary_selection)
|
|
|
|
SDL_SetPrimarySelectionText(text);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IrrDeviceLinux)
|
|
|
|
IrrDeviceLinux->copyToPrimarySelection(text);
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//! gets text from the clipboard
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COSOperator::getTextFromClipboard() const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
|
|
|
|
SDL_free(ClipboardSelectionText);
|
|
|
|
ClipboardSelectionText = SDL_GetClipboardText();
|
|
|
|
return ClipboardSelectionText;
|
|
|
|
|
|
|
|
#elif defined(_IRR_WINDOWS_API_)
|
|
|
|
if (!OpenClipboard(NULL))
|
|
|
|
return 0;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
HANDLE hData = GetClipboardData(CF_UNICODETEXT);
|
2024-01-04 21:24:50 +01:00
|
|
|
if (hData == NULL) // Probably not in Unicode text format
|
|
|
|
return 0;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
wchar_t *buffer = (wchar_t *)GlobalLock(hData);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2023-10-05 17:55:55 +02:00
|
|
|
core::wStringToUTF8(ClipboardBuf, buffer);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
GlobalUnlock(hData);
|
2023-10-03 20:37:00 +02:00
|
|
|
CloseClipboard();
|
|
|
|
|
|
|
|
return ClipboardBuf.c_str();
|
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
|
2024-03-20 19:35:52 +01:00
|
|
|
NSString *str = nil;
|
|
|
|
NSPasteboard *board = nil;
|
|
|
|
char *result = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
board = [NSPasteboard generalPasteboard];
|
|
|
|
str = [board stringForType:NSStringPboardType];
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (str != nil)
|
|
|
|
result = (char *)[str cStringUsingEncoding:NSUTF8StringEncoding];
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
return (result);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IrrDeviceLinux)
|
|
|
|
return IrrDeviceLinux->getTextFromClipboard();
|
|
|
|
return 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//! gets text from the primary selection
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COSOperator::getTextFromPrimarySelection() const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
#if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
|
|
|
|
#if SDL_VERSION_ATLEAST(2, 25, 0)
|
|
|
|
if (sdl_supports_primary_selection) {
|
|
|
|
SDL_free(PrimarySelectionText);
|
|
|
|
PrimarySelectionText = SDL_GetPrimarySelectionText();
|
|
|
|
return PrimarySelectionText;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
2024-03-20 19:35:52 +01:00
|
|
|
if (IrrDeviceLinux)
|
2023-10-03 20:37:00 +02:00
|
|
|
return IrrDeviceLinux->getTextFromPrimarySelection();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
bool COSOperator::getSystemMemory(u32 *Total, u32 *Avail) const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
#if defined(_IRR_WINDOWS_API_)
|
|
|
|
|
|
|
|
MEMORYSTATUSEX MemoryStatusEx;
|
2024-03-20 19:35:52 +01:00
|
|
|
MemoryStatusEx.dwLength = sizeof(MEMORYSTATUSEX);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
// cannot fail
|
|
|
|
GlobalMemoryStatusEx(&MemoryStatusEx);
|
|
|
|
|
|
|
|
if (Total)
|
2024-03-20 19:35:52 +01:00
|
|
|
*Total = (u32)(MemoryStatusEx.ullTotalPhys >> 10);
|
2023-10-03 20:37:00 +02:00
|
|
|
if (Avail)
|
2024-03-20 19:35:52 +01:00
|
|
|
*Avail = (u32)(MemoryStatusEx.ullAvailPhys >> 10);
|
2023-10-03 20:37:00 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
#elif defined(_IRR_POSIX_API_) && defined(_SC_PHYS_PAGES) && defined(_SC_AVPHYS_PAGES)
|
2024-03-20 19:35:52 +01:00
|
|
|
long ps = sysconf(_SC_PAGESIZE);
|
|
|
|
long pp = sysconf(_SC_PHYS_PAGES);
|
|
|
|
long ap = sysconf(_SC_AVPHYS_PAGES);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
if (ps == -1 || (Total && pp == -1) || (Avail && ap == -1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Total)
|
2024-03-20 19:35:52 +01:00
|
|
|
*Total = (u32)((pp >> 10) * ps);
|
2023-10-03 20:37:00 +02:00
|
|
|
if (Avail)
|
2024-03-20 19:35:52 +01:00
|
|
|
*Avail = (u32)((ap >> 10) * ps);
|
2023-10-03 20:37:00 +02:00
|
|
|
return true;
|
|
|
|
#elif defined(_IRR_OSX_PLATFORM_)
|
|
|
|
int mib[2];
|
|
|
|
int64_t physical_memory;
|
|
|
|
size_t length;
|
|
|
|
|
|
|
|
// Get the Physical memory size
|
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_MEMSIZE;
|
|
|
|
length = sizeof(int64_t);
|
|
|
|
sysctl(mib, 2, &physical_memory, &length, NULL, 0);
|
|
|
|
|
|
|
|
if (Total)
|
2024-03-20 19:35:52 +01:00
|
|
|
*Total = (u32)(physical_memory >> 10);
|
2023-10-03 20:37:00 +02:00
|
|
|
if (Avail)
|
2024-03-20 19:35:52 +01:00
|
|
|
*Avail = (u32)(physical_memory >> 10); // we don't know better
|
2023-10-03 20:37:00 +02:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
// TODO: implement for others
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|