]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/tui/tui-layout.c
Move current_layout to tui-layout.c
[thirdparty/binutils-gdb.git] / gdb / tui / tui-layout.c
1 /* TUI layout window management.
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 #include "defs.h"
23 #include "arch-utils.h"
24 #include "command.h"
25 #include "symtab.h"
26 #include "frame.h"
27 #include "source.h"
28 #include <ctype.h>
29
30 #include "tui/tui.h"
31 #include "tui/tui-command.h"
32 #include "tui/tui-data.h"
33 #include "tui/tui-wingeneral.h"
34 #include "tui/tui-stack.h"
35 #include "tui/tui-regs.h"
36 #include "tui/tui-win.h"
37 #include "tui/tui-winsource.h"
38 #include "tui/tui-disasm.h"
39 #include "tui/tui-layout.h"
40 #include "tui/tui-source.h"
41 #include "gdb_curses.h"
42
43 /*******************************
44 ** Static Local Decls
45 ********************************/
46 static void show_layout (enum tui_layout_type);
47 static void show_source_or_disasm_and_command (enum tui_layout_type);
48 static void show_source_command (void);
49 static void show_disasm_command (void);
50 static void show_source_disasm_command (void);
51 static void show_data (enum tui_layout_type);
52 static enum tui_layout_type next_layout (void);
53 static enum tui_layout_type prev_layout (void);
54 static void tui_layout_command (const char *, int);
55 static void extract_display_start_addr (struct gdbarch **, CORE_ADDR *);
56
57
58 static enum tui_layout_type current_layout = UNDEFINED_LAYOUT;
59
60 /* Accessor for the current layout. */
61 enum tui_layout_type
62 tui_current_layout (void)
63 {
64 return current_layout;
65 }
66
67 /***************************************
68 ** DEFINITIONS
69 ***************************************/
70
71 /* Show the screen layout defined. */
72 static void
73 show_layout (enum tui_layout_type layout)
74 {
75 enum tui_layout_type cur_layout = tui_current_layout ();
76
77 if (layout != cur_layout)
78 {
79 /* Since the new layout may cause changes in window size, we
80 should free the content and reallocate on next display of
81 source/asm. */
82 tui_clear_source_windows ();
83 if (layout == SRC_DATA_COMMAND
84 || layout == DISASSEM_DATA_COMMAND)
85 {
86 show_data (layout);
87 tui_refresh_all ();
88 }
89 else
90 {
91 /* First make the current layout be invisible. */
92 tui_make_all_invisible ();
93 tui_locator_win_info_ptr ()->make_visible (false);
94
95 switch (layout)
96 {
97 /* Now show the new layout. */
98 case SRC_COMMAND:
99 show_source_command ();
100 tui_add_to_source_windows (TUI_SRC_WIN);
101 break;
102 case DISASSEM_COMMAND:
103 show_disasm_command ();
104 tui_add_to_source_windows (TUI_DISASM_WIN);
105 break;
106 case SRC_DISASSEM_COMMAND:
107 show_source_disasm_command ();
108 tui_add_to_source_windows (TUI_SRC_WIN);
109 tui_add_to_source_windows (TUI_DISASM_WIN);
110 break;
111 default:
112 break;
113 }
114 }
115 }
116 }
117
118
119 /* Function to set the layout to SRC_COMMAND, DISASSEM_COMMAND,
120 SRC_DISASSEM_COMMAND, SRC_DATA_COMMAND, or DISASSEM_DATA_COMMAND. */
121 void
122 tui_set_layout (enum tui_layout_type layout_type)
123 {
124 gdb_assert (layout_type != UNDEFINED_LAYOUT);
125
126 enum tui_layout_type cur_layout = tui_current_layout ();
127 struct gdbarch *gdbarch;
128 CORE_ADDR addr;
129 struct tui_win_info *win_with_focus = tui_win_with_focus ();
130
131 extract_display_start_addr (&gdbarch, &addr);
132
133 enum tui_layout_type new_layout = layout_type;
134
135 if (new_layout != cur_layout)
136 {
137 show_layout (new_layout);
138
139 /* Now determine where focus should be. */
140 if (win_with_focus != TUI_CMD_WIN)
141 {
142 switch (new_layout)
143 {
144 case SRC_COMMAND:
145 tui_set_win_focus_to (TUI_SRC_WIN);
146 break;
147 case DISASSEM_COMMAND:
148 /* The previous layout was not showing code.
149 This can happen if there is no source
150 available:
151
152 1. if the source file is in another dir OR
153 2. if target was compiled without -g
154 We still want to show the assembly though! */
155
156 tui_get_begin_asm_address (&gdbarch, &addr);
157 tui_set_win_focus_to (TUI_DISASM_WIN);
158 break;
159 case SRC_DISASSEM_COMMAND:
160 /* The previous layout was not showing code.
161 This can happen if there is no source
162 available:
163
164 1. if the source file is in another dir OR
165 2. if target was compiled without -g
166 We still want to show the assembly though! */
167
168 tui_get_begin_asm_address (&gdbarch, &addr);
169 if (win_with_focus == TUI_SRC_WIN)
170 tui_set_win_focus_to (TUI_SRC_WIN);
171 else
172 tui_set_win_focus_to (TUI_DISASM_WIN);
173 break;
174 case SRC_DATA_COMMAND:
175 if (win_with_focus != TUI_DATA_WIN)
176 tui_set_win_focus_to (TUI_SRC_WIN);
177 else
178 tui_set_win_focus_to (TUI_DATA_WIN);
179 break;
180 case DISASSEM_DATA_COMMAND:
181 /* The previous layout was not showing code.
182 This can happen if there is no source
183 available:
184
185 1. if the source file is in another dir OR
186 2. if target was compiled without -g
187 We still want to show the assembly though! */
188
189 tui_get_begin_asm_address (&gdbarch, &addr);
190 if (win_with_focus != TUI_DATA_WIN)
191 tui_set_win_focus_to (TUI_DISASM_WIN);
192 else
193 tui_set_win_focus_to (TUI_DATA_WIN);
194 break;
195 default:
196 break;
197 }
198 }
199 /*
200 * Now update the window content.
201 */
202 tui_update_source_windows_with_addr (gdbarch, addr);
203 if (new_layout == SRC_DATA_COMMAND
204 || new_layout == DISASSEM_DATA_COMMAND)
205 tui_show_registers (TUI_DATA_WIN->current_group);
206 }
207 }
208
209 /* Add the specified window to the layout in a logical way. This
210 means setting up the most logical layout given the window to be
211 added. */
212 void
213 tui_add_win_to_layout (enum tui_win_type type)
214 {
215 enum tui_layout_type cur_layout = tui_current_layout ();
216
217 switch (type)
218 {
219 case SRC_WIN:
220 if (cur_layout != SRC_COMMAND
221 && cur_layout != SRC_DISASSEM_COMMAND
222 && cur_layout != SRC_DATA_COMMAND)
223 {
224 tui_clear_source_windows_detail ();
225 if (cur_layout == DISASSEM_DATA_COMMAND)
226 show_layout (SRC_DATA_COMMAND);
227 else
228 show_layout (SRC_COMMAND);
229 }
230 break;
231 case DISASSEM_WIN:
232 if (cur_layout != DISASSEM_COMMAND
233 && cur_layout != SRC_DISASSEM_COMMAND
234 && cur_layout != DISASSEM_DATA_COMMAND)
235 {
236 tui_clear_source_windows_detail ();
237 if (cur_layout == SRC_DATA_COMMAND)
238 show_layout (DISASSEM_DATA_COMMAND);
239 else
240 show_layout (DISASSEM_COMMAND);
241 }
242 break;
243 case DATA_WIN:
244 if (cur_layout != SRC_DATA_COMMAND
245 && cur_layout != DISASSEM_DATA_COMMAND)
246 {
247 if (cur_layout == DISASSEM_COMMAND)
248 show_layout (DISASSEM_DATA_COMMAND);
249 else
250 show_layout (SRC_DATA_COMMAND);
251 }
252 break;
253 default:
254 break;
255 }
256 }
257
258
259 /* Answer the height of a window. If it hasn't been created yet,
260 answer what the height of a window would be based upon its type and
261 the layout. */
262 int
263 tui_default_win_height (enum tui_win_type type,
264 enum tui_layout_type layout)
265 {
266 int h;
267
268 if (tui_win_list[type] != NULL)
269 h = tui_win_list[type]->height;
270 else
271 {
272 switch (layout)
273 {
274 case SRC_COMMAND:
275 case DISASSEM_COMMAND:
276 if (TUI_CMD_WIN == NULL)
277 h = tui_term_height () / 2;
278 else
279 h = tui_term_height () - TUI_CMD_WIN->height;
280 break;
281 case SRC_DISASSEM_COMMAND:
282 case SRC_DATA_COMMAND:
283 case DISASSEM_DATA_COMMAND:
284 if (TUI_CMD_WIN == NULL)
285 h = tui_term_height () / 3;
286 else
287 h = (tui_term_height () - TUI_CMD_WIN->height) / 2;
288 break;
289 default:
290 h = 0;
291 break;
292 }
293 }
294
295 return h;
296 }
297
298
299 /* Answer the height of a window. If it hasn't been created yet,
300 answer what the height of a window would be based upon its type and
301 the layout. */
302 int
303 tui_default_win_viewport_height (enum tui_win_type type,
304 enum tui_layout_type layout)
305 {
306 int h;
307
308 h = tui_default_win_height (type, layout);
309
310 if (tui_win_list[type] == TUI_CMD_WIN)
311 h -= 1;
312 else
313 h -= 2;
314
315 return h;
316 }
317
318 /* Complete possible layout names. TEXT is the complete text entered so
319 far, WORD is the word currently being completed. */
320
321 static void
322 layout_completer (struct cmd_list_element *ignore,
323 completion_tracker &tracker,
324 const char *text, const char *word)
325 {
326 static const char *layout_names [] =
327 { "src", "asm", "split", "regs", "next", "prev", NULL };
328
329 complete_on_enum (tracker, layout_names, text, word);
330 }
331
332 /* Function to initialize gdb commands, for tui window layout
333 manipulation. */
334
335 void
336 _initialize_tui_layout (void)
337 {
338 struct cmd_list_element *cmd;
339
340 cmd = add_com ("layout", class_tui, tui_layout_command, _("\
341 Change the layout of windows.\n\
342 Usage: layout prev | next | LAYOUT-NAME\n\
343 Layout names are:\n\
344 src : Displays source and command windows.\n\
345 asm : Displays disassembly and command windows.\n\
346 split : Displays source, disassembly and command windows.\n\
347 regs : Displays register window. If existing layout\n\
348 is source/command or assembly/command, the \n\
349 register window is displayed. If the\n\
350 source/assembly/command (split) is displayed, \n\
351 the register window is displayed with \n\
352 the window that has current logical focus."));
353 set_cmd_completer (cmd, layout_completer);
354 }
355
356
357 /*************************
358 ** STATIC LOCAL FUNCTIONS
359 **************************/
360
361
362 /* Function to set the layout to SRC, ASM, SPLIT, NEXT, PREV, DATA, or
363 REGS. */
364 static void
365 tui_layout_command (const char *layout_name, int from_tty)
366 {
367 int i;
368 enum tui_layout_type new_layout = UNDEFINED_LAYOUT;
369 enum tui_layout_type cur_layout = tui_current_layout ();
370
371 if (layout_name == NULL)
372 error (_("Usage: layout prev | next | LAYOUT-NAME"));
373
374 std::string copy = layout_name;
375 for (i = 0; i < copy.size (); i++)
376 copy[i] = toupper (copy[i]);
377 const char *buf_ptr = copy.c_str ();
378
379 /* First check for ambiguous input. */
380 if (strlen (buf_ptr) <= 1 && *buf_ptr == 'S')
381 error (_("Ambiguous command input."));
382
383 if (subset_compare (buf_ptr, "SRC"))
384 new_layout = SRC_COMMAND;
385 else if (subset_compare (buf_ptr, "ASM"))
386 new_layout = DISASSEM_COMMAND;
387 else if (subset_compare (buf_ptr, "SPLIT"))
388 new_layout = SRC_DISASSEM_COMMAND;
389 else if (subset_compare (buf_ptr, "REGS"))
390 {
391 if (cur_layout == SRC_COMMAND
392 || cur_layout == SRC_DATA_COMMAND)
393 new_layout = SRC_DATA_COMMAND;
394 else
395 new_layout = DISASSEM_DATA_COMMAND;
396 }
397 else if (subset_compare (buf_ptr, "NEXT"))
398 new_layout = next_layout ();
399 else if (subset_compare (buf_ptr, "PREV"))
400 new_layout = prev_layout ();
401 else
402 error (_("Unrecognized layout: %s"), layout_name);
403
404 /* Make sure the curses mode is enabled. */
405 tui_enable ();
406 tui_set_layout (new_layout);
407 }
408
409
410 static void
411 extract_display_start_addr (struct gdbarch **gdbarch_p, CORE_ADDR *addr_p)
412 {
413 enum tui_layout_type cur_layout = tui_current_layout ();
414 struct gdbarch *gdbarch = get_current_arch ();
415 CORE_ADDR addr;
416 CORE_ADDR pc;
417 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
418
419 switch (cur_layout)
420 {
421 case SRC_COMMAND:
422 case SRC_DATA_COMMAND:
423 gdbarch = TUI_SRC_WIN->gdbarch;
424 find_line_pc (cursal.symtab,
425 TUI_SRC_WIN->start_line_or_addr.u.line_no,
426 &pc);
427 addr = pc;
428 break;
429 case DISASSEM_COMMAND:
430 case SRC_DISASSEM_COMMAND:
431 case DISASSEM_DATA_COMMAND:
432 gdbarch = TUI_DISASM_WIN->gdbarch;
433 addr = TUI_DISASM_WIN->start_line_or_addr.u.addr;
434 break;
435 default:
436 addr = 0;
437 break;
438 }
439
440 *gdbarch_p = gdbarch;
441 *addr_p = addr;
442 }
443
444
445 /* Answer the previous layout to cycle to. */
446 static enum tui_layout_type
447 next_layout (void)
448 {
449 int new_layout;
450
451 new_layout = tui_current_layout ();
452 if (new_layout == UNDEFINED_LAYOUT)
453 new_layout = SRC_COMMAND;
454 else
455 {
456 new_layout++;
457 if (new_layout == UNDEFINED_LAYOUT)
458 new_layout = SRC_COMMAND;
459 }
460
461 return (enum tui_layout_type) new_layout;
462 }
463
464
465 /* Answer the next layout to cycle to. */
466 static enum tui_layout_type
467 prev_layout (void)
468 {
469 int new_layout;
470
471 new_layout = tui_current_layout ();
472 if (new_layout == SRC_COMMAND)
473 new_layout = DISASSEM_DATA_COMMAND;
474 else
475 {
476 new_layout--;
477 if (new_layout == UNDEFINED_LAYOUT)
478 new_layout = DISASSEM_DATA_COMMAND;
479 }
480
481 return (enum tui_layout_type) new_layout;
482 }
483
484 /* Show the Source/Command layout. */
485 static void
486 show_source_command (void)
487 {
488 show_source_or_disasm_and_command (SRC_COMMAND);
489 }
490
491
492 /* Show the Dissassem/Command layout. */
493 static void
494 show_disasm_command (void)
495 {
496 show_source_or_disasm_and_command (DISASSEM_COMMAND);
497 }
498
499
500 /* Show the Source/Disassem/Command layout. */
501 static void
502 show_source_disasm_command (void)
503 {
504 if (tui_current_layout () != SRC_DISASSEM_COMMAND)
505 {
506 int cmd_height, src_height, asm_height;
507
508 if (TUI_CMD_WIN != NULL)
509 cmd_height = TUI_CMD_WIN->height;
510 else
511 cmd_height = tui_term_height () / 3;
512
513 src_height = (tui_term_height () - cmd_height) / 2;
514 asm_height = tui_term_height () - (src_height + cmd_height);
515
516 if (TUI_SRC_WIN == NULL)
517 tui_win_list[SRC_WIN] = new tui_source_window ();
518 TUI_SRC_WIN->reset (src_height,
519 tui_term_width (),
520 0,
521 0);
522 TUI_SRC_WIN->make_visible (true);
523 TUI_SRC_WIN->m_has_locator = false;
524
525 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
526 gdb_assert (locator != nullptr);
527
528 tui_show_source_content (TUI_SRC_WIN);
529 if (TUI_DISASM_WIN == NULL)
530 tui_win_list[DISASSEM_WIN] = new tui_disasm_window ();
531 TUI_DISASM_WIN->reset (asm_height,
532 tui_term_width (),
533 0,
534 src_height - 1);
535 TUI_DISASM_WIN->make_visible (true);
536 locator->reset (2 /* 1 */ ,
537 tui_term_width (),
538 0,
539 (src_height + asm_height) - 1);
540 TUI_SRC_WIN->m_has_locator = false;
541 TUI_DISASM_WIN->m_has_locator = true;
542 locator->make_visible (true);
543 tui_show_locator_content ();
544 tui_show_source_content (TUI_DISASM_WIN);
545
546 if (TUI_CMD_WIN == NULL)
547 tui_win_list[CMD_WIN] = new tui_cmd_window ();
548 TUI_CMD_WIN->reset (cmd_height,
549 tui_term_width (),
550 0,
551 tui_term_height () - cmd_height);
552 /* FIXME tui_cmd_window won't recreate the handle on
553 make_visible, so we need this instead. */
554 tui_make_window (TUI_CMD_WIN, DONT_BOX_WINDOW);
555 current_layout = SRC_DISASSEM_COMMAND;
556 }
557 }
558
559
560 /* Show the Source/Data/Command or the Dissassembly/Data/Command
561 layout. */
562 static void
563 show_data (enum tui_layout_type new_layout)
564 {
565 int total_height = (tui_term_height () - TUI_CMD_WIN->height);
566 int src_height, data_height;
567 enum tui_win_type win_type;
568
569 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
570 gdb_assert (locator != nullptr);
571
572 data_height = total_height / 2;
573 src_height = total_height - data_height;
574 tui_make_all_invisible ();
575 locator->make_visible (false);
576 if (tui_win_list[DATA_WIN] == nullptr)
577 tui_win_list[DATA_WIN] = new tui_data_window ();
578 tui_win_list[DATA_WIN]->reset (data_height, tui_term_width (), 0, 0);
579 tui_win_list[DATA_WIN]->make_visible (true);
580
581 if (new_layout == SRC_DATA_COMMAND)
582 win_type = SRC_WIN;
583 else
584 win_type = DISASSEM_WIN;
585
586 if (tui_win_list[win_type] == NULL)
587 {
588 if (win_type == SRC_WIN)
589 tui_win_list[win_type] = new tui_source_window ();
590 else
591 tui_win_list[win_type] = new tui_disasm_window ();
592 }
593
594 tui_source_window_base *base
595 = (tui_source_window_base *) tui_win_list[win_type];
596 tui_win_list[win_type]->reset (src_height,
597 tui_term_width (),
598 0,
599 data_height - 1);
600 locator->reset (2 /* 1 */ ,
601 tui_term_width (),
602 0,
603 total_height - 1);
604 base->make_visible (true);
605 base->m_has_locator = true;
606 locator->make_visible (true);
607 tui_show_locator_content ();
608 tui_add_to_source_windows (base);
609 current_layout = new_layout;
610 }
611
612 void
613 tui_gen_win_info::reset (int height_, int width_,
614 int origin_x_, int origin_y_)
615 {
616 int h = height_;
617
618 width = width_;
619 height = h;
620 if (h > 1)
621 {
622 viewport_height = h - 1;
623 if (type != CMD_WIN)
624 viewport_height--;
625 }
626 else
627 viewport_height = 1;
628 origin.x = origin_x_;
629 origin.y = origin_y_;
630 }
631
632 /* Show the Source/Command or the Disassem layout. */
633 static void
634 show_source_or_disasm_and_command (enum tui_layout_type layout_type)
635 {
636 if (tui_current_layout () != layout_type)
637 {
638 struct tui_source_window_base *win_info;
639 int src_height, cmd_height;
640 struct tui_locator_window *locator = tui_locator_win_info_ptr ();
641 gdb_assert (locator != nullptr);
642
643 if (TUI_CMD_WIN != NULL)
644 cmd_height = TUI_CMD_WIN->height;
645 else
646 cmd_height = tui_term_height () / 3;
647 src_height = tui_term_height () - cmd_height;
648
649 if (layout_type == SRC_COMMAND)
650 {
651 if (tui_win_list[SRC_WIN] == nullptr)
652 tui_win_list[SRC_WIN] = new tui_source_window ();
653 win_info = TUI_SRC_WIN;
654 }
655 else
656 {
657 if (tui_win_list[DISASSEM_WIN] == nullptr)
658 tui_win_list[DISASSEM_WIN] = new tui_disasm_window ();
659 win_info = TUI_DISASM_WIN;
660 }
661
662 locator->reset (2 /* 1 */ ,
663 tui_term_width (),
664 0,
665 src_height - 1);
666 win_info->reset (src_height - 1,
667 tui_term_width (),
668 0,
669 0);
670 win_info->make_visible (true);
671
672
673 win_info->m_has_locator = true;
674 locator->make_visible (true);
675 tui_show_locator_content ();
676 tui_show_source_content (win_info);
677
678 if (TUI_CMD_WIN == NULL)
679 tui_win_list[CMD_WIN] = new tui_cmd_window ();
680 TUI_CMD_WIN->reset (cmd_height,
681 tui_term_width (),
682 0,
683 src_height);
684 /* FIXME tui_cmd_window won't recreate the handle on
685 make_visible, so we need this instead. */
686 tui_make_window (TUI_CMD_WIN, DONT_BOX_WINDOW);
687 current_layout = layout_type;
688 }
689 }