1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "COSOperator.h"
\r
7 #ifdef _IRR_WINDOWS_API_
\r
12 #ifndef _IRR_ANDROID_PLATFORM_
\r
13 #include <sys/types.h>
\r
14 #ifdef _IRR_OSX_PLATFORM_
\r
15 #include <sys/sysctl.h>
\r
20 #if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
21 #include "CIrrDeviceLinux.h"
\r
23 #if defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
\r
24 #import <Cocoa/Cocoa.h>
\r
27 #include "fast_atof.h"
\r
32 #if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
33 // constructor linux
\r
34 COSOperator::COSOperator(const core::stringc& osVersion, CIrrDeviceLinux* device)
\r
35 : OperatingSystem(osVersion), IrrDeviceLinux(device)
\r
41 COSOperator::COSOperator(const core::stringc& osVersion) : OperatingSystem(osVersion)
\r
44 setDebugName("COSOperator");
\r
49 //! returns the current operating system version as string.
\r
50 const core::stringc& COSOperator::getOperatingSystemVersion() const
\r
52 return OperatingSystem;
\r
56 //! copies text to the clipboard
\r
57 //! \param text: text in utf-8
\r
58 void COSOperator::copyToClipboard(const c8 *text) const
\r
60 if (strlen(text)==0)
\r
64 #if defined(_IRR_WINDOWS_API_)
\r
65 if (!OpenClipboard(NULL) || text == 0)
\r
70 core::stringw tempbuffer;
\r
71 core::multibyteToWString(tempbuffer, text);
\r
72 const u32 size = (tempbuffer.size() + 1) * sizeof(wchar_t);
\r
77 clipbuffer = GlobalAlloc(GMEM_MOVEABLE, size);
\r
78 buffer = GlobalLock(clipbuffer);
\r
80 memcpy(buffer, tempbuffer.c_str(), size);
\r
82 GlobalUnlock(clipbuffer);
\r
83 SetClipboardData(CF_UNICODETEXT, clipbuffer);
\r
86 #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
\r
87 NSString *str = nil;
\r
88 NSPasteboard *board = nil;
\r
90 if ((text != NULL) && (strlen(text) > 0))
\r
92 str = [NSString stringWithCString:text encoding:NSUTF8StringEncoding];
\r
93 board = [NSPasteboard generalPasteboard];
\r
94 [board declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:NSApp];
\r
95 [board setString:str forType:NSStringPboardType];
\r
98 #elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
99 if ( IrrDeviceLinux )
\r
100 IrrDeviceLinux->copyToClipboard(text);
\r
105 //! gets text from the clipboard
\r
106 //! \return Returns 0 if no string is in there, otherwise an utf-8 string.
\r
107 const c8* COSOperator::getTextFromClipboard() const
\r
109 #if defined(_IRR_WINDOWS_API_)
\r
110 if (!OpenClipboard(NULL))
\r
113 wchar_t * buffer = 0;
\r
115 HANDLE hData = GetClipboardData( CF_UNICODETEXT );
\r
116 buffer = (wchar_t*) GlobalLock( hData );
\r
118 core::wStringToMultibyte(ClipboardBuf, buffer);
\r
120 GlobalUnlock( hData );
\r
123 return ClipboardBuf.c_str();
\r
125 #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
\r
126 NSString* str = nil;
\r
127 NSPasteboard* board = nil;
\r
130 board = [NSPasteboard generalPasteboard];
\r
131 str = [board stringForType:NSStringPboardType];
\r
134 result = (char*)[str cStringUsingEncoding:NSUTF8StringEncoding];
\r
138 #elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
139 if ( IrrDeviceLinux )
\r
140 return IrrDeviceLinux->getTextFromClipboard();
\r
150 bool COSOperator::getSystemMemory(u32* Total, u32* Avail) const
\r
152 #if defined(_IRR_WINDOWS_API_)
\r
154 MEMORYSTATUSEX MemoryStatusEx;
\r
155 MemoryStatusEx.dwLength = sizeof(MEMORYSTATUSEX);
\r
158 GlobalMemoryStatusEx(&MemoryStatusEx);
\r
161 *Total = (u32)(MemoryStatusEx.ullTotalPhys>>10);
\r
163 *Avail = (u32)(MemoryStatusEx.ullAvailPhys>>10);
\r
166 #elif defined(_IRR_POSIX_API_) && defined(_SC_PHYS_PAGES) && defined(_SC_AVPHYS_PAGES)
\r
167 long ps = sysconf(_SC_PAGESIZE);
\r
168 long pp = sysconf(_SC_PHYS_PAGES);
\r
169 long ap = sysconf(_SC_AVPHYS_PAGES);
\r
171 if (ps == -1 || (Total && pp == -1) || (Avail && ap == -1))
\r
175 *Total = (u32)((pp>>10)*ps);
\r
177 *Avail = (u32)((ap>>10)*ps);
\r
179 #elif defined(_IRR_OSX_PLATFORM_)
\r
181 int64_t physical_memory;
\r
184 // Get the Physical memory size
\r
186 mib[1] = HW_MEMSIZE;
\r
187 length = sizeof(int64_t);
\r
188 sysctl(mib, 2, &physical_memory, &length, NULL, 0);
\r
191 *Total = (u32)(physical_memory>>10);
\r
193 *Avail = (u32)(physical_memory>>10); // we don't know better
\r
196 // TODO: implement for others
\r