Network: better prensentation

This commit is contained in:
2023-09-30 02:39:12 +02:00
parent fc92f629a6
commit a345141a71
12 changed files with 314 additions and 58 deletions

View File

@@ -28,6 +28,14 @@ void process_gui();
void app_init();
void app_deinit();
enum Section_Status
{
SECTION_STATUS_NONE,
SECTION_STATUS_OK,
SECTION_STATUS_WARNING,
SECTION_STATUS_ERROR
};
struct App_Data
{
f64 glib_iteration_t = -1;
@@ -62,6 +70,8 @@ struct System_Info
u64 ram_total;
u64 ram_used;
u64 ram_available;
Section_Status status;
};
struct Network_Device
@@ -75,6 +85,8 @@ struct Network_Info
{
s32 device_count;
Network_Device devices[32];
Section_Status status;
};
struct Virt_Domain
@@ -95,6 +107,8 @@ struct Virt_Info
{
s32 domain_count;
Virt_Domain domains[32];
Section_Status status;
};
struct FS_Entry
@@ -110,6 +124,8 @@ struct FS_Info
{
s32 fs_count;
FS_Entry fs[32];
Section_Status status;
};
System_Info system_info;
@@ -118,6 +134,46 @@ Virt_Info virt_info;
FS_Info fs_info;
App_Data app_data;
Gui_Style style_default;
void style_select(Gui_Context *ctx, Section_Status status)
{
switch(status)
{
case SECTION_STATUS_NONE: {
ctx->style.button_color = style_default.button_color;
ctx->style.button_color_hovered = style_default.button_color_hovered;
ctx->style.window_background_color = style_default.window_background_color;
ctx->style.window_background_color_inactive = style_default.window_background_color_inactive;
ctx->style.window_border_color = style_default.window_border_color;
ctx->style.window_border_color_inactive = style_default.window_border_color_inactive;
} break;
case SECTION_STATUS_OK: {
ctx->style.button_color_hovered = ctx->style.button_color = v4{0,1,0,1};
ctx->style.window_background_color_inactive = ctx->style.window_background_color = v4{0,.6,0,1};
ctx->style.window_border_color_inactive = ctx->style.window_border_color = v4{0,1,0,1};
} break;
case SECTION_STATUS_WARNING: {
ctx->style.button_color_hovered = ctx->style.button_color = v4{1,.8,0,1};
ctx->style.window_background_color_inactive = ctx->style.window_background_color = v4{.6,.5,0,1};
ctx->style.window_border_color_inactive = ctx->style.window_border_color = v4{1,.8,0,1};
} break;
case SECTION_STATUS_ERROR: {
ctx->style.button_color_hovered = ctx->style.button_color = v4{1,0,0,1};
ctx->style.window_background_color_inactive = ctx->style.window_background_color = v4{0.6,0,0,1};
ctx->style.window_border_color_inactive = ctx->style.window_border_color = v4{1,0,0,1};
} break;
}
}
// Icons
r_texture icon_disk;
r_texture icon_lan;
r_texture icon_ram;
r_texture icon_vpn;
r_texture icon_wifi;
r_texture icon_cpu;
u32 seconds_to_duration_text(char *text, f64 seconds, bool show_millis = false)
{
@@ -147,10 +203,16 @@ u32 seconds_to_duration_text(char *text, f64 seconds, bool show_millis = false)
}
void load_image(const char *filename, u8 **data, s32 *width, s32 *height, s32 *channels)
r_texture load_image_monochrome(const char *filename)
{
s32 width, height, channels;
stbi_set_flip_vertically_on_load(false);
*data = stbi_load(filename, width, height, channels, 4);
u8 *data = stbi_load(filename, &width, &height, &channels, 1);
for(u64 i = 0; i < (width*height); i++) // Flipped colors
data[i] = 0xFF - data[i];
return r_texture_create(data, {width, height}, R_TEXTURE_ALPHA);
}
@@ -284,94 +346,188 @@ bool process_input()
void system_info_gui(Gui_Layout_Grid *grid)
{
Rect r = grid->rect_at({0,0}, {4,4});
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, r.size, 4, 4, 0.2*engine.gui_scaling);
Gui_Context *ctx = &global_gui_state.default_context;
// No window info
// Clock and date
system_info.time = time(NULL);
struct tm *time_info = localtime(&system_info.time);
char clock[64], timezone[64];
strftime(clock, 128, "%H:%M:%S", time_info);
strftime(timezone, 128, "%Z", time_info);
f32 old_font_size = ctx->style.font_size;
ctx->style.font_size *= 3;
gui_text_aligned(grid->rect_at({2,1}, {2,2}), clock, GUI_ALIGN_CENTER);
ctx->style.font_size = old_font_size;
gui_text_aligned(grid->rect_at({2,3}, {2,1}), timezone, GUI_ALIGN_CENTER);
gui_text_aligned(grid->rect_at({2,0}, {2,1}), system_info.cached_date_string, GUI_ALIGN_CENTER);
// Host
gui_text_aligned(grid->rect_at({0,0}, {2,1}), system_info.hostname, GUI_ALIGN_LEFT);
// Uptime
char uptime[128] = "Uptime: "; seconds_to_duration_text(uptime + strlen("Uptime: "), system_info.uptime);
gui_text_aligned(grid->rect_at({4,0}, {2,1}), uptime, GUI_ALIGN_RIGHT);
// Window with cpu n., load, ram
Rect r = grid->rect_at({4,1}, {2,3});
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, r.size, 2, 3, 0.2*engine.gui_scaling);
style_select(ctx, system_info.status);
gui_window_start(r, 0xabcdef01);
style_select(ctx, SECTION_STATUS_NONE);
// Host date and time
gui_text_aligned(layout.cell(1), system_info.hostname, GUI_ALIGN_LEFT);
gui_text_aligned(layout.cell(2), system_info.cached_time_string, GUI_ALIGN_CENTER);
gui_text_aligned(layout.cell(1), system_info.cached_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);
char load[128]; snprintf(load, 128, "Load: %.2f %.2f %.2f", system_info.load[0], system_info.load[1], system_info.load[2]);
char processors[128]; snprintf(processors, 128, "CPUs: %d/%d", system_info.cpus_active, system_info.cpus_total);
// slider for ram?
char ram[128]; snprintf(ram, 128, "RAM: %.2f/%.2f GiB", system_info.ram_used / (1024.0*1024.0*1024.0), system_info.ram_total / (1024.0*1024.0*1024.0));
layout.row(2);
gui_text_aligned(layout.cell(1), processors, GUI_ALIGN_LEFT);
gui_text_aligned(layout.cell(1), ram, GUI_ALIGN_LEFT);
gui_text_aligned(layout.cell(2), uptime, GUI_ALIGN_RIGHT);
gui_text_aligned(layout.cell(1), load, GUI_ALIGN_LEFT);
gui_text_aligned(layout.cell(2), processors, GUI_ALIGN_LEFT);
gui_text_aligned(layout.cell(2), ram, GUI_ALIGN_LEFT);
gui_text_aligned(layout.cell(2), load, GUI_ALIGN_LEFT);
gui_window_end();
}
void network_gui(Gui_Layout_Grid *grid)
{
Rect r = grid->rect_at({0,4}, {4,13});
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, r.size, 4, 13, 0.2*engine.gui_scaling);
gui_window_start(r, 0xabcdef02);
Gui_Context *ctx = &global_gui_state.default_context;
Rect r = grid->rect_at({0,4}, {3,13});
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, r.size, 3, 13, 0.2*engine.gui_scaling);
style_select(ctx, network_info.status);
gui_window_start(r, 0xabcdef02);
style_select(ctx, SECTION_STATUS_NONE);
gui_text_aligned(layout.cell(GUI_LAYOUT_MAX_CELLS), "Network", GUI_ALIGN_CENTER);
layout.row();
r = layout.rect_at({0,1}, layout.max_cells_count - v2s{0,1});
layout = gui_layout_grid_create_by_divisions(r.position, r.size, 3, 3, 0.2*engine.gui_scaling);
for(s32 i = 0; i < network_info.device_count; i++)
{
Network_Device *device = &network_info.devices[i];
gui_button(layout.cell(), device->name);
if(device->type != NM_DEVICE_TYPE_ETHERNET && device->type != NM_DEVICE_TYPE_WIFI && device->type != NM_DEVICE_TYPE_WIREGUARD)
continue;
gui_id_stack_push(ctx, gui_id_from_pointer(ctx, device->name));
switch(device->type)
{
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;
}
Section_Status status = SECTION_STATUS_OK;
if(device->state == NM_DEVICE_STATE_PREPARE ||
device->state == NM_DEVICE_STATE_CONFIG ||
device->state == NM_DEVICE_STATE_IP_CONFIG ||
device->state == NM_DEVICE_STATE_IP_CHECK)
{
// Bump status
if(status < SECTION_STATUS_WARNING)
status = SECTION_STATUS_WARNING;
}
else if(device->state != NM_DEVICE_STATE_ACTIVATED)
{
// Bump status
if(status < SECTION_STATUS_ERROR)
status = SECTION_STATUS_ERROR;
}
switch(device->state)
{
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;
Rect r = layout.cell();
style_select(ctx, status);
gui_panel(r);
style_select(ctx, SECTION_STATUS_NONE);
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(r.position, r.size, 9, 4, 0.1*engine.gui_scaling);
const char *type_text = "";
r_texture *icon = &icon_lan;
switch(device->type)
{
case NM_DEVICE_TYPE_ETHERNET: {
icon = &icon_lan;
type_text = "Ethernet";
} break;
case NM_DEVICE_TYPE_WIFI: {
icon = &icon_wifi;
type_text = "WiFi";
} break;
case NM_DEVICE_TYPE_TUN: {
icon = &icon_vpn;
type_text = "TAP/TUN";
} break;
case NM_DEVICE_TYPE_BRIDGE: {
icon = &icon_lan;
type_text = "Bridge";
} break;
case NM_DEVICE_TYPE_VLAN: {
icon = &icon_lan;
type_text = "vlan";
} break;
case NM_DEVICE_TYPE_WIREGUARD: {
icon = &icon_vpn;
type_text = "VPN";
} break;
default: {
icon = &icon_lan;
type_text = "";
} break;
}
const char *state_text = "";
switch(device->state)
{
case NM_DEVICE_STATE_UNKNOWN: state_text = "UNKNOWN"; break;
case NM_DEVICE_STATE_UNMANAGED: state_text = "UNMANAGED"; break;
case NM_DEVICE_STATE_UNAVAILABLE: state_text = "UNAVAILABLE"; break;
case NM_DEVICE_STATE_DISCONNECTED: state_text = "DISCONNECTED"; break;
case NM_DEVICE_STATE_PREPARE: state_text = "PREPARE"; break;
case NM_DEVICE_STATE_CONFIG: state_text = "CONFIG"; break;
case NM_DEVICE_STATE_NEED_AUTH: state_text = "NEED_AUTH"; break;
case NM_DEVICE_STATE_IP_CONFIG: state_text = "IP_CONFIG"; break;
case NM_DEVICE_STATE_IP_CHECK: state_text = "IP_CHECK"; break;
case NM_DEVICE_STATE_SECONDARIES: state_text = "SECONDARIES"; break;
case NM_DEVICE_STATE_ACTIVATED: state_text = "ACTIVATED"; break;
case NM_DEVICE_STATE_DEACTIVATING: state_text = "DEACTIVATING"; break;
case NM_DEVICE_STATE_FAILED: state_text = "FAILED"; break;
default: gui_button(layout.cell(), ""); break;
}
Rect icon_r = layout.cell(1);
icon_r.position += (icon_r.size - v2{1,1}*minimum(icon_r.size.x, icon_r.size.y)) * .5;
icon_r.size = v2{1,1} * minimum(icon_r.size.x, icon_r.size.y);
gui_image(icon_r, icon);
gui_text(layout.cell(layout.max_cells_count.x - layout.cursor.x), device->name);
layout.cell(1);
gui_text(layout.cell(layout.max_cells_count.x - layout.cursor.x), type_text);
layout.row(2);
layout.cell(1);
gui_text(layout.cell(layout.max_cells_count.x - layout.cursor.x), state_text);
}
gui_id_stack_pop(ctx);
layout.row();
}
gui_window_end();
}
void vm_gui(Gui_Layout_Grid *grid)
{
Gui_Context *ctx = &global_gui_state.default_context;
Rect r = grid->rect_at({0,17}, {4,5});
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, r.size, 4, 5, 0.2*engine.gui_scaling);
gui_window_start(r, 0xabcdef03);
Gui_Context *ctx = &global_gui_state.default_context;
style_select(ctx, virt_info.status);
gui_window_start(r, 0xabcdef03);
style_select(ctx, SECTION_STATUS_NONE);
Gui_Style old_style = ctx->style;
@@ -431,12 +587,16 @@ void vm_gui(Gui_Layout_Grid *grid)
void fs_gui(Gui_Layout_Grid *grid)
{
Rect r = grid->rect_at({4,0}, {2,22});
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, r.size, 2, 22, 0.2*engine.gui_scaling);
gui_window_start(r, 0xabcdef04);
Gui_Context *ctx = &global_gui_state.default_context;
Rect r = grid->rect_at({4,4}, {2,18});
Gui_Layout_Grid layout = gui_layout_grid_create_by_divisions(v2{0,0}, r.size, 2, 18, 0.2*engine.gui_scaling);
style_select(ctx, fs_info.status);
gui_window_start(r, 0xabcdef04);
style_select(ctx, SECTION_STATUS_NONE);
for(s32 i = 0; i < fs_info.fs_count; i++)
{
@@ -509,6 +669,9 @@ void collect_new_data_if_needed()
system_info.ram_used = sys_info.totalram * sys_info.mem_unit - system_info.ram_available;
// Status
system_info.status = SECTION_STATUS_NONE;
app_data.system_sample_t = engine.time;
}
@@ -526,6 +689,30 @@ void collect_new_data_if_needed()
network_info.devices[i].state = nm_device_get_state(device);
}
// Update section status
network_info.status = SECTION_STATUS_OK;
for(int i = 0; i < network_info.device_count; i++)
{
Network_Device *device = &network_info.devices[i];
if(device->type == NM_DEVICE_TYPE_ETHERNET || device->type == NM_DEVICE_TYPE_WIREGUARD)
{
if(device->state == NM_DEVICE_STATE_PREPARE ||
device->state == NM_DEVICE_STATE_CONFIG ||
device->state == NM_DEVICE_STATE_IP_CONFIG ||
device->state == NM_DEVICE_STATE_IP_CHECK)
{
// Bump status
if(network_info.status < SECTION_STATUS_WARNING)
network_info.status = SECTION_STATUS_WARNING;
}
else if(device->state != NM_DEVICE_STATE_ACTIVATED)
{
// Bump status
if(network_info.status < SECTION_STATUS_ERROR)
network_info.status = SECTION_STATUS_ERROR;
}
}
}
app_data.network_sample_t = engine.time;
}
@@ -560,6 +747,25 @@ void collect_new_data_if_needed()
p_free(domains);
// Update section status
virt_info.status = SECTION_STATUS_OK;
for(int i = 0; i < virt_info.domain_count; i++)
{
Virt_Domain *domain = &virt_info.domains[i];
if(domain->state == VIR_DOMAIN_PAUSED ||
domain->state == VIR_DOMAIN_PMSUSPENDED)
{
// Bump status
if(virt_info.status < SECTION_STATUS_WARNING)
virt_info.status = SECTION_STATUS_WARNING;
}
else if(domain->state != VIR_DOMAIN_RUNNING)
{
// Bump status
if(virt_info.status < SECTION_STATUS_ERROR)
virt_info.status = SECTION_STATUS_ERROR;
}
}
app_data.virt_sample_t = engine.time;
}
@@ -592,6 +798,8 @@ void collect_new_data_if_needed()
}
endmntent(file);
fs_info.status = SECTION_STATUS_NONE;
}
}
@@ -635,6 +843,16 @@ void app_init()
virt_connection = virConnectOpenReadOnly("qemu:///system");
collect_static_data();
style_default = global_gui_state.default_context.style;
// Icons
icon_disk = load_image_monochrome("assets/icons/harddisk-symbolic.png");
icon_lan = load_image_monochrome("assets/icons/lan-symbolic.png");
icon_ram = load_image_monochrome("assets/icons/memory-symbolic.png");
icon_vpn = load_image_monochrome("assets/icons/network-vpn-symbolic.png");
icon_wifi = load_image_monochrome("assets/icons/network-wireless-symbolic.png");
icon_cpu = load_image_monochrome("assets/icons/processor-symbolic.png");
}
void app_deinit()