annotate system/System.cpp @ 169:603991c63ff6

...
author Chris Cannam
date Mon, 25 Sep 2006 20:32:44 +0000
parents 04baa690f90d
children b23eea68357e
rev   line source
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 <QStringList>
Chris@168 19 #include <QString>
Chris@168 20
Chris@150 21 #ifndef _WIN32
Chris@150 22 #include <signal.h>
Chris@168 23 #include <sys/statvfs.h>
Chris@150 24 #endif
Chris@150 25
Chris@150 26 #include <iostream>
Chris@150 27
Chris@150 28 #ifdef _WIN32
Chris@150 29
Chris@150 30 extern "C" {
Chris@150 31
Chris@150 32 void usleep(unsigned long usec)
Chris@150 33 {
Chris@150 34 ::Sleep(usec / 1000);
Chris@150 35 }
Chris@150 36
Chris@150 37 void gettimeofday(struct timeval *tv, void *tz)
Chris@150 38 {
Chris@150 39 union {
Chris@150 40 long long ns100;
Chris@150 41 FILETIME ft;
Chris@150 42 } now;
Chris@150 43
Chris@150 44 ::GetSystemTimeAsFileTime(&now.ft);
Chris@150 45 tv->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);
Chris@150 46 tv->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);
Chris@150 47 }
Chris@150 48
Chris@150 49 }
Chris@150 50
Chris@150 51 #endif
Chris@150 52
Chris@150 53 ProcessStatus
Chris@150 54 GetProcessStatus(int pid)
Chris@150 55 {
Chris@150 56 #ifdef _WIN32
Chris@150 57 HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
Chris@150 58 if (!handle) {
Chris@150 59 return ProcessNotRunning;
Chris@150 60 } else {
Chris@150 61 CloseHandle(handle);
Chris@150 62 return ProcessRunning;
Chris@150 63 }
Chris@150 64 #else
Chris@150 65 if (kill(getpid(), 0) == 0) {
Chris@150 66 if (kill(pid, 0) == 0) {
Chris@150 67 return ProcessRunning;
Chris@150 68 } else {
Chris@150 69 return ProcessNotRunning;
Chris@150 70 }
Chris@150 71 } else {
Chris@150 72 return UnknownProcessStatus;
Chris@150 73 }
Chris@150 74 #endif
Chris@150 75 }
Chris@150 76
Chris@168 77 int
Chris@168 78 GetRealMemoryMBAvailable()
Chris@168 79 {
Chris@169 80 FILE *meminfo = fopen("/proc/meminfo", "r");
Chris@169 81 if (!meminfo) return -1;
Chris@169 82
Chris@169 83 char buf[256];
Chris@169 84 while (!feof(meminfo)) {
Chris@169 85 fgets(buf, 256, meminfo);
Chris@169 86 if (strncmp(buf, "MemFree:", 8)) {
Chris@169 87 fclose(meminfo);
Chris@169 88 QString line = QString(buf).trimmed();
Chris@169 89 QStringList elements = line.split(' ', QString::SkipEmptyParts);
Chris@169 90 QString unit = "kB";
Chris@169 91 if (elements.size() > 2) unit = elements[2];
Chris@169 92 int size = elements[1].toInt();
Chris@169 93 // std::cerr << "have size \"" << size << "\", unit \""
Chris@169 94 // << unit.toStdString() << "\"" << std::endl;
Chris@169 95 if (unit.toLower() == "gb") return size * 1024;
Chris@169 96 if (unit.toLower() == "mb") return size;
Chris@169 97 if (unit.toLower() == "kb") return size / 1024;
Chris@169 98 return size / 1048576;
Chris@168 99 }
Chris@168 100 }
Chris@169 101 fclose(meminfo);
Chris@168 102 return -1;
Chris@168 103 }
Chris@168 104
Chris@168 105 int
Chris@168 106 GetDiscSpaceMBAvailable(const char *path)
Chris@168 107 {
Chris@168 108 #ifdef _WIN32
Chris@168 109 __int64 available, total, totalFree;
Chris@168 110 if (GetDiskFreeSpaceEx(path, &available, &total, &totalFree)) {
Chris@168 111 available /= 1048576;
Chris@168 112 return int(available);
Chris@168 113 } else {
Chris@168 114 std::cerr << "WARNING: GetDiskFreeSpaceEx failed: error code "
Chris@168 115 << GetLastError() << std::endl;
Chris@168 116 return -1;
Chris@168 117 }
Chris@168 118 #else
Chris@168 119 struct statvfs buf;
Chris@168 120 if (!statvfs(path, &buf)) {
Chris@168 121 // do the multiplies and divides in this order to reduce the
Chris@168 122 // likelihood of arithmetic overflow
Chris@168 123 std::cerr << "statvfs(" << path << ") says available: " << buf.f_bavail << ", block size: " << buf.f_bsize << std::endl;
Chris@168 124 return ((buf.f_bavail / 1024) * buf.f_bsize) / 1024;
Chris@168 125 } else {
Chris@168 126 perror("statvfs failed");
Chris@168 127 return -1;
Chris@168 128 }
Chris@168 129 #endif
Chris@168 130 }
Chris@168 131
Chris@168 132
Chris@150 133 double mod(double x, double y) { return x - (y * floor(x / y)); }
Chris@150 134 float modf(float x, float y) { return x - (y * floorf(x / y)); }
Chris@150 135
Chris@150 136 double princarg(double a) { return mod(a + M_PI, -2 * M_PI) + M_PI; }
Chris@150 137 float princargf(float a) { return modf(a + M_PI, -2 * M_PI) + M_PI; }
Chris@150 138