refactored project structure

This commit is contained in:
Linus Dierheimer
2021-02-27 16:40:05 +01:00
parent 558639086a
commit b44ab16dd1
26 changed files with 219 additions and 211 deletions
+56
View File
@@ -0,0 +1,56 @@
#include "fastfetch.h"
void ffPrintBattery(FFstate* state)
{
int scanned;
FILE* fullFile = fopen("/sys/class/power_supply/BAT0/charge_full", "r");
if(fullFile == NULL)
{
ffPrintError(state, "Battery", "fopen(\"/sys/class/power_supply/BAT0/charge_full\", \"r\") == NULL");
return;
}
uint32_t full;
scanned = fscanf(fullFile, "%u", &full);
if(scanned != 1)
{
ffPrintError(state, "Battery", "fscanf(fullFile, \"%u\", &full) != 1");
return;
}
fclose(fullFile);
FILE* nowFile = fopen("/sys/class/power_supply/BAT0/charge_now", "r");
if(nowFile == NULL)
{
ffPrintError(state, "Battery", "fopen(\"/sys/class/power_supply/BAT0/charge_now\", \"r\") == NULL");
return;
}
uint32_t now;
scanned = fscanf(nowFile, "%u", &now);
if(scanned != 1)
{
ffPrintError(state, "Battery", "fscanf(nowFile, \"%u\", &now) != 1");
return;
}
fclose(nowFile);
FILE* statusFile = fopen("/sys/class/power_supply/BAT0/status", "r");
if(statusFile == NULL)
{
ffPrintError(state, "Battery", "fopen(\"/sys/class/power_supply/BAT0/status\", \"r\") == NULL");
return;
}
char status[256];
scanned = fscanf(statusFile, "%s", status);
if(scanned != 1)
{
ffPrintError(state, "Battery", "fscanf(statusFile, \"%u\", &status) != 1");
return;
}
fclose(statusFile);
uint32_t percentage = (now / (double) full) * 100;
ffPrintLogoAndKey(state, "Battery");
printf("%u%% [%s]\n", percentage, status);
}
+7
View File
@@ -0,0 +1,7 @@
#include "fastfetch.h"
void ffPrintBreak(FFstate* state)
{
ffPrintLogoLine(state);
putchar('\n');
}
+18
View File
@@ -0,0 +1,18 @@
#include "fastfetch.h"
void ffPrintColors(FFstate* state)
{
ffPrintLogoLine(state);
for(uint8_t i = 0; i < 8; i++)
printf("\033[4%dm ", i);
puts("\033[0m");
ffPrintLogoLine(state);
for(uint8_t i = 8; i < 16; i++)
printf("\033[48;5;%dm ", i);
puts("\033[0m");
}
+40
View File
@@ -0,0 +1,40 @@
#include "fastfetch.h"
#include <float.h>
void ffPrintCPU(FFstate* state)
{
if(ffPrintCachedValue(state, "CPU"))
return;
char name[256];
ffParsePropFile("/proc/cpuinfo", "model name%*s %[^\n]", name);
if(name[0] == '\0')
{
ffPrintError(state, "CPU", "\"model name%*s %[^\\n]\" not found in \"/proc/cpuinfo\"");
return;
}
FILE* frequencyFile = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", "r");
if(frequencyFile == NULL)
{
ffPrintError(state, "CPU", "fopen(\"/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq\", \"r\") == NULL");
return;
}
uint32_t frequency;
int scanned = fscanf(frequencyFile, "%u", &frequency);
if(scanned != 1)
{
ffPrintError(state, "CPU", "fscanf(frequencyFile, \"%s\", frequency) != 1");
return;
}
fclose(frequencyFile);
frequency /= 1000; //to MHz
double ghz = (double) frequency / 1000.0; //to GHz
char value[1024];
sprintf(value, "%s (%i) @ %.9gGHz", name, get_nprocs(), ghz);
ffPrintAndSaveCachedValue(state, "CPU", value);
}
+48
View File
@@ -0,0 +1,48 @@
#include "fastfetch.h"
#include <string.h>
#include <malloc.h>
static void printKDE()
{
char version[256];
ffParsePropFile("/usr/share/xsessions/plasma.desktop", "X-KDE-PluginInfo-Version=%[^\n]", version);
if(version[0] == '\0')
{
printf("KDE Plasma ");
return;
}
printf("KDE Plasma %s", version);
}
void ffPrintDesktopEnvironment(FFstate* state)
{
const char* currentDesktop = getenv("XDG_CURRENT_DESKTOP");
if(currentDesktop == NULL)
{
ffPrintError(state, "DE", "getenv(\"XDG_CURRENT_DESKTOP\") == NULL");
return;
}
ffPrintLogoAndKey(state, "DE");
if(strcmp(currentDesktop, "KDE") == 0)
printKDE();
else
printf("%s", currentDesktop);
const char* sessionType = getenv("XDG_SESSION_TYPE");
if(sessionType == NULL)
putchar('\n');
else if(strcmp(sessionType, "wayland") == 0)
puts(" (Wayland)");
else if(strcmp(sessionType, "x11") == 0)
puts(" (X11)");
else if(strcmp(sessionType, "tty") == 0)
puts(" TTY");
else if(strcmp(sessionType, "mir") == 0)
puts(" (Mir)");
else
printf(" (%s)\n", sessionType);
}
+25
View File
@@ -0,0 +1,25 @@
#include "fastfetch.h"
#include <sys/statvfs.h>
void ffPrintDisk(FFstate* state)
{
struct statvfs fs;
int ret = statvfs("/", &fs);
if(ret != 0)
{
ffPrintError(state, "Disk (/)", "statvfs(\"/\", &fs) != 0");
return;
}
const uint32_t GB = (1024 * 1024) * 1024;
const uint32_t total = (fs.f_blocks * fs.f_frsize) / GB;
const uint32_t available = (fs.f_bfree * fs.f_frsize) / GB;
const uint32_t used = total - available;
const uint32_t percentage = (used / (double) total) * 100.0;
ffPrintLogoAndKey(state, "Disk (/)");
printf("%uGB / %uGB (%u%%)\n", used, total, percentage);
}
+66
View File
@@ -0,0 +1,66 @@
#include "fastfetch.h"
#include <string.h>
static void parseFont(char* font, char* buffer)
{
char name[64];
char size[32];
int scanned = sscanf(font, "%[^,], %[^,]", name, size);
if(scanned == 0)
strcpy(buffer, font);
else if(scanned == 1)
strcpy(buffer, name);
else
sprintf(buffer, "%s (%spt)", name, size);
}
void ffPrintFont(FFstate* state)
{
char plasma[256];
ffParsePropFileHome(state, ".config/kdeglobals", "font=%[^\n]", plasma);
char gtk2[256];
ffParsePropFileHome(state, ".gtkrc-2.0", "gtk-font-name=\"%[^\"]+", gtk2);
char gtk2Pretty[256];
if(gtk2[0] == '\0')
gtk2Pretty[0] = '\0';
else
parseFont(gtk2, gtk2Pretty);
char gtk3[256];
ffParsePropFileHome(state, ".config/gtk-3.0/settings.ini", "gtk-font-name=%[^\n]", gtk3);
char gtk3Pretty[256];
if(gtk3[0] == '\0')
gtk3Pretty[0] = '\0';
else
parseFont(gtk3, gtk3Pretty);
char gtk4[256];
ffParsePropFileHome(state, ".config/gtk-4.0/settings.ini", "gtk-font-name=%[^\n]", gtk4);
char gtk4Pretty[256];
if(gtk4Pretty[0] == '\0')
gtk4Pretty[0] = '\0';
else
parseFont(gtk4, gtk4Pretty);
ffPrintLogoAndKey(state, "Font");
if(plasma[0] == '\0')
{
printf("Noto Sans (10pt) [Plasma], ");
}
else
{
char plasmaPretty[256];
parseFont(plasma, plasmaPretty);
printf("%s [Plasma], ", plasmaPretty);
}
ffPrintGtkPretty(gtk2Pretty, gtk3Pretty, gtk4Pretty);
putchar('\n');
}
+61
View File
@@ -0,0 +1,61 @@
#include "fastfetch.h"
#include <string.h>
#include <pci/pci.h>
static void handleGPU(FFstate* state, struct pci_access* pacc, struct pci_dev* dev, uint16_t counter)
{
char key[8];
sprintf(key, "GPU%hu", counter);
if(ffPrintCachedValue(state, key))
return;
char gpu[1024];
char vendor[512];
pci_lookup_name(pacc, vendor, sizeof(vendor), PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id);
char name[512];
pci_lookup_name(pacc, name, sizeof(name), PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id);
if(strcmp(vendor, "Advanced Micro Devices, Inc. [AMD/ATI]") == 0)
{
strcpy(gpu, "AMD ATI ");
}
else
{
strcpy(gpu, vendor);
strcat(gpu, " ");
}
strcat(gpu, name);
ffPrintAndSaveCachedValue(state, key, gpu);
}
void ffPrintGPU(FFstate* state)
{
uint16_t counter = 0;
struct pci_access *pacc;
struct pci_dev *dev;
pacc = pci_alloc();
pci_init(pacc);
pci_scan_bus(pacc);
for (dev=pacc->devices; dev; dev=dev->next)
{
pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS);
char class[1024];
pci_lookup_name(pacc, class, sizeof(class), PCI_LOOKUP_CLASS, dev->device_class);
if(
strcmp("VGA compatible controller", class) == 0 ||
strcmp("3D controller", class) == 0 ||
strcmp("Display controller", class) == 0 )
{
handleGPU(state, pacc, dev, counter++);
}
}
pci_cleanup(pacc);
}
+35
View File
@@ -0,0 +1,35 @@
#include "fastfetch.h"
#include <string.h>
void ffPrintHost(FFstate* state)
{
if(ffPrintCachedValue(state, "Host"))
return;
char host[1024];
FILE* nameFile = fopen("/sys/devices/virtual/dmi/id/product_name", "r");
if(nameFile == NULL)
{
ffPrintError(state, "Host", "fopen(\"/sys/devices/virtual/dmi/id/product_name\", \"r\") == NULL");
return;
}
if(fscanf(nameFile, "%[^\n]", host) != 1)
{
ffPrintError(state, "Host", "fscanf(nameFile, \"%[^\\n]\", name) != 1");
return;
}
fclose(nameFile);
FILE* versionFile = fopen("/sys/devices/virtual/dmi/id/product_version", "r");
if(versionFile != NULL)
{
ssize_t len = strlen(host);
host[len] = ' ';
if(fscanf(versionFile, "%[^\n]", host + len + 1) == 0); //fail silently, if reading of version fails, just show now version
fclose(versionFile);
}
ffPrintAndSaveCachedValue(state, "Host", host);
}
+34
View File
@@ -0,0 +1,34 @@
#include "fastfetch.h"
#include <string.h>
void ffPrintIcons(FFstate* state)
{
char plasma[256];
ffParsePropFileHome(state, ".config/kdeglobals", "Theme=%[^\n]", plasma);
char gtk2[256];
ffParsePropFileHome(state, ".gtkrc-2.0", "gtk-icon-theme-name=\"%[^\"]+", gtk2);
if(gtk2[0] == '\0')
strcpy(gtk2, "Adwaita");
char gtk3[256];
ffParsePropFileHome(state, ".config/gtk-3.0/settings.ini", "gtk-icon-theme-name=%[^\n]", gtk3);
if(gtk3[0] == '\0')
strcpy(gtk3, "Adwaita");
char gtk4[256];
ffParsePropFileHome(state, ".config/gtk-4.0/settings.ini", "gtk-icon-theme-name=%[^\n]", gtk4);
if(gtk4[0] == '\0')
strcpy(gtk4, "Adwaita");
ffPrintLogoAndKey(state, "Icons");
if(plasma[0] == '\0')
printf("Breeze [Plasma], ");
else
printf("%s [Plasma], ", plasma);
ffPrintGtkPretty(gtk2, gtk3, gtk4);
putchar('\n');
}
+7
View File
@@ -0,0 +1,7 @@
#include "fastfetch.h"
void ffPrintKernel(FFstate* state)
{
ffPrintLogoAndKey(state, "Kernel");
printf("%s\n", state->utsname.release);
}
+17
View File
@@ -0,0 +1,17 @@
#include "fastfetch.h"
void ffPrintLocale(FFstate* state)
{
if(ffPrintCachedValue(state, "Locale"))
return;
char locale[256];
ffParsePropFile("/etc/locale.conf", "LANG=%[^\n]", locale);
if(locale[0] == '\0')
{
ffPrintError(state, "Locale", "\"LANG=%[^\\n]\" not found in \"/etc/locale.conf\"");
return;
}
ffPrintAndSaveCachedValue(state, "Locale", locale);
}
+35
View File
@@ -0,0 +1,35 @@
#include "fastfetch.h"
// Impl by: https://github.com/sam-barr/paleofetch/blob/b7c58a52c0de39b53c9b5f417889a5886d324bfa/paleofetch.c#L544
void ffPrintMemory(FFstate* state)
{
FILE* meminfo = fopen("/proc/meminfo", "r");
if(meminfo == NULL) {
ffPrintError(state, "Memory", "fopen(\"/proc/meminfo\", \"r\") == NULL");
return;
}
char* line = NULL;
size_t len;
uint32_t total, shared, memfree, buffers, cached, reclaimable;
while (getline(&line, &len, meminfo) != -1) {
sscanf(line, "MemTotal: %u", &total);
sscanf(line, "Shmem: %u", &shared);
sscanf(line, "MemFree: %u", &memfree);
sscanf(line, "Buffers: %u", &buffers);
sscanf(line, "Cached: %u", &cached);
sscanf(line, "SReclaimable: %u", &reclaimable);
}
free(line);
fclose(meminfo);
uint32_t used_mem = (total + shared - memfree - buffers - cached - reclaimable) / 1024;
uint32_t total_mem = total / 1024;
uint8_t percentage = (uint8_t) ((used_mem / (double) total_mem) * 100);
ffPrintLogoAndKey(state, "Memory");
printf("%uMiB / %uMiB (%u%%)\n", used_mem, total_mem, percentage);
}
+20
View File
@@ -0,0 +1,20 @@
#include "fastfetch.h"
void ffPrintOS(FFstate* state)
{
if(ffPrintCachedValue(state, "OS"))
return;
char name[256];
ffParsePropFile("/etc/os-release", "NAME=\"%[^\"]+", name);
if(name[0] == '\0')
{
ffPrintError(state, "OS", "\"NAME=\\\"%[^\\\"]+\" not found in \"/etc/os-release\"");
return;
}
char os[1024];
sprintf(os, "%s %s", name, state->utsname.machine);
ffPrintAndSaveCachedValue(state, "OS", os);
}
+39
View File
@@ -0,0 +1,39 @@
#include "fastfetch.h"
#include <dirent.h>
static uint32_t get_num_dirs(const char* dirname) {
uint32_t num_dirs = 0;
DIR * dirp;
struct dirent *entry;
dirp = opendir(dirname);
if(dirp == NULL)
return 0;
while((entry = readdir(dirp)) != NULL) {
if(entry->d_type == DT_DIR)
++num_dirs;
}
num_dirs -= 2; // accounting for . and ..
closedir(dirp);
return num_dirs;
}
static void printPacmanPackages()
{
uint32_t nums = get_num_dirs("/var/lib/pacman/local");
if(nums > 0)
printf("%i (pacman) ", nums);
}
void ffPrintPackages(FFstate* state)
{
ffPrintLogoAndKey(state, "Packages");
printPacmanPackages();
putchar('\n');
}
+77
View File
@@ -0,0 +1,77 @@
#include "fastfetch.h"
#include "X11/extensions/Xrandr.h"
#include "dlfcn.h"
short getCurrentRate(Display* display)
{
void* xrandr = dlopen("libXrandr.so", RTLD_LAZY);
if(xrandr == NULL)
return 0;
Window root = RootWindow(display, 0);
XRRScreenConfiguration*(*ffXRRGetScreenInfo)(Display*, Window) = dlsym(xrandr, "XRRGetScreenInfo");
if(ffXRRGetScreenInfo == NULL)
return 0;
short(*ffXRRConfigCurrentRate)(XRRScreenConfiguration*) = dlsym(xrandr, "XRRConfigCurrentRate");
if(ffXRRConfigCurrentRate == NULL)
return 0;
XRRScreenConfiguration* xrrscreenconf = ffXRRGetScreenInfo(display, root);
if(xrrscreenconf == NULL)
return 0;
short currentRate = ffXRRConfigCurrentRate(xrrscreenconf);
void(*ffXRRFreeScreenConfigInfo)(XRRScreenConfiguration*) = dlsym(xrandr, "XRRFreeScreenConfigInfo");
if(ffXRRFreeScreenConfigInfo != NULL)
ffXRRFreeScreenConfigInfo(xrrscreenconf);
dlclose(xrandr);
return currentRate;
}
void ffPrintResolution(FFstate* state)
{
if(ffPrintCachedValue(state, "Resolution"))
return;
void* x11 = dlopen("libX11.so", RTLD_LAZY);
if(x11 == NULL)
{
ffPrintError(state, "Resolution", "dlopen(\"libX11.so\", RTLD_LAZY) == NULL");
return;
}
Display*(*ffXOpenDisplay)(const char*) = dlsym(x11, "XOpenDisplay");
if(ffXOpenDisplay == NULL)
{
ffPrintError(state, "Resolution", "dlsym(x11, \"XOpenDisplay\") == NULL");
return;
}
Display* display = ffXOpenDisplay(NULL);
if(display == NULL)
{
ffPrintError(state, "Resolution", "ffXOpenDisplay(NULL) == NULL");
return;
}
Screen* screen = DefaultScreenOfDisplay(display);
short currentRate = getCurrentRate(display);
dlclose(x11);
char resolution[1024];
if(currentRate == 0)
sprintf(resolution, "%ix%i", screen->width, screen->height);
else
sprintf(resolution, "%ix%i @ %dHz", screen->width, screen->height, currentRate);
ffPrintAndSaveCachedValue(state, "Resolution", resolution);
}
+10
View File
@@ -0,0 +1,10 @@
#include "fastfetch.h"
void ffPrintSeperator(FFstate* state)
{
ffPrintLogoLine(state);
for(uint8_t i = 0; i < state->titleLength; i++)
putchar('-');
putchar('\n');
}
+25
View File
@@ -0,0 +1,25 @@
#include "fastfetch.h"
#include <string.h>
void ffPrintShell(FFstate* state)
{
if(ffPrintCachedValue(state, "Shell"))
return;
char* shellPath = getenv("SHELL");
if(shellPath == NULL)
{
ffPrintError(state, "Shell", "getenv(\"SHELL\") == NULL");
return;
}
char* shellName = strrchr(shellPath, '/');
if(shellName == NULL)
shellName = shellPath;
else if(shellName[0] == '/')
++shellName;
ffPrintAndSaveCachedValue(state, "Shell", shellName);
}
+64
View File
@@ -0,0 +1,64 @@
#include "fastfetch.h"
#include <string.h>
void printTerminalName(FFstate* state, const char* pid)
{
char file[256];
sprintf(file, "/proc/%s/stat", pid);
FILE* stat = fopen(file, "r");
if(stat == NULL)
{
char error[300];
sprintf(error, "fopen(\"%s\", \"r\") == NULL", file);
ffPrintError(state, "Terminal", error);
return;
}
char name[256];
char ppid[256];
if(fscanf(stat, "%*s (%[^)])%*s%s", name, ppid) != 2)
{
ffPrintError(state, "Terminal", "fscanf(stat, \"%*s (%[^)])%*s%s\", name, ppid) != 2");
return;
}
fclose(stat);
if (
strcmp(name, "bash") == 0 ||
strcmp(name, "sh") == 0 ||
strcmp(name, "zsh") == 0 ||
strcmp(name, "ksh") == 0 ||
strcmp(name, "fish") == 0 ||
strcmp(name, "sudo") == 0 ||
strcmp(name, "su") == 0 ||
strcmp(name, "doas") == 0 ||
strcmp(name, "strace") == 0 )
{
printTerminalName(state, ppid);
return;
}
ffPrintLogoAndKey(state, "Terminal");
if(
strcmp(name, "systemd") == 0 ||
strcmp(name, "init") == 0 ||
strcmp(ppid, "0") == 0 )
{
puts("TTY");
}
else
{
puts(name);
}
}
void ffPrintTerminal(FFstate* state)
{
char ppid[256];
sprintf(ppid, "%i", getppid());
printTerminalName(state, ppid);
}
+34
View File
@@ -0,0 +1,34 @@
#include "fastfetch.h"
#include <string.h>
void ffPrintTheme(FFstate* state)
{
char plasma[256];
ffParsePropFileHome(state, ".config/kdeglobals", "Name=%[^\n]", plasma);
char gtk2[256];
ffParsePropFileHome(state, ".gtkrc-2.0", "gtk-theme-name=\"%[^\"]+", gtk2);
if(gtk2[0] == '\0')
strcpy(gtk2, "Raleigh");
char gtk3[256];
ffParsePropFileHome(state, ".config/gtk-3.0/settings.ini", "gtk-theme-name=%[^\n]", gtk3);
if(gtk3[0] == '\0')
strcpy(gtk3, "Adwaita");
char gtk4[256];
ffParsePropFileHome(state, ".config/gtk-4.0/settings.ini", "gtk-theme-name=%[^\n]", gtk4);
if(gtk4[0] == '\0')
strcpy(gtk4, "Adwaita");
ffPrintLogoAndKey(state, "Theme");
if(plasma[0] == '\0')
printf("Breeze [Plasma], ");
else
printf("%s [Plasma], ", plasma);
ffPrintGtkPretty(gtk2, gtk3, gtk4);
putchar('\n');
}
+17
View File
@@ -0,0 +1,17 @@
#include "fastfetch.h"
#include <string.h>
void ffPrintTitle(FFstate* state)
{
char hostname[256];
gethostname(hostname, 256);
state->titleLength = strlen(state->passwd->pw_name) + 1 + strlen(hostname);
ffPrintLogoLine(state);
printf(FASTFETCH_TEXT_MODIFIER_BOLT"%s%s"FASTFETCH_TEXT_MODIFIER_RESET"@"FASTFETCH_TEXT_MODIFIER_BOLT"%s%s"FASTFETCH_TEXT_MODIFIER_RESET"\n",
state->color, state->passwd->pw_name, state->color, hostname
);
}
+27
View File
@@ -0,0 +1,27 @@
#include "fastfetch.h"
void ffPrintUptime(FFstate* state)
{
ffPrintLogoAndKey(state, "Uptime");
if(state->sysinfo.uptime < 60)
{
printf("%ld seconds\n", state->sysinfo.uptime);
return;
}
int days = state->sysinfo.uptime / 86400;
int hours = (state->sysinfo.uptime - (days * 86400)) / 3600;
int minutes = (state->sysinfo.uptime - (days * 86400) - (hours * 3600)) / 60;
if(days > 0)
printf("%d day%s, ", days, days <= 1 ? "" : "s");
if(hours > 0)
printf("%d hour%s, ", hours, hours <= 1 ? "" : "s");
if(minutes > 0)
printf("%d min%s", minutes, minutes <= 1 ? "" : "s");
putchar('\n');
}