Add a multicore CPU monitor gadget.

As always this prove the coolness of edgar :) the whole
gadget are just 69 lines of python code (plus 150 of the edc)

A bit more of info at:
https://phab.enlightenment.org/w/emodules/edgar/
This commit is contained in:
Davide Andreoli 2015-09-09 21:42:51 +02:00
parent c1f8428199
commit e396e51f33
9 changed files with 272 additions and 0 deletions

36
GADGETS/cpu/Makefile Normal file
View File

@ -0,0 +1,36 @@
# Simple Makefile for Enlightenment (edgar) gadgets
# gadget specific config
GADGET_NAME = cpu
EXTRA_FILES =
# nothing should be changed below this point
GADGET_FILES = __init__.pyc $(GADGET_NAME).edj
prefix = $(shell pkg-config --variable=libdir enlightenment)
gadget_folder = ${prefix}/enlightenment/gadgets/$(GADGET_NAME)
.PHONY: all install clean
all: $(GADGET_FILES) $(EXTRA_FILES)
install: all
@mkdir -p -v ${gadget_folder}
@cp --preserve=mode -v $(GADGET_FILES) $(EXTRA_FILES) $(gadget_folder)
uninstall: all
@rm -rfv ${gadget_folder}
clean:
@rm -fv *.edj *.pyc
EDJE_CC = edje_cc
EDJE_FLAGS = -v -id images/ -fd fonts/
%.edj: %.edc images/*
$(EDJE_CC) $(EDJE_FLAGS) $<
@chmod -v og+r $@
%.pyc: %.py
python3 -c "from py_compile import compile; compile('$<', '$@')"

68
GADGETS/cpu/__init__.py Normal file
View File

@ -0,0 +1,68 @@
# This python file use the following encoding: utf-8
import psutil
import e
from efl import ecore
from efl import evas
from efl import edje
from efl.evas import EXPAND_BOTH, FILL_BOTH
__gadget_name__ = 'CPU Monitor'
__gadget_vers__ = '0.1'
__gadget_auth__ = 'DaveMDS'
__gadget_mail__ = 'dave@gurumeditation.it'
__gadget_desc__ = 'Multicore CPU monitor'
__gadget_vapi__ = 1
__gadget_opts__ = { 'popup_on_desktop': False }
class Gadget(e.Gadget):
def __init__(self):
super().__init__()
self.num_cores = psutil.cpu_count()
self.aspect = None
self.poller = None
def instance_created(self, obj, site):
super().instance_created(obj, site)
if self.aspect is None:
w, h = obj.data_get('aspect_w'), obj.data_get('aspect_h')
self.aspect = int(w), int(h)
obj.data['bars'] = list()
for i in range(self.num_cores):
bar = edje.Edje(obj.evas, size_hint_expand=EXPAND_BOTH,
size_hint_fill=FILL_BOTH)
e.theme_object_set(bar, 'cpu', 'bar')
obj.part_box_append('main.box', bar)
bar.show()
obj.data['bars'].append(bar)
obj.size_hint_aspect = (evas.EVAS_ASPECT_CONTROL_BOTH,
self.aspect[0] * self.num_cores, self.aspect[1])
if self.poller is None:
self.poller = ecore.Poller(8, self.poller_cb, ecore.ECORE_POLLER_CORE)
def instance_destroyed(self, obj):
super().instance_destroyed(obj)
if len(self._instances) < 1 and self.poller is not None:
self.poller.delete()
self.poller = None
def poller_cb(self):
percents = psutil.cpu_percent(percpu=True)
for obj in self._instances:
for i, bar in enumerate(obj.data['bars']):
bar.message_send(0, percents[i])
return ecore.ECORE_CALLBACK_RENEW

168
GADGETS/cpu/cpu.edc Normal file
View File

@ -0,0 +1,168 @@
/**
* EDGAR Dropbox Gadget
*/
#define DBG_SHOW(_PART, _R, _G, _B) \
part { name: "dbg_show"_PART; \
type: RECT; mouse_events: 0; \
description { state: "default" 0.0; \
rel1.to: _PART; rel2.to: _PART; \
color: _R _G _B 50; } }
#define SHOW(_PART) DBG_SHOW(_PART, 50, 0, 0)
images {
image: "cpu.png" COMP;
image: "bar_bg.png" COMP;
image: "green_on.png" COMP;
image: "yellow_on.png" COMP;
image: "red_on.png" COMP;
}
collections {
/**
* API [e/gadget/icon] The group used for the icon of the gadget
*/
group { name: "e/gadgets/cpu/icon";
parts {
image { "icon";
desc { "default";
aspect: 1.0 1.0;
aspect_preference: BOTH;
image.normal: "cpu.png";
}
}
}
}
/**
* API [e/gadget/main] The main group of the gadget
*/
group { name: "e/gadgets/cpu/main";
data {
item: "aspect_w" "10";
item: "aspect_h" "54";
}
parts {
box { "main.box";
desc { "default";
box.layout: "horizontal";
box.min: 1 1;
}
}
}
}
group { name: "e/gadgets/cpu/bar";
script {
public message(Msg_Type:type, id, ...) {
if ((type == MSG_FLOAT) && (id == 0)) {
new Float:val = getfarg(2);
if (val > (100 / 7 * 1)) set_state(PART:"led1", "on", 0.0);
else set_state(PART:"led1", "default", 0.0);
if (val > (100 / 7 * 2)) set_state(PART:"led2", "on", 0.0);
else set_state(PART:"led2", "default", 0.0);
if (val > (100 / 7 * 3)) set_state(PART:"led3", "on", 0.0);
else set_state(PART:"led3", "default", 0.0);
if (val > (100 / 7 * 4)) set_state(PART:"led4", "on", 0.0);
else set_state(PART:"led4", "default", 0.0);
if (val > (100 / 7 * 5)) set_state(PART:"led5", "on", 0.0);
else set_state(PART:"led5", "default", 0.0);
if (val > (100 / 7 * 6)) set_state(PART:"led6", "on", 0.0);
else set_state(PART:"led6", "default", 0.0);
// custom_state(PART:"test_bar", "default", 0.0);
// set_state_val(PART:"test_bar", STATE_REL1, 0.0, 1.0 - val / 100);
// set_state(PART:"test_bar", "custom", 0.0);
}
}
}
parts {
part { name: "bg";
desc { "default";
color: 255 255 255 200;
aspect: (10 / 54) (10 / 54); aspect_preference: BOTH;
image.normal: "bar_bg.png";
}
part { name: "led1";
desc { "default"; hid;
rel1.relative: 0.0 (1.0 / 6 * 5);
rel2.relative: 1.0 (1.0 / 6 * 6);
image.normal: "green_on.png";
}
desc { "on"; inherit: "default"; vis; }
}
part { name: "led2";
desc { "default"; hid;
rel1.relative: 0.0 (1.0 / 6 * 4);
rel2.relative: 1.0 (1.0 / 6 * 5);
image.normal: "green_on.png";
}
desc { "on"; inherit: "default"; vis; }
}
part { name: "led3";
desc { "default"; hid;
rel1.relative: 0.0 (1.0 / 6 * 3);
rel2.relative: 1.0 (1.0 / 6 * 4);
image.normal: "green_on.png";
}
desc { "on"; inherit: "default"; vis; }
}
part { name: "led4";
desc { "default"; hid;
rel1.relative: 0.0 (1.0 / 6 * 2);
rel2.relative: 1.0 (1.0 / 6 * 3);
image.normal: "yellow_on.png";
}
desc { "on"; inherit: "default"; vis; }
}
part { name: "led5";
desc { "default"; hid;
rel1.relative: 0.0 (1.0 / 6 * 1);
rel2.relative: 1.0 (1.0 / 6 * 2);
image.normal: "yellow_on.png";
}
desc { "on"; inherit: "default"; vis; }
}
part { name: "led6";
desc { "default"; hid;
rel1.relative: 0.0 (1.0 / 6 * 0);
rel2.relative: 1.0 (1.0 / 6 * 1);
image.normal: "red_on.png";
}
desc { "on"; inherit: "default"; vis; }
}
}
// rect { "test_bar";
// desc { "default";
// color: 255 255 255 50;
// rel1.relative: 0.0 1.0;
// rel1.to: "bg";
// rel2.to: "bg";
// }
// }
}
}
/**
* API [e/gadget/popup] This is the group that will be placed inside popups
*/
/*
group { name: "e/gadgets/cpu/popup";
min: 310 0;
parts {
box { "popup.box";
desc { "default";
box {
layout: "vertical";
padding: 0 6;
min: 1 1;
}
}
}
}
}
*/
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 664 B

BIN
GADGETS/cpu/images/cpu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 B

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 276 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 B