241 lines
7.0 KiB
Python
241 lines
7.0 KiB
Python
#!/usr/bin/env python3
|
|
# Process Manager - monitor (Python/XFrame).
|
|
#
|
|
# A plain Python/GTK program: no CMake. Run with:
|
|
# PYTHONPATH=/root/pm-work/lightgui_python python3 \
|
|
# examples/monitor_process_manager/main.py
|
|
# with the LightGUI Python package on PYTHONPATH.
|
|
#
|
|
# One 1024x768 window: a live XProcessList (/proc, sortable columns,
|
|
# click a row to select it), a GtkEntry filter (name or pid substring),
|
|
# and GtkButton Terminate / Kill / Refresh. Terminate sends SIGTERM and
|
|
# Kill sends SIGKILL to the SELECTED row's pid only -- never anything
|
|
# else (pid <= 1 and our own pid are refused outright).
|
|
# Esc or close the window to quit.
|
|
|
|
import os
|
|
import signal
|
|
import sys
|
|
import time
|
|
|
|
import gi
|
|
|
|
gi.require_version("Gtk", "3.0")
|
|
gi.require_version("Gdk", "3.0")
|
|
|
|
from gi.repository import Gdk, GLib, Gtk # noqa: E402
|
|
from lightgui.xframe.core import create, parse_color # noqa: E402
|
|
|
|
|
|
def _signal_selected(sig):
|
|
"""Signal the selected row's pid only; refresh afterwards."""
|
|
widget = _WIDGETS[0]
|
|
pid = widget.selected_pid()
|
|
if pid is None or pid <= 1 or pid == os.getpid():
|
|
return
|
|
try:
|
|
os.kill(pid, sig)
|
|
except (ProcessLookupError, PermissionError):
|
|
return
|
|
widget.refresh_processes()
|
|
if _AREA:
|
|
_AREA[0].queue_draw()
|
|
|
|
|
|
def on_Terminate_001_OnClick(widget):
|
|
_signal_selected(signal.SIGTERM)
|
|
|
|
|
|
def on_Kill_001_OnClick(widget):
|
|
_signal_selected(signal.SIGKILL)
|
|
|
|
|
|
def on_Refresh_001_OnClick(widget):
|
|
_WIDGETS[0].refresh_processes()
|
|
if _AREA:
|
|
_AREA[0].queue_draw()
|
|
|
|
|
|
# Event handlers, wired to the widgets in main():
|
|
_HANDLERS = {
|
|
('Terminate_001', 'OnClick'): on_Terminate_001_OnClick,
|
|
('Kill_001', 'OnClick'): on_Kill_001_OnClick,
|
|
('Refresh_001', 'OnClick'): on_Refresh_001_OnClick,
|
|
}
|
|
|
|
|
|
def _build_widgets():
|
|
"""Create every placed widget with its props."""
|
|
widgets = []
|
|
plist = create('XProcessList', 'XProcessList_001', 16, 58, 992, 694)
|
|
plist.set_prop("label", "Processes")
|
|
# Two samples 0.25 s apart so CPU% is real, not 0.0, on first paint.
|
|
plist.refresh_processes()
|
|
time.sleep(0.25)
|
|
plist.refresh_processes()
|
|
widgets.append(plist)
|
|
entry = create('GtkEntry', 'Filter_001', 16, 16, 300, 30)
|
|
entry.set_text("")
|
|
entry.set_prop("placeholder-text", "Filter: name or pid")
|
|
widgets.append(entry)
|
|
for name, label, x in (('Terminate_001', 'Terminate', 336),
|
|
('Kill_001', 'Kill', 466),
|
|
('Refresh_001', 'Refresh', 596)):
|
|
btn = create('GtkButton', name, x, 16, 120, 30)
|
|
btn.set_text(label)
|
|
widgets.append(btn)
|
|
return widgets
|
|
|
|
|
|
_DESIGN_SIZE = (1024, 768)
|
|
_BACKGROUND = ''
|
|
_WIDGETS = []
|
|
_LAST_SIZE = [None]
|
|
_PRESSED = [None] # Task 4: widget that got the ButtonPress
|
|
_FOCUSED = [None] # GtkEntry with keyboard focus (None = none)
|
|
_AREA = []
|
|
|
|
|
|
def _hit_widget(widgets, x, y):
|
|
"""Topmost visible widget containing (*x*, *y*)."""
|
|
for widget in reversed(widgets):
|
|
if widget.get_prop("visible", "true") in ("false", "0"):
|
|
continue
|
|
if widget.contains(x, y):
|
|
return widget
|
|
return None
|
|
|
|
|
|
def _dispatch_to(widget, event):
|
|
"""Call the *event* handler of *widget* (already chosen)."""
|
|
if widget is None:
|
|
return
|
|
if widget.get_prop("enabled", "true") in ("false", "0"):
|
|
return
|
|
handler = _HANDLERS.get((widget.name, event))
|
|
if handler is not None:
|
|
handler(widget)
|
|
|
|
|
|
def _dispatch(widgets, x, y, event):
|
|
"""Call the *event* handler of the widget at (*x*, *y*),
|
|
if it has one wired."""
|
|
_dispatch_to(_hit_widget(widgets, x, y), event)
|
|
|
|
|
|
def _on_draw(area, cr):
|
|
bg = parse_color(_BACKGROUND, None)
|
|
if bg is None:
|
|
bg = (1.0, 1.0, 1.0)
|
|
cr.set_source_rgb(*bg)
|
|
cr.paint()
|
|
for widget in _WIDGETS:
|
|
widget.draw(cr)
|
|
return False
|
|
|
|
|
|
def _on_button_press(area, event):
|
|
widget = _hit_widget(_WIDGETS, event.x, event.y)
|
|
if widget is not None and widget.kind == "XProcessList":
|
|
# Widget-relative click: header sorts, body row selects.
|
|
widget.select_at(event.x - widget.x, event.y - widget.y)
|
|
_FOCUSED[0] = None
|
|
area.queue_draw()
|
|
elif widget is not None and widget.kind == "GtkEntry":
|
|
_FOCUSED[0] = widget
|
|
else:
|
|
_FOCUSED[0] = None
|
|
if event.type == Gdk.EventType.DOUBLE_BUTTON_PRESS:
|
|
_dispatch(_WIDGETS, event.x, event.y, "OnDblClick")
|
|
elif event.type == Gdk.EventType.BUTTON_PRESS:
|
|
_dispatch(_WIDGETS, event.x, event.y, "OnMouseDown")
|
|
_dispatch(_WIDGETS, event.x, event.y, "OnClick")
|
|
# Task 4: remember the pressed widget; release goes there.
|
|
_PRESSED[0] = widget
|
|
return False
|
|
|
|
|
|
def _on_button_release(area, event):
|
|
# Task 4: release goes to the widget that got the press,
|
|
# even if the pointer moved off it.
|
|
widget = _PRESSED[0]
|
|
_PRESSED[0] = None
|
|
_dispatch_to(widget, "OnMouseUp")
|
|
return False
|
|
|
|
|
|
def _on_key(window, event):
|
|
"""Forward typing to the focused filter entry; Esc quits."""
|
|
if event.keyval == Gdk.KEY_Escape:
|
|
Gtk.main_quit()
|
|
return True
|
|
entry = _FOCUSED[0]
|
|
if entry is None:
|
|
return False
|
|
text = entry.text or ""
|
|
if event.keyval == Gdk.KEY_BackSpace:
|
|
text = text[:-1]
|
|
elif event.keyval == Gdk.KEY_Return:
|
|
return True
|
|
else:
|
|
ch = Gdk.keyval_to_unicode(event.keyval)
|
|
if ch == 0 or ch < 0x20 or ch >= 0x7f:
|
|
return False
|
|
text += chr(ch)
|
|
entry.set_text(text)
|
|
_WIDGETS[0].set_filter(text)
|
|
if _AREA:
|
|
_AREA[0].queue_draw()
|
|
return True
|
|
|
|
|
|
def _on_area_resize(area, allocation):
|
|
size = (allocation.width, allocation.height)
|
|
if size == _LAST_SIZE[0]:
|
|
return False
|
|
_LAST_SIZE[0] = size
|
|
area.queue_draw()
|
|
return False
|
|
|
|
|
|
# --- live demo ------------------------------------------------------------
|
|
# The list shows live data: _tick() re-scans /proc every 2000 ms and
|
|
# redraws the drawing area.
|
|
def _tick():
|
|
_WIDGETS[0].refresh_processes()
|
|
if _AREA:
|
|
_AREA[0].queue_draw()
|
|
return True
|
|
|
|
|
|
def main():
|
|
window = Gtk.Window(title='Process Manager - monitor')
|
|
window.set_default_size(1024, 768)
|
|
window.connect("destroy", Gtk.main_quit)
|
|
window.connect("key-press-event", _on_key)
|
|
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
|
|
spacing=0)
|
|
window.add(vbox)
|
|
area = Gtk.DrawingArea()
|
|
area.set_hexpand(True)
|
|
area.set_vexpand(True)
|
|
area.set_can_focus(True)
|
|
area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK |
|
|
Gdk.EventMask.BUTTON_RELEASE_MASK)
|
|
area.connect("draw", _on_draw)
|
|
area.connect("button-press-event", _on_button_press)
|
|
area.connect("button-release-event", _on_button_release)
|
|
area.connect("size-allocate", _on_area_resize)
|
|
vbox.pack_start(area, True, True, 0)
|
|
_WIDGETS.extend(_build_widgets())
|
|
_AREA.append(area)
|
|
GLib.timeout_add(2000, _tick)
|
|
window.show_all()
|
|
area.grab_focus()
|
|
Gtk.main()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|