System info & network
This commit is contained in:
315
code/main.cpp
Normal file
315
code/main.cpp
Normal file
@@ -0,0 +1,315 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "platform.h"
|
||||
#include "render/render.h"
|
||||
#include "enginestate.h"
|
||||
#include "lib/types.h"
|
||||
#include "lib/math.h"
|
||||
#include "gui/gui.h"
|
||||
#include "debug/logger.h"
|
||||
#include "debug/log_viewer.h"
|
||||
#include "gui/layout.h"
|
||||
#include "stb_image.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#include <NetworkManager.h>
|
||||
|
||||
|
||||
bool process_input(); // Returns true when the program needs to exit
|
||||
void process_gui();
|
||||
void app_init();
|
||||
void app_deinit();
|
||||
|
||||
u32 seconds_to_duration_text(char *text, f64 seconds, bool show_millis = false)
|
||||
{
|
||||
u32 written = 0;
|
||||
u32 days = seconds / (24 * 3600);
|
||||
seconds -= days * (24 * 3600);
|
||||
u32 hours = seconds / 3600;
|
||||
seconds -= hours * 3600;
|
||||
u32 minutes = seconds / 60;
|
||||
seconds -= minutes * 60;
|
||||
|
||||
if(days)
|
||||
written += sprintf(text + written, "%s%dd", (written ? " " : ""), days);
|
||||
if(days || hours)
|
||||
written += sprintf(text + written, "%s%dh", (written ? " " : ""), hours);
|
||||
if(days || hours || minutes)
|
||||
written += sprintf(text + written, "%s%dm", (written ? " " : ""), minutes);
|
||||
if(days || hours || minutes || seconds)
|
||||
{
|
||||
if(show_millis)
|
||||
written += sprintf(text + written, "%s%.3lfs", (written ? " " : ""), seconds);
|
||||
else
|
||||
written += sprintf(text + written, "%s%.0lfs", (written ? " " : ""), seconds);
|
||||
}
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
|
||||
void load_image(const char *filename, u8 **data, s32 *width, s32 *height, s32 *channels)
|
||||
{
|
||||
stbi_set_flip_vertically_on_load(false);
|
||||
*data = stbi_load(filename, width, height, channels, 4);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool status;
|
||||
|
||||
LOG_INIT();
|
||||
p_init(true);
|
||||
|
||||
LogViewer log_viewer = LogViewer::Init(&global_logger);
|
||||
|
||||
p_window_open();
|
||||
p_window_name((char*)"Server Monitor");
|
||||
|
||||
r_init();
|
||||
gui_init();
|
||||
|
||||
log_viewer.Print_New_Messages_On_Console();
|
||||
|
||||
|
||||
app_init();
|
||||
|
||||
|
||||
f64 start = p_time();
|
||||
f64 prev_t = 0;
|
||||
while(1)
|
||||
{
|
||||
// Engine
|
||||
engine.time = p_time() - start;
|
||||
engine.delta_t = engine.time - prev_t;
|
||||
//LOG(LOG_INFO, "Frame time: %.3lf ms FPS: %.2lf", 1000*delta_t, 1/delta_t);
|
||||
r_time_update(engine.time);
|
||||
|
||||
// Input
|
||||
bool exit = process_input();
|
||||
if(exit)
|
||||
break;
|
||||
|
||||
// GUI
|
||||
r_framebuffer_select(&r_render_state.framebuffer_SCREEN);
|
||||
r_clear({0,0,0,0});
|
||||
gui_frame_begin(engine.time);
|
||||
|
||||
process_gui();
|
||||
|
||||
gui_frame_end();
|
||||
log_viewer.Print_New_Messages_On_Console();
|
||||
r_swap();
|
||||
prev_t = engine.time;
|
||||
}
|
||||
|
||||
app_deinit();
|
||||
gui_deinit();
|
||||
p_window_close();
|
||||
|
||||
LOG_DEINIT();
|
||||
p_deinit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool process_input()
|
||||
{
|
||||
// Events
|
||||
Event event;
|
||||
while(p_next_event(&event))
|
||||
{
|
||||
gui_handle_event(&event);
|
||||
|
||||
switch(event.type)
|
||||
{
|
||||
case EVENT_QUIT: {
|
||||
return true;
|
||||
};
|
||||
case EVENT_MOUSE_MOVE:
|
||||
{
|
||||
|
||||
} break;
|
||||
case EVENT_KEY:
|
||||
{
|
||||
switch(event.key.key_code)
|
||||
{
|
||||
|
||||
default: {
|
||||
// Other keys. Put default to suppress warnings
|
||||
} break;
|
||||
}
|
||||
} break;
|
||||
case EVENT_RESIZE:
|
||||
{
|
||||
//LOG(LOG_DEBUG, "New size: %u %u", signal.resize.width, signal.resize.height);
|
||||
s32 width = event.resize.width;
|
||||
s32 height = event.resize.height;
|
||||
r_size_update(width, height);
|
||||
|
||||
global_gui_state.default_context.width = width;
|
||||
global_gui_state.default_context.height = height;
|
||||
|
||||
engine.gui_scaling = minimum(width, height) * 0.03;
|
||||
global_gui_state.default_context.style.font_size = engine.gui_scaling;
|
||||
global_gui_state.default_context.style.button_radius = engine.gui_scaling / 10;
|
||||
|
||||
} break;
|
||||
case EVENT_FOCUS:
|
||||
{
|
||||
} break;
|
||||
case EVENT_UNFOCUS:
|
||||
{
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
NMClient *nmclient;
|
||||
void app_init()
|
||||
{
|
||||
nmclient = nm_client_new(NULL, NULL);
|
||||
}
|
||||
|
||||
void app_deinit()
|
||||
{
|
||||
}
|
||||
|
||||
void system_info_window()
|
||||
{
|
||||
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, v2{40,10}*engine.gui_scaling, 3, 6, 0.4*engine.gui_scaling);
|
||||
gui_window_start(Rect{0.1*engine.gui_scaling, 0.1*engine.gui_scaling, layout.window_size.x, layout.window_size.y}, 0xabcdef01);
|
||||
|
||||
// Hostname
|
||||
struct utsname host_info;
|
||||
uname(&host_info);
|
||||
|
||||
char hostname[128] = "Server manager";
|
||||
if(host_info.nodename[0])
|
||||
strcpy(hostname, host_info.nodename);
|
||||
char kernel[256];
|
||||
sprintf(kernel, "%s %s", host_info.sysname, host_info.release);
|
||||
|
||||
// Clock
|
||||
time_t time_now = time(NULL);
|
||||
struct tm *time_info = localtime(&time_now);
|
||||
char date_string[128];
|
||||
strftime(date_string, 128, "%a %e %b %Y", time_info);
|
||||
char time_string[128];
|
||||
strftime(time_string, 128, "%H:%M:%S %Z", time_info);
|
||||
|
||||
|
||||
gui_text_aligned(layout.cell(), hostname, GUI_ALIGN_LEFT);
|
||||
gui_text_aligned(layout.cell(), time_string, GUI_ALIGN_CENTER);
|
||||
gui_text_aligned(layout.cell(), date_string, GUI_ALIGN_RIGHT);
|
||||
|
||||
|
||||
// Load, Memory, Uptime
|
||||
struct sysinfo sys_info;
|
||||
sysinfo(&sys_info);
|
||||
char uptime[128] = "Uptime: "; seconds_to_duration_text(uptime + strlen("Uptime: "), sys_info.uptime);
|
||||
f32 load_scale = 1.0f / (1 << SI_LOAD_SHIFT);
|
||||
f32 loads[3] = {
|
||||
load_scale * sys_info.loads[1],
|
||||
load_scale * sys_info.loads[1],
|
||||
load_scale * sys_info.loads[1]
|
||||
};
|
||||
for(int i = 0; i < 3; i++)
|
||||
loads[i] = round(load_scale * sys_info.loads[i] * 100) / 100;
|
||||
char load[128]; sprintf(load, "Load: %.2f %.2f %.2f", loads[0], loads[1], loads[2]);
|
||||
|
||||
int n_processors = get_nprocs();
|
||||
int n_processors_active = get_nprocs_conf();
|
||||
char processors[128]; sprintf(processors, "CPUs: %d/%d", n_processors_active, n_processors);
|
||||
|
||||
u64 ram_total = sys_info.totalram * sys_info.mem_unit;
|
||||
u64 ram_used = (sys_info.totalram - sys_info.freeram - sys_info.bufferram) * sys_info.mem_unit;
|
||||
char ram[128]; sprintf(ram, "RAM: %.2f/%.2f GiB", ram_used / (1024.0*1024.0*1024.0), ram_total / (1024.0*1024.0*1024.0));
|
||||
|
||||
layout.row(2);
|
||||
gui_text_aligned(layout.cell(), processors, GUI_ALIGN_LEFT);
|
||||
gui_text_aligned(layout.cell(), load, GUI_ALIGN_CENTER);
|
||||
gui_text_aligned(layout.cell(), uptime, GUI_ALIGN_RIGHT);
|
||||
gui_text_aligned(layout.cell(), ram, GUI_ALIGN_LEFT);
|
||||
|
||||
|
||||
gui_window_end();
|
||||
}
|
||||
|
||||
void network_window()
|
||||
{
|
||||
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, v2{40,12}*engine.gui_scaling, 3, 7, 0.4*engine.gui_scaling);
|
||||
gui_window_start(Rect{0.1*engine.gui_scaling, 11*engine.gui_scaling, layout.window_size.x, layout.window_size.y}, 0xabcdef02);
|
||||
|
||||
const GPtrArray *devices = nm_client_get_devices(nmclient);
|
||||
for(int i = 0; i < devices->len; i++)
|
||||
{
|
||||
NMDevice *device = (NMDevice*)devices->pdata[i];
|
||||
const char *device_name = nm_device_get_iface(device);
|
||||
gui_button(layout.cell(), device_name);
|
||||
|
||||
Gui_Context *ctx = &global_gui_state.default_context;
|
||||
gui_id_stack_push(ctx, gui_id_from_pointer(ctx, device_name));
|
||||
|
||||
switch(nm_device_get_device_type(device))
|
||||
{
|
||||
case NM_DEVICE_TYPE_ETHERNET: gui_button(layout.cell(), "ETHERNET"); break;
|
||||
case NM_DEVICE_TYPE_WIFI: gui_button(layout.cell(), "WIFI"); break;
|
||||
case NM_DEVICE_TYPE_TUN: gui_button(layout.cell(), "TAP or TUN"); break;
|
||||
case NM_DEVICE_TYPE_BRIDGE: gui_button(layout.cell(), "BRIDGE"); break;
|
||||
case NM_DEVICE_TYPE_VLAN: gui_button(layout.cell(), "VLAN"); break;
|
||||
case NM_DEVICE_TYPE_WIREGUARD: gui_button(layout.cell(), "WIREGUARD"); break;
|
||||
default: gui_button(layout.cell(), ""); break;
|
||||
}
|
||||
|
||||
switch(nm_device_get_state(device))
|
||||
{
|
||||
case NM_DEVICE_STATE_UNKNOWN: gui_button(layout.cell(), "UNKNOWN"); break;
|
||||
case NM_DEVICE_STATE_UNMANAGED: gui_button(layout.cell(), "UNMANAGED"); break;
|
||||
case NM_DEVICE_STATE_UNAVAILABLE: gui_button(layout.cell(), "UNAVAILABLE"); break;
|
||||
case NM_DEVICE_STATE_DISCONNECTED: gui_button(layout.cell(), "DISCONNECTED"); break;
|
||||
case NM_DEVICE_STATE_PREPARE: gui_button(layout.cell(), "PREPARE"); break;
|
||||
case NM_DEVICE_STATE_CONFIG: gui_button(layout.cell(), "CONFIG"); break;
|
||||
case NM_DEVICE_STATE_NEED_AUTH: gui_button(layout.cell(), "NEED_AUTH"); break;
|
||||
case NM_DEVICE_STATE_IP_CONFIG: gui_button(layout.cell(), "IP_CONFIG"); break;
|
||||
case NM_DEVICE_STATE_IP_CHECK: gui_button(layout.cell(), "IP_CHECK"); break;
|
||||
case NM_DEVICE_STATE_SECONDARIES: gui_button(layout.cell(), "SECONDARIES"); break;
|
||||
case NM_DEVICE_STATE_ACTIVATED: gui_button(layout.cell(), "ACTIVATED"); break;
|
||||
case NM_DEVICE_STATE_DEACTIVATING: gui_button(layout.cell(), "DEACTIVATING"); break;
|
||||
case NM_DEVICE_STATE_FAILED: gui_button(layout.cell(), "FAILED"); break;
|
||||
default: gui_button(layout.cell(), ""); break;
|
||||
}
|
||||
|
||||
gui_id_stack_pop(ctx);
|
||||
layout.row();
|
||||
}
|
||||
gui_window_end();
|
||||
}
|
||||
|
||||
void vm_window()
|
||||
{
|
||||
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, v2{40,8}*engine.gui_scaling, 3, 7, 0.4*engine.gui_scaling);
|
||||
gui_window_start(Rect{0.1*engine.gui_scaling, 24*engine.gui_scaling, layout.window_size.x, layout.window_size.y}, 0xabcdef03);
|
||||
|
||||
|
||||
|
||||
gui_window_end();
|
||||
}
|
||||
|
||||
void process_gui()
|
||||
{
|
||||
g_main_context_iteration(NULL, false);
|
||||
system_info_window();
|
||||
network_window();
|
||||
vm_window();
|
||||
}
|
||||
Reference in New Issue
Block a user