diff --git a/CMakeLists.txt b/CMakeLists.txt index 8763546..a73b350 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,10 +16,7 @@ # limitations under the License. # -cmake_minimum_required(VERSION 2.6) -if(COMMAND cmake_policy) - cmake_policy(VERSION 2.6) -endif(COMMAND cmake_policy) +cmake_minimum_required(VERSION 3.5) ### General Package stuff project(tmux-mem-cpu-load) @@ -67,26 +64,32 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") message(STATUS "FreeBSD detected") set(METER_SOURCES "freebsd/memory.cc" "freebsd/cpu.cc" "common/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") - # OpenBSD Stuff Here message(STATUS "OpenBSD detected") set(METER_SOURCES "openbsd/memory.cc" "openbsd/cpu.cc" "common/load.cc") if(CMAKE_SYSTEM_VERSION VERSION_LESS 5.7) add_definitions(-DOPENBSD_WORKAROUND=1) endif() elseif(CMAKE_SYSTEM_NAME MATCHES "NetBSD") + message(STATUS "NetBSD detected") set(METER_SOURCES "netbsd/memory.cc" "netbsd/cpu.cc" "common/load.cc") +elseif(CMAKE_SYSTEM_NAME MATCHES "Windows") + message(STATUS "Windows detected") + set(METER_SOURCES "windows/memory.cc" "windows/cpu.cc" "windows/load.cc") else() + message(FATAL_ERROR "${CMAKE_SYSTEM_NAME} Cannot be compiled on this system") message(FATAL_ERROR "Cannot be compiled on this system") endif() # set common source files set(COMMON_SOURCES "common/main.cc" "common/memory.cc" "common/graph.cc" "common/powerline.cc") -# add binary tree so we find version.h -include_directories("${PROJECT_BINARY_DIR}") -include_directories("${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/common") - add_executable(tmux-mem-cpu-load ${COMMON_SOURCES} ${METER_SOURCES}) +# add binary tree so we find version.h +target_include_directories(tmux-mem-cpu-load PUBLIC "${PROJECT_BINARY_DIR}") +target_include_directories(tmux-mem-cpu-load PUBLIC "${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/common") +if(CMAKE_SYSTEM_NAME MATCHES "Windows") + target_link_libraries(tmux-mem-cpu-load Pdh) +endif() install(TARGETS tmux-mem-cpu-load RUNTIME DESTINATION bin) include(CTest) diff --git a/common/main.cc b/common/main.cc index 2b237d8..a792c62 100644 --- a/common/main.cc +++ b/common/main.cc @@ -247,6 +247,8 @@ int main( int argc, char** argv ) << cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right ) << load_string( use_colors, use_powerline_left, use_powerline_right, averages_count ); + std::cout << std::endl; + return EXIT_SUCCESS; } diff --git a/common/powerline.cc b/common/powerline.cc index 365a2b6..d4e5cf8 100644 --- a/common/powerline.cc +++ b/common/powerline.cc @@ -28,7 +28,7 @@ const char * bg2fg( const char s[] ) { static char buf[40] = {0}; - const char *substr = index(s, ','); + const char *substr = strchr(s, ','); buf[0] = '#'; buf[1] = '['; buf[2] = 'f'; diff --git a/windows/cpu.cc b/windows/cpu.cc new file mode 100644 index 0000000..4a200a3 --- /dev/null +++ b/windows/cpu.cc @@ -0,0 +1,53 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include // usleep + +#include + +#include "TCHAR.h" +#include "pdh.h" + +static PDH_HQUERY cpuQuery; +static PDH_HCOUNTER cpuTotal; + +#include "cpu.h" +#include "luts.h" + +uint8_t get_cpu_count() +{ + return static_cast< uint8_t >( std::thread::hardware_concurrency() ); +} + +float cpu_percentage( unsigned cpu_usage_delay ) +{ + PdhOpenQuery(NULL, 0, &cpuQuery); + PdhAddCounter(cpuQuery, "\\Processor(_Total)\\% Processor Time", 0, &cpuTotal); + PdhCollectQueryData(cpuQuery); + + usleep( cpu_usage_delay ); + + PDH_FMT_COUNTERVALUE counterVal; + + PdhCollectQueryData(cpuQuery); + PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal); + return static_cast< float >( counterVal.doubleValue ); +} + diff --git a/windows/load.cc b/windows/load.cc new file mode 100644 index 0000000..76434e0 --- /dev/null +++ b/windows/load.cc @@ -0,0 +1,44 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2013 Justin Crawford + * Copyright 2015 Pawel 'l0ner' Soltys + * Copyright 2016 Compilenix + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Based on: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c +// Based on: Apple.cpp for load_string/mem_string and apple's documentation + +#include +#include +#include // floorf() +#include + +#include "cpu.h" +#include "load.h" +#include "luts.h" + +#include "powerline.h" + +// Load Averages +std::string load_string( bool use_colors, + bool use_powerline_left, bool use_powerline_right, + short num_averages ) +{ + // Not implemented + std::ostringstream ss; + ss << (char) 0; + return ss.str(); +} diff --git a/windows/memory.cc b/windows/memory.cc new file mode 100644 index 0000000..45a98f3 --- /dev/null +++ b/windows/memory.cc @@ -0,0 +1,40 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "windows.h" + +#include "memory.h" +#include "conversions.h" + +void mem_status( MemoryStatus & status ) +{ + MEMORYSTATUSEX memory_status; + memory_status.dwLength = sizeof(memory_status); + + GlobalMemoryStatusEx(&memory_status); + + // we want megabytes on output, but since the values already come as + // kilobytes we need to divide them by 1024 only once, thus we use + // KILOBYTES + status.used_mem = convert_unit(static_cast< float >( memory_status.ullAvailPhys ), MEGABYTES, BYTES); + status.total_mem = convert_unit(static_cast< float >( memory_status.ullTotalPhys ), MEGABYTES, BYTES); +} +