1 /* TUI data manipulation routines.
3 Copyright (C) 1998-2019 Free Software Foundation, Inc.
5 Contributed by Hewlett-Packard Company.
7 This file is part of GDB.
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.
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.
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/>. */
25 #include "tui/tui-data.h"
26 #include "tui/tui-wingeneral.h"
27 #include "gdb_curses.h"
29 /****************************
30 ** GLOBAL DECLARATIONS
31 ****************************/
32 struct tui_win_info
*tui_win_list
[MAX_MAJOR_WINDOWS
];
34 /***************************
36 ****************************/
37 static enum tui_layout_type current_layout
= UNDEFINED_LAYOUT
;
38 static int term_height
, term_width
;
39 static struct tui_gen_win_info _locator
;
40 static struct tui_gen_win_info exec_info
[2];
41 static std::vector
<tui_win_info
*> source_windows
;
42 static struct tui_win_info
*win_with_focus
= NULL
;
43 static struct tui_layout_def layout_def
= {
44 SRC_WIN
, /* DISPLAY_MODE */
47 static int win_resized
= FALSE
;
50 /*********************************
51 ** Static function forward decls
52 **********************************/
53 static void free_content (tui_win_content
,
56 static void free_content_elements (tui_win_content
,
62 /*********************************
64 **********************************/
67 tui_win_is_auxillary (enum tui_win_type win_type
)
69 return (win_type
> MAX_MAJOR_WINDOWS
);
73 tui_set_win_highlight (struct tui_win_info
*win_info
,
77 win_info
->is_highlighted
= highlight
;
80 /******************************************
81 ** ACCESSORS & MUTATORS FOR PRIVATE DATA
82 ******************************************/
84 /* Answer a whether the terminal window has been resized or not. */
86 tui_win_resized (void)
92 /* Set a whether the terminal window has been resized or not. */
94 tui_set_win_resized_to (int resized
)
96 win_resized
= resized
;
100 /* Answer a pointer to the current layout definition. */
101 struct tui_layout_def
*
102 tui_layout_def (void)
108 /* Answer the window with the logical focus. */
109 struct tui_win_info
*
110 tui_win_with_focus (void)
112 return win_with_focus
;
116 /* Set the window that has the logical focus. */
118 tui_set_win_with_focus (struct tui_win_info
*win_info
)
120 win_with_focus
= win_info
;
124 /* Accessor for the current source window. Usually there is only one
125 source window (either source or disassembly), but both can be
126 displayed at the same time. */
127 std::vector
<tui_win_info
*> &
128 tui_source_windows ()
130 return source_windows
;
134 /* Clear the list of source windows. Usually there is only one source
135 window (either source or disassembly), but both can be displayed at
138 tui_clear_source_windows ()
140 source_windows
.clear ();
144 /* Clear the pertinant detail in the source windows. */
146 tui_clear_source_windows_detail ()
148 for (tui_win_info
*win
: tui_source_windows ())
149 win
->clear_detail ();
153 /* Add a window to the list of source windows. Usually there is only
154 one source window (either source or disassembly), but both can be
155 displayed at the same time. */
157 tui_add_to_source_windows (struct tui_win_info
*win_info
)
159 if (source_windows
.size () < 2)
160 source_windows
.push_back (win_info
);
163 /* See tui-data.h. */
166 tui_source_window_base::clear_detail ()
169 start_line_or_addr
.loa
= LOA_ADDRESS
;
170 start_line_or_addr
.u
.addr
= 0;
171 horizontal_offset
= 0;
174 /* See tui-data.h. */
177 tui_cmd_window::clear_detail ()
179 wmove (generic
.handle
, 0, 0);
182 /* See tui-data.h. */
185 tui_data_window::clear_detail ()
188 data_content_count
= 0;
190 regs_content_count
= 0;
191 regs_column_count
= 1;
192 display_regs
= false;
195 /* Accessor for the source execution info ptr. */
196 struct tui_gen_win_info
*
197 tui_source_exec_info_win_ptr (void)
199 return &exec_info
[0];
203 /* Accessor for the disassem execution info ptr. */
204 struct tui_gen_win_info
*
205 tui_disassem_exec_info_win_ptr (void)
207 return &exec_info
[1];
211 /* Accessor for the locator win info. Answers a pointer to the static
212 locator win info struct. */
213 struct tui_gen_win_info
*
214 tui_locator_win_info_ptr (void)
220 /* Accessor for the term_height. */
222 tui_term_height (void)
228 /* Mutator for the term height. */
230 tui_set_term_height_to (int h
)
236 /* Accessor for the term_width. */
238 tui_term_width (void)
244 /* Mutator for the term_width. */
246 tui_set_term_width_to (int w
)
252 /* Accessor for the current layout. */
254 tui_current_layout (void)
256 return current_layout
;
260 /* Mutator for the current layout. */
262 tui_set_current_layout_to (enum tui_layout_type new_layout
)
264 current_layout
= new_layout
;
268 /*****************************
269 ** OTHER PUBLIC FUNCTIONS
270 *****************************/
273 /* Answer the next window in the list, cycling back to the top if
275 struct tui_win_info
*
276 tui_next_win (struct tui_win_info
*cur_win
)
278 int type
= cur_win
->generic
.type
;
279 struct tui_win_info
*next_win
= NULL
;
281 if (cur_win
->generic
.type
== CMD_WIN
)
284 type
= cur_win
->generic
.type
+ 1;
285 while (type
!= cur_win
->generic
.type
&& (next_win
== NULL
))
287 if (tui_win_list
[type
]
288 && tui_win_list
[type
]->generic
.is_visible
)
289 next_win
= tui_win_list
[type
];
303 /* Answer the prev window in the list, cycling back to the bottom if
305 struct tui_win_info
*
306 tui_prev_win (struct tui_win_info
*cur_win
)
308 int type
= cur_win
->generic
.type
;
309 struct tui_win_info
*prev
= NULL
;
311 if (cur_win
->generic
.type
== SRC_WIN
)
314 type
= cur_win
->generic
.type
- 1;
315 while (type
!= cur_win
->generic
.type
&& (prev
== NULL
))
317 if (tui_win_list
[type
]
318 && tui_win_list
[type
]->generic
.is_visible
)
319 prev
= tui_win_list
[type
];
333 /* Answer the window represented by name. */
334 struct tui_win_info
*
335 tui_partial_win_by_name (const char *name
)
337 struct tui_win_info
*win_info
= NULL
;
343 while (i
< MAX_MAJOR_WINDOWS
&& win_info
== NULL
)
345 if (tui_win_list
[i
] != 0)
347 const char *cur_name
=
348 tui_win_name (&tui_win_list
[i
]->generic
);
350 if (strlen (name
) <= strlen (cur_name
)
351 && startswith (cur_name
, name
))
352 win_info
= tui_win_list
[i
];
362 /* Answer the name of the window. */
364 tui_win_name (const struct tui_gen_win_info
*win_info
)
366 const char *name
= NULL
;
368 switch (win_info
->type
)
377 name
= DISASSEM_NAME
;
392 tui_initialize_static_data (void)
394 tui_init_generic_part (tui_source_exec_info_win_ptr ());
395 tui_init_generic_part (tui_disassem_exec_info_win_ptr ());
396 tui_init_generic_part (tui_locator_win_info_ptr ());
400 struct tui_gen_win_info
*
401 tui_alloc_generic_win_info (void)
403 struct tui_gen_win_info
*win
= XNEW (struct tui_gen_win_info
);
405 tui_init_generic_part (win
);
412 tui_init_generic_part (struct tui_gen_win_info
*win
)
418 win
->viewport_height
=
420 win
->last_visible_line
= 0;
423 win
->content_in_use
=
424 win
->is_visible
= FALSE
;
429 /* init_content_element().
432 init_content_element (struct tui_win_element
*element
,
433 enum tui_win_type type
)
439 element
->which_element
.source
.line
= NULL
;
440 element
->which_element
.source
.line_or_addr
.loa
= LOA_LINE
;
441 element
->which_element
.source
.line_or_addr
.u
.line_no
= 0;
442 element
->which_element
.source
.is_exec_point
= FALSE
;
443 element
->which_element
.source
.has_break
= FALSE
;
446 tui_init_generic_part (&element
->which_element
.data_window
);
447 element
->which_element
.data_window
.type
= DATA_ITEM_WIN
;
448 element
->which_element
.data_window
.content
=
449 tui_alloc_content (1, DATA_ITEM_WIN
);
450 element
->which_element
.data_window
.content_size
= 1;
453 element
->which_element
.command
.line
= NULL
;
456 element
->which_element
.data
.name
= NULL
;
457 element
->which_element
.data
.type
= TUI_REGISTER
;
458 element
->which_element
.data
.item_no
= UNDEFINED_ITEM
;
459 element
->which_element
.data
.value
= NULL
;
460 element
->which_element
.data
.highlight
= FALSE
;
461 element
->which_element
.data
.content
= NULL
;
464 element
->which_element
.locator
.full_name
[0] =
465 element
->which_element
.locator
.proc_name
[0] = (char) 0;
466 element
->which_element
.locator
.line_no
= 0;
467 element
->which_element
.locator
.addr
= 0;
470 memset(element
->which_element
.simple_string
, ' ',
471 sizeof(element
->which_element
.simple_string
));
478 tui_win_info::tui_win_info (enum tui_win_type type
)
481 tui_init_generic_part (&generic
);
484 tui_source_window_base::tui_source_window_base (enum tui_win_type type
)
485 : tui_win_info (type
)
487 gdb_assert (type
== SRC_WIN
|| type
== DISASSEM_WIN
);
488 start_line_or_addr
.loa
= LOA_ADDRESS
;
489 start_line_or_addr
.u
.addr
= 0;
492 struct tui_win_info
*
493 tui_alloc_win_info (enum tui_win_type type
)
498 return new tui_source_window ();
501 return new tui_disasm_window ();
504 return new tui_data_window ();
507 return new tui_cmd_window ();
510 gdb_assert_not_reached (_("Unhandled window type"));
514 /* Allocates the content and elements in a block. */
516 tui_alloc_content (int num_elements
, enum tui_win_type type
)
518 tui_win_content content
;
519 struct tui_win_element
*element_block_ptr
;
522 content
= XNEWVEC (struct tui_win_element
*, num_elements
);
525 * All windows, except the data window, can allocate the
526 * elements in a chunk. The data window cannot because items
527 * can be added/removed from the data display by the user at any
530 if (type
!= DATA_WIN
)
532 element_block_ptr
= XNEWVEC (struct tui_win_element
, num_elements
);
533 for (i
= 0; i
< num_elements
; i
++)
535 content
[i
] = element_block_ptr
;
536 init_content_element (content
[i
], type
);
545 /* Adds the input number of elements to the windows's content. If no
546 content has been allocated yet, alloc_content() is called to do
547 this. The index of the first element added is returned, unless
548 there is a memory allocation error, in which case, (-1) is
551 tui_add_content_elements (struct tui_gen_win_info
*win_info
,
554 struct tui_win_element
*element_ptr
;
557 if (win_info
->content
== NULL
)
559 win_info
->content
= tui_alloc_content (num_elements
, win_info
->type
);
563 index_start
= win_info
->content_size
;
564 if (win_info
->content
!= NULL
)
566 for (i
= index_start
; (i
< num_elements
+ index_start
); i
++)
568 element_ptr
= XNEW (struct tui_win_element
);
569 win_info
->content
[i
] = element_ptr
;
570 init_content_element (element_ptr
, win_info
->type
);
571 win_info
->content_size
++;
578 tui_source_window_base::~tui_source_window_base ()
581 struct tui_gen_win_info
*generic_win
= execution_info
;
582 if (generic_win
!= NULL
)
584 tui_delete_win (generic_win
->handle
);
585 generic_win
->handle
= NULL
;
586 tui_free_win_content (generic_win
);
590 tui_data_window::~tui_data_window ()
592 if (generic
.content
!= NULL
)
594 tui_free_data_content (regs_content
, regs_content_count
);
596 regs_content_count
= 0;
597 tui_free_data_content (data_content
, data_content_count
);
599 data_content_count
= 0;
600 regs_column_count
= 1;
601 display_regs
= false;
602 generic
.content
= NULL
;
603 generic
.content_size
= 0;
607 tui_win_info::~tui_win_info ()
609 if (generic
.handle
!= NULL
)
611 tui_delete_win (generic
.handle
);
612 generic
.handle
= NULL
;
613 tui_free_win_content (&generic
);
616 xfree (generic
.title
);
621 tui_free_all_source_wins_content ()
623 for (tui_win_info
*win_info
: tui_source_windows ())
625 tui_free_win_content (&(win_info
->generic
));
626 tui_source_window_base
*base
= (tui_source_window_base
*) win_info
;
627 tui_free_win_content (base
->execution_info
);
633 tui_free_win_content (struct tui_gen_win_info
*win_info
)
635 if (win_info
->content
!= NULL
)
637 free_content (win_info
->content
,
638 win_info
->content_size
,
640 win_info
->content
= NULL
;
642 win_info
->content_size
= 0;
647 tui_free_data_content (tui_win_content content
,
652 /* Remember that data window content elements are of type struct
653 tui_gen_win_info *, each of which whose single element is a data
655 for (i
= 0; i
< content_size
; i
++)
657 struct tui_gen_win_info
*generic_win
658 = &content
[i
]->which_element
.data_window
;
660 if (generic_win
!= NULL
)
662 tui_delete_win (generic_win
->handle
);
663 generic_win
->handle
= NULL
;
664 tui_free_win_content (generic_win
);
667 free_content (content
,
673 /**********************************
674 ** LOCAL STATIC FUNCTIONS **
675 **********************************/
679 free_content (tui_win_content content
,
681 enum tui_win_type win_type
)
685 free_content_elements (content
, content_size
, win_type
);
691 /* free_content_elements().
694 free_content_elements (tui_win_content content
,
696 enum tui_win_type type
)
702 if (type
== DISASSEM_WIN
)
704 /* Free whole source block. */
705 xfree (content
[0]->which_element
.source
.line
);
709 for (i
= 0; i
< content_size
; i
++)
711 struct tui_win_element
*element
;
713 element
= content
[i
];
719 xfree (element
->which_element
.source
.line
);
725 /* Note that data elements are not allocated in
726 a single block, but individually, as
728 if (element
->which_element
.data
.type
!= TUI_REGISTER
)
729 xfree ((void *)element
->which_element
.data
.name
);
730 xfree (element
->which_element
.data
.value
);
731 xfree (element
->which_element
.data
.content
);
735 xfree (element
->which_element
.command
.line
);
743 if (type
!= DATA_WIN
&& type
!= DATA_ITEM_WIN
)
744 xfree (content
[0]); /* Free the element block. */