2020-01-03 20:05:16 +01: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
|
|
|
|
#include <string.h>
|
|
|
|
#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_X11_DEVICE_)
|
|
|
|
#include "CIrrDeviceLinux.h"
|
|
|
|
#endif
|
|
|
|
#if defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "fast_atof.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
|
|
|
|
#if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
|
|
|
// constructor linux
|
|
|
|
COSOperator::COSOperator(const core::stringc& osVersion, CIrrDeviceLinux* device)
|
|
|
|
: OperatingSystem(osVersion), IrrDeviceLinux(device)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// constructor
|
|
|
|
COSOperator::COSOperator(const core::stringc& osVersion) : OperatingSystem(osVersion)
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
setDebugName("COSOperator");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! returns the current operating system version as string.
|
|
|
|
const core::stringc& COSOperator::getOperatingSystemVersion() const
|
|
|
|
{
|
|
|
|
return OperatingSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//! copies text to the clipboard
|
2021-08-30 21:44:56 +02:00
|
|
|
void COSOperator::copyToClipboard(const c8 *text) const
|
2020-01-03 20:05:16 +01:00
|
|
|
{
|
|
|
|
if (strlen(text)==0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Windows version
|
2022-02-26 11:38:43 +01:00
|
|
|
#if defined(_IRR_WINDOWS_API_)
|
2020-01-03 20:05:16 +01:00
|
|
|
if (!OpenClipboard(NULL) || text == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
EmptyClipboard();
|
|
|
|
|
2021-08-30 21:51:24 +02:00
|
|
|
core::stringw tempbuffer;
|
|
|
|
core::multibyteToWString(tempbuffer, text);
|
|
|
|
const u32 size = (tempbuffer.size() + 1) * sizeof(wchar_t);
|
|
|
|
|
2020-01-03 20:05:16 +01:00
|
|
|
HGLOBAL clipbuffer;
|
2021-08-30 21:51:24 +02:00
|
|
|
void * buffer;
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2021-08-30 21:51:24 +02:00
|
|
|
clipbuffer = GlobalAlloc(GMEM_MOVEABLE, size);
|
|
|
|
buffer = GlobalLock(clipbuffer);
|
2020-01-03 20:05:16 +01:00
|
|
|
|
2021-08-30 21:51:24 +02:00
|
|
|
memcpy(buffer, tempbuffer.c_str(), size);
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
GlobalUnlock(clipbuffer);
|
2021-08-30 21:51:24 +02:00
|
|
|
SetClipboardData(CF_UNICODETEXT, clipbuffer);
|
2020-01-03 20:05:16 +01:00
|
|
|
CloseClipboard();
|
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
|
|
|
|
NSString *str = nil;
|
|
|
|
NSPasteboard *board = nil;
|
|
|
|
|
|
|
|
if ((text != NULL) && (strlen(text) > 0))
|
|
|
|
{
|
2021-08-30 21:53:04 +02:00
|
|
|
str = [NSString stringWithCString:text encoding:NSUTF8StringEncoding];
|
2020-01-03 20:05:16 +01:00
|
|
|
board = [NSPasteboard generalPasteboard];
|
|
|
|
[board declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:NSApp];
|
|
|
|
[board setString:str forType:NSStringPboardType];
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
|
|
|
if ( IrrDeviceLinux )
|
|
|
|
IrrDeviceLinux->copyToClipboard(text);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-23 17:19:23 +02:00
|
|
|
//! copies text to the primary selection
|
|
|
|
void COSOperator::copyToPrimarySelection(const c8 *text) const
|
|
|
|
{
|
|
|
|
if (strlen(text)==0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
|
|
|
if ( IrrDeviceLinux )
|
|
|
|
IrrDeviceLinux->copyToPrimarySelection(text);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-03 20:05:16 +01:00
|
|
|
//! gets text from the clipboard
|
|
|
|
const c8* COSOperator::getTextFromClipboard() const
|
|
|
|
{
|
2022-02-26 11:38:43 +01:00
|
|
|
#if defined(_IRR_WINDOWS_API_)
|
2020-01-03 20:05:16 +01:00
|
|
|
if (!OpenClipboard(NULL))
|
|
|
|
return 0;
|
|
|
|
|
2021-08-30 21:51:24 +02:00
|
|
|
wchar_t * buffer = 0;
|
|
|
|
|
|
|
|
HANDLE hData = GetClipboardData( CF_UNICODETEXT );
|
|
|
|
buffer = (wchar_t*) GlobalLock( hData );
|
|
|
|
|
|
|
|
core::wStringToMultibyte(ClipboardBuf, buffer);
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
GlobalUnlock( hData );
|
|
|
|
CloseClipboard();
|
2021-08-30 21:51:24 +02:00
|
|
|
|
|
|
|
return ClipboardBuf.c_str();
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
|
|
|
|
NSString* str = nil;
|
|
|
|
NSPasteboard* board = nil;
|
|
|
|
char* result = 0;
|
|
|
|
|
|
|
|
board = [NSPasteboard generalPasteboard];
|
|
|
|
str = [board stringForType:NSStringPboardType];
|
|
|
|
|
|
|
|
if (str != nil)
|
2021-08-30 21:53:04 +02:00
|
|
|
result = (char*)[str cStringUsingEncoding:NSUTF8StringEncoding];
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
#elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
|
|
|
if ( IrrDeviceLinux )
|
|
|
|
return IrrDeviceLinux->getTextFromClipboard();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-23 17:19:23 +02:00
|
|
|
//! gets text from the primary selection
|
|
|
|
const c8* COSOperator::getTextFromPrimarySelection() const
|
|
|
|
{
|
|
|
|
#if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
|
|
|
|
if ( IrrDeviceLinux )
|
|
|
|
return IrrDeviceLinux->getTextFromPrimarySelection();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-03 20:05:16 +01:00
|
|
|
bool COSOperator::getSystemMemory(u32* Total, u32* Avail) const
|
|
|
|
{
|
2022-02-26 11:38:43 +01:00
|
|
|
#if defined(_IRR_WINDOWS_API_)
|
2020-01-03 20:05:16 +01:00
|
|
|
|
|
|
|
MEMORYSTATUSEX MemoryStatusEx;
|
|
|
|
MemoryStatusEx.dwLength = sizeof(MEMORYSTATUSEX);
|
|
|
|
|
|
|
|
// cannot fail
|
|
|
|
GlobalMemoryStatusEx(&MemoryStatusEx);
|
|
|
|
|
|
|
|
if (Total)
|
|
|
|
*Total = (u32)(MemoryStatusEx.ullTotalPhys>>10);
|
|
|
|
if (Avail)
|
|
|
|
*Avail = (u32)(MemoryStatusEx.ullAvailPhys>>10);
|
|
|
|
return true;
|
|
|
|
|
2022-03-09 22:39:25 +01:00
|
|
|
#elif defined(_IRR_POSIX_API_) && defined(_SC_PHYS_PAGES) && defined(_SC_AVPHYS_PAGES)
|
2020-01-03 20:05:16 +01:00
|
|
|
long ps = sysconf(_SC_PAGESIZE);
|
|
|
|
long pp = sysconf(_SC_PHYS_PAGES);
|
|
|
|
long ap = sysconf(_SC_AVPHYS_PAGES);
|
|
|
|
|
2022-03-09 22:39:25 +01:00
|
|
|
if (ps == -1 || (Total && pp == -1) || (Avail && ap == -1))
|
2020-01-03 20:05:16 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Total)
|
2022-03-09 22:39:25 +01:00
|
|
|
*Total = (u32)((pp>>10)*ps);
|
2020-01-03 20:05:16 +01:00
|
|
|
if (Avail)
|
2022-03-09 22:39:25 +01:00
|
|
|
*Avail = (u32)((ap>>10)*ps);
|
2020-01-03 20:05:16 +01: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);
|
2022-03-09 22:39:25 +01:00
|
|
|
|
|
|
|
if (Total)
|
|
|
|
*Total = (u32)(physical_memory>>10);
|
|
|
|
if (Avail)
|
|
|
|
*Avail = (u32)(physical_memory>>10); // we don't know better
|
2020-01-03 20:05:16 +01:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
// TODO: implement for others
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|