SectorZone: add attribute arc_boundary
[xcsoar.git] / src / UtilsSystem.cpp
blobbe8506bedbcbe8da05d769d90efaebaf84ae810c
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "UtilsSystem.hpp"
25 #include "CommandLine.hpp"
26 #include "LocalPath.hpp"
27 #include "LogFile.hpp"
28 #include "OS/FileUtil.hpp"
29 #include "OS/MemInfo.hpp"
30 #include "Screen/Point.hpp"
32 #ifdef ANDROID
33 #include "Android/NativeView.hpp"
34 #include "Android/Main.hpp"
35 #endif
37 #include <tchar.h>
39 #ifdef HAVE_POSIX
40 #ifndef ANDROID
41 #include <sys/statvfs.h>
42 #endif
43 #include <sys/stat.h>
44 #endif
46 #ifdef WIN32
47 #include <windows.h>
48 #endif
50 #ifdef USE_VIDEOCORE
51 #include <bcm_host.h>
52 #endif
54 #ifdef _WIN32_WCE
55 // This is necessary to be called periodically to get rid of
56 // memory defragmentation, since on pocket pc platforms there is no
57 // automatic defragmentation.
58 void MyCompactHeaps() {
59 #if defined(GNAV) && !defined(__GNUC__)
60 HeapCompact(GetProcessHeap(), 0);
61 #else
62 typedef DWORD (_stdcall *CompactAllHeapsFn) (void);
63 static CompactAllHeapsFn CompactAllHeaps = NULL;
64 static bool init = false;
65 if (!init) {
66 // get the pointer to the function
67 CompactAllHeaps = (CompactAllHeapsFn)GetProcAddress(
68 LoadLibrary(_T("coredll.dll")), _T("CompactAllHeaps"));
69 init = true;
71 if (CompactAllHeaps)
72 CompactAllHeaps();
73 #endif
75 #endif /* _WIN32_WCE */
77 /**
78 * Calculates the free disk space for the given path
79 * @param path The path defining the "drive" to look on
80 * @return Number of KiB free on the destination drive
82 unsigned long FindFreeSpace(const TCHAR *path) {
83 #ifdef HAVE_POSIX
84 #ifdef ANDROID
85 return 64 * 1024 * 1024;
86 #else
87 struct statvfs s;
88 if (statvfs(path, &s) < 0)
89 return 0;
90 return s.f_bsize * s.f_bavail / 1024;
91 #endif
92 #else /* !HAVE_POSIX */
93 ULARGE_INTEGER FreeBytesAvailableToCaller;
94 ULARGE_INTEGER TotalNumberOfBytes;
95 ULARGE_INTEGER TotalNumberOfFreeBytes;
96 if (GetDiskFreeSpaceEx(path,
97 &FreeBytesAvailableToCaller,
98 &TotalNumberOfBytes,
99 &TotalNumberOfFreeBytes)) {
100 return FreeBytesAvailableToCaller.LowPart / 1024;
101 } else
102 return 0;
103 #endif /* !HAVE_POSIX */
106 void
107 StartupLogFreeRamAndStorage()
109 #ifdef HAVE_MEM_INFO
110 unsigned long freeram = SystemFreeRAM() / 1024;
111 LogFormat("Free ram %lu KB", freeram);
112 #endif
113 unsigned long freestorage = FindFreeSpace(GetPrimaryDataPath());
114 LogFormat("Free storage %lu KB", freestorage);
118 * Returns the screen dimension rect to be used
119 * @return The screen dimension rect to be used
121 PixelSize
122 SystemWindowSize()
124 #if defined(WIN32) && !defined(_WIN32_WCE)
125 unsigned width = CommandLine::width + 2 * GetSystemMetrics(SM_CXFIXEDFRAME);
126 unsigned height = CommandLine::height + 2 * GetSystemMetrics(SM_CYFIXEDFRAME)
127 + GetSystemMetrics(SM_CYCAPTION);
129 return { width, height };
130 #else
131 #ifdef WIN32
132 return { GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) };
133 #elif defined(ANDROID)
134 return native_view->GetSize();
135 #elif defined(USE_VIDEOCORE)
136 uint32_t width, height;
137 return graphics_get_display_size(0, &width, &height) >= 0
138 ? PixelSize(width, height)
139 : PixelSize(640, 480);
140 #else
141 /// @todo implement this properly for SDL/UNIX
142 return { CommandLine::width, CommandLine::height };
143 #endif /* !WIN32 */
145 #endif