1 /* TUI windows implemented in Python
3 Copyright (C) 2020 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "arch-utils.h"
23 #include "python-internal.h"
27 /* Note that Python's public headers may define HAVE_NCURSES_H, so if
28 we unconditionally include this (outside the #ifdef above), then we
29 can get a compile error when ncurses is not in fact installed. See
30 PR tui/25597; or the upstream Python bug
31 https://bugs.python.org/issue20768. */
32 #include "gdb_curses.h"
34 #include "tui/tui-data.h"
35 #include "tui/tui-io.h"
36 #include "tui/tui-layout.h"
37 #include "tui/tui-wingeneral.h"
38 #include "tui/tui-winsource.h"
42 /* A PyObject representing a TUI window. */
44 struct gdbpy_tui_window
48 /* The TUI window, or nullptr if the window has been deleted. */
49 tui_py_window *window;
52 extern PyTypeObject gdbpy_tui_window_object_type
53 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("gdbpy_tui_window");
55 /* A TUI window written in Python. */
57 class tui_py_window : public tui_win_info
61 tui_py_window (const char *name, gdbpy_ref<gdbpy_tui_window> wrapper)
63 m_wrapper (std::move (wrapper))
65 m_wrapper->window = this;
70 DISABLE_COPY_AND_ASSIGN (tui_py_window);
72 /* Set the "user window" to the indicated reference. The user
73 window is the object returned the by user-defined window
75 void set_user_window (gdbpy_ref<> &&user_window)
77 m_window = std::move (user_window);
80 const char *name () const override
82 return m_name.c_str ();
85 void rerender () override;
86 void do_scroll_vertical (int num_to_scroll) override;
87 void do_scroll_horizontal (int num_to_scroll) override;
89 /* Erase and re-box the window. */
94 werase (handle.get ());
95 check_and_display_highlight_if_needed ();
101 /* Write STR to the window. */
102 void output (const char *str);
104 /* A helper function to compute the viewport width. */
105 int viewport_width () const
107 return std::max (0, width - 2);
110 /* A helper function to compute the viewport height. */
111 int viewport_height () const
113 return std::max (0, height - 2);
118 /* Location of the cursor. */
122 /* The name of this window. */
125 /* The underlying Python window object. */
126 gdbpy_ref<> m_window;
128 /* The Python wrapper for this object. */
129 gdbpy_ref<gdbpy_tui_window> m_wrapper;
132 tui_py_window::~tui_py_window ()
134 gdbpy_enter enter_py (get_current_arch (), current_language);
136 /* This can be null if the user-provided Python construction
138 if (m_window != nullptr
139 && PyObject_HasAttrString (m_window.get (), "close"))
141 gdbpy_ref<> result (PyObject_CallMethod (m_window.get (), "close",
143 if (result == nullptr)
144 gdbpy_print_stack ();
148 m_wrapper->window = nullptr;
149 /* Explicitly free the Python references. We have to do this
150 manually because we need to hold the GIL while doing so. */
151 m_wrapper.reset (nullptr);
152 m_window.reset (nullptr);
156 tui_py_window::rerender ()
158 gdbpy_enter enter_py (get_current_arch (), current_language);
160 if (PyObject_HasAttrString (m_window.get (), "render"))
162 gdbpy_ref<> result (PyObject_CallMethod (m_window.get (), "render",
164 if (result == nullptr)
165 gdbpy_print_stack ();
170 tui_py_window::do_scroll_horizontal (int num_to_scroll)
172 gdbpy_enter enter_py (get_current_arch (), current_language);
174 if (PyObject_HasAttrString (m_window.get (), "hscroll"))
176 gdbpy_ref<> result (PyObject_CallMethod (m_window.get(), "hscroll",
177 "i", num_to_scroll, nullptr));
178 if (result == nullptr)
179 gdbpy_print_stack ();
184 tui_py_window::do_scroll_vertical (int num_to_scroll)
186 gdbpy_enter enter_py (get_current_arch (), current_language);
188 if (PyObject_HasAttrString (m_window.get (), "vscroll"))
190 gdbpy_ref<> result (PyObject_CallMethod (m_window.get (), "vscroll",
191 "i", num_to_scroll, nullptr));
192 if (result == nullptr)
193 gdbpy_print_stack ();
198 tui_py_window::output (const char *text)
200 int vwidth = viewport_width ();
202 while (cursor_y < viewport_height () && *text != '\0')
204 wmove (handle.get (), cursor_y + 1, cursor_x + 1);
206 std::string line = tui_copy_source_line (&text, 0, 0,
207 vwidth - cursor_x, 0);
208 tui_puts (line.c_str (), handle.get ());
217 cursor_x = getcurx (handle.get ()) - 1;
220 wrefresh (handle.get ());
225 /* A callable that is used to create a TUI window. It wraps the
226 user-supplied window constructor. */
228 class gdbpy_tui_window_maker
232 explicit gdbpy_tui_window_maker (gdbpy_ref<> &&constr)
233 : m_constr (std::move (constr))
237 ~gdbpy_tui_window_maker ();
239 gdbpy_tui_window_maker (gdbpy_tui_window_maker &&other) noexcept
240 : m_constr (std::move (other.m_constr))
244 gdbpy_tui_window_maker (const gdbpy_tui_window_maker &other)
246 gdbpy_enter enter_py (get_current_arch (), current_language);
247 m_constr = other.m_constr;
250 gdbpy_tui_window_maker &operator= (gdbpy_tui_window_maker &&other)
252 m_constr = std::move (other.m_constr);
256 gdbpy_tui_window_maker &operator= (const gdbpy_tui_window_maker &other)
258 gdbpy_enter enter_py (get_current_arch (), current_language);
259 m_constr = other.m_constr;
263 tui_win_info *operator() (const char *name);
267 /* A constructor that is called to make a TUI window. */
268 gdbpy_ref<> m_constr;
271 gdbpy_tui_window_maker::~gdbpy_tui_window_maker ()
273 gdbpy_enter enter_py (get_current_arch (), current_language);
274 m_constr.reset (nullptr);
278 gdbpy_tui_window_maker::operator() (const char *win_name)
280 gdbpy_enter enter_py (get_current_arch (), current_language);
282 gdbpy_ref<gdbpy_tui_window> wrapper
283 (PyObject_New (gdbpy_tui_window, &gdbpy_tui_window_object_type));
284 if (wrapper == nullptr)
286 gdbpy_print_stack ();
290 std::unique_ptr<tui_py_window> window
291 (new tui_py_window (win_name, wrapper));
293 gdbpy_ref<> user_window
294 (PyObject_CallFunctionObjArgs (m_constr.get (),
295 (PyObject *) wrapper.get (),
297 if (user_window == nullptr)
299 gdbpy_print_stack ();
303 window->set_user_window (std::move (user_window));
304 /* Window is now owned by the TUI. */
305 return window.release ();
308 /* Implement "gdb.register_window_type". */
311 gdbpy_register_tui_window (PyObject *self, PyObject *args, PyObject *kw)
313 static const char *keywords[] = { "name", "constructor", nullptr };
318 if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "sO", keywords,
324 gdbpy_tui_window_maker constr (gdbpy_ref<>::new_reference (cons_obj));
325 tui_register_window (name, constr);
327 catch (const gdb_exception &except)
329 gdbpy_convert_exception (except);
338 /* Require that "Window" be a valid window. */
340 #define REQUIRE_WINDOW(Window) \
342 if ((Window)->window == nullptr) \
343 return PyErr_Format (PyExc_RuntimeError, \
344 _("TUI window is invalid.")); \
347 /* Python function which checks the validity of a TUI window
350 gdbpy_tui_is_valid (PyObject *self, PyObject *args)
352 gdbpy_tui_window *win = (gdbpy_tui_window *) self;
354 if (win->window != nullptr)
359 /* Python function that erases the TUI window. */
361 gdbpy_tui_erase (PyObject *self, PyObject *args)
363 gdbpy_tui_window *win = (gdbpy_tui_window *) self;
365 REQUIRE_WINDOW (win);
367 win->window->erase ();
372 /* Python function that writes some text to a TUI window. */
374 gdbpy_tui_write (PyObject *self, PyObject *args)
376 gdbpy_tui_window *win = (gdbpy_tui_window *) self;
379 if (!PyArg_ParseTuple (args, "s", &text))
382 REQUIRE_WINDOW (win);
384 win->window->output (text);
389 /* Return the width of the TUI window. */
391 gdbpy_tui_width (PyObject *self, void *closure)
393 gdbpy_tui_window *win = (gdbpy_tui_window *) self;
394 REQUIRE_WINDOW (win);
395 return PyLong_FromLong (win->window->viewport_width ());
398 /* Return the height of the TUI window. */
400 gdbpy_tui_height (PyObject *self, void *closure)
402 gdbpy_tui_window *win = (gdbpy_tui_window *) self;
403 REQUIRE_WINDOW (win);
404 return PyLong_FromLong (win->window->viewport_height ());
407 /* Return the title of the TUI window. */
409 gdbpy_tui_title (PyObject *self, void *closure)
411 gdbpy_tui_window *win = (gdbpy_tui_window *) self;
412 REQUIRE_WINDOW (win);
413 return host_string_to_python_string (win->window->title.c_str ()).release ();
416 /* Set the title of the TUI window. */
418 gdbpy_tui_set_title (PyObject *self, PyObject *newvalue, void *closure)
420 gdbpy_tui_window *win = (gdbpy_tui_window *) self;
422 if (win->window == nullptr)
424 PyErr_Format (PyExc_RuntimeError, _("TUI window is invalid."));
428 if (win->window == nullptr)
430 PyErr_Format (PyExc_TypeError, _("Cannot delete \"title\" attribute."));
434 gdb::unique_xmalloc_ptr<char> value
435 = python_string_to_host_string (newvalue);
436 if (value == nullptr)
439 win->window->title = value.get ();
443 static gdb_PyGetSetDef tui_object_getset[] =
445 { "width", gdbpy_tui_width, NULL, "Width of the window.", NULL },
446 { "height", gdbpy_tui_height, NULL, "Height of the window.", NULL },
447 { "title", gdbpy_tui_title, gdbpy_tui_set_title, "Title of the window.",
449 { NULL } /* Sentinel */
452 static PyMethodDef tui_object_methods[] =
454 { "is_valid", gdbpy_tui_is_valid, METH_NOARGS,
455 "is_valid () -> Boolean\n\
456 Return true if this TUI window is valid, false if not." },
457 { "erase", gdbpy_tui_erase, METH_NOARGS,
458 "Erase the TUI window." },
459 { "write", (PyCFunction) gdbpy_tui_write, METH_VARARGS,
460 "Append a string to the TUI window." },
461 { NULL } /* Sentinel. */
464 PyTypeObject gdbpy_tui_window_object_type =
466 PyVarObject_HEAD_INIT (NULL, 0)
467 "gdb.TuiWindow", /*tp_name*/
468 sizeof (gdbpy_tui_window), /*tp_basicsize*/
477 0, /*tp_as_sequence*/
485 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
486 "GDB TUI window object", /* tp_doc */
489 0, /* tp_richcompare */
490 0, /* tp_weaklistoffset */
493 tui_object_methods, /* tp_methods */
495 tui_object_getset, /* tp_getset */
498 0, /* tp_descr_get */
499 0, /* tp_descr_set */
500 0, /* tp_dictoffset */
507 /* Initialize this module. */
510 gdbpy_initialize_tui ()
513 gdbpy_tui_window_object_type.tp_new = PyType_GenericNew;
514 if (PyType_Ready (&gdbpy_tui_window_object_type) < 0)