Introduce max_height method
[deliverable/binutils-gdb.git] / gdb / tui / tui-data.c
1 /* TUI data manipulation routines.
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 "symtab.h"
24 #include "tui/tui.h"
25 #include "tui/tui-data.h"
26 #include "tui/tui-wingeneral.h"
27 #include "gdb_curses.h"
28
29 /****************************
30 ** GLOBAL DECLARATIONS
31 ****************************/
32 struct tui_win_info *tui_win_list[MAX_MAJOR_WINDOWS];
33
34 /***************************
35 ** Private data
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_source_window_base *> 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 */
45 FALSE}; /* SPLIT */
46
47 static int win_resized = FALSE;
48
49
50 /*********************************
51 ** Static function forward decls
52 **********************************/
53 static void free_content (tui_win_content,
54 int,
55 enum tui_win_type);
56 static void free_content_elements (tui_win_content,
57 int,
58 enum tui_win_type);
59
60
61
62 /*********************************
63 ** PUBLIC FUNCTIONS
64 **********************************/
65
66 int
67 tui_win_is_auxillary (enum tui_win_type win_type)
68 {
69 return (win_type > MAX_MAJOR_WINDOWS);
70 }
71
72 void
73 tui_set_win_highlight (struct tui_win_info *win_info,
74 int highlight)
75 {
76 if (win_info != NULL)
77 win_info->is_highlighted = highlight;
78 }
79
80 /******************************************
81 ** ACCESSORS & MUTATORS FOR PRIVATE DATA
82 ******************************************/
83
84 /* Answer a whether the terminal window has been resized or not. */
85 int
86 tui_win_resized (void)
87 {
88 return win_resized;
89 }
90
91
92 /* Set a whether the terminal window has been resized or not. */
93 void
94 tui_set_win_resized_to (int resized)
95 {
96 win_resized = resized;
97 }
98
99
100 /* Answer a pointer to the current layout definition. */
101 struct tui_layout_def *
102 tui_layout_def (void)
103 {
104 return &layout_def;
105 }
106
107
108 /* Answer the window with the logical focus. */
109 struct tui_win_info *
110 tui_win_with_focus (void)
111 {
112 return win_with_focus;
113 }
114
115
116 /* Set the window that has the logical focus. */
117 void
118 tui_set_win_with_focus (struct tui_win_info *win_info)
119 {
120 win_with_focus = win_info;
121 }
122
123
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_source_window_base *> &
128 tui_source_windows ()
129 {
130 return source_windows;
131 }
132
133
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
136 the same time. */
137 void
138 tui_clear_source_windows ()
139 {
140 source_windows.clear ();
141 }
142
143
144 /* Clear the pertinant detail in the source windows. */
145 void
146 tui_clear_source_windows_detail ()
147 {
148 for (tui_source_window_base *win : tui_source_windows ())
149 win->clear_detail ();
150 }
151
152
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. */
156 void
157 tui_add_to_source_windows (struct tui_source_window_base *win_info)
158 {
159 if (source_windows.size () < 2)
160 source_windows.push_back (win_info);
161 }
162
163 /* See tui-data.h. */
164
165 void
166 tui_source_window_base::clear_detail ()
167 {
168 gdbarch = NULL;
169 start_line_or_addr.loa = LOA_ADDRESS;
170 start_line_or_addr.u.addr = 0;
171 horizontal_offset = 0;
172 }
173
174 /* See tui-data.h. */
175
176 void
177 tui_cmd_window::clear_detail ()
178 {
179 wmove (generic.handle, 0, 0);
180 }
181
182 /* See tui-data.h. */
183
184 void
185 tui_data_window::clear_detail ()
186 {
187 data_content = NULL;
188 data_content_count = 0;
189 regs_content = NULL;
190 regs_content_count = 0;
191 regs_column_count = 1;
192 display_regs = false;
193 }
194
195 /* Accessor for the source execution info ptr. */
196 struct tui_gen_win_info *
197 tui_source_exec_info_win_ptr (void)
198 {
199 return &exec_info[0];
200 }
201
202
203 /* Accessor for the disassem execution info ptr. */
204 struct tui_gen_win_info *
205 tui_disassem_exec_info_win_ptr (void)
206 {
207 return &exec_info[1];
208 }
209
210
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)
215 {
216 return &_locator;
217 }
218
219
220 /* Accessor for the term_height. */
221 int
222 tui_term_height (void)
223 {
224 return term_height;
225 }
226
227
228 /* Mutator for the term height. */
229 void
230 tui_set_term_height_to (int h)
231 {
232 term_height = h;
233 }
234
235
236 /* Accessor for the term_width. */
237 int
238 tui_term_width (void)
239 {
240 return term_width;
241 }
242
243
244 /* Mutator for the term_width. */
245 void
246 tui_set_term_width_to (int w)
247 {
248 term_width = w;
249 }
250
251
252 /* Accessor for the current layout. */
253 enum tui_layout_type
254 tui_current_layout (void)
255 {
256 return current_layout;
257 }
258
259
260 /* Mutator for the current layout. */
261 void
262 tui_set_current_layout_to (enum tui_layout_type new_layout)
263 {
264 current_layout = new_layout;
265 }
266
267
268 /*****************************
269 ** OTHER PUBLIC FUNCTIONS
270 *****************************/
271
272
273 /* Answer the next window in the list, cycling back to the top if
274 necessary. */
275 struct tui_win_info *
276 tui_next_win (struct tui_win_info *cur_win)
277 {
278 int type = cur_win->generic.type;
279 struct tui_win_info *next_win = NULL;
280
281 if (cur_win->generic.type == CMD_WIN)
282 type = SRC_WIN;
283 else
284 type = cur_win->generic.type + 1;
285 while (type != cur_win->generic.type && (next_win == NULL))
286 {
287 if (tui_win_list[type]
288 && tui_win_list[type]->generic.is_visible)
289 next_win = tui_win_list[type];
290 else
291 {
292 if (type == CMD_WIN)
293 type = SRC_WIN;
294 else
295 type++;
296 }
297 }
298
299 return next_win;
300 }
301
302
303 /* Answer the prev window in the list, cycling back to the bottom if
304 necessary. */
305 struct tui_win_info *
306 tui_prev_win (struct tui_win_info *cur_win)
307 {
308 int type = cur_win->generic.type;
309 struct tui_win_info *prev = NULL;
310
311 if (cur_win->generic.type == SRC_WIN)
312 type = CMD_WIN;
313 else
314 type = cur_win->generic.type - 1;
315 while (type != cur_win->generic.type && (prev == NULL))
316 {
317 if (tui_win_list[type]
318 && tui_win_list[type]->generic.is_visible)
319 prev = tui_win_list[type];
320 else
321 {
322 if (type == SRC_WIN)
323 type = CMD_WIN;
324 else
325 type--;
326 }
327 }
328
329 return prev;
330 }
331
332
333 /* Answer the window represented by name. */
334 struct tui_win_info *
335 tui_partial_win_by_name (const char *name)
336 {
337 struct tui_win_info *win_info = NULL;
338
339 if (name != NULL)
340 {
341 int i = 0;
342
343 while (i < MAX_MAJOR_WINDOWS && win_info == NULL)
344 {
345 if (tui_win_list[i] != 0)
346 {
347 const char *cur_name =
348 tui_win_name (&tui_win_list[i]->generic);
349
350 if (strlen (name) <= strlen (cur_name)
351 && startswith (cur_name, name))
352 win_info = tui_win_list[i];
353 }
354 i++;
355 }
356 }
357
358 return win_info;
359 }
360
361
362 /* Answer the name of the window. */
363 const char *
364 tui_win_name (const struct tui_gen_win_info *win_info)
365 {
366 const char *name = NULL;
367
368 switch (win_info->type)
369 {
370 case SRC_WIN:
371 name = SRC_NAME;
372 break;
373 case CMD_WIN:
374 name = CMD_NAME;
375 break;
376 case DISASSEM_WIN:
377 name = DISASSEM_NAME;
378 break;
379 case DATA_WIN:
380 name = DATA_NAME;
381 break;
382 default:
383 name = "";
384 break;
385 }
386
387 return name;
388 }
389
390
391 void
392 tui_initialize_static_data (void)
393 {
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 ());
397 }
398
399
400 struct tui_gen_win_info *
401 tui_alloc_generic_win_info (void)
402 {
403 struct tui_gen_win_info *win = XNEW (struct tui_gen_win_info);
404
405 tui_init_generic_part (win);
406
407 return win;
408 }
409
410
411 void
412 tui_init_generic_part (struct tui_gen_win_info *win)
413 {
414 win->width =
415 win->height =
416 win->origin.x =
417 win->origin.y =
418 win->viewport_height =
419 win->content_size =
420 win->last_visible_line = 0;
421 win->handle = NULL;
422 win->content = NULL;
423 win->content_in_use = FALSE;
424 win->is_visible = false;
425 win->title = 0;
426 }
427
428
429 /* init_content_element().
430 */
431 static void
432 init_content_element (struct tui_win_element *element,
433 enum tui_win_type type)
434 {
435 switch (type)
436 {
437 case SRC_WIN:
438 case DISASSEM_WIN:
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;
444 break;
445 case DATA_WIN:
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;
451 break;
452 case CMD_WIN:
453 element->which_element.command.line = NULL;
454 break;
455 case DATA_ITEM_WIN:
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;
462 break;
463 case LOCATOR_WIN:
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;
468 break;
469 case EXEC_INFO_WIN:
470 memset(element->which_element.simple_string, ' ',
471 sizeof(element->which_element.simple_string));
472 break;
473 default:
474 break;
475 }
476 }
477
478 tui_win_info::tui_win_info (enum tui_win_type type)
479 {
480 generic.type = type;
481 tui_init_generic_part (&generic);
482 }
483
484 tui_source_window_base::tui_source_window_base (enum tui_win_type type)
485 : tui_win_info (type)
486 {
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;
490 }
491
492 struct tui_win_info *
493 tui_alloc_win_info (enum tui_win_type type)
494 {
495 switch (type)
496 {
497 case SRC_WIN:
498 return new tui_source_window ();
499
500 case DISASSEM_WIN:
501 return new tui_disasm_window ();
502
503 case DATA_WIN:
504 return new tui_data_window ();
505
506 case CMD_WIN:
507 return new tui_cmd_window ();
508 }
509
510 gdb_assert_not_reached (_("Unhandled window type"));
511 }
512
513
514 /* Allocates the content and elements in a block. */
515 tui_win_content
516 tui_alloc_content (int num_elements, enum tui_win_type type)
517 {
518 tui_win_content content;
519 struct tui_win_element *element_block_ptr;
520 int i;
521
522 content = XNEWVEC (struct tui_win_element *, num_elements);
523
524 /*
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
528 * time.
529 */
530 if (type != DATA_WIN)
531 {
532 element_block_ptr = XNEWVEC (struct tui_win_element, num_elements);
533 for (i = 0; i < num_elements; i++)
534 {
535 content[i] = element_block_ptr;
536 init_content_element (content[i], type);
537 element_block_ptr++;
538 }
539 }
540
541 return content;
542 }
543
544
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
549 returned. */
550 int
551 tui_add_content_elements (struct tui_gen_win_info *win_info,
552 int num_elements)
553 {
554 struct tui_win_element *element_ptr;
555 int i, index_start;
556
557 if (win_info->content == NULL)
558 {
559 win_info->content = tui_alloc_content (num_elements, win_info->type);
560 index_start = 0;
561 }
562 else
563 index_start = win_info->content_size;
564 if (win_info->content != NULL)
565 {
566 for (i = index_start; (i < num_elements + index_start); i++)
567 {
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++;
572 }
573 }
574
575 return index_start;
576 }
577
578 tui_source_window_base::~tui_source_window_base ()
579 {
580 xfree (fullname);
581 struct tui_gen_win_info *generic_win = execution_info;
582 if (generic_win != NULL)
583 {
584 tui_delete_win (generic_win->handle);
585 generic_win->handle = NULL;
586 tui_free_win_content (generic_win);
587 }
588 }
589
590 tui_data_window::~tui_data_window ()
591 {
592 if (generic.content != NULL)
593 {
594 tui_free_data_content (regs_content, regs_content_count);
595 regs_content = NULL;
596 regs_content_count = 0;
597 tui_free_data_content (data_content, data_content_count);
598 data_content = NULL;
599 data_content_count = 0;
600 regs_column_count = 1;
601 display_regs = false;
602 generic.content = NULL;
603 generic.content_size = 0;
604 }
605 }
606
607 tui_win_info::~tui_win_info ()
608 {
609 if (generic.handle != NULL)
610 {
611 tui_delete_win (generic.handle);
612 generic.handle = NULL;
613 tui_free_win_content (&generic);
614 }
615 if (generic.title)
616 xfree (generic.title);
617 }
618
619
620 void
621 tui_free_all_source_wins_content ()
622 {
623 for (tui_source_window_base *win_info : tui_source_windows ())
624 {
625 tui_free_win_content (&(win_info->generic));
626 tui_free_win_content (win_info->execution_info);
627 }
628 }
629
630
631 void
632 tui_free_win_content (struct tui_gen_win_info *win_info)
633 {
634 if (win_info->content != NULL)
635 {
636 free_content (win_info->content,
637 win_info->content_size,
638 win_info->type);
639 win_info->content = NULL;
640 }
641 win_info->content_size = 0;
642 }
643
644
645 void
646 tui_free_data_content (tui_win_content content,
647 int content_size)
648 {
649 int i;
650
651 /* Remember that data window content elements are of type struct
652 tui_gen_win_info *, each of which whose single element is a data
653 element. */
654 for (i = 0; i < content_size; i++)
655 {
656 struct tui_gen_win_info *generic_win
657 = &content[i]->which_element.data_window;
658
659 if (generic_win != NULL)
660 {
661 tui_delete_win (generic_win->handle);
662 generic_win->handle = NULL;
663 tui_free_win_content (generic_win);
664 }
665 }
666 free_content (content,
667 content_size,
668 DATA_WIN);
669 }
670
671
672 /**********************************
673 ** LOCAL STATIC FUNCTIONS **
674 **********************************/
675
676
677 static void
678 free_content (tui_win_content content,
679 int content_size,
680 enum tui_win_type win_type)
681 {
682 if (content != NULL)
683 {
684 free_content_elements (content, content_size, win_type);
685 xfree (content);
686 }
687 }
688
689
690 /* free_content_elements().
691 */
692 static void
693 free_content_elements (tui_win_content content,
694 int content_size,
695 enum tui_win_type type)
696 {
697 if (content != NULL)
698 {
699 int i;
700
701 if (type == DISASSEM_WIN)
702 {
703 /* Free whole source block. */
704 xfree (content[0]->which_element.source.line);
705 }
706 else
707 {
708 for (i = 0; i < content_size; i++)
709 {
710 struct tui_win_element *element;
711
712 element = content[i];
713 if (element != NULL)
714 {
715 switch (type)
716 {
717 case SRC_WIN:
718 xfree (element->which_element.source.line);
719 break;
720 case DATA_WIN:
721 xfree (element);
722 break;
723 case DATA_ITEM_WIN:
724 /* Note that data elements are not allocated in
725 a single block, but individually, as
726 needed. */
727 if (element->which_element.data.type != TUI_REGISTER)
728 xfree ((void *)element->which_element.data.name);
729 xfree (element->which_element.data.value);
730 xfree (element->which_element.data.content);
731 xfree (element);
732 break;
733 case CMD_WIN:
734 xfree (element->which_element.command.line);
735 break;
736 default:
737 break;
738 }
739 }
740 }
741 }
742 if (type != DATA_WIN && type != DATA_ITEM_WIN)
743 xfree (content[0]); /* Free the element block. */
744 }
745 }
This page took 0.0442 seconds and 4 git commands to generate.