Debian Codegraph @

samples/gtk3

raw
1 #!/usr/bin/env python3
2
3 from pathlib import Path
4 from sys import argv, exit
5 from signal import SIGINT
6 from struct import unpack
7 import common
8
9 import gi
10 gi.require_versions({'GLib': '2.0', 'Hinawa': '4.0', 'Gtk': '3.0'})
11 from gi.repository import GLib, Hinawa, Gtk
12
13
14 def main() -> int:
15 if len(argv) < 2:
16 msg = ('One argument is required for path to special file of Linux FireWire character '
17 'device')
18 common.print_help_with_msg(Path(__file__).name, msg)
19 return 1
20 cmd, literal = argv[:2]
21
22 try:
23 path = common.detect_fw_cdev(literal)
24 except Exception as e:
25 common.print_help_with_msg(cmd, str(e))
26 return 1
27
28 try:
29 node = Hinawa.FwNode.new()
30 _ = node.open(str(path), 0)
31 common.print_fw_node_information(node)
32 except Exception as e:
33 msg = str(e)
34 common.print_help_with_msg(cmd, msg)
35 return 1
36
37 with common.listen_node_event(node, path):
38 sample = Sample(node)
39 sample.show_all()
40 node.connect('disconnected', lambda n, s: Gtk.main_quit(), sample)
41 Gtk.main()
42
43 return 0
44
45
46 class Sample(Gtk.Window):
47 def __init__(self, node):
48 Gtk.Window.__init__(self, title="Hinawa-4.0 gir sample")
49
50 self.node = node
51 self.req = Hinawa.FwReq.new()
52
53 grid = Gtk.Grid(row_spacing=10, row_homogeneous=True,
54 column_spacing=10, column_homogeneous=True,
55 margin_start=20, margin_end=20, margin_top=20,
56 margin_bottom=20)
57 self.add(grid)
58
59 button = Gtk.Button(label="Read")
60 button.connect("clicked", self.run_transaction)
61 grid.attach(button, 0, 0, 1, 1)
62
63 button = Gtk.Button(label="_Close", use_underline=True)
64 button.connect("clicked", lambda s: Gtk.main_quit())
65 grid.attach(button, 1, 0, 1, 1)
66
67 self.entry = Gtk.Entry()
68 self.entry.set_text("0xfffff0000980")
69 grid.attach(self.entry, 0, 1, 1, 1)
70
71 self.label = Gtk.Label(label="result")
72 self.label.set_text("0x00000000")
73 grid.attach(self.label, 1, 1, 1, 1)
74
75 # handle unix signal
76 GLib.unix_signal_add(GLib.PRIORITY_HIGH, SIGINT, lambda s: Gtk.main_quit(), self)
77
78 def run_transaction(self, button):
79 addr = int(self.entry.get_text(), 16)
80 frames = [0] * 4
81 try:
82 quadlet = common.read_quadlet(self.node, self.req, addr)
83 except Exception as e:
84 print(e)
85
86 label = '0x{:08x}'.format(quadlet)
87 self.label.set_text(label)
88
89
90 if __name__ == '__main__':
91 exit(main())