]> Git Repo - binutils.git/blob - gdb/python/py-tui.c
gdb/python: use return values of add_setshow functions in add_setshow_generic
[binutils.git] / gdb / python / py-tui.c
1 /* TUI windows implemented in Python
2
3    Copyright (C) 2020-2021 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20
21 #include "defs.h"
22 #include "arch-utils.h"
23 #include "python-internal.h"
24
25 #ifdef TUI
26
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"
33
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"
39
40 class tui_py_window;
41
42 /* A PyObject representing a TUI window.  */
43
44 struct gdbpy_tui_window
45 {
46   PyObject_HEAD
47
48   /* The TUI window, or nullptr if the window has been deleted.  */
49   tui_py_window *window;
50
51   /* Return true if this object is valid.  */
52   bool is_valid () const;
53 };
54
55 extern PyTypeObject gdbpy_tui_window_object_type
56     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("gdbpy_tui_window");
57
58 /* A TUI window written in Python.  */
59
60 class tui_py_window : public tui_win_info
61 {
62 public:
63
64   tui_py_window (const char *name, gdbpy_ref<gdbpy_tui_window> wrapper)
65     : m_name (name),
66       m_wrapper (std::move (wrapper))
67   {
68     m_wrapper->window = this;
69   }
70
71   ~tui_py_window ();
72
73   DISABLE_COPY_AND_ASSIGN (tui_py_window);
74
75   /* Set the "user window" to the indicated reference.  The user
76      window is the object returned the by user-defined window
77      constructor.  */
78   void set_user_window (gdbpy_ref<> &&user_window)
79   {
80     m_window = std::move (user_window);
81   }
82
83   const char *name () const override
84   {
85     return m_name.c_str ();
86   }
87
88   void rerender () override;
89   void do_scroll_vertical (int num_to_scroll) override;
90   void do_scroll_horizontal (int num_to_scroll) override;
91
92   void refresh_window () override
93   {
94     if (m_inner_window != nullptr)
95       {
96         wnoutrefresh (handle.get ());
97         touchwin (m_inner_window.get ());
98         tui_wrefresh (m_inner_window.get ());
99       }
100     else
101       tui_win_info::refresh_window ();
102   }
103
104   /* Erase and re-box the window.  */
105   void erase ()
106   {
107     if (is_visible () && m_inner_window != nullptr)
108       {
109         werase (m_inner_window.get ());
110         check_and_display_highlight_if_needed ();
111       }
112   }
113
114   /* Write STR to the window.  */
115   void output (const char *str);
116
117   /* A helper function to compute the viewport width.  */
118   int viewport_width () const
119   {
120     return std::max (0, width - 2);
121   }
122
123   /* A helper function to compute the viewport height.  */
124   int viewport_height () const
125   {
126     return std::max (0, height - 2);
127   }
128
129 private:
130
131   /* The name of this window.  */
132   std::string m_name;
133
134   /* We make our own inner window, so that it is easy to print without
135      overwriting the border.  */
136   std::unique_ptr<WINDOW, curses_deleter> m_inner_window;
137
138   /* The underlying Python window object.  */
139   gdbpy_ref<> m_window;
140
141   /* The Python wrapper for this object.  */
142   gdbpy_ref<gdbpy_tui_window> m_wrapper;
143 };
144
145 /* See gdbpy_tui_window declaration above.  */
146
147 bool
148 gdbpy_tui_window::is_valid () const
149 {
150   return window != nullptr && tui_active;
151 }
152
153 tui_py_window::~tui_py_window ()
154 {
155   gdbpy_enter enter_py (get_current_arch (), current_language);
156
157   /* This can be null if the user-provided Python construction
158      function failed.  */
159   if (m_window != nullptr
160       && PyObject_HasAttrString (m_window.get (), "close"))
161     {
162       gdbpy_ref<> result (PyObject_CallMethod (m_window.get (), "close",
163                                                nullptr));
164       if (result == nullptr)
165         gdbpy_print_stack ();
166     }
167
168   /* Unlink.  */
169   m_wrapper->window = nullptr;
170   /* Explicitly free the Python references.  We have to do this
171      manually because we need to hold the GIL while doing so.  */
172   m_wrapper.reset (nullptr);
173   m_window.reset (nullptr);
174 }
175
176 void
177 tui_py_window::rerender ()
178 {
179   tui_win_info::rerender ();
180
181   gdbpy_enter enter_py (get_current_arch (), current_language);
182
183   int h = viewport_height ();
184   int w = viewport_width ();
185   if (h == 0 || w == 0)
186     {
187       /* The window would be too small, so just remove the
188          contents.  */
189       m_inner_window.reset (nullptr);
190       return;
191     }
192   m_inner_window.reset (newwin (h, w, y + 1, x + 1));
193
194   if (PyObject_HasAttrString (m_window.get (), "render"))
195     {
196       gdbpy_ref<> result (PyObject_CallMethod (m_window.get (), "render",
197                                                nullptr));
198       if (result == nullptr)
199         gdbpy_print_stack ();
200     }
201 }
202
203 void
204 tui_py_window::do_scroll_horizontal (int num_to_scroll)
205 {
206   gdbpy_enter enter_py (get_current_arch (), current_language);
207
208   if (PyObject_HasAttrString (m_window.get (), "hscroll"))
209     {
210       gdbpy_ref<> result (PyObject_CallMethod (m_window.get(), "hscroll",
211                                                "i", num_to_scroll, nullptr));
212       if (result == nullptr)
213         gdbpy_print_stack ();
214     }
215 }
216
217 void
218 tui_py_window::do_scroll_vertical (int num_to_scroll)
219 {
220   gdbpy_enter enter_py (get_current_arch (), current_language);
221
222   if (PyObject_HasAttrString (m_window.get (), "vscroll"))
223     {
224       gdbpy_ref<> result (PyObject_CallMethod (m_window.get (), "vscroll",
225                                                "i", num_to_scroll, nullptr));
226       if (result == nullptr)
227         gdbpy_print_stack ();
228     }
229 }
230
231 void
232 tui_py_window::output (const char *text)
233 {
234   if (m_inner_window != nullptr)
235     {
236       tui_puts (text, m_inner_window.get ());
237       tui_wrefresh (m_inner_window.get ());
238     }
239 }
240
241 \f
242
243 /* A callable that is used to create a TUI window.  It wraps the
244    user-supplied window constructor.  */
245
246 class gdbpy_tui_window_maker
247 {
248 public:
249
250   explicit gdbpy_tui_window_maker (gdbpy_ref<> &&constr)
251     : m_constr (std::move (constr))
252   {
253   }
254
255   ~gdbpy_tui_window_maker ();
256
257   gdbpy_tui_window_maker (gdbpy_tui_window_maker &&other) noexcept
258     : m_constr (std::move (other.m_constr))
259   {
260   }
261
262   gdbpy_tui_window_maker (const gdbpy_tui_window_maker &other)
263   {
264     gdbpy_enter enter_py (get_current_arch (), current_language);
265     m_constr = other.m_constr;
266   }
267
268   gdbpy_tui_window_maker &operator= (gdbpy_tui_window_maker &&other)
269   {
270     m_constr = std::move (other.m_constr);
271     return *this;
272   }
273
274   gdbpy_tui_window_maker &operator= (const gdbpy_tui_window_maker &other)
275   {
276     gdbpy_enter enter_py (get_current_arch (), current_language);
277     m_constr = other.m_constr;
278     return *this;
279   }
280
281   tui_win_info *operator() (const char *name);
282
283 private:
284
285   /* A constructor that is called to make a TUI window.  */
286   gdbpy_ref<> m_constr;
287 };
288
289 gdbpy_tui_window_maker::~gdbpy_tui_window_maker ()
290 {
291   gdbpy_enter enter_py (get_current_arch (), current_language);
292   m_constr.reset (nullptr);
293 }
294
295 tui_win_info *
296 gdbpy_tui_window_maker::operator() (const char *win_name)
297 {
298   gdbpy_enter enter_py (get_current_arch (), current_language);
299
300   gdbpy_ref<gdbpy_tui_window> wrapper
301     (PyObject_New (gdbpy_tui_window, &gdbpy_tui_window_object_type));
302   if (wrapper == nullptr)
303     {
304       gdbpy_print_stack ();
305       return nullptr;
306     }
307
308   std::unique_ptr<tui_py_window> window
309     (new tui_py_window (win_name, wrapper));
310
311   gdbpy_ref<> user_window
312     (PyObject_CallFunctionObjArgs (m_constr.get (),
313                                    (PyObject *) wrapper.get (),
314                                    nullptr));
315   if (user_window == nullptr)
316     {
317       gdbpy_print_stack ();
318       return nullptr;
319     }
320
321   window->set_user_window (std::move (user_window));
322   /* Window is now owned by the TUI.  */
323   return window.release ();
324 }
325
326 /* Implement "gdb.register_window_type".  */
327
328 PyObject *
329 gdbpy_register_tui_window (PyObject *self, PyObject *args, PyObject *kw)
330 {
331   static const char *keywords[] = { "name", "constructor", nullptr };
332
333   const char *name;
334   PyObject *cons_obj;
335
336   if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "sO", keywords,
337                                         &name, &cons_obj))
338     return nullptr;
339
340   try
341     {
342       gdbpy_tui_window_maker constr (gdbpy_ref<>::new_reference (cons_obj));
343       tui_register_window (name, constr);
344     }
345   catch (const gdb_exception &except)
346     {
347       gdbpy_convert_exception (except);
348       return nullptr;
349     }
350
351   Py_RETURN_NONE;
352 }
353
354 \f
355
356 /* Require that "Window" be a valid window.  */
357
358 #define REQUIRE_WINDOW(Window)                                  \
359     do {                                                        \
360       if (!(Window)->is_valid ())                               \
361         return PyErr_Format (PyExc_RuntimeError,                \
362                              _("TUI window is invalid."));      \
363     } while (0)
364
365 /* Require that "Window" be a valid window.  */
366
367 #define REQUIRE_WINDOW_FOR_SETTER(Window)                       \
368     do {                                                        \
369       if (!(Window)->is_valid ())                               \
370         {                                                       \
371           PyErr_Format (PyExc_RuntimeError,                     \
372                         _("TUI window is invalid."));           \
373           return -1;                                            \
374         }                                                       \
375     } while (0)
376
377 /* Python function which checks the validity of a TUI window
378    object.  */
379 static PyObject *
380 gdbpy_tui_is_valid (PyObject *self, PyObject *args)
381 {
382   gdbpy_tui_window *win = (gdbpy_tui_window *) self;
383
384   if (win->is_valid ())
385     Py_RETURN_TRUE;
386   Py_RETURN_FALSE;
387 }
388
389 /* Python function that erases the TUI window.  */
390 static PyObject *
391 gdbpy_tui_erase (PyObject *self, PyObject *args)
392 {
393   gdbpy_tui_window *win = (gdbpy_tui_window *) self;
394
395   REQUIRE_WINDOW (win);
396
397   win->window->erase ();
398
399   Py_RETURN_NONE;
400 }
401
402 /* Python function that writes some text to a TUI window.  */
403 static PyObject *
404 gdbpy_tui_write (PyObject *self, PyObject *args)
405 {
406   gdbpy_tui_window *win = (gdbpy_tui_window *) self;
407   const char *text;
408
409   if (!PyArg_ParseTuple (args, "s", &text))
410     return nullptr;
411
412   REQUIRE_WINDOW (win);
413
414   win->window->output (text);
415
416   Py_RETURN_NONE;
417 }
418
419 /* Return the width of the TUI window.  */
420 static PyObject *
421 gdbpy_tui_width (PyObject *self, void *closure)
422 {
423   gdbpy_tui_window *win = (gdbpy_tui_window *) self;
424   REQUIRE_WINDOW (win);
425   gdbpy_ref<> result
426     = gdb_py_object_from_longest (win->window->viewport_width ());
427   return result.release ();
428 }
429
430 /* Return the height of the TUI window.  */
431 static PyObject *
432 gdbpy_tui_height (PyObject *self, void *closure)
433 {
434   gdbpy_tui_window *win = (gdbpy_tui_window *) self;
435   REQUIRE_WINDOW (win);
436   gdbpy_ref<> result
437     = gdb_py_object_from_longest (win->window->viewport_height ());
438   return result.release ();
439 }
440
441 /* Return the title of the TUI window.  */
442 static PyObject *
443 gdbpy_tui_title (PyObject *self, void *closure)
444 {
445   gdbpy_tui_window *win = (gdbpy_tui_window *) self;
446   REQUIRE_WINDOW (win);
447   return host_string_to_python_string (win->window->title.c_str ()).release ();
448 }
449
450 /* Set the title of the TUI window.  */
451 static int
452 gdbpy_tui_set_title (PyObject *self, PyObject *newvalue, void *closure)
453 {
454   gdbpy_tui_window *win = (gdbpy_tui_window *) self;
455
456   REQUIRE_WINDOW_FOR_SETTER (win);
457
458   if (newvalue == nullptr)
459     {
460       PyErr_Format (PyExc_TypeError, _("Cannot delete \"title\" attribute."));
461       return -1;
462     }
463
464   gdb::unique_xmalloc_ptr<char> value
465     = python_string_to_host_string (newvalue);
466   if (value == nullptr)
467     return -1;
468
469   win->window->title = value.get ();
470   return 0;
471 }
472
473 static gdb_PyGetSetDef tui_object_getset[] =
474 {
475   { "width", gdbpy_tui_width, NULL, "Width of the window.", NULL },
476   { "height", gdbpy_tui_height, NULL, "Height of the window.", NULL },
477   { "title", gdbpy_tui_title, gdbpy_tui_set_title, "Title of the window.",
478     NULL },
479   { NULL }  /* Sentinel */
480 };
481
482 static PyMethodDef tui_object_methods[] =
483 {
484   { "is_valid", gdbpy_tui_is_valid, METH_NOARGS,
485     "is_valid () -> Boolean\n\
486 Return true if this TUI window is valid, false if not." },
487   { "erase", gdbpy_tui_erase, METH_NOARGS,
488     "Erase the TUI window." },
489   { "write", (PyCFunction) gdbpy_tui_write, METH_VARARGS,
490     "Append a string to the TUI window." },
491   { NULL } /* Sentinel.  */
492 };
493
494 PyTypeObject gdbpy_tui_window_object_type =
495 {
496   PyVarObject_HEAD_INIT (NULL, 0)
497   "gdb.TuiWindow",                /*tp_name*/
498   sizeof (gdbpy_tui_window),      /*tp_basicsize*/
499   0,                              /*tp_itemsize*/
500   0,                              /*tp_dealloc*/
501   0,                              /*tp_print*/
502   0,                              /*tp_getattr*/
503   0,                              /*tp_setattr*/
504   0,                              /*tp_compare*/
505   0,                              /*tp_repr*/
506   0,                              /*tp_as_number*/
507   0,                              /*tp_as_sequence*/
508   0,                              /*tp_as_mapping*/
509   0,                              /*tp_hash */
510   0,                              /*tp_call*/
511   0,                              /*tp_str*/
512   0,                              /*tp_getattro*/
513   0,                              /*tp_setattro */
514   0,                              /*tp_as_buffer*/
515   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
516   "GDB TUI window object",        /* tp_doc */
517   0,                              /* tp_traverse */
518   0,                              /* tp_clear */
519   0,                              /* tp_richcompare */
520   0,                              /* tp_weaklistoffset */
521   0,                              /* tp_iter */
522   0,                              /* tp_iternext */
523   tui_object_methods,             /* tp_methods */
524   0,                              /* tp_members */
525   tui_object_getset,              /* tp_getset */
526   0,                              /* tp_base */
527   0,                              /* tp_dict */
528   0,                              /* tp_descr_get */
529   0,                              /* tp_descr_set */
530   0,                              /* tp_dictoffset */
531   0,                              /* tp_init */
532   0,                              /* tp_alloc */
533 };
534
535 #endif /* TUI */
536
537 /* Initialize this module.  */
538
539 int
540 gdbpy_initialize_tui ()
541 {
542 #ifdef TUI
543   gdbpy_tui_window_object_type.tp_new = PyType_GenericNew;
544   if (PyType_Ready (&gdbpy_tui_window_object_type) < 0)
545     return -1;
546 #endif  /* TUI */
547
548   return 0;
549 }
This page took 0.057048 seconds and 4 git commands to generate.