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