evisum/src/bin/ui/ui_memory.c

307 lines
8.4 KiB
C
Raw Normal View History

#include "ui_memory.h"
#include "system/filesystems.h"
2020-12-15 09:23:32 -08:00
#include <Elementary.h>
typedef struct {
2020-12-29 18:41:44 -08:00
Ecore_Thread *thread;
2020-12-15 09:23:32 -08:00
Evas_Object *win;
Evas_Object *used;
Evas_Object *cached;
Evas_Object *buffered;
Evas_Object *shared;
Evas_Object *swap;
Evas_Object *video[MEM_VIDEO_CARD_MAX];
2020-12-15 09:23:32 -08:00
Ui *ui;
} Ui_Data;
static Evas_Object *
_label_mem(Evas_Object *parent, const char *text)
{
2020-12-15 09:23:32 -08:00
Evas_Object *lb = elm_label_add(parent);
evas_object_size_hint_weight_set(lb, 0, EXPAND);
evas_object_size_hint_align_set(lb, FILL, FILL);
elm_object_text_set(lb, eina_slstr_printf("%s",text));
evas_object_show(lb);
2020-12-15 09:23:32 -08:00
return lb;
}
2020-06-03 05:54:08 -07:00
static Evas_Object *
_progress_add(Evas_Object *parent)
{
2020-06-17 14:34:25 -07:00
Evas_Object *pb = elm_progressbar_add(parent);
evas_object_size_hint_align_set(pb, FILL, FILL);
evas_object_size_hint_weight_set(pb, EXPAND, EXPAND);
elm_progressbar_span_size_set(pb, 1.0);
evas_object_show(pb);
2020-06-03 05:54:08 -07:00
2020-06-17 14:34:25 -07:00
return pb;
2020-06-03 05:54:08 -07:00
}
2020-12-15 09:23:32 -08:00
static void
_mem_usage_main_cb(void *data EINA_UNUSED, Ecore_Thread *thread)
{
static meminfo_t memory;
while (!ecore_thread_check(thread))
{
memset(&memory, 0, sizeof(memory));
system_memory_usage_get(&memory);
if (file_system_in_use("ZFS"))
memory.used += memory.zfs_arc_used;
ecore_thread_feedback(thread, &memory);
usleep(1000000);
2020-12-15 09:23:32 -08:00
}
}
static void
_update_widgets(Ui_Data *pd, meminfo_t *memory)
{
Evas_Object *pb;
double ratio, value;
ratio = memory->total / 100.0;
pb = pd->used;
value = memory->used / ratio;
elm_progressbar_value_set(pb, value / 100);
elm_progressbar_unit_format_set(pb,
eina_slstr_printf("%s / %s",
2020-12-15 09:23:32 -08:00
evisum_size_format(memory->used),
evisum_size_format(memory->total)));
2020-12-15 09:23:32 -08:00
pb = pd->cached;
value = memory->cached / ratio;
elm_progressbar_value_set(pb, value / 100);
elm_progressbar_unit_format_set(pb,
eina_slstr_printf("%s / %s",
2020-12-15 09:23:32 -08:00
evisum_size_format(memory->cached),
evisum_size_format(memory->total)));
2020-12-15 09:23:32 -08:00
pb = pd->buffered;
value = memory->buffered / ratio;
elm_progressbar_value_set(pb, value / 100);
elm_progressbar_unit_format_set(pb,
eina_slstr_printf("%s / %s",
2020-12-15 09:23:32 -08:00
evisum_size_format(memory->buffered),
evisum_size_format(memory->total)));
pb = pd->shared;
value = memory->shared / ratio;
elm_progressbar_value_set(pb, value / 100);
elm_progressbar_unit_format_set(pb,
eina_slstr_printf("%s / %s",
2020-12-15 09:23:32 -08:00
evisum_size_format(memory->shared),
evisum_size_format(memory->total)));
pb = pd->swap;
if (memory->swap_total)
{
elm_object_disabled_set(pb, EINA_FALSE);
2020-12-15 09:23:32 -08:00
ratio = memory->swap_total / 100.0;
value = memory->swap_used / ratio;
}
else
{
elm_object_disabled_set(pb, EINA_TRUE);
value = 0.0;
}
elm_progressbar_value_set(pb, value / 100);
elm_progressbar_unit_format_set(pb,
eina_slstr_printf("%s / %s",
2020-12-15 09:23:32 -08:00
evisum_size_format(memory->swap_used),
evisum_size_format(memory->swap_total)));
2020-12-15 09:23:32 -08:00
for (int i = 0; i < memory->video_count; i++)
{
2020-12-15 09:23:32 -08:00
pb = pd->video[i];
if (!pb) break;
2020-12-15 09:23:32 -08:00
if (memory->video[i].total) elm_object_disabled_set(pb, EINA_FALSE);
else elm_object_disabled_set(pb, EINA_TRUE);
2020-12-15 09:23:32 -08:00
ratio = memory->video[i].total / 100.0;
value = memory->video[i].used / ratio;
elm_progressbar_value_set(pb, value / 100);
elm_progressbar_unit_format_set(pb,
eina_slstr_printf("%s / %s",
2020-12-15 09:23:32 -08:00
evisum_size_format(memory->video[i].used),
evisum_size_format(memory->video[i].total)));
}
}
static void
_mem_usage_feedback_cb(void *data, Ecore_Thread *thread EINA_UNUSED, void *msgdata)
{
Ui_Data *pd;
meminfo_t *memory;
pd = data;
memory = msgdata;
if (ecore_thread_check(thread)) return;
_update_widgets(pd, memory);
}
static void
_win_key_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
Evas_Event_Key_Down *ev;
Ui_Data *pd;
pd = data;
ev = event_info;
if (!ev || !ev->keyname)
return;
if (!strcmp(ev->keyname, "Escape"))
evas_object_del(pd->win);
}
2020-12-30 11:31:50 -08:00
static void
_win_move_cb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
{
Ui_Data *pd;
Ui *ui;
pd = data;
ui = pd->ui;
evas_object_geometry_get(obj, &ui->mem.x, &ui->mem.y, NULL, NULL);
2020-12-30 11:31:50 -08:00
}
static void
_win_del_cb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj,
void *event_info EINA_UNUSED)
{
Ui *ui;
2020-12-15 09:23:32 -08:00
Ui_Data *pd = data;
ui = pd->ui;
evisum_ui_config_save(ui);
2020-12-29 18:41:44 -08:00
ecore_thread_cancel(pd->thread);
ecore_thread_wait(pd->thread, 0.5);
evas_object_del(obj);
ui->mem.win = NULL;
2020-12-15 09:23:32 -08:00
free(pd);
}
static void
_win_resize_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
2020-12-15 09:23:32 -08:00
Ui_Data *pd = data;
evisum_ui_config_save(pd->ui);
}
void
ui_win_memory_add(Ui *ui)
{
Evas_Object *win, *lb, *bx, *tbl, *pb;
2020-12-15 09:23:32 -08:00
Evas_Object *fr;
int i;
meminfo_t memory;
if (ui->mem.win)
{
elm_win_raise(ui->mem.win);
return;
}
2020-12-15 09:23:32 -08:00
Ui_Data *pd = calloc(1, sizeof(Ui_Data));
if (!pd) return;
pd->ui = ui;
memset(&memory, 0, sizeof(memory));
system_memory_usage_get(&memory);
ui->mem.win = win = elm_win_util_standard_add("evisum", _("Memory Usage"));
2020-12-15 09:23:32 -08:00
pd->win = win;
elm_win_autodel_set(win, EINA_TRUE);
evas_object_size_hint_weight_set(win, EXPAND, EXPAND);
evas_object_size_hint_align_set(win, FILL, FILL);
2020-12-17 03:12:15 -08:00
evisum_ui_background_random_add(win,
evisum_ui_backgrounds_enabled_get());
2020-12-15 09:23:32 -08:00
bx = elm_box_add(win);
evas_object_size_hint_weight_set(bx, EXPAND, 0);
evas_object_size_hint_align_set(bx, FILL, FILL);
evas_object_show(bx);
elm_object_content_set(win, bx);
fr = elm_frame_add(win);
evas_object_size_hint_weight_set(fr, EXPAND, EXPAND);
evas_object_size_hint_align_set(fr, FILL, FILL);
elm_object_style_set(fr, "pad_medium");
evas_object_show(fr);
tbl = elm_table_add(win);
evas_object_size_hint_weight_set(tbl, EXPAND, EXPAND);
evas_object_size_hint_align_set(tbl, FILL, FILL);
elm_table_padding_set(tbl, 8, 2);
evas_object_show(tbl);
elm_object_content_set(fr, tbl);
elm_box_pack_end(bx, fr);
lb = _label_mem(tbl, _("Used"));
pd->used = pb = _progress_add(tbl);
elm_table_pack(tbl, lb, 1, 1, 1, 1);
elm_table_pack(tbl, pb, 2, 1, 1, 1);
lb = _label_mem(tbl, _("Cached"));
pd->cached = pb = _progress_add(tbl);
elm_table_pack(tbl, lb, 1, 2, 1, 1);
elm_table_pack(tbl, pb, 2, 2, 1, 1);
lb = _label_mem(tbl, _("Buffered"));
pd->buffered = pb = _progress_add(tbl);
elm_table_pack(tbl, lb, 1, 3, 1, 1);
elm_table_pack(tbl, pb, 2, 3, 1, 1);
lb = _label_mem(tbl, _("Shared"));
pd->shared = pb = _progress_add(tbl);
elm_table_pack(tbl, lb, 1, 4, 1, 1);
elm_table_pack(tbl, pb, 2, 4, 1, 1);
lb = _label_mem(tbl, _("Swapped"));
pd->swap = pb = _progress_add(tbl);
elm_table_pack(tbl, lb, 1, 5, 1, 1);
elm_table_pack(tbl, pb, 2, 5, 1, 1);
for (i = 0; i < memory.video_count; i++)
{
2020-12-15 09:23:32 -08:00
lb = _label_mem(tbl, _("Video"));
pd->video[i] = pb = _progress_add(tbl);
elm_table_pack(tbl, lb, 1, 6 + i, 1, 1);
elm_table_pack(tbl, pb, 2, 6 + i, 1, 1);
}
if (ui->mem.width > 0 && ui->mem.height > 0)
evas_object_resize(win, ui->mem.width, ui->mem.height);
else
2020-12-15 09:23:32 -08:00
evas_object_resize(win, UI_CHILD_WIN_WIDTH , UI_CHILD_WIN_HEIGHT);
if (ui->mem.x > 0 && ui->mem.y > 0)
evas_object_move(win, ui->mem.x, ui->mem.y);
else
elm_win_center(win, 1, 1);
2020-12-15 09:23:32 -08:00
evas_object_event_callback_add(win, EVAS_CALLBACK_RESIZE, _win_resize_cb, pd);
evas_object_event_callback_add(win, EVAS_CALLBACK_DEL, _win_del_cb, pd);
2020-12-30 11:31:50 -08:00
evas_object_event_callback_add(win, EVAS_CALLBACK_MOVE, _win_move_cb, pd);
evas_object_event_callback_add(tbl, EVAS_CALLBACK_KEY_DOWN, _win_key_down_cb, pd);
2020-12-15 09:23:32 -08:00
evas_object_show(win);
2020-12-29 18:41:44 -08:00
pd->thread = ecore_thread_feedback_run(_mem_usage_main_cb,
_mem_usage_feedback_cb,
NULL,
NULL,
pd, EINA_TRUE);
}