1# Copyright (C) 2021 Free Software Foundation, Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16# A TUI window implemented in Python that responds to, and displays,
17# stop and exit events.
18
19import gdb
20
21# When an event arrives we ask the window to redraw itself.  We should
22# only do this if the window is valid.  When this flag is true we
23# perform the is_valid check.  When this flag is false
24perform_valid_check = True
25update_title = False
26cleanup_properly = False
27
28# A global place into which we can write the window title.
29titles_at_the_close = {}
30
31
32class EventWindow:
33    def __init__(self, win):
34        self._win = win
35        self._count = 0
36        win.title = "This Is The Event Window"
37        self._stop_listener = lambda e: self._event("stop", e)
38        gdb.events.stop.connect(self._stop_listener)
39        self._exit_listener = lambda e: self._event("exit", e)
40        gdb.events.exited.connect(self._exit_listener)
41        self._events = []
42
43        # Ensure we can erase and write to the window from the
44        # constructor, the window should be valid by this point.
45        self._win.erase()
46        self._win.write("Hello world...")
47
48    def close(self):
49        global cleanup_properly
50        global titles_at_the_close
51
52        # Ensure that window properties can be read within the close method.
53        titles_at_the_close[self._win.title] = dict(
54            width=self._win.width, height=self._win.height
55        )
56
57        # The following calls are pretty pointless, but this ensures
58        # that we can erase and write to a window from the close
59        # method, the last moment a window should be valid.
60        self._win.erase()
61        self._win.write("Goodbye cruel world...")
62
63        if cleanup_properly:
64            # Disconnect the listeners and delete the lambda functions.
65            # This removes cyclic references to SELF, and so alows SELF to
66            # be deleted.
67            gdb.events.stop.disconnect(self._stop_listener)
68            gdb.events.exited.disconnect(self._exit_listener)
69            self._stop_listener = None
70            self._exit_listener = None
71
72    def _event(self, type, event):
73        global perform_valid_check
74        global update_title
75
76        self._count += 1
77        self._events.insert(0, type)
78        if not perform_valid_check or self._win.is_valid():
79            if update_title:
80                self._win.title = "This Is The Event Window (" + str(self._count) + ")"
81            else:
82                self.render()
83
84    def render(self):
85        self._win.erase()
86        w = self._win.width
87        h = self._win.height
88        for i in range(min(h, len(self._events))):
89            self._win.write(self._events[i] + "\n")
90
91
92gdb.register_window_type("events", EventWindow)
93