Chris@150
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@150
|
2
|
Chris@150
|
3 /*
|
Chris@150
|
4 Sonic Visualiser
|
Chris@150
|
5 An audio file viewer and annotation editor.
|
Chris@150
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@150
|
7 This file copyright 2006 Chris Cannam.
|
Chris@150
|
8
|
Chris@150
|
9 This program is free software; you can redistribute it and/or
|
Chris@150
|
10 modify it under the terms of the GNU General Public License as
|
Chris@150
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@150
|
12 License, or (at your option) any later version. See the file
|
Chris@150
|
13 COPYING included with this distribution for more information.
|
Chris@150
|
14 */
|
Chris@150
|
15
|
Chris@150
|
16 #include "System.h"
|
Chris@150
|
17
|
Chris@168
|
18 #include <QFile>
|
Chris@168
|
19 #include <QTextStream>
|
Chris@168
|
20 #include <QStringList>
|
Chris@168
|
21 #include <QString>
|
Chris@168
|
22
|
Chris@150
|
23 #ifndef _WIN32
|
Chris@150
|
24 #include <signal.h>
|
Chris@168
|
25 #include <sys/statvfs.h>
|
Chris@150
|
26 #endif
|
Chris@150
|
27
|
Chris@150
|
28 #include <iostream>
|
Chris@150
|
29
|
Chris@150
|
30 #ifdef _WIN32
|
Chris@150
|
31
|
Chris@150
|
32 extern "C" {
|
Chris@150
|
33
|
Chris@150
|
34 void usleep(unsigned long usec)
|
Chris@150
|
35 {
|
Chris@150
|
36 ::Sleep(usec / 1000);
|
Chris@150
|
37 }
|
Chris@150
|
38
|
Chris@150
|
39 void gettimeofday(struct timeval *tv, void *tz)
|
Chris@150
|
40 {
|
Chris@150
|
41 union {
|
Chris@150
|
42 long long ns100;
|
Chris@150
|
43 FILETIME ft;
|
Chris@150
|
44 } now;
|
Chris@150
|
45
|
Chris@150
|
46 ::GetSystemTimeAsFileTime(&now.ft);
|
Chris@150
|
47 tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);
|
Chris@150
|
48 tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);
|
Chris@150
|
49 }
|
Chris@150
|
50
|
Chris@150
|
51 }
|
Chris@150
|
52
|
Chris@150
|
53 #endif
|
Chris@150
|
54
|
Chris@150
|
55 ProcessStatus
|
Chris@150
|
56 GetProcessStatus(int pid)
|
Chris@150
|
57 {
|
Chris@150
|
58 #ifdef _WIN32
|
Chris@150
|
59 HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
|
Chris@150
|
60 if (!handle) {
|
Chris@150
|
61 return ProcessNotRunning;
|
Chris@150
|
62 } else {
|
Chris@150
|
63 CloseHandle(handle);
|
Chris@150
|
64 return ProcessRunning;
|
Chris@150
|
65 }
|
Chris@150
|
66 #else
|
Chris@150
|
67 if (kill(getpid(), 0) == 0) {
|
Chris@150
|
68 if (kill(pid, 0) == 0) {
|
Chris@150
|
69 return ProcessRunning;
|
Chris@150
|
70 } else {
|
Chris@150
|
71 return ProcessNotRunning;
|
Chris@150
|
72 }
|
Chris@150
|
73 } else {
|
Chris@150
|
74 return UnknownProcessStatus;
|
Chris@150
|
75 }
|
Chris@150
|
76 #endif
|
Chris@150
|
77 }
|
Chris@150
|
78
|
Chris@168
|
79 int
|
Chris@168
|
80 GetRealMemoryMBAvailable()
|
Chris@168
|
81 {
|
Chris@168
|
82 // ugh
|
Chris@168
|
83 QFile meminfo("/proc/meminfo");
|
Chris@168
|
84 if (meminfo.open(QFile::ReadOnly)) {
|
Chris@168
|
85 std::cerr << "opened meminfo" << std::endl;
|
Chris@168
|
86 QTextStream in(&meminfo);
|
Chris@168
|
87 while (!in.atEnd()) {
|
Chris@168
|
88 QString line = in.readLine(256);
|
Chris@168
|
89 std::cerr << "read: \"" << line.toStdString() << "\"" << std::endl;
|
Chris@168
|
90 if (line.startsWith("MemFree:")) {
|
Chris@168
|
91 QStringList elements = line.split(' ', QString::SkipEmptyParts);
|
Chris@168
|
92 QString unit = "kB";
|
Chris@168
|
93 if (elements.size() > 2) unit = elements[2];
|
Chris@168
|
94 int size = elements[1].toInt();
|
Chris@168
|
95 std::cerr << "have size \"" << size << "\", unit \""
|
Chris@168
|
96 << unit.toStdString() << "\"" << std::endl;
|
Chris@168
|
97 if (unit.toLower() == "gb") return size * 1024;
|
Chris@168
|
98 if (unit.toLower() == "mb") return size;
|
Chris@168
|
99 if (unit.toLower() == "kb") return size / 1024;
|
Chris@168
|
100 return size / 1048576;
|
Chris@168
|
101 }
|
Chris@168
|
102 }
|
Chris@168
|
103 }
|
Chris@168
|
104 return -1;
|
Chris@168
|
105 }
|
Chris@168
|
106
|
Chris@168
|
107 int
|
Chris@168
|
108 GetDiscSpaceMBAvailable(const char *path)
|
Chris@168
|
109 {
|
Chris@168
|
110 #ifdef _WIN32
|
Chris@168
|
111 __int64 available, total, totalFree;
|
Chris@168
|
112 if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) {
|
Chris@168
|
113 available /= 1048576;
|
Chris@168
|
114 return int(available);
|
Chris@168
|
115 } else {
|
Chris@168
|
116 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
|
Chris@168
|
117 << GetLastError() << std::endl;
|
Chris@168
|
118 return -1;
|
Chris@168
|
119 }
|
Chris@168
|
120 #else
|
Chris@168
|
121 struct statvfs buf;
|
Chris@168
|
122 if (!statvfs(path, &buf)) {
|
Chris@168
|
123 // do the multiplies and divides in this order to reduce the
|
Chris@168
|
124 // likelihood of arithmetic overflow
|
Chris@168
|
125 std::cerr << "statvfs(" << path << ") says available: " << buf.f_bavail << ", block size: " << buf.f_bsize << std::endl;
|
Chris@168
|
126 return ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
|
Chris@168
|
127 } else {
|
Chris@168
|
128 perror("statvfs failed");
|
Chris@168
|
129 return -1;
|
Chris@168
|
130 }
|
Chris@168
|
131 #endif
|
Chris@168
|
132 }
|
Chris@168
|
133
|
Chris@168
|
134
|
Chris@150
|
135 double mod(double x, double y) { return x - (y * floor(x / y)); }
|
Chris@150
|
136 float modf(float x, float y) { return x - (y * floorf(x / y)); }
|
Chris@150
|
137
|
Chris@150
|
138 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; }
|
Chris@150
|
139 float princargf(float a) { return modf(a + M_PI, -2 * M_PI) + M_PI; }
|
Chris@150
|
140
|