Remove separator comments from TUI
[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, 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, 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 char *buf_ptr = xstrdup (arg);
780 int i;
781 struct tui_win_info *win_info = NULL;
782
783 for (i = 0; (i < strlen (buf_ptr)); i++)
784 buf_ptr[i] = tolower (arg[i]);
785
786 if (subset_compare (buf_ptr, "next"))
787 win_info = tui_next_win (tui_win_with_focus ());
788 else if (subset_compare (buf_ptr, "prev"))
789 win_info = tui_prev_win (tui_win_with_focus ());
790 else
791 win_info = tui_partial_win_by_name (buf_ptr);
792
793 if (win_info == NULL || !win_info->is_visible ())
794 warning (_("Invalid window specified. \n\
795 The window name specified must be valid and visible.\n"));
796 else
797 {
798 tui_set_win_focus_to (win_info);
799 keypad (TUI_CMD_WIN->handle, (win_info != TUI_CMD_WIN));
800 }
801
802 xfree (buf_ptr);
803 printf_filtered (_("Focus set to %s window.\n"),
804 tui_win_with_focus ()->name ());
805 }
806 else
807 warning (_("Incorrect Number of Arguments.\n%s"), FOCUS_USAGE);
808 }
809
810 static void
811 tui_all_windows_info (const char *arg, int from_tty)
812 {
813 struct tui_win_info *win_with_focus = tui_win_with_focus ();
814 struct ui_out *uiout = current_uiout;
815
816 ui_out_emit_table table_emitter (uiout, 3, -1, "tui-windows");
817 uiout->table_header (10, ui_left, "name", "Name");
818 uiout->table_header (5, ui_right, "lines", "Lines");
819 uiout->table_header (10, ui_left, "focus", "Focus");
820 uiout->table_body ();
821
822 for (tui_win_info *win_info : all_tui_windows ())
823 if (win_info->is_visible ())
824 {
825 ui_out_emit_tuple tuple_emitter (uiout, nullptr);
826
827 uiout->field_string ("name", win_info->name ());
828 uiout->field_signed ("lines", win_info->height);
829 if (win_with_focus == win_info)
830 uiout->field_string ("focus", _("(has focus)"));
831 else
832 uiout->field_skip ("focus");
833 uiout->text ("\n");
834 }
835 }
836
837
838 static void
839 tui_refresh_all_command (const char *arg, int from_tty)
840 {
841 /* Make sure the curses mode is enabled. */
842 tui_enable ();
843
844 tui_refresh_all_win ();
845 }
846
847 /* The tab width that should be used by the TUI. */
848
849 unsigned int tui_tab_width = DEFAULT_TAB_LEN;
850
851 /* The tab width as set by the user. */
852
853 static unsigned int internal_tab_width = DEFAULT_TAB_LEN;
854
855 /* After the tab width is set, call this to update the relevant
856 windows. */
857
858 static void
859 update_tab_width ()
860 {
861 for (tui_win_info *win_info : all_tui_windows ())
862 {
863 if (win_info->is_visible ())
864 win_info->update_tab_width ();
865 }
866 }
867
868 /* Callback for "set tui tab-width". */
869
870 static void
871 tui_set_tab_width (const char *ignore,
872 int from_tty, struct cmd_list_element *c)
873 {
874 if (internal_tab_width == 0)
875 {
876 internal_tab_width = tui_tab_width;
877 error (_("Tab width must not be 0"));
878 }
879
880 tui_tab_width = internal_tab_width;
881 update_tab_width ();
882 }
883
884 /* Callback for "show tui tab-width". */
885
886 static void
887 tui_show_tab_width (struct ui_file *file, int from_tty,
888 struct cmd_list_element *c, const char *value)
889 {
890 fprintf_filtered (gdb_stdout, _("TUI tab width is %s spaces.\n"), value);
891
892 }
893
894 /* Set the tab width of the specified window. */
895 static void
896 tui_set_tab_width_command (const char *arg, int from_tty)
897 {
898 /* Make sure the curses mode is enabled. */
899 tui_enable ();
900 if (arg != NULL)
901 {
902 int ts;
903
904 ts = atoi (arg);
905 if (ts <= 0)
906 warning (_("Tab widths greater than 0 must be specified."));
907 else
908 {
909 internal_tab_width = ts;
910 tui_tab_width = ts;
911
912 update_tab_width ();
913 }
914 }
915 }
916
917
918 /* Set the height of the specified window. */
919 static void
920 tui_set_win_height_command (const char *arg, int from_tty)
921 {
922 /* Make sure the curses mode is enabled. */
923 tui_enable ();
924 if (arg != NULL)
925 {
926 std::string copy = arg;
927 char *buf = &copy[0];
928 char *buf_ptr = buf;
929 char *wname = NULL;
930 int new_height, i;
931 struct tui_win_info *win_info;
932
933 wname = buf_ptr;
934 buf_ptr = strchr (buf_ptr, ' ');
935 if (buf_ptr != NULL)
936 {
937 *buf_ptr = (char) 0;
938
939 /* Validate the window name. */
940 for (i = 0; i < strlen (wname); i++)
941 wname[i] = tolower (wname[i]);
942 win_info = tui_partial_win_by_name (wname);
943
944 if (win_info == NULL || !win_info->is_visible ())
945 warning (_("Invalid window specified. \n\
946 The window name specified must be valid and visible.\n"));
947 else
948 {
949 /* Process the size. */
950 while (*(++buf_ptr) == ' ')
951 ;
952
953 if (*buf_ptr != (char) 0)
954 {
955 int negate = FALSE;
956 int fixed_size = TRUE;
957 int input_no;;
958
959 if (*buf_ptr == '+' || *buf_ptr == '-')
960 {
961 if (*buf_ptr == '-')
962 negate = TRUE;
963 fixed_size = FALSE;
964 buf_ptr++;
965 }
966 input_no = atoi (buf_ptr);
967 if (input_no > 0)
968 {
969 if (negate)
970 input_no *= (-1);
971 if (fixed_size)
972 new_height = input_no;
973 else
974 new_height = win_info->height + input_no;
975
976 /* Now change the window's height, and adjust
977 all other windows around it. */
978 if (tui_adjust_win_heights (win_info,
979 new_height) == TUI_FAILURE)
980 warning (_("Invalid window height specified.\n%s"),
981 WIN_HEIGHT_USAGE);
982 else
983 tui_update_gdb_sizes ();
984 }
985 else
986 warning (_("Invalid window height specified.\n%s"),
987 WIN_HEIGHT_USAGE);
988 }
989 }
990 }
991 else
992 printf_filtered (WIN_HEIGHT_USAGE);
993 }
994 else
995 printf_filtered (WIN_HEIGHT_USAGE);
996 }
997
998 /* Function to adjust all window heights around the primary. */
999 static enum tui_status
1000 tui_adjust_win_heights (struct tui_win_info *primary_win_info,
1001 int new_height)
1002 {
1003 enum tui_status status = TUI_FAILURE;
1004
1005 if (new_height_ok (primary_win_info, new_height))
1006 {
1007 status = TUI_SUCCESS;
1008 if (new_height != primary_win_info->height)
1009 {
1010 int diff;
1011 struct tui_win_info *win_info;
1012 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
1013 enum tui_layout_type cur_layout = tui_current_layout ();
1014 int width = tui_term_width ();
1015
1016 diff = (new_height - primary_win_info->height) * (-1);
1017 if (cur_layout == SRC_COMMAND
1018 || cur_layout == DISASSEM_COMMAND)
1019 {
1020 struct tui_win_info *src_win_info;
1021
1022 primary_win_info->resize (new_height, width,
1023 0, primary_win_info->origin.y);
1024 if (primary_win_info->type == CMD_WIN)
1025 {
1026 win_info = *(tui_source_windows ().begin ());
1027 src_win_info = win_info;
1028 }
1029 else
1030 {
1031 win_info = tui_win_list[CMD_WIN];
1032 src_win_info = primary_win_info;
1033 }
1034 win_info->resize (win_info->height + diff, width,
1035 0, win_info->origin.y);
1036 TUI_CMD_WIN->origin.y = locator->origin.y + 1;
1037 if ((src_win_info->type == SRC_WIN
1038 || src_win_info->type == DISASSEM_WIN))
1039 {
1040 tui_source_window_base *src_base
1041 = (tui_source_window_base *) src_win_info;
1042 if (src_base->content.empty ())
1043 src_base->erase_source_content ();
1044 }
1045 }
1046 else
1047 {
1048 struct tui_win_info *first_win;
1049 struct tui_source_window_base *second_win;
1050 tui_source_window_base *src1;
1051
1052 if (cur_layout == SRC_DISASSEM_COMMAND)
1053 {
1054 src1 = TUI_SRC_WIN;
1055 first_win = src1;
1056 second_win = TUI_DISASM_WIN;
1057 }
1058 else
1059 {
1060 src1 = nullptr;
1061 first_win = TUI_DATA_WIN;
1062 second_win = *(tui_source_windows ().begin ());
1063 }
1064 if (primary_win_info == TUI_CMD_WIN)
1065 { /* Split the change in height accross the 1st & 2nd
1066 windows, adjusting them as well. */
1067 /* Subtract the locator. */
1068 int first_split_diff = diff / 2;
1069 int second_split_diff = first_split_diff;
1070
1071 if (diff % 2)
1072 {
1073 if (first_win->height >
1074 second_win->height)
1075 if (diff < 0)
1076 first_split_diff--;
1077 else
1078 first_split_diff++;
1079 else
1080 {
1081 if (diff < 0)
1082 second_split_diff--;
1083 else
1084 second_split_diff++;
1085 }
1086 }
1087 /* Make sure that the minimum hieghts are
1088 honored. */
1089 while ((first_win->height + first_split_diff) < 3)
1090 {
1091 first_split_diff++;
1092 second_split_diff--;
1093 }
1094 while ((second_win->height + second_split_diff) < 3)
1095 {
1096 second_split_diff++;
1097 first_split_diff--;
1098 }
1099 first_win->resize (first_win->height + first_split_diff,
1100 width,
1101 0, first_win->origin.y);
1102 second_win->resize (second_win->height + second_split_diff,
1103 width,
1104 0, first_win->height - 1);
1105 locator->resize (1, width,
1106 0, (second_win->origin.y
1107 + second_win->height + 1));
1108
1109 TUI_CMD_WIN->resize (new_height, width,
1110 0, locator->origin.y + 1);
1111 }
1112 else
1113 {
1114 if ((TUI_CMD_WIN->height + diff) < 1)
1115 { /* If there is no way to increase the command
1116 window take real estate from the 1st or 2nd
1117 window. */
1118 if ((TUI_CMD_WIN->height + diff) < 1)
1119 {
1120 int i;
1121
1122 for (i = TUI_CMD_WIN->height + diff;
1123 (i < 1); i++)
1124 if (primary_win_info == first_win)
1125 second_win->height--;
1126 else
1127 first_win->height--;
1128 }
1129 }
1130 if (primary_win_info == first_win)
1131 first_win->resize (new_height, width, 0, 0);
1132 else
1133 first_win->resize (first_win->height, width, 0, 0);
1134 second_win->origin.y = first_win->height - 1;
1135 if (primary_win_info == second_win)
1136 second_win->resize (new_height, width,
1137 0, first_win->height - 1);
1138 else
1139 second_win->resize (second_win->height, width,
1140 0, first_win->height - 1);
1141 locator->resize (1, width,
1142 0, (second_win->origin.y
1143 + second_win->height + 1));
1144 TUI_CMD_WIN->origin.y = locator->origin.y + 1;
1145 if ((TUI_CMD_WIN->height + diff) < 1)
1146 TUI_CMD_WIN->resize (1, width, 0, locator->origin.y + 1);
1147 else
1148 TUI_CMD_WIN->resize (TUI_CMD_WIN->height + diff, width,
1149 0, locator->origin.y + 1);
1150 }
1151 if (src1 != nullptr && src1->content.empty ())
1152 src1->erase_source_content ();
1153 if (second_win->content.empty ())
1154 second_win->erase_source_content ();
1155 }
1156 }
1157 }
1158
1159 return status;
1160 }
1161
1162 /* See tui-data.h. */
1163
1164 int
1165 tui_win_info::max_height () const
1166 {
1167 return tui_term_height () - 2;
1168 }
1169
1170 static int
1171 new_height_ok (struct tui_win_info *primary_win_info,
1172 int new_height)
1173 {
1174 int ok = (new_height < tui_term_height ());
1175
1176 if (ok)
1177 {
1178 int diff;
1179 enum tui_layout_type cur_layout = tui_current_layout ();
1180
1181 diff = (new_height - primary_win_info->height) * (-1);
1182 if (cur_layout == SRC_COMMAND || cur_layout == DISASSEM_COMMAND)
1183 {
1184 ok = (new_height <= primary_win_info->max_height ()
1185 && new_height >= MIN_CMD_WIN_HEIGHT);
1186 if (ok)
1187 { /* Check the total height. */
1188 struct tui_win_info *win_info;
1189
1190 if (primary_win_info == TUI_CMD_WIN)
1191 win_info = *(tui_source_windows ().begin ());
1192 else
1193 win_info = TUI_CMD_WIN;
1194 ok = ((new_height +
1195 (win_info->height + diff)) <= tui_term_height ());
1196 }
1197 }
1198 else
1199 {
1200 int cur_total_height, total_height, min_height = 0;
1201 struct tui_win_info *first_win;
1202 struct tui_win_info *second_win;
1203
1204 if (cur_layout == SRC_DISASSEM_COMMAND)
1205 {
1206 first_win = TUI_SRC_WIN;
1207 second_win = TUI_DISASM_WIN;
1208 }
1209 else
1210 {
1211 first_win = TUI_DATA_WIN;
1212 second_win = *(tui_source_windows ().begin ());
1213 }
1214 /* We could simply add all the heights to obtain the same
1215 result but below is more explicit since we subtract 1 for
1216 the line that the first and second windows share, and add
1217 one for the locator. */
1218 total_height = cur_total_height =
1219 (first_win->height + second_win->height - 1)
1220 + TUI_CMD_WIN->height + 1; /* Locator. */
1221 if (primary_win_info == TUI_CMD_WIN)
1222 {
1223 /* Locator included since first & second win share a line. */
1224 ok = ((first_win->height +
1225 second_win->height + diff) >=
1226 (MIN_WIN_HEIGHT * 2)
1227 && new_height >= MIN_CMD_WIN_HEIGHT);
1228 if (ok)
1229 {
1230 total_height = new_height +
1231 (first_win->height +
1232 second_win->height + diff);
1233 min_height = MIN_CMD_WIN_HEIGHT;
1234 }
1235 }
1236 else
1237 {
1238 min_height = MIN_WIN_HEIGHT;
1239
1240 /* First see if we can increase/decrease the command
1241 window. And make sure that the command window is at
1242 least 1 line. */
1243 ok = ((TUI_CMD_WIN->height + diff) > 0);
1244 if (!ok)
1245 { /* Looks like we have to increase/decrease one of
1246 the other windows. */
1247 if (primary_win_info == first_win)
1248 ok = (second_win->height + diff) >= min_height;
1249 else
1250 ok = (first_win->height + diff) >= min_height;
1251 }
1252 if (ok)
1253 {
1254 if (primary_win_info == first_win)
1255 total_height = new_height +
1256 second_win->height +
1257 TUI_CMD_WIN->height + diff;
1258 else
1259 total_height = new_height +
1260 first_win->height +
1261 TUI_CMD_WIN->height + diff;
1262 }
1263 }
1264 /* Now make sure that the proposed total height doesn't
1265 exceed the old total height. */
1266 if (ok)
1267 ok = (new_height >= min_height
1268 && total_height <= cur_total_height);
1269 }
1270 }
1271
1272 return ok;
1273 }
1274
1275
1276 static void
1277 parse_scrolling_args (const char *arg,
1278 struct tui_win_info **win_to_scroll,
1279 int *num_to_scroll)
1280 {
1281 if (num_to_scroll)
1282 *num_to_scroll = 0;
1283 *win_to_scroll = tui_win_with_focus ();
1284
1285 /* First set up the default window to scroll, in case there is no
1286 window name arg. */
1287 if (arg != NULL)
1288 {
1289 char *buf_ptr;
1290
1291 /* Process the number of lines to scroll. */
1292 std::string copy = arg;
1293 buf_ptr = &copy[0];
1294 if (isdigit (*buf_ptr))
1295 {
1296 char *num_str;
1297
1298 num_str = buf_ptr;
1299 buf_ptr = strchr (buf_ptr, ' ');
1300 if (buf_ptr != NULL)
1301 {
1302 *buf_ptr = (char) 0;
1303 if (num_to_scroll)
1304 *num_to_scroll = atoi (num_str);
1305 buf_ptr++;
1306 }
1307 else if (num_to_scroll)
1308 *num_to_scroll = atoi (num_str);
1309 }
1310
1311 /* Process the window name if one is specified. */
1312 if (buf_ptr != NULL)
1313 {
1314 const char *wname;
1315
1316 if (*buf_ptr == ' ')
1317 while (*(++buf_ptr) == ' ')
1318 ;
1319
1320 if (*buf_ptr != (char) 0)
1321 {
1322 /* Validate the window name. */
1323 for (char *p = buf_ptr; *p != '\0'; p++)
1324 *p = tolower (*p);
1325
1326 wname = buf_ptr;
1327 }
1328 else
1329 wname = "?";
1330
1331 *win_to_scroll = tui_partial_win_by_name (wname);
1332
1333 if (*win_to_scroll == NULL)
1334 error (_("Unrecognized window `%s'"), wname);
1335 if (!(*win_to_scroll)->is_visible ())
1336 error (_("Window is not visible"));
1337 else if (*win_to_scroll == TUI_CMD_WIN)
1338 *win_to_scroll = *(tui_source_windows ().begin ());
1339 }
1340 }
1341 }
1342
1343 /* Function to initialize gdb commands, for tui window
1344 manipulation. */
1345
1346 void
1347 _initialize_tui_win (void)
1348 {
1349 static struct cmd_list_element *tui_setlist;
1350 static struct cmd_list_element *tui_showlist;
1351 struct cmd_list_element *cmd;
1352
1353 /* Define the classes of commands.
1354 They will appear in the help list in the reverse of this order. */
1355 add_prefix_cmd ("tui", class_tui, set_tui_cmd,
1356 _("TUI configuration variables."),
1357 &tui_setlist, "set tui ",
1358 0 /* allow-unknown */, &setlist);
1359 add_prefix_cmd ("tui", class_tui, show_tui_cmd,
1360 _("TUI configuration variables."),
1361 &tui_showlist, "show tui ",
1362 0 /* allow-unknown */, &showlist);
1363
1364 add_com ("refresh", class_tui, tui_refresh_all_command,
1365 _("Refresh the terminal display."));
1366
1367 cmd = add_com ("tabset", class_tui, tui_set_tab_width_command, _("\
1368 Set the width (in characters) of tab stops.\n\
1369 Usage: tabset N"));
1370 deprecate_cmd (cmd, "set tui tab-width");
1371
1372 cmd = add_com ("winheight", class_tui, tui_set_win_height_command, _("\
1373 Set or modify the height of a specified window.\n"
1374 WIN_HEIGHT_USAGE
1375 "Window names are:\n\
1376 src : the source window\n\
1377 cmd : the command window\n\
1378 asm : the disassembly window\n\
1379 regs : the register display"));
1380 add_com_alias ("wh", "winheight", class_tui, 0);
1381 set_cmd_completer (cmd, winheight_completer);
1382 add_info ("win", tui_all_windows_info,
1383 _("List of all displayed windows."));
1384 cmd = add_com ("focus", class_tui, tui_set_focus_command, _("\
1385 Set focus to named window or next/prev window.\n"
1386 FOCUS_USAGE
1387 "Valid Window names are:\n\
1388 src : the source window\n\
1389 asm : the disassembly window\n\
1390 regs : the register display\n\
1391 cmd : the command window"));
1392 add_com_alias ("fs", "focus", class_tui, 0);
1393 set_cmd_completer (cmd, focus_completer);
1394 add_com ("+", class_tui, tui_scroll_forward_command, _("\
1395 Scroll window forward.\n\
1396 Usage: + [WIN] [N]"));
1397 add_com ("-", class_tui, tui_scroll_backward_command, _("\
1398 Scroll window backward.\n\
1399 Usage: - [WIN] [N]"));
1400 add_com ("<", class_tui, tui_scroll_left_command, _("\
1401 Scroll window text to the left.\n\
1402 Usage: < [WIN] [N]"));
1403 add_com (">", class_tui, tui_scroll_right_command, _("\
1404 Scroll window text to the right.\n\
1405 Usage: > [WIN] [N]"));
1406
1407 /* Define the tui control variables. */
1408 add_setshow_enum_cmd ("border-kind", no_class, tui_border_kind_enums,
1409 &tui_border_kind, _("\
1410 Set the kind of border for TUI windows."), _("\
1411 Show the kind of border for TUI windows."), _("\
1412 This variable controls the border of TUI windows:\n\
1413 space use a white space\n\
1414 ascii use ascii characters + - | for the border\n\
1415 acs use the Alternate Character Set"),
1416 tui_set_var_cmd,
1417 show_tui_border_kind,
1418 &tui_setlist, &tui_showlist);
1419
1420 add_setshow_enum_cmd ("border-mode", no_class, tui_border_mode_enums,
1421 &tui_border_mode, _("\
1422 Set the attribute mode to use for the TUI window borders."), _("\
1423 Show the attribute mode to use for the TUI window borders."), _("\
1424 This variable controls the attributes to use for the window borders:\n\
1425 normal normal display\n\
1426 standout use highlight mode of terminal\n\
1427 reverse use reverse video mode\n\
1428 half use half bright\n\
1429 half-standout use half bright and standout mode\n\
1430 bold use extra bright or bold\n\
1431 bold-standout use extra bright or bold with standout mode"),
1432 tui_set_var_cmd,
1433 show_tui_border_mode,
1434 &tui_setlist, &tui_showlist);
1435
1436 add_setshow_enum_cmd ("active-border-mode", no_class, tui_border_mode_enums,
1437 &tui_active_border_mode, _("\
1438 Set the attribute mode to use for the active TUI window border."), _("\
1439 Show the attribute mode to use for the active TUI window border."), _("\
1440 This variable controls the attributes to use for the active window border:\n\
1441 normal normal display\n\
1442 standout use highlight mode of terminal\n\
1443 reverse use reverse video mode\n\
1444 half use half bright\n\
1445 half-standout use half bright and standout mode\n\
1446 bold use extra bright or bold\n\
1447 bold-standout use extra bright or bold with standout mode"),
1448 tui_set_var_cmd,
1449 show_tui_active_border_mode,
1450 &tui_setlist, &tui_showlist);
1451
1452 add_setshow_zuinteger_cmd ("tab-width", no_class,
1453 &internal_tab_width, _("\
1454 Set the tab width, in characters, for the TUI."), _("\
1455 Show the tab witdh, in characters, for the TUI."), _("\
1456 This variable controls how many spaces are used to display a tab character."),
1457 tui_set_tab_width, tui_show_tab_width,
1458 &tui_setlist, &tui_showlist);
1459 }