From 2689a20889e3fb8c7c54aa79a8a5f5ef43953048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 13 Oct 2022 10:39:11 +0800 Subject: [PATCH] Uptime: support Windows --- CMakeLists.txt | 3 +++ src/detection/uptime/uptime.h | 10 ++++++++++ src/detection/uptime/uptime_apple.c | 19 +++++++++++++++++++ src/detection/uptime/uptime_linux.c | 11 +++++++++++ src/detection/uptime/uptime_windows.c | 10 ++++++++++ src/modules/uptime.c | 25 ++----------------------- 6 files changed, 55 insertions(+), 23 deletions(-) create mode 100644 src/detection/uptime/uptime.h create mode 100644 src/detection/uptime/uptime_apple.c create mode 100644 src/detection/uptime/uptime_linux.c create mode 100644 src/detection/uptime/uptime_windows.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1eac66074..2a3117707 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -316,6 +316,7 @@ if(LINUX OR ANDROID OR BSD) src/detection/temps/temps_linux.c src/detection/opengl/opengl_linux.c src/detection/processes/processes_linux.c + src/detection/uptime/uptime_linux.c src/detection/poweradapter/poweradapter_nosupport.c ) @@ -370,6 +371,7 @@ if(MSYS) src/detection/packages/packages_windows.c src/detection/kernel/kernel_windows.cpp src/detection/localip/localip_windows.c + src/detection/uptime/uptime_windows.c src/util/windows/wmi.cpp src/detection/poweradapter/poweradapter_nosupport.c @@ -398,6 +400,7 @@ if(APPLE) src/detection/font/font_apple.m src/detection/opengl/opengl_apple.c src/detection/processes/processes_apple.c + src/detection/uptime/uptime_apple.c src/detection/bios/bios_nosupport.c src/detection/board/board_nosupport.c diff --git a/src/detection/uptime/uptime.h b/src/detection/uptime/uptime.h new file mode 100644 index 000000000..a44d4cecc --- /dev/null +++ b/src/detection/uptime/uptime.h @@ -0,0 +1,10 @@ +#pragma once + +#ifndef FF_INCLUDED_detection_uptime_uptime +#define FF_INCLUDED_detection_uptime_uptime + +#include "fastfetch.h" + +uint64_t ffDetectUptime(const FFinstance* instance); + +#endif diff --git a/src/detection/uptime/uptime_apple.c b/src/detection/uptime/uptime_apple.c new file mode 100644 index 000000000..c32496ef3 --- /dev/null +++ b/src/detection/uptime/uptime_apple.c @@ -0,0 +1,19 @@ +#include "uptime.h" + +#include +#include + +uint64_t ffDetectUptime(const FFinstance* instance) +{ + FF_UNUSED(instance) + struct timeval bootTime; + size_t bootTimeSize = sizeof(bootTime); + if(sysctl( + (int[]) {CTL_KERN, KERN_BOOTTIME}, 2, + &bootTime, &bootTimeSize, + NULL, 0 + ) == 0) + return (uint64_t) difftime(time(NULL), bootTime.tv_sec); + + return 0; +} diff --git a/src/detection/uptime/uptime_linux.c b/src/detection/uptime/uptime_linux.c new file mode 100644 index 000000000..e58fa6505 --- /dev/null +++ b/src/detection/uptime/uptime_linux.c @@ -0,0 +1,11 @@ +#include "uptime.h" + +uint64_t ffDetectUptime(const FFinstance* instance) +{ + #if FF_HAVE_SYSINFO_H + return (uint64_t) instance->state.sysinfo.uptime; + #else + FF_UNUSED(instance) + return 0; + #endif +} diff --git a/src/detection/uptime/uptime_windows.c b/src/detection/uptime/uptime_windows.c new file mode 100644 index 000000000..8909ad596 --- /dev/null +++ b/src/detection/uptime/uptime_windows.c @@ -0,0 +1,10 @@ +#include "uptime.h" + +#define WIN32_LEAN_AND_MEAN +#include + +uint64_t ffDetectUptime(const FFinstance* instance) +{ + FF_UNUSED(instance) + return GetTickCount64() / 1000; +} diff --git a/src/modules/uptime.c b/src/modules/uptime.c index 9cbd9f9f8..cf371778d 100644 --- a/src/modules/uptime.c +++ b/src/modules/uptime.c @@ -1,34 +1,13 @@ #include "fastfetch.h" #include "common/printing.h" - -#if __APPLE__ - #include "time.h" - #include "sys/sysctl.h" -#endif +#include "detection/uptime/uptime.h" #define FF_UPTIME_MODULE_NAME "Uptime" #define FF_UPTIME_NUM_FORMAT_ARGS 4 void ffPrintUptime(FFinstance* instance) { - uint64_t uptime; - - #if FF_HAVE_SYSINFO_H - uptime = (uint64_t) instance->state.sysinfo.uptime; - #elif __APPLE__ - struct timeval bootTime; - size_t bootTimeSize = sizeof(bootTime); - if(sysctl( - (int[]) {CTL_KERN, KERN_BOOTTIME}, 2, - &bootTime, &bootTimeSize, - NULL, 0 - ) == 0) - uptime = (uint64_t) difftime(time(NULL), bootTime.tv_sec); - else - uptime = 0; - #else - uptime = 0; - #endif + uint64_t uptime = ffDetectUptime(instance); if(uptime == 0) {