Make TUI window handle a unique_ptr
[deliverable/binutils-gdb.git] / gdb / tui / tui-win.c
1 /* TUI window generic functions.
2
3 Copyright (C) 1998-2019 Free Software Foundation, Inc.
4
5 Contributed by Hewlett-Packard Company.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* This module contains procedures for handling tui window functions
23 like resize, scrolling, scrolling, changing focus, etc.
24
25 Author: Susan B. Macchia */
26
27 #include "defs.h"
28 #include "command.h"
29 #include "symtab.h"
30 #include "breakpoint.h"
31 #include "frame.h"
32 #include "cli/cli-cmds.h"
33 #include "top.h"
34 #include "source.h"
35 #include "event-loop.h"
36
37 #include "tui/tui.h"
38 #include "tui/tui-io.h"
39 #include "tui/tui-command.h"
40 #include "tui/tui-data.h"
41 #include "tui/tui-layout.h"
42 #include "tui/tui-wingeneral.h"
43 #include "tui/tui-stack.h"
44 #include "tui/tui-regs.h"
45 #include "tui/tui-disasm.h"
46 #include "tui/tui-source.h"
47 #include "tui/tui-winsource.h"
48 #include "tui/tui-win.h"
49
50 #include "gdb_curses.h"
51 #include <ctype.h>
52 #include "readline/readline.h"
53
54 #include <signal.h>
55
56 static enum tui_status tui_adjust_win_heights (struct tui_win_info *,
57 int);
58 static int new_height_ok (struct tui_win_info *, int);
59 static void tui_set_tab_width_command (const char *, int);
60 static void tui_refresh_all_command (const char *, int);
61 static void tui_all_windows_info (const char *, int);
62 static void tui_scroll_forward_command (const char *, int);
63 static void tui_scroll_backward_command (const char *, int);
64 static void tui_scroll_left_command (const char *, int);
65 static void tui_scroll_right_command (const char *, int);
66 static void parse_scrolling_args (const char *,
67 struct tui_win_info **,
68 int *);
69
70
71 #define WIN_HEIGHT_USAGE "Usage: winheight WINDOW-NAME [+ | -] NUM-LINES\n"
72 #define FOCUS_USAGE "Usage: focus [WINDOW-NAME | next | prev]\n"
73
74 #ifndef ACS_LRCORNER
75 # define ACS_LRCORNER '+'
76 #endif
77 #ifndef ACS_LLCORNER
78 # define ACS_LLCORNER '+'
79 #endif
80 #ifndef ACS_ULCORNER
81 # define ACS_ULCORNER '+'
82 #endif
83 #ifndef ACS_URCORNER
84 # define ACS_URCORNER '+'
85 #endif
86 #ifndef ACS_HLINE
87 # define ACS_HLINE '-'
88 #endif
89 #ifndef ACS_VLINE
90 # define ACS_VLINE '|'
91 #endif
92
93 /* Possible values for tui-border-kind variable. */
94 static const char *const tui_border_kind_enums[] = {
95 "space",
96 "ascii",
97 "acs",
98 NULL
99 };
100
101 /* Possible values for tui-border-mode and tui-active-border-mode. */
102 static const char *const tui_border_mode_enums[] = {
103 "normal",
104 "standout",
105 "reverse",
106 "half",
107 "half-standout",
108 "bold",
109 "bold-standout",
110 NULL
111 };
112
113 struct tui_translate
114 {
115 const char *name;
116 int value;
117 };
118
119 /* Translation table for border-mode variables.
120 The list of values must be terminated by a NULL.
121 After the NULL value, an entry defines the default. */
122 struct tui_translate tui_border_mode_translate[] = {
123 { "normal", A_NORMAL },
124 { "standout", A_STANDOUT },
125 { "reverse", A_REVERSE },
126 { "half", A_DIM },
127 { "half-standout", A_DIM | A_STANDOUT },
128 { "bold", A_BOLD },
129 { "bold-standout", A_BOLD | A_STANDOUT },
130 { 0, 0 },
131 { "normal", A_NORMAL }
132 };
133
134 /* Translation tables for border-kind, one for each border
135 character (see wborder, border curses operations).
136 -1 is used to indicate the ACS because ACS characters
137 are determined at run time by curses (depends on terminal). */
138 struct tui_translate tui_border_kind_translate_vline[] = {
139 { "space", ' ' },
140 { "ascii", '|' },
141 { "acs", -1 },
142 { 0, 0 },
143 { "ascii", '|' }
144 };
145
146 struct tui_translate tui_border_kind_translate_hline[] = {
147 { "space", ' ' },
148 { "ascii", '-' },
149 { "acs", -1 },
150 { 0, 0 },
151 { "ascii", '-' }
152 };
153
154 struct tui_translate tui_border_kind_translate_ulcorner[] = {
155 { "space", ' ' },
156 { "ascii", '+' },
157 { "acs", -1 },
158 { 0, 0 },
159 { "ascii", '+' }
160 };
161
162 struct tui_translate tui_border_kind_translate_urcorner[] = {
163 { "space", ' ' },
164 { "ascii", '+' },
165 { "acs", -1 },
166 { 0, 0 },
167 { "ascii", '+' }
168 };
169
170 struct tui_translate tui_border_kind_translate_llcorner[] = {
171 { "space", ' ' },
172 { "ascii", '+' },
173 { "acs", -1 },
174 { 0, 0 },
175 { "ascii", '+' }
176 };
177
178 struct tui_translate tui_border_kind_translate_lrcorner[] = {
179 { "space", ' ' },
180 { "ascii", '+' },
181 { "acs", -1 },
182 { 0, 0 },
183 { "ascii", '+' }
184 };
185
186
187 /* Tui configuration variables controlled with set/show command. */
188 const char *tui_active_border_mode = "bold-standout";
189 static void
190 show_tui_active_border_mode (struct ui_file *file,
191 int from_tty,
192 struct cmd_list_element *c,
193 const char *value)
194 {
195 fprintf_filtered (file, _("\
196 The attribute mode to use for the active TUI window border is \"%s\".\n"),
197 value);
198 }
199
200 const char *tui_border_mode = "normal";
201 static void
202 show_tui_border_mode (struct ui_file *file,
203 int from_tty,
204 struct cmd_list_element *c,
205 const char *value)
206 {
207 fprintf_filtered (file, _("\
208 The attribute mode to use for the TUI window borders is \"%s\".\n"),
209 value);
210 }
211
212 const char *tui_border_kind = "acs";
213 static void
214 show_tui_border_kind (struct ui_file *file,
215 int from_tty,
216 struct cmd_list_element *c,
217 const char *value)
218 {
219 fprintf_filtered (file, _("The kind of border for TUI windows is \"%s\".\n"),
220 value);
221 }
222
223
224 /* Tui internal configuration variables. These variables are updated
225 by tui_update_variables to reflect the tui configuration
226 variables. */
227 chtype tui_border_vline;
228 chtype tui_border_hline;
229 chtype tui_border_ulcorner;
230 chtype tui_border_urcorner;
231 chtype tui_border_llcorner;
232 chtype tui_border_lrcorner;
233
234 int tui_border_attrs;
235 int tui_active_border_attrs;
236
237 /* Identify the item in the translation table.
238 When the item is not recognized, use the default entry. */
239 static struct tui_translate *
240 translate (const char *name, struct tui_translate *table)
241 {
242 while (table->name)
243 {
244 if (name && strcmp (table->name, name) == 0)
245 return table;
246 table++;
247 }
248
249 /* Not found, return default entry. */
250 table++;
251 return table;
252 }
253
254 /* Update the tui internal configuration according to gdb settings.
255 Returns 1 if the configuration has changed and the screen should
256 be redrawn. */
257 int
258 tui_update_variables (void)
259 {
260 int need_redraw = 0;
261 struct tui_translate *entry;
262
263 entry = translate (tui_border_mode, tui_border_mode_translate);
264 if (tui_border_attrs != entry->value)
265 {
266 tui_border_attrs = entry->value;
267 need_redraw = 1;
268 }
269 entry = translate (tui_active_border_mode, tui_border_mode_translate);
270 if (tui_active_border_attrs != entry->value)
271 {
272 tui_active_border_attrs = entry->value;
273 need_redraw = 1;
274 }
275
276 /* If one corner changes, all characters are changed.
277 Only check the first one. The ACS characters are determined at
278 run time by curses terminal management. */
279 entry = translate (tui_border_kind, tui_border_kind_translate_lrcorner);
280 if (tui_border_lrcorner != (chtype) entry->value)
281 {
282 tui_border_lrcorner = (entry->value < 0) ? ACS_LRCORNER : entry->value;
283 need_redraw = 1;
284 }
285 entry = translate (tui_border_kind, tui_border_kind_translate_llcorner);
286 tui_border_llcorner = (entry->value < 0) ? ACS_LLCORNER : entry->value;
287
288 entry = translate (tui_border_kind, tui_border_kind_translate_ulcorner);
289 tui_border_ulcorner = (entry->value < 0) ? ACS_ULCORNER : entry->value;
290
291 entry = translate (tui_border_kind, tui_border_kind_translate_urcorner);
292 tui_border_urcorner = (entry->value < 0) ? ACS_URCORNER : entry->value;
293
294 entry = translate (tui_border_kind, tui_border_kind_translate_hline);
295 tui_border_hline = (entry->value < 0) ? ACS_HLINE : entry->value;
296
297 entry = translate (tui_border_kind, tui_border_kind_translate_vline);
298 tui_border_vline = (entry->value < 0) ? ACS_VLINE : entry->value;
299
300 return need_redraw;
301 }
302
303 static void
304 set_tui_cmd (const char *args, int from_tty)
305 {
306 }
307
308 static void
309 show_tui_cmd (const char *args, int from_tty)
310 {
311 }
312
313 static struct cmd_list_element *tuilist;
314
315 static void
316 tui_command (const char *args, int from_tty)
317 {
318 printf_unfiltered (_("\"tui\" must be followed by the name of a "
319 "tui command.\n"));
320 help_list (tuilist, "tui ", all_commands, gdb_stdout);
321 }
322
323 struct cmd_list_element **
324 tui_get_cmd_list (void)
325 {
326 if (tuilist == 0)
327 add_prefix_cmd ("tui", class_tui, tui_command,
328 _("Text User Interface commands."),
329 &tuilist, "tui ", 0, &cmdlist);
330 return &tuilist;
331 }
332
333 /* The set_func hook of "set tui ..." commands that affect the window
334 borders on the TUI display. */
335 void
336 tui_set_var_cmd (const char *null_args,
337 int from_tty, struct cmd_list_element *c)
338 {
339 if (tui_update_variables () && tui_active)
340 tui_rehighlight_all ();
341 }
342
343 /* Generic window name completion function. Complete window name pointed
344 to by TEXT and WORD. If INCLUDE_NEXT_PREV_P is true then the special
345 window names 'next' and 'prev' will also be considered as possible
346 completions of the window name. */
347
348 static void
349 window_name_completer (completion_tracker &tracker,
350 int include_next_prev_p,
351 const char *text, const char *word)
352 {
353 std::vector<const char *> completion_name_vec;
354
355 for (tui_win_info *win_info : all_tui_windows ())
356 {
357 const char *completion_name = NULL;
358
359 /* We can't focus on an invisible window. */
360 if (!win_info->is_visible ())
361 continue;
362
363 completion_name = win_info->name ();
364 gdb_assert (completion_name != NULL);
365 completion_name_vec.push_back (completion_name);
366 }
367
368 /* If no windows are considered visible then the TUI has not yet been
369 initialized. But still "focus src" and "focus cmd" will work because
370 invoking the focus command will entail initializing the TUI which sets the
371 default layout to SRC_COMMAND. */
372 if (completion_name_vec.empty ())
373 {
374 completion_name_vec.push_back (SRC_NAME);
375 completion_name_vec.push_back (CMD_NAME);
376 }
377
378 if (include_next_prev_p)
379 {
380 completion_name_vec.push_back ("next");
381 completion_name_vec.push_back ("prev");
382 }
383
384
385 completion_name_vec.push_back (NULL);
386 complete_on_enum (tracker, completion_name_vec.data (), text, word);
387 }
388
389 /* Complete possible window names to focus on. TEXT is the complete text
390 entered so far, WORD is the word currently being completed. */
391
392 static void
393 focus_completer (struct cmd_list_element *ignore,
394 completion_tracker &tracker,
395 const char *text, const char *word)
396 {
397 window_name_completer (tracker, 1, text, word);
398 }
399
400 /* Complete possible window names for winheight command. TEXT is the
401 complete text entered so far, WORD is the word currently being
402 completed. */
403
404 static void
405 winheight_completer (struct cmd_list_element *ignore,
406 completion_tracker &tracker,
407 const char *text, const char *word)
408 {
409 /* The first word is the window name. That we can complete. Subsequent
410 words can't be completed. */
411 if (word != text)
412 return;
413
414 window_name_completer (tracker, 0, text, word);
415 }
416
417 /* Update gdb's knowledge of the terminal size. */
418 void
419 tui_update_gdb_sizes (void)
420 {
421 int width, height;
422
423 if (tui_active)
424 {
425 width = TUI_CMD_WIN->width;
426 height = TUI_CMD_WIN->height;
427 }
428 else
429 {
430 width = tui_term_width ();
431 height = tui_term_height ();
432 }
433
434 set_screen_width_and_height (width, height);
435 }
436
437
438 /* Set the logical focus to win_info. */
439 void
440 tui_set_win_focus_to (struct tui_win_info *win_info)
441 {
442 if (win_info != NULL)
443 {
444 struct tui_win_info *win_with_focus = tui_win_with_focus ();
445
446 tui_unhighlight_win (win_with_focus);
447 tui_set_win_with_focus (win_info);
448 tui_highlight_win (win_info);
449 }
450 }
451
452
453 void
454 tui_win_info::forward_scroll (int num_to_scroll)
455 {
456 if (num_to_scroll == 0)
457 num_to_scroll = height - 3;
458
459 do_scroll_vertical (num_to_scroll);
460 }
461
462 void
463 tui_win_info::backward_scroll (int num_to_scroll)
464 {
465 if (num_to_scroll == 0)
466 num_to_scroll = height - 3;
467
468 do_scroll_vertical (-num_to_scroll);
469 }
470
471
472 void
473 tui_win_info::left_scroll (int num_to_scroll)
474 {
475 if (num_to_scroll == 0)
476 num_to_scroll = 1;
477
478 do_scroll_horizontal (num_to_scroll);
479 }
480
481
482 void
483 tui_win_info::right_scroll (int num_to_scroll)
484 {
485 if (num_to_scroll == 0)
486 num_to_scroll = 1;
487
488 do_scroll_horizontal (-num_to_scroll);
489 }
490
491
492 void
493 tui_refresh_all_win (void)
494 {
495 clearok (curscr, TRUE);
496 tui_refresh_all ();
497 }
498
499 void
500 tui_rehighlight_all (void)
501 {
502 for (tui_win_info *win_info : all_tui_windows ())
503 win_info->check_and_display_highlight_if_needed ();
504 }
505
506 /* Resize all the windows based on the terminal size. This function
507 gets called from within the readline SIGWINCH handler. */
508 void
509 tui_resize_all (void)
510 {
511 int height_diff, width_diff;
512 int screenheight, screenwidth;
513
514 rl_get_screen_size (&screenheight, &screenwidth);
515 width_diff = screenwidth - tui_term_width ();
516 height_diff = screenheight - tui_term_height ();
517 if (height_diff || width_diff)
518 {
519 enum tui_layout_type cur_layout = tui_current_layout ();
520 struct tui_win_info *win_with_focus = tui_win_with_focus ();
521 struct tui_win_info *first_win;
522 struct tui_win_info *second_win;
523 tui_source_window_base *src_win;
524 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
525 int new_height, split_diff, cmd_split_diff, num_wins_displayed = 2;
526
527 #ifdef HAVE_RESIZE_TERM
528 resize_term (screenheight, screenwidth);
529 #endif
530 /* Turn keypad off while we resize. */
531 if (win_with_focus != TUI_CMD_WIN)
532 keypad (TUI_CMD_WIN->handle.get (), FALSE);
533 tui_update_gdb_sizes ();
534 tui_set_term_height_to (screenheight);
535 tui_set_term_width_to (screenwidth);
536 if (cur_layout == SRC_DISASSEM_COMMAND
537 || cur_layout == SRC_DATA_COMMAND
538 || cur_layout == DISASSEM_DATA_COMMAND)
539 num_wins_displayed++;
540 split_diff = height_diff / num_wins_displayed;
541 cmd_split_diff = split_diff;
542 if (height_diff % num_wins_displayed)
543 {
544 if (height_diff < 0)
545 cmd_split_diff--;
546 else
547 cmd_split_diff++;
548 }
549 /* Now adjust each window. */
550 /* erase + clearok are used instead of a straightforward clear as
551 AIX 5.3 does not define clear. */
552 erase ();
553 clearok (curscr, TRUE);
554 switch (cur_layout)
555 {
556 case SRC_COMMAND:
557 case DISASSEM_COMMAND:
558 src_win = *(tui_source_windows ().begin ());
559 /* Check for invalid heights. */
560 if (height_diff == 0)
561 new_height = src_win->height;
562 else if ((src_win->height + split_diff) >=
563 (screenheight - MIN_CMD_WIN_HEIGHT - 1))
564 new_height = screenheight - MIN_CMD_WIN_HEIGHT - 1;
565 else if ((src_win->height + split_diff) <= 0)
566 new_height = MIN_WIN_HEIGHT;
567 else
568 new_height = src_win->height + split_diff;
569
570 src_win->resize (new_height, screenwidth, 0, 0);
571
572 locator->resize (1, screenwidth, 0, new_height);
573
574 new_height = screenheight - (new_height + 1);
575 TUI_CMD_WIN->resize (new_height, screenwidth,
576 0, locator->origin.y + 1);
577 break;
578 default:
579 if (cur_layout == SRC_DISASSEM_COMMAND)
580 {
581 src_win = TUI_SRC_WIN;
582 first_win = src_win;
583 second_win = TUI_DISASM_WIN;
584 }
585 else
586 {
587 first_win = TUI_DATA_WIN;
588 src_win = *(tui_source_windows ().begin ());
589 second_win = src_win;
590 }
591 /* Change the first window's height/width. */
592 /* Check for invalid heights. */
593 if (height_diff == 0)
594 new_height = first_win->height;
595 else if ((first_win->height +
596 second_win->height + (split_diff * 2)) >=
597 (screenheight - MIN_CMD_WIN_HEIGHT - 1))
598 new_height = (screenheight - MIN_CMD_WIN_HEIGHT - 1) / 2;
599 else if ((first_win->height + split_diff) <= 0)
600 new_height = MIN_WIN_HEIGHT;
601 else
602 new_height = first_win->height + split_diff;
603
604 first_win->resize (new_height, screenwidth, 0, 0);
605
606 /* Change the second window's height/width. */
607 /* Check for invalid heights. */
608 if (height_diff == 0)
609 new_height = second_win->height;
610 else if ((first_win->height +
611 second_win->height + (split_diff * 2)) >=
612 (screenheight - MIN_CMD_WIN_HEIGHT - 1))
613 {
614 new_height = screenheight - MIN_CMD_WIN_HEIGHT - 1;
615 if (new_height % 2)
616 new_height = (new_height / 2) + 1;
617 else
618 new_height /= 2;
619 }
620 else if ((second_win->height + split_diff) <= 0)
621 new_height = MIN_WIN_HEIGHT;
622 else
623 new_height = second_win->height + split_diff;
624
625 second_win->resize (new_height, screenwidth,
626 0, first_win->height - 1);
627
628 locator->resize (1, screenwidth,
629 0, second_win->origin.y + new_height);
630
631 /* Change the command window's height/width. */
632 new_height = screenheight - (locator->origin.y + 1);
633 TUI_CMD_WIN->resize (new_height, screenwidth,
634 0, locator->origin.y + 1);
635 break;
636 }
637
638 tui_delete_invisible_windows ();
639 /* Turn keypad back on, unless focus is in the command
640 window. */
641 if (win_with_focus != TUI_CMD_WIN)
642 keypad (TUI_CMD_WIN->handle.get (), TRUE);
643 }
644 }
645
646 #ifdef SIGWINCH
647 /* Token for use by TUI's asynchronous SIGWINCH handler. */
648 static struct async_signal_handler *tui_sigwinch_token;
649
650 /* TUI's SIGWINCH signal handler. */
651 static void
652 tui_sigwinch_handler (int signal)
653 {
654 mark_async_signal_handler (tui_sigwinch_token);
655 tui_set_win_resized_to (true);
656 }
657
658 /* Callback for asynchronously resizing TUI following a SIGWINCH signal. */
659 static void
660 tui_async_resize_screen (gdb_client_data arg)
661 {
662 rl_resize_terminal ();
663
664 if (!tui_active)
665 {
666 int screen_height, screen_width;
667
668 rl_get_screen_size (&screen_height, &screen_width);
669 set_screen_width_and_height (screen_width, screen_height);
670
671 /* win_resized is left set so that the next call to tui_enable()
672 resizes the TUI windows. */
673 }
674 else
675 {
676 tui_set_win_resized_to (false);
677 tui_resize_all ();
678 tui_refresh_all_win ();
679 tui_update_gdb_sizes ();
680 tui_redisplay_readline ();
681 }
682 }
683 #endif
684
685 /* Initialize TUI's SIGWINCH signal handler. Note that the handler is not
686 uninstalled when we exit TUI, so the handler should not assume that TUI is
687 always active. */
688 void
689 tui_initialize_win (void)
690 {
691 #ifdef SIGWINCH
692 tui_sigwinch_token
693 = create_async_signal_handler (tui_async_resize_screen, NULL);
694
695 {
696 #ifdef HAVE_SIGACTION
697 struct sigaction old_winch;
698
699 memset (&old_winch, 0, sizeof (old_winch));
700 old_winch.sa_handler = &tui_sigwinch_handler;
701 #ifdef SA_RESTART
702 old_winch.sa_flags = SA_RESTART;
703 #endif
704 sigaction (SIGWINCH, &old_winch, NULL);
705 #else
706 signal (SIGWINCH, &tui_sigwinch_handler);
707 #endif
708 }
709 #endif
710 }
711
712
713 static void
714 tui_scroll_forward_command (const char *arg, int from_tty)
715 {
716 int num_to_scroll = 1;
717 struct tui_win_info *win_to_scroll;
718
719 /* Make sure the curses mode is enabled. */
720 tui_enable ();
721 if (arg == NULL)
722 parse_scrolling_args (arg, &win_to_scroll, NULL);
723 else
724 parse_scrolling_args (arg, &win_to_scroll, &num_to_scroll);
725 win_to_scroll->forward_scroll (num_to_scroll);
726 }
727
728
729 static void
730 tui_scroll_backward_command (const char *arg, int from_tty)
731 {
732 int num_to_scroll = 1;
733 struct tui_win_info *win_to_scroll;
734
735 /* Make sure the curses mode is enabled. */
736 tui_enable ();
737 if (arg == NULL)
738 parse_scrolling_args (arg, &win_to_scroll, NULL);
739 else
740 parse_scrolling_args (arg, &win_to_scroll, &num_to_scroll);
741 win_to_scroll->backward_scroll (num_to_scroll);
742 }
743
744
745 static void
746 tui_scroll_left_command (const char *arg, int from_tty)
747 {
748 int num_to_scroll;
749 struct tui_win_info *win_to_scroll;
750
751 /* Make sure the curses mode is enabled. */
752 tui_enable ();
753 parse_scrolling_args (arg, &win_to_scroll, &num_to_scroll);
754 win_to_scroll->left_scroll (num_to_scroll);
755 }
756
757
758 static void
759 tui_scroll_right_command (const char *arg, int from_tty)
760 {
761 int num_to_scroll;
762 struct tui_win_info *win_to_scroll;
763
764 /* Make sure the curses mode is enabled. */
765 tui_enable ();
766 parse_scrolling_args (arg, &win_to_scroll, &num_to_scroll);
767 win_to_scroll->right_scroll (num_to_scroll);
768 }
769
770
771 /* Set focus to the window named by 'arg'. */
772 static void
773 tui_set_focus_command (const char *arg, int from_tty)
774 {
775 tui_enable ();
776
777 if (arg != NULL)
778 {
779 struct tui_win_info *win_info = NULL;
780
781 if (subset_compare (arg, "next"))
782 win_info = tui_next_win (tui_win_with_focus ());
783 else if (subset_compare (arg, "prev"))
784 win_info = tui_prev_win (tui_win_with_focus ());
785 else
786 win_info = tui_partial_win_by_name (arg);
787
788 if (win_info == NULL)
789 error (_("Unrecognized window name \"%s\""), arg);
790 if (!win_info->is_visible ())
791 error (_("Window \"%s\" is not visible"), arg);
792
793 tui_set_win_focus_to (win_info);
794 keypad (TUI_CMD_WIN->handle.get (), win_info != TUI_CMD_WIN);
795 printf_filtered (_("Focus set to %s window.\n"),
796 tui_win_with_focus ()->name ());
797 }
798 else
799 error (_("Incorrect Number of Arguments.\n%s"), FOCUS_USAGE);
800 }
801
802 static void
803 tui_all_windows_info (const char *arg, int from_tty)
804 {
805 struct tui_win_info *win_with_focus = tui_win_with_focus ();
806 struct ui_out *uiout = current_uiout;
807
808 ui_out_emit_table table_emitter (uiout, 3, -1, "tui-windows");
809 uiout->table_header (10, ui_left, "name", "Name");
810 uiout->table_header (5, ui_right, "lines", "Lines");
811 uiout->table_header (10, ui_left, "focus", "Focus");
812 uiout->table_body ();
813
814 for (tui_win_info *win_info : all_tui_windows ())
815 if (win_info->is_visible ())
816 {
817 ui_out_emit_tuple tuple_emitter (uiout, nullptr);
818
819 uiout->field_string ("name", win_info->name ());
820 uiout->field_signed ("lines", win_info->height);
821 if (win_with_focus == win_info)
822 uiout->field_string ("focus", _("(has focus)"));
823 else
824 uiout->field_skip ("focus");
825 uiout->text ("\n");
826 }
827 }
828
829
830 static void
831 tui_refresh_all_command (const char *arg, int from_tty)
832 {
833 /* Make sure the curses mode is enabled. */
834 tui_enable ();
835
836 tui_refresh_all_win ();
837 }
838
839 /* The tab width that should be used by the TUI. */
840
841 unsigned int tui_tab_width = DEFAULT_TAB_LEN;
842
843 /* The tab width as set by the user. */
844
845 static unsigned int internal_tab_width = DEFAULT_TAB_LEN;
846
847 /* After the tab width is set, call this to update the relevant
848 windows. */
849
850 static void
851 update_tab_width ()
852 {
853 for (tui_win_info *win_info : all_tui_windows ())
854 {
855 if (win_info->is_visible ())
856 win_info->update_tab_width ();
857 }
858 }
859
860 /* Callback for "set tui tab-width". */
861
862 static void
863 tui_set_tab_width (const char *ignore,
864 int from_tty, struct cmd_list_element *c)
865 {
866 if (internal_tab_width == 0)
867 {
868 internal_tab_width = tui_tab_width;
869 error (_("Tab width must not be 0"));
870 }
871
872 tui_tab_width = internal_tab_width;
873 update_tab_width ();
874 }
875
876 /* Callback for "show tui tab-width". */
877
878 static void
879 tui_show_tab_width (struct ui_file *file, int from_tty,
880 struct cmd_list_element *c, const char *value)
881 {
882 fprintf_filtered (gdb_stdout, _("TUI tab width is %s spaces.\n"), value);
883
884 }
885
886 /* Set the tab width of the specified window. */
887 static void
888 tui_set_tab_width_command (const char *arg, int from_tty)
889 {
890 /* Make sure the curses mode is enabled. */
891 tui_enable ();
892 if (arg != NULL)
893 {
894 int ts;
895
896 ts = atoi (arg);
897 if (ts <= 0)
898 warning (_("Tab widths greater than 0 must be specified."));
899 else
900 {
901 internal_tab_width = ts;
902 tui_tab_width = ts;
903
904 update_tab_width ();
905 }
906 }
907 }
908
909
910 /* Set the height of the specified window. */
911 static void
912 tui_set_win_height_command (const char *arg, int from_tty)
913 {
914 /* Make sure the curses mode is enabled. */
915 tui_enable ();
916 if (arg != NULL)
917 {
918 std::string copy = arg;
919 char *buf = &copy[0];
920 char *buf_ptr = buf;
921 char *wname = NULL;
922 int new_height;
923 struct tui_win_info *win_info;
924
925 wname = buf_ptr;
926 buf_ptr = strchr (buf_ptr, ' ');
927 if (buf_ptr != NULL)
928 {
929 *buf_ptr = '\0';
930
931 /* Validate the window name. */
932 win_info = tui_partial_win_by_name (wname);
933
934 if (win_info == NULL)
935 error (_("Unrecognized window name \"%s\""), arg);
936 if (!win_info->is_visible ())
937 error (_("Window \"%s\" is not visible"), arg);
938
939 /* Process the size. */
940 buf_ptr = skip_spaces (buf_ptr);
941
942 if (*buf_ptr != '\0')
943 {
944 bool negate = false;
945 bool fixed_size = true;
946 int input_no;;
947
948 if (*buf_ptr == '+' || *buf_ptr == '-')
949 {
950 if (*buf_ptr == '-')
951 negate = true;
952 fixed_size = false;
953 buf_ptr++;
954 }
955 input_no = atoi (buf_ptr);
956 if (input_no > 0)
957 {
958 if (negate)
959 input_no *= (-1);
960 if (fixed_size)
961 new_height = input_no;
962 else
963 new_height = win_info->height + input_no;
964
965 /* Now change the window's height, and adjust
966 all other windows around it. */
967 if (tui_adjust_win_heights (win_info,
968 new_height) == TUI_FAILURE)
969 warning (_("Invalid window height specified.\n%s"),
970 WIN_HEIGHT_USAGE);
971 else
972 tui_update_gdb_sizes ();
973 }
974 else
975 warning (_("Invalid window height specified.\n%s"),
976 WIN_HEIGHT_USAGE);
977 }
978 }
979 else
980 printf_filtered (WIN_HEIGHT_USAGE);
981 }
982 else
983 printf_filtered (WIN_HEIGHT_USAGE);
984 }
985
986 /* Function to adjust all window heights around the primary. */
987 static enum tui_status
988 tui_adjust_win_heights (struct tui_win_info *primary_win_info,
989 int new_height)
990 {
991 enum tui_status status = TUI_FAILURE;
992
993 if (new_height_ok (primary_win_info, new_height))
994 {
995 status = TUI_SUCCESS;
996 if (new_height != primary_win_info->height)
997 {
998 int diff;
999 struct tui_win_info *win_info;
1000 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
1001 enum tui_layout_type cur_layout = tui_current_layout ();
1002 int width = tui_term_width ();
1003
1004 diff = (new_height - primary_win_info->height) * (-1);
1005 if (cur_layout == SRC_COMMAND
1006 || cur_layout == DISASSEM_COMMAND)
1007 {
1008 struct tui_win_info *src_win_info;
1009
1010 primary_win_info->resize (new_height, width,
1011 0, primary_win_info->origin.y);
1012 if (primary_win_info->type == CMD_WIN)
1013 {
1014 win_info = *(tui_source_windows ().begin ());
1015 src_win_info = win_info;
1016 }
1017 else
1018 {
1019 win_info = tui_win_list[CMD_WIN];
1020 src_win_info = primary_win_info;
1021 }
1022 win_info->resize (win_info->height + diff, width,
1023 0, win_info->origin.y);
1024 TUI_CMD_WIN->origin.y = locator->origin.y + 1;
1025 if ((src_win_info->type == SRC_WIN
1026 || src_win_info->type == DISASSEM_WIN))
1027 {
1028 tui_source_window_base *src_base
1029 = (tui_source_window_base *) src_win_info;
1030 if (src_base->content.empty ())
1031 src_base->erase_source_content ();
1032 }
1033 }
1034 else
1035 {
1036 struct tui_win_info *first_win;
1037 struct tui_source_window_base *second_win;
1038 tui_source_window_base *src1;
1039
1040 if (cur_layout == SRC_DISASSEM_COMMAND)
1041 {
1042 src1 = TUI_SRC_WIN;
1043 first_win = src1;
1044 second_win = TUI_DISASM_WIN;
1045 }
1046 else
1047 {
1048 src1 = nullptr;
1049 first_win = TUI_DATA_WIN;
1050 second_win = *(tui_source_windows ().begin ());
1051 }
1052 if (primary_win_info == TUI_CMD_WIN)
1053 { /* Split the change in height accross the 1st & 2nd
1054 windows, adjusting them as well. */
1055 /* Subtract the locator. */
1056 int first_split_diff = diff / 2;
1057 int second_split_diff = first_split_diff;
1058
1059 if (diff % 2)
1060 {
1061 if (first_win->height >
1062 second_win->height)
1063 if (diff < 0)
1064 first_split_diff--;
1065 else
1066 first_split_diff++;
1067 else
1068 {
1069 if (diff < 0)
1070 second_split_diff--;
1071 else
1072 second_split_diff++;
1073 }
1074 }
1075 /* Make sure that the minimum hieghts are
1076 honored. */
1077 while ((first_win->height + first_split_diff) < 3)
1078 {
1079 first_split_diff++;
1080 second_split_diff--;
1081 }
1082 while ((second_win->height + second_split_diff) < 3)
1083 {
1084 second_split_diff++;
1085 first_split_diff--;
1086 }
1087 first_win->resize (first_win->height + first_split_diff,
1088 width,
1089 0, first_win->origin.y);
1090 second_win->resize (second_win->height + second_split_diff,
1091 width,
1092 0, first_win->height - 1);
1093 locator->resize (1, width,
1094 0, (second_win->origin.y
1095 + second_win->height + 1));
1096
1097 TUI_CMD_WIN->resize (new_height, width,
1098 0, locator->origin.y + 1);
1099 }
1100 else
1101 {
1102 if ((TUI_CMD_WIN->height + diff) < 1)
1103 { /* If there is no way to increase the command
1104 window take real estate from the 1st or 2nd
1105 window. */
1106 if ((TUI_CMD_WIN->height + diff) < 1)
1107 {
1108 int i;
1109
1110 for (i = TUI_CMD_WIN->height + diff;
1111 (i < 1); i++)
1112 if (primary_win_info == first_win)
1113 second_win->height--;
1114 else
1115 first_win->height--;
1116 }
1117 }
1118 if (primary_win_info == first_win)
1119 first_win->resize (new_height, width, 0, 0);
1120 else
1121 first_win->resize (first_win->height, width, 0, 0);
1122 second_win->origin.y = first_win->height - 1;
1123 if (primary_win_info == second_win)
1124 second_win->resize (new_height, width,
1125 0, first_win->height - 1);
1126 else
1127 second_win->resize (second_win->height, width,
1128 0, first_win->height - 1);
1129 locator->resize (1, width,
1130 0, (second_win->origin.y
1131 + second_win->height + 1));
1132 TUI_CMD_WIN->origin.y = locator->origin.y + 1;
1133 if ((TUI_CMD_WIN->height + diff) < 1)
1134 TUI_CMD_WIN->resize (1, width, 0, locator->origin.y + 1);
1135 else
1136 TUI_CMD_WIN->resize (TUI_CMD_WIN->height + diff, width,
1137 0, locator->origin.y + 1);
1138 }
1139 if (src1 != nullptr && src1->content.empty ())
1140 src1->erase_source_content ();
1141 if (second_win->content.empty ())
1142 second_win->erase_source_content ();
1143 }
1144 }
1145 }
1146
1147 return status;
1148 }
1149
1150 /* See tui-data.h. */
1151
1152 int
1153 tui_win_info::max_height () const
1154 {
1155 return tui_term_height () - 2;
1156 }
1157
1158 static int
1159 new_height_ok (struct tui_win_info *primary_win_info,
1160 int new_height)
1161 {
1162 int ok = (new_height < tui_term_height ());
1163
1164 if (ok)
1165 {
1166 int diff;
1167 enum tui_layout_type cur_layout = tui_current_layout ();
1168
1169 diff = (new_height - primary_win_info->height) * (-1);
1170 if (cur_layout == SRC_COMMAND || cur_layout == DISASSEM_COMMAND)
1171 {
1172 ok = (new_height <= primary_win_info->max_height ()
1173 && new_height >= MIN_CMD_WIN_HEIGHT);
1174 if (ok)
1175 { /* Check the total height. */
1176 struct tui_win_info *win_info;
1177
1178 if (primary_win_info == TUI_CMD_WIN)
1179 win_info = *(tui_source_windows ().begin ());
1180 else
1181 win_info = TUI_CMD_WIN;
1182 ok = ((new_height +
1183 (win_info->height + diff)) <= tui_term_height ());
1184 }
1185 }
1186 else
1187 {
1188 int cur_total_height, total_height, min_height = 0;
1189 struct tui_win_info *first_win;
1190 struct tui_win_info *second_win;
1191
1192 if (cur_layout == SRC_DISASSEM_COMMAND)
1193 {
1194 first_win = TUI_SRC_WIN;
1195 second_win = TUI_DISASM_WIN;
1196 }
1197 else
1198 {
1199 first_win = TUI_DATA_WIN;
1200 second_win = *(tui_source_windows ().begin ());
1201 }
1202 /* We could simply add all the heights to obtain the same
1203 result but below is more explicit since we subtract 1 for
1204 the line that the first and second windows share, and add
1205 one for the locator. */
1206 total_height = cur_total_height =
1207 (first_win->height + second_win->height - 1)
1208 + TUI_CMD_WIN->height + 1; /* Locator. */
1209 if (primary_win_info == TUI_CMD_WIN)
1210 {
1211 /* Locator included since first & second win share a line. */
1212 ok = ((first_win->height +
1213 second_win->height + diff) >=
1214 (MIN_WIN_HEIGHT * 2)
1215 && new_height >= MIN_CMD_WIN_HEIGHT);
1216 if (ok)
1217 {
1218 total_height = new_height +
1219 (first_win->height +
1220 second_win->height + diff);
1221 min_height = MIN_CMD_WIN_HEIGHT;
1222 }
1223 }
1224 else
1225 {
1226 min_height = MIN_WIN_HEIGHT;
1227
1228 /* First see if we can increase/decrease the command
1229 window. And make sure that the command window is at
1230 least 1 line. */
1231 ok = ((TUI_CMD_WIN->height + diff) > 0);
1232 if (!ok)
1233 { /* Looks like we have to increase/decrease one of
1234 the other windows. */
1235 if (primary_win_info == first_win)
1236 ok = (second_win->height + diff) >= min_height;
1237 else
1238 ok = (first_win->height + diff) >= min_height;
1239 }
1240 if (ok)
1241 {
1242 if (primary_win_info == first_win)
1243 total_height = new_height +
1244 second_win->height +
1245 TUI_CMD_WIN->height + diff;
1246 else
1247 total_height = new_height +
1248 first_win->height +
1249 TUI_CMD_WIN->height + diff;
1250 }
1251 }
1252 /* Now make sure that the proposed total height doesn't
1253 exceed the old total height. */
1254 if (ok)
1255 ok = (new_height >= min_height
1256 && total_height <= cur_total_height);
1257 }
1258 }
1259
1260 return ok;
1261 }
1262
1263
1264 static void
1265 parse_scrolling_args (const char *arg,
1266 struct tui_win_info **win_to_scroll,
1267 int *num_to_scroll)
1268 {
1269 if (num_to_scroll)
1270 *num_to_scroll = 0;
1271 *win_to_scroll = tui_win_with_focus ();
1272
1273 /* First set up the default window to scroll, in case there is no
1274 window name arg. */
1275 if (arg != NULL)
1276 {
1277 char *buf_ptr;
1278
1279 /* Process the number of lines to scroll. */
1280 std::string copy = arg;
1281 buf_ptr = &copy[0];
1282 if (isdigit (*buf_ptr))
1283 {
1284 char *num_str;
1285
1286 num_str = buf_ptr;
1287 buf_ptr = strchr (buf_ptr, ' ');
1288 if (buf_ptr != NULL)
1289 {
1290 *buf_ptr = '\0';
1291 if (num_to_scroll)
1292 *num_to_scroll = atoi (num_str);
1293 buf_ptr++;
1294 }
1295 else if (num_to_scroll)
1296 *num_to_scroll = atoi (num_str);
1297 }
1298
1299 /* Process the window name if one is specified. */
1300 if (buf_ptr != NULL)
1301 {
1302 const char *wname;
1303
1304 wname = skip_spaces (buf_ptr);
1305
1306 if (*wname != '\0')
1307 {
1308 *win_to_scroll = tui_partial_win_by_name (wname);
1309
1310 if (*win_to_scroll == NULL)
1311 error (_("Unrecognized window `%s'"), wname);
1312 if (!(*win_to_scroll)->is_visible ())
1313 error (_("Window is not visible"));
1314 else if (*win_to_scroll == TUI_CMD_WIN)
1315 *win_to_scroll = *(tui_source_windows ().begin ());
1316 }
1317 }
1318 }
1319 }
1320
1321 /* Function to initialize gdb commands, for tui window
1322 manipulation. */
1323
1324 void
1325 _initialize_tui_win (void)
1326 {
1327 static struct cmd_list_element *tui_setlist;
1328 static struct cmd_list_element *tui_showlist;
1329 struct cmd_list_element *cmd;
1330
1331 /* Define the classes of commands.
1332 They will appear in the help list in the reverse of this order. */
1333 add_prefix_cmd ("tui", class_tui, set_tui_cmd,
1334 _("TUI configuration variables."),
1335 &tui_setlist, "set tui ",
1336 0 /* allow-unknown */, &setlist);
1337 add_prefix_cmd ("tui", class_tui, show_tui_cmd,
1338 _("TUI configuration variables."),
1339 &tui_showlist, "show tui ",
1340 0 /* allow-unknown */, &showlist);
1341
1342 add_com ("refresh", class_tui, tui_refresh_all_command,
1343 _("Refresh the terminal display."));
1344
1345 cmd = add_com ("tabset", class_tui, tui_set_tab_width_command, _("\
1346 Set the width (in characters) of tab stops.\n\
1347 Usage: tabset N"));
1348 deprecate_cmd (cmd, "set tui tab-width");
1349
1350 cmd = add_com ("winheight", class_tui, tui_set_win_height_command, _("\
1351 Set or modify the height of a specified window.\n"
1352 WIN_HEIGHT_USAGE
1353 "Window names are:\n\
1354 src : the source window\n\
1355 cmd : the command window\n\
1356 asm : the disassembly window\n\
1357 regs : the register display"));
1358 add_com_alias ("wh", "winheight", class_tui, 0);
1359 set_cmd_completer (cmd, winheight_completer);
1360 add_info ("win", tui_all_windows_info,
1361 _("List of all displayed windows."));
1362 cmd = add_com ("focus", class_tui, tui_set_focus_command, _("\
1363 Set focus to named window or next/prev window.\n"
1364 FOCUS_USAGE
1365 "Valid Window names are:\n\
1366 src : the source window\n\
1367 asm : the disassembly window\n\
1368 regs : the register display\n\
1369 cmd : the command window"));
1370 add_com_alias ("fs", "focus", class_tui, 0);
1371 set_cmd_completer (cmd, focus_completer);
1372 add_com ("+", class_tui, tui_scroll_forward_command, _("\
1373 Scroll window forward.\n\
1374 Usage: + [WIN] [N]"));
1375 add_com ("-", class_tui, tui_scroll_backward_command, _("\
1376 Scroll window backward.\n\
1377 Usage: - [WIN] [N]"));
1378 add_com ("<", class_tui, tui_scroll_left_command, _("\
1379 Scroll window text to the left.\n\
1380 Usage: < [WIN] [N]"));
1381 add_com (">", class_tui, tui_scroll_right_command, _("\
1382 Scroll window text to the right.\n\
1383 Usage: > [WIN] [N]"));
1384
1385 /* Define the tui control variables. */
1386 add_setshow_enum_cmd ("border-kind", no_class, tui_border_kind_enums,
1387 &tui_border_kind, _("\
1388 Set the kind of border for TUI windows."), _("\
1389 Show the kind of border for TUI windows."), _("\
1390 This variable controls the border of TUI windows:\n\
1391 space use a white space\n\
1392 ascii use ascii characters + - | for the border\n\
1393 acs use the Alternate Character Set"),
1394 tui_set_var_cmd,
1395 show_tui_border_kind,
1396 &tui_setlist, &tui_showlist);
1397
1398 add_setshow_enum_cmd ("border-mode", no_class, tui_border_mode_enums,
1399 &tui_border_mode, _("\
1400 Set the attribute mode to use for the TUI window borders."), _("\
1401 Show the attribute mode to use for the TUI window borders."), _("\
1402 This variable controls the attributes to use for the window borders:\n\
1403 normal normal display\n\
1404 standout use highlight mode of terminal\n\
1405 reverse use reverse video mode\n\
1406 half use half bright\n\
1407 half-standout use half bright and standout mode\n\
1408 bold use extra bright or bold\n\
1409 bold-standout use extra bright or bold with standout mode"),
1410 tui_set_var_cmd,
1411 show_tui_border_mode,
1412 &tui_setlist, &tui_showlist);
1413
1414 add_setshow_enum_cmd ("active-border-mode", no_class, tui_border_mode_enums,
1415 &tui_active_border_mode, _("\
1416 Set the attribute mode to use for the active TUI window border."), _("\
1417 Show the attribute mode to use for the active TUI window border."), _("\
1418 This variable controls the attributes to use for the active window border:\n\
1419 normal normal display\n\
1420 standout use highlight mode of terminal\n\
1421 reverse use reverse video mode\n\
1422 half use half bright\n\
1423 half-standout use half bright and standout mode\n\
1424 bold use extra bright or bold\n\
1425 bold-standout use extra bright or bold with standout mode"),
1426 tui_set_var_cmd,
1427 show_tui_active_border_mode,
1428 &tui_setlist, &tui_showlist);
1429
1430 add_setshow_zuinteger_cmd ("tab-width", no_class,
1431 &internal_tab_width, _("\
1432 Set the tab width, in characters, for the TUI."), _("\
1433 Show the tab witdh, in characters, for the TUI."), _("\
1434 This variable controls how many spaces are used to display a tab character."),
1435 tui_set_tab_width, tui_show_tab_width,
1436 &tui_setlist, &tui_showlist);
1437 }
This page took 0.059184 seconds and 5 git commands to generate.