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_SDL_DEVICE_)
\r
21 #include <SDL_clipboard.h>
\r
22 #elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
23 #include "CIrrDeviceLinux.h"
\r
25 #if defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
\r
26 #import <Cocoa/Cocoa.h>
\r
29 #include "fast_atof.h"
\r
34 #if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
35 // constructor linux
\r
36 COSOperator::COSOperator(const core::stringc& osVersion, CIrrDeviceLinux* device)
\r
37 : OperatingSystem(osVersion), IrrDeviceLinux(device)
\r
43 COSOperator::COSOperator(const core::stringc& osVersion) : OperatingSystem(osVersion)
\r
46 setDebugName("COSOperator");
\r
51 //! returns the current operating system version as string.
\r
52 const core::stringc& COSOperator::getOperatingSystemVersion() const
\r
54 return OperatingSystem;
\r
58 //! copies text to the clipboard
\r
59 void COSOperator::copyToClipboard(const c8 *text) const
\r
61 if (strlen(text)==0)
\r
64 #if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
\r
65 SDL_SetClipboardText(text);
\r
67 #elif defined(_IRR_WINDOWS_API_)
\r
68 if (!OpenClipboard(NULL) || text == 0)
\r
73 core::stringw tempbuffer;
\r
74 core::multibyteToWString(tempbuffer, text);
\r
75 const u32 size = (tempbuffer.size() + 1) * sizeof(wchar_t);
\r
80 clipbuffer = GlobalAlloc(GMEM_MOVEABLE, size);
\r
81 buffer = GlobalLock(clipbuffer);
\r
83 memcpy(buffer, tempbuffer.c_str(), size);
\r
85 GlobalUnlock(clipbuffer);
\r
86 SetClipboardData(CF_UNICODETEXT, clipbuffer);
\r
89 #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
\r
90 NSString *str = nil;
\r
91 NSPasteboard *board = nil;
\r
93 if ((text != NULL) && (strlen(text) > 0))
\r
95 str = [NSString stringWithCString:text encoding:NSUTF8StringEncoding];
\r
96 board = [NSPasteboard generalPasteboard];
\r
97 [board declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:NSApp];
\r
98 [board setString:str forType:NSStringPboardType];
\r
101 #elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
102 if ( IrrDeviceLinux )
\r
103 IrrDeviceLinux->copyToClipboard(text);
\r
108 //! copies text to the primary selection
\r
109 void COSOperator::copyToPrimarySelection(const c8 *text) const
\r
111 if (strlen(text)==0)
\r
114 #if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
115 if ( IrrDeviceLinux )
\r
116 IrrDeviceLinux->copyToPrimarySelection(text);
\r
121 //! gets text from the clipboard
\r
122 const c8* COSOperator::getTextFromClipboard() const
\r
124 #if defined(_IRR_COMPILE_WITH_SDL_DEVICE_)
\r
125 static char *text = nullptr;
\r
128 text = SDL_GetClipboardText();
\r
131 #elif defined(_IRR_WINDOWS_API_)
\r
132 if (!OpenClipboard(NULL))
\r
135 wchar_t * buffer = 0;
\r
137 HANDLE hData = GetClipboardData( CF_UNICODETEXT );
\r
138 buffer = (wchar_t*) GlobalLock( hData );
\r
140 core::wStringToMultibyte(ClipboardBuf, buffer);
\r
142 GlobalUnlock( hData );
\r
145 return ClipboardBuf.c_str();
\r
147 #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_)
\r
148 NSString* str = nil;
\r
149 NSPasteboard* board = nil;
\r
152 board = [NSPasteboard generalPasteboard];
\r
153 str = [board stringForType:NSStringPboardType];
\r
156 result = (char*)[str cStringUsingEncoding:NSUTF8StringEncoding];
\r
160 #elif defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
161 if ( IrrDeviceLinux )
\r
162 return IrrDeviceLinux->getTextFromClipboard();
\r
172 //! gets text from the primary selection
\r
173 const c8* COSOperator::getTextFromPrimarySelection() const
\r
175 #if defined(_IRR_COMPILE_WITH_X11_DEVICE_)
\r
176 if ( IrrDeviceLinux )
\r
177 return IrrDeviceLinux->getTextFromPrimarySelection();
\r
187 bool COSOperator::getSystemMemory(u32* Total, u32* Avail) const
\r
189 #if defined(_IRR_WINDOWS_API_)
\r
191 MEMORYSTATUSEX MemoryStatusEx;
\r
192 MemoryStatusEx.dwLength = sizeof(MEMORYSTATUSEX);
\r
195 GlobalMemoryStatusEx(&MemoryStatusEx);
\r
198 *Total = (u32)(MemoryStatusEx.ullTotalPhys>>10);
\r
200 *Avail = (u32)(MemoryStatusEx.ullAvailPhys>>10);
\r
203 #elif defined(_IRR_POSIX_API_) && defined(_SC_PHYS_PAGES) && defined(_SC_AVPHYS_PAGES)
\r
204 long ps = sysconf(_SC_PAGESIZE);
\r
205 long pp = sysconf(_SC_PHYS_PAGES);
\r
206 long ap = sysconf(_SC_AVPHYS_PAGES);
\r
208 if (ps == -1 || (Total && pp == -1) || (Avail && ap == -1))
\r
212 *Total = (u32)((pp>>10)*ps);
\r
214 *Avail = (u32)((ap>>10)*ps);
\r
216 #elif defined(_IRR_OSX_PLATFORM_)
\r
218 int64_t physical_memory;
\r
221 // Get the Physical memory size
\r
223 mib[1] = HW_MEMSIZE;
\r
224 length = sizeof(int64_t);
\r
225 sysctl(mib, 2, &physical_memory, &length, NULL, 0);
\r
228 *Total = (u32)(physical_memory>>10);
\r
230 *Avail = (u32)(physical_memory>>10); // we don't know better
\r
233 // TODO: implement for others
\r