mirror of
https://codeberg.org/tmayoff/.dotfiles.git
synced 2025-12-06 16:58:34 -05:00
added sketchybar config
This commit is contained in:
parent
87bb07f015
commit
95ebd7af03
38 changed files with 2329 additions and 0 deletions
58
dot_config/sketchybar/helpers/event_providers/cpu_load/cpu.h
Normal file
58
dot_config/sketchybar/helpers/event_providers/cpu_load/cpu.h
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#include <mach/mach.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct cpu {
|
||||
host_t host;
|
||||
mach_msg_type_number_t count;
|
||||
host_cpu_load_info_data_t load;
|
||||
host_cpu_load_info_data_t prev_load;
|
||||
bool has_prev_load;
|
||||
|
||||
int user_load;
|
||||
int sys_load;
|
||||
int total_load;
|
||||
};
|
||||
|
||||
static inline void cpu_init(struct cpu* cpu) {
|
||||
cpu->host = mach_host_self();
|
||||
cpu->count = HOST_CPU_LOAD_INFO_COUNT;
|
||||
cpu->has_prev_load = false;
|
||||
}
|
||||
|
||||
static inline void cpu_update(struct cpu* cpu) {
|
||||
kern_return_t error = host_statistics(cpu->host,
|
||||
HOST_CPU_LOAD_INFO,
|
||||
(host_info_t)&cpu->load,
|
||||
&cpu->count );
|
||||
|
||||
if (error != KERN_SUCCESS) {
|
||||
printf("Error: Could not read cpu host statistics.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (cpu->has_prev_load) {
|
||||
uint32_t delta_user = cpu->load.cpu_ticks[CPU_STATE_USER]
|
||||
- cpu->prev_load.cpu_ticks[CPU_STATE_USER];
|
||||
|
||||
uint32_t delta_system = cpu->load.cpu_ticks[CPU_STATE_SYSTEM]
|
||||
- cpu->prev_load.cpu_ticks[CPU_STATE_SYSTEM];
|
||||
|
||||
uint32_t delta_idle = cpu->load.cpu_ticks[CPU_STATE_IDLE]
|
||||
- cpu->prev_load.cpu_ticks[CPU_STATE_IDLE];
|
||||
|
||||
cpu->user_load = (double)delta_user / (double)(delta_system
|
||||
+ delta_user
|
||||
+ delta_idle) * 100.0;
|
||||
|
||||
cpu->sys_load = (double)delta_system / (double)(delta_system
|
||||
+ delta_user
|
||||
+ delta_idle) * 100.0;
|
||||
|
||||
cpu->total_load = cpu->user_load + cpu->sys_load;
|
||||
}
|
||||
|
||||
cpu->prev_load = cpu->load;
|
||||
cpu->has_prev_load = true;
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
#include "cpu.h"
|
||||
#include "../sketchybar.h"
|
||||
|
||||
int main (int argc, char** argv) {
|
||||
float update_freq;
|
||||
if (argc < 3 || (sscanf(argv[2], "%f", &update_freq) != 1)) {
|
||||
printf("Usage: %s \"<event-name>\" \"<event_freq>\"\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
alarm(0);
|
||||
struct cpu cpu;
|
||||
cpu_init(&cpu);
|
||||
|
||||
// Setup the event in sketchybar
|
||||
char event_message[512];
|
||||
snprintf(event_message, 512, "--add event '%s'", argv[1]);
|
||||
sketchybar(event_message);
|
||||
|
||||
char trigger_message[512];
|
||||
for (;;) {
|
||||
// Acquire new info
|
||||
cpu_update(&cpu);
|
||||
|
||||
// Prepare the event message
|
||||
snprintf(trigger_message,
|
||||
512,
|
||||
"--trigger '%s' user_load='%d' sys_load='%02d' total_load='%02d'",
|
||||
argv[1],
|
||||
cpu.user_load,
|
||||
cpu.sys_load,
|
||||
cpu.total_load );
|
||||
|
||||
// Trigger the event
|
||||
sketchybar(trigger_message);
|
||||
|
||||
// Wait
|
||||
usleep(update_freq * 1000000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
bin/cpu_load: cpu_load.c cpu.h ../sketchybar.h | bin
|
||||
clang -std=c99 -O3 $< -o $@
|
||||
|
||||
bin:
|
||||
mkdir bin
|
||||
Loading…
Add table
Add a link
Reference in a new issue