diff --git a/src/bin/ui/ui_cpu.c b/src/bin/ui/ui_cpu.c index d78337c..0b214dd 100644 --- a/src/bin/ui/ui_cpu.c +++ b/src/bin/ui/ui_cpu.c @@ -260,6 +260,7 @@ ui_cpu_win_add(Evisum_Ui *ui) exit(1); } pd = vis->func(box); + pd->win = win; pd->ui = ui; elm_object_content_set(scr, tb); diff --git a/src/bin/ui/ui_cpu.h b/src/bin/ui/ui_cpu.h index 34ae496..346c537 100644 --- a/src/bin/ui/ui_cpu.h +++ b/src/bin/ui/ui_cpu.h @@ -31,6 +31,7 @@ typedef struct { Ecore_Thread *thread; Evas_Object *menu; + Evas_Object *win; Elm_Layout *btn_menu; Eina_Bool btn_visible; diff --git a/src/bin/ui/visuals/cpu_bars.c b/src/bin/ui/visuals/cpu_bars.c new file mode 100644 index 0000000..bb7b7e0 --- /dev/null +++ b/src/bin/ui/visuals/cpu_bars.c @@ -0,0 +1,135 @@ +#include "cpu_bars.h" + +#define BAR_WIDTH 16 + +typedef struct { + int cpu_count; + int *cpu_order; + Eina_List *objects; + Evas_Object *tb; + Evas_Object *bg; +} Ext; + +static void +_core_times_main_cb(void *data, Ecore_Thread *thread) +{ + int ncpu; + Ui_Cpu_Data *pd = data; + Ext *ext = pd->ext; + + while (!ecore_thread_check(thread)) + { + cpu_core_t **cores = system_cpu_usage_delayed_get(&ncpu, 400000); + Core *cores_out = calloc(ncpu, sizeof(Core)); + + if (cores_out) + { + for (int n = 0; n < ncpu; n++) + { + int id = ext->cpu_order[n]; + Core *core = &(cores_out[n]); + core->id = id; + core->percent = cores[id]->percent; + free(cores[id]); + } + ecore_thread_feedback(thread, cores_out); + } + free(cores); + } +} + +static void +_core_times_feedback_cb(void *data, Ecore_Thread *thread EINA_UNUSED, void *msgdata) +{ + Ui_Cpu_Data *pd; + Core *cores; + Ext *ext; + Evas_Coord oh, ow, ox, oy; + int step = 0; + + pd = data; + ext = pd->ext; + cores = msgdata; + + evas_object_geometry_get(ext->tb, NULL, &oy, &ow, &oh); + evas_object_geometry_get(pd->win, NULL, NULL, NULL, &oh); + + step = (oh / 100); + + for (int i = 0; i < ext->cpu_count; i++) + { + Core *core = &cores[i]; + Evas_Object *rec = eina_list_nth(ext->objects, i); + evas_object_geometry_get(rec, &ox, NULL, NULL, NULL); + int c = temp_colormap[core->percent & 0xff]; + evas_object_color_set(rec, RVAL(c), GVAL(c), BVAL(c), AVAL(c)); + evas_object_resize(rec, ELM_SCALE_SIZE(BAR_WIDTH), ELM_SCALE_SIZE(core->percent * step)); + evas_object_move(rec, ox, oy - ELM_SCALE_SIZE(core->percent * step)); + elm_table_align_set(ext->tb, 0.0, 1.0); + } + + free(cores); +} + +static void +_cb_free(void *data) +{ + Ext *ext = data; + + eina_list_free(ext->objects); + + free(ext->cpu_order); + free(ext); +} + +Ui_Cpu_Data * +cpu_visual_bars(Evas_Object *parent_bx) +{ + Evas_Object *tb, *rec; + Ext *ext; + int i; + + Ui_Cpu_Data *pd = calloc(1, sizeof(Ui_Cpu_Data)); + if (!pd) return NULL; + + pd->ext = ext = calloc(1, sizeof(Ext)); + EINA_SAFETY_ON_NULL_RETURN_VAL(ext, NULL); + pd->ext_free_cb = _cb_free; + + /* Populate lookup table to match id with topology core id */ + ext->cpu_count = system_cpu_count_get(); + ext->cpu_order = malloc((ext->cpu_count) * sizeof(int)); + for (i = 0; i < ext->cpu_count; i++) + ext->cpu_order[i] = i; + system_cpu_topology_get(ext->cpu_order, ext->cpu_count); + + ext->tb = tb = elm_table_add(parent_bx); + elm_table_padding_set(tb, ELM_SCALE_SIZE(2), ELM_SCALE_SIZE(2)); + evas_object_size_hint_weight_set(tb, EXPAND, EXPAND); + evas_object_size_hint_align_set(tb, 0.5, 1.0); + evas_object_show(tb); + + for (i = 0; i < ext->cpu_count; i++) + { + rec = evas_object_rectangle_add(evas_object_evas_get(tb)); + evas_object_size_hint_min_set(rec, ELM_SCALE_SIZE(BAR_WIDTH), 1); + elm_table_pack(tb, rec, i, 0, 1, 1); + evas_object_show(rec); + ext->objects = eina_list_append(ext->objects, rec); + } + + rec = evas_object_rectangle_add(evas_object_evas_get(tb)); + evas_object_size_hint_weight_set(rec, EXPAND, EXPAND); + evas_object_size_hint_align_set(rec, FILL, FILL); + elm_table_pack(tb, rec, 0, 0, i, 2); + + elm_box_pack_end(parent_bx, tb); + + pd->thread = ecore_thread_feedback_run(_core_times_main_cb, + _core_times_feedback_cb, + NULL, + NULL, + pd, 1); + return pd; +} + diff --git a/src/bin/ui/visuals/cpu_bars.h b/src/bin/ui/visuals/cpu_bars.h new file mode 100644 index 0000000..978296c --- /dev/null +++ b/src/bin/ui/visuals/cpu_bars.h @@ -0,0 +1,9 @@ +#ifndef CPU_BARS_H +#define CPU_BARS_H + +#include "ui/ui_cpu.h" + +Ui_Cpu_Data * +cpu_visual_bars(Evas_Object *parent); + +#endif diff --git a/src/bin/ui/visuals/meson.build b/src/bin/ui/visuals/meson.build index f00a2a7..3a504f5 100644 --- a/src/bin/ui/visuals/meson.build +++ b/src/bin/ui/visuals/meson.build @@ -3,4 +3,6 @@ src += files([ 'cpu_default.h', 'cpu_basic.c', 'cpu_basic.h', + 'cpu_bars.c', + 'cpu_bars.h', ]) diff --git a/src/bin/ui/visuals/visuals.x b/src/bin/ui/visuals/visuals.x index 408fc98..5a575ab 100644 --- a/src/bin/ui/visuals/visuals.x +++ b/src/bin/ui/visuals/visuals.x @@ -1,7 +1,9 @@ #include "cpu_default.h" #include "cpu_basic.h" +#include "cpu_bars.h" Cpu_Visual visualizations[] = { { .name = "default", .func = cpu_visual_default }, { .name = "basic", .func = cpu_visual_basic }, + { .name = "bars", .func = cpu_visual_bars }, };