1 /* TUI windows implemented in Python
3 Copyright (C) 2020-2021 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 void refresh_window () override
91 tui_win_info::refresh_window ();
92 if (m_inner_window
!= nullptr)
94 touchwin (m_inner_window
.get ());
95 tui_wrefresh (m_inner_window
.get ());
99 /* Erase and re-box the window. */
102 if (is_visible () && m_inner_window
!= nullptr)
104 werase (m_inner_window
.get ());
105 check_and_display_highlight_if_needed ();
109 /* Write STR to the window. */
110 void output (const char *str
);
112 /* A helper function to compute the viewport width. */
113 int viewport_width () const
115 return std::max (0, width
- 2);
118 /* A helper function to compute the viewport height. */
119 int viewport_height () const
121 return std::max (0, height
- 2);
126 /* The name of this window. */
129 /* We make our own inner window, so that it is easy to print without
130 overwriting the border. */
131 std::unique_ptr
<WINDOW
, curses_deleter
> m_inner_window
;
133 /* The underlying Python window object. */
134 gdbpy_ref
<> m_window
;
136 /* The Python wrapper for this object. */
137 gdbpy_ref
<gdbpy_tui_window
> m_wrapper
;
140 tui_py_window::~tui_py_window ()
142 gdbpy_enter
enter_py (get_current_arch (), current_language
);
144 /* This can be null if the user-provided Python construction
146 if (m_window
!= nullptr
147 && PyObject_HasAttrString (m_window
.get (), "close"))
149 gdbpy_ref
<> result (PyObject_CallMethod (m_window
.get (), "close",
151 if (result
== nullptr)
152 gdbpy_print_stack ();
156 m_wrapper
->window
= nullptr;
157 /* Explicitly free the Python references. We have to do this
158 manually because we need to hold the GIL while doing so. */
159 m_wrapper
.reset (nullptr);
160 m_window
.reset (nullptr);
164 tui_py_window::rerender ()
166 tui_win_info::rerender ();
168 gdbpy_enter
enter_py (get_current_arch (), current_language
);
170 int h
= viewport_height ();
171 int w
= viewport_width ();
172 if (h
== 0 || w
== 0)
174 /* The window would be too small, so just remove the
176 m_inner_window
.reset (nullptr);
179 m_inner_window
.reset (newwin (h
, w
, y
+ 1, x
+ 1));
181 if (PyObject_HasAttrString (m_window
.get (), "render"))
183 gdbpy_ref
<> result (PyObject_CallMethod (m_window
.get (), "render",
185 if (result
== nullptr)
186 gdbpy_print_stack ();
191 tui_py_window::do_scroll_horizontal (int num_to_scroll
)
193 gdbpy_enter
enter_py (get_current_arch (), current_language
);
195 if (PyObject_HasAttrString (m_window
.get (), "hscroll"))
197 gdbpy_ref
<> result (PyObject_CallMethod (m_window
.get(), "hscroll",
198 "i", num_to_scroll
, nullptr));
199 if (result
== nullptr)
200 gdbpy_print_stack ();
205 tui_py_window::do_scroll_vertical (int num_to_scroll
)
207 gdbpy_enter
enter_py (get_current_arch (), current_language
);
209 if (PyObject_HasAttrString (m_window
.get (), "vscroll"))
211 gdbpy_ref
<> result (PyObject_CallMethod (m_window
.get (), "vscroll",
212 "i", num_to_scroll
, nullptr));
213 if (result
== nullptr)
214 gdbpy_print_stack ();
219 tui_py_window::output (const char *text
)
221 if (m_inner_window
!= nullptr)
223 tui_puts (text
, m_inner_window
.get ());
224 tui_wrefresh (m_inner_window
.get ());
230 /* A callable that is used to create a TUI window. It wraps the
231 user-supplied window constructor. */
233 class gdbpy_tui_window_maker
237 explicit gdbpy_tui_window_maker (gdbpy_ref
<> &&constr
)
238 : m_constr (std::move (constr
))
242 ~gdbpy_tui_window_maker ();
244 gdbpy_tui_window_maker (gdbpy_tui_window_maker
&&other
) noexcept
245 : m_constr (std::move (other
.m_constr
))
249 gdbpy_tui_window_maker (const gdbpy_tui_window_maker
&other
)
251 gdbpy_enter
enter_py (get_current_arch (), current_language
);
252 m_constr
= other
.m_constr
;
255 gdbpy_tui_window_maker
&operator= (gdbpy_tui_window_maker
&&other
)
257 m_constr
= std::move (other
.m_constr
);
261 gdbpy_tui_window_maker
&operator= (const gdbpy_tui_window_maker
&other
)
263 gdbpy_enter
enter_py (get_current_arch (), current_language
);
264 m_constr
= other
.m_constr
;
268 tui_win_info
*operator() (const char *name
);
272 /* A constructor that is called to make a TUI window. */
273 gdbpy_ref
<> m_constr
;
276 gdbpy_tui_window_maker::~gdbpy_tui_window_maker ()
278 gdbpy_enter
enter_py (get_current_arch (), current_language
);
279 m_constr
.reset (nullptr);
283 gdbpy_tui_window_maker::operator() (const char *win_name
)
285 gdbpy_enter
enter_py (get_current_arch (), current_language
);
287 gdbpy_ref
<gdbpy_tui_window
> wrapper
288 (PyObject_New (gdbpy_tui_window
, &gdbpy_tui_window_object_type
));
289 if (wrapper
== nullptr)
291 gdbpy_print_stack ();
295 std::unique_ptr
<tui_py_window
> window
296 (new tui_py_window (win_name
, wrapper
));
298 gdbpy_ref
<> user_window
299 (PyObject_CallFunctionObjArgs (m_constr
.get (),
300 (PyObject
*) wrapper
.get (),
302 if (user_window
== nullptr)
304 gdbpy_print_stack ();
308 window
->set_user_window (std::move (user_window
));
309 /* Window is now owned by the TUI. */
310 return window
.release ();
313 /* Implement "gdb.register_window_type". */
316 gdbpy_register_tui_window (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
318 static const char *keywords
[] = { "name", "constructor", nullptr };
323 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "sO", keywords
,
329 gdbpy_tui_window_maker
constr (gdbpy_ref
<>::new_reference (cons_obj
));
330 tui_register_window (name
, constr
);
332 catch (const gdb_exception
&except
)
334 gdbpy_convert_exception (except
);
343 /* Require that "Window" be a valid window. */
345 #define REQUIRE_WINDOW(Window) \
347 if ((Window)->window == nullptr) \
348 return PyErr_Format (PyExc_RuntimeError, \
349 _("TUI window is invalid.")); \
352 /* Python function which checks the validity of a TUI window
355 gdbpy_tui_is_valid (PyObject
*self
, PyObject
*args
)
357 gdbpy_tui_window
*win
= (gdbpy_tui_window
*) self
;
359 if (win
->window
!= nullptr)
364 /* Python function that erases the TUI window. */
366 gdbpy_tui_erase (PyObject
*self
, PyObject
*args
)
368 gdbpy_tui_window
*win
= (gdbpy_tui_window
*) self
;
370 REQUIRE_WINDOW (win
);
372 win
->window
->erase ();
377 /* Python function that writes some text to a TUI window. */
379 gdbpy_tui_write (PyObject
*self
, PyObject
*args
)
381 gdbpy_tui_window
*win
= (gdbpy_tui_window
*) self
;
384 if (!PyArg_ParseTuple (args
, "s", &text
))
387 REQUIRE_WINDOW (win
);
389 win
->window
->output (text
);
394 /* Return the width of the TUI window. */
396 gdbpy_tui_width (PyObject
*self
, void *closure
)
398 gdbpy_tui_window
*win
= (gdbpy_tui_window
*) self
;
399 REQUIRE_WINDOW (win
);
401 = gdb_py_object_from_longest (win
->window
->viewport_width ());
402 return result
.release ();
405 /* Return the height of the TUI window. */
407 gdbpy_tui_height (PyObject
*self
, void *closure
)
409 gdbpy_tui_window
*win
= (gdbpy_tui_window
*) self
;
410 REQUIRE_WINDOW (win
);
412 = gdb_py_object_from_longest (win
->window
->viewport_height ());
413 return result
.release ();
416 /* Return the title of the TUI window. */
418 gdbpy_tui_title (PyObject
*self
, void *closure
)
420 gdbpy_tui_window
*win
= (gdbpy_tui_window
*) self
;
421 REQUIRE_WINDOW (win
);
422 return host_string_to_python_string (win
->window
->title
.c_str ()).release ();
425 /* Set the title of the TUI window. */
427 gdbpy_tui_set_title (PyObject
*self
, PyObject
*newvalue
, void *closure
)
429 gdbpy_tui_window
*win
= (gdbpy_tui_window
*) self
;
431 if (win
->window
== nullptr)
433 PyErr_Format (PyExc_RuntimeError
, _("TUI window is invalid."));
437 if (win
->window
== nullptr)
439 PyErr_Format (PyExc_TypeError
, _("Cannot delete \"title\" attribute."));
443 gdb::unique_xmalloc_ptr
<char> value
444 = python_string_to_host_string (newvalue
);
445 if (value
== nullptr)
448 win
->window
->title
= value
.get ();
452 static gdb_PyGetSetDef tui_object_getset
[] =
454 { "width", gdbpy_tui_width
, NULL
, "Width of the window.", NULL
},
455 { "height", gdbpy_tui_height
, NULL
, "Height of the window.", NULL
},
456 { "title", gdbpy_tui_title
, gdbpy_tui_set_title
, "Title of the window.",
458 { NULL
} /* Sentinel */
461 static PyMethodDef tui_object_methods
[] =
463 { "is_valid", gdbpy_tui_is_valid
, METH_NOARGS
,
464 "is_valid () -> Boolean\n\
465 Return true if this TUI window is valid, false if not." },
466 { "erase", gdbpy_tui_erase
, METH_NOARGS
,
467 "Erase the TUI window." },
468 { "write", (PyCFunction
) gdbpy_tui_write
, METH_VARARGS
,
469 "Append a string to the TUI window." },
470 { NULL
} /* Sentinel. */
473 PyTypeObject gdbpy_tui_window_object_type
=
475 PyVarObject_HEAD_INIT (NULL
, 0)
476 "gdb.TuiWindow", /*tp_name*/
477 sizeof (gdbpy_tui_window
), /*tp_basicsize*/
486 0, /*tp_as_sequence*/
494 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /*tp_flags*/
495 "GDB TUI window object", /* tp_doc */
498 0, /* tp_richcompare */
499 0, /* tp_weaklistoffset */
502 tui_object_methods
, /* tp_methods */
504 tui_object_getset
, /* tp_getset */
507 0, /* tp_descr_get */
508 0, /* tp_descr_set */
509 0, /* tp_dictoffset */
516 /* Initialize this module. */
519 gdbpy_initialize_tui ()
522 gdbpy_tui_window_object_type
.tp_new
= PyType_GenericNew
;
523 if (PyType_Ready (&gdbpy_tui_window_object_type
) < 0)