#include "system.hpp" #include "string.hpp" #include #include #include #if defined ARCH_LIN || defined ARCH_MAC #include #include #include // for backtrace and backtrace_symbols #endif #if defined ARCH_WIN #include #include #include #include #endif namespace rack { namespace system { std::list listEntries(const std::string &path) { std::list filenames; DIR *dir = opendir(path.c_str()); if (dir) { struct dirent *d; while ((d = readdir(dir))) { std::string filename = d->d_name; if (filename == "." || filename == "..") continue; filenames.push_back(path + "/" + filename); } closedir(dir); } return filenames; } bool isFile(const std::string &path) { struct stat statbuf; if (stat(path.c_str(), &statbuf)) return false; return S_ISREG(statbuf.st_mode); } bool isDirectory(const std::string &path) { struct stat statbuf; if (stat(path.c_str(), &statbuf)) return false; return S_ISDIR(statbuf.st_mode); } void copyFile(const std::string &srcPath, const std::string &destPath) { // Open files FILE *source = fopen(srcPath.c_str(), "rb"); if (!source) return; DEFER({ fclose(source); }); FILE *dest = fopen(destPath.c_str(), "wb"); if (!dest) return; DEFER({ fclose(dest); }); // Copy buffer const int bufferSize = (1<<15); char buffer[bufferSize]; while (1) { size_t size = fread(buffer, 1, bufferSize, source); if (size == 0) break; size = fwrite(buffer, 1, size, dest); if (size == 0) break; } } void createDirectory(const std::string &path) { #if defined ARCH_WIN CreateDirectory(path.c_str(), NULL); #else mkdir(path.c_str(), 0755); #endif } int getPhysicalCoreCount() { // TODO Return the physical cores, not logical cores. return std::thread::hardware_concurrency(); } void setThreadName(const std::string &name) { #if defined ARCH_LIN pthread_setname_np(pthread_self(), name.c_str()); #elif defined ARCH_WIN // Unsupported on Windows #endif } void setThreadRealTime() { #if defined ARCH_LIN || defined ARCH_MAC // Round-robin scheduler policy int policy = SCHED_RR; struct sched_param param; param.sched_priority = sched_get_priority_max(policy); pthread_setschedparam(pthread_self(), policy, ¶m); #elif defined ARCH_WIN // Set process class first SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); #endif } std::string getStackTrace() { int stackLen = 128; void *stack[stackLen]; std::string s; #if defined ARCH_LIN || defined ARCH_MAC stackLen = backtrace(stack, stackLen); char **strings = backtrace_symbols(stack, stackLen); for (int i = 1; i < stackLen; i++) { s += string::f("%d: %s\n", stackLen - i - 1, strings[i]); } free(strings); #elif defined ARCH_WIN HANDLE process = GetCurrentProcess(); SymInitialize(process, NULL, true); stackLen = CaptureStackBackTrace(0, stackLen, stack, NULL); SYMBOL_INFO *symbol = (SYMBOL_INFO*) calloc(sizeof(SYMBOL_INFO) + 256, 1); symbol->MaxNameLen = 255; symbol->SizeOfStruct = sizeof(SYMBOL_INFO); for (int i = 1; i < stackLen; i++) { SymFromAddr(process, (DWORD64) stack[i], 0, symbol); s += string::f("%d: %s 0x%0x\n", stackLen - i - 1, symbol->Name, symbol->Address); } free(symbol); #endif return s; } void openBrowser(const std::string &url) { #if defined ARCH_LIN std::string command = "xdg-open " + url; (void) std::system(command.c_str()); #endif #if defined ARCH_MAC std::string command = "open " + url; std::system(command.c_str()); #endif #if defined ARCH_WIN ShellExecute(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL); #endif } } // namespace system } // namespace rack