2004-02-16 Andrew Cagney <cagney@redhat.com>
[external/binutils.git] / gdb / tui / tui-layout.c
1 /* TUI layout window management.
2
3    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
4    Foundation, Inc.
5
6    Contributed by Hewlett-Packard Company.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, USA.  */
24
25 #include "defs.h"
26 #include "command.h"
27 #include "symtab.h"
28 #include "frame.h"
29 #include "source.h"
30 #include <ctype.h>
31
32 #include "tui/tui.h"
33 #include "tui/tui-data.h"
34 #include "tui/tui-windata.h"
35 #include "tui/tui-wingeneral.h"
36 #include "tui/tui-stack.h"
37 #include "tui/tui-regs.h"
38 #include "tui/tui-win.h"
39 #include "tui/tui-winsource.h"
40 #include "tui/tui-disasm.h"
41
42 #include "gdb_string.h"
43 #include "gdb_curses.h"
44
45 /*******************************
46 ** Static Local Decls
47 ********************************/
48 static void show_layout (enum tui_layout_type);
49 static void init_gen_win_info (struct tui_gen_win_info *, enum tui_win_type, int, int, int, int);
50 static void init_and_make_win (void **, enum tui_win_type, int, int, int, int, int);
51 static void show_source_or_disasm_and_command (enum tui_layout_type);
52 static void make_source_or_disasm_window (struct tui_win_info * *, enum tui_win_type, int, int);
53 static void make_command_window (struct tui_win_info * *, int, int);
54 static void make_source_window (struct tui_win_info * *, int, int);
55 static void make_disasm_window (struct tui_win_info * *, int, int);
56 static void make_data_window (struct tui_win_info * *, int, int);
57 static void show_source_command (void);
58 static void show_disasm_command (void);
59 static void show_source_disasm_command (void);
60 static void show_data (enum tui_layout_type);
61 static enum tui_layout_type next_layout (void);
62 static enum tui_layout_type prev_layout (void);
63 static void tui_layout_command (char *, int);
64 static void tui_toggle_layout_command (char *, int);
65 static void tui_toggle_split_layout_command (char *, int);
66 static CORE_ADDR extract_display_start_addr (void);
67 static void tui_handle_xdb_layout (struct tui_layout_def *);
68
69
70 /***************************************
71 ** DEFINITIONS
72 ***************************************/
73
74 #define LAYOUT_USAGE     "Usage: layout prev | next | <layout_name> \n"
75
76 /* Show the screen layout defined.  */
77 static void
78 show_layout (enum tui_layout_type layout)
79 {
80   enum tui_layout_type cur_layout = tui_current_layout ();
81
82   if (layout != cur_layout)
83     {
84       /*
85          ** Since the new layout may cause changes in window size, we
86          ** should free the content and reallocate on next display of
87          ** source/asm
88        */
89       tui_free_all_source_wins_content ();
90       tui_clear_source_windows ();
91       if (layout == SRC_DATA_COMMAND || layout == DISASSEM_DATA_COMMAND)
92         {
93           show_data (layout);
94           tui_refresh_all (tui_win_list);
95         }
96       else
97         {
98           /* First make the current layout be invisible */
99           tui_make_all_invisible ();
100           tui_make_invisible (tui_locator_win_info_ptr ());
101
102           switch (layout)
103             {
104               /* Now show the new layout */
105             case SRC_COMMAND:
106               show_source_command ();
107               tui_add_to_source_windows (TUI_SRC_WIN);
108               break;
109             case DISASSEM_COMMAND:
110               show_disasm_command ();
111               tui_add_to_source_windows (TUI_DISASM_WIN);
112               break;
113             case SRC_DISASSEM_COMMAND:
114               show_source_disasm_command ();
115               tui_add_to_source_windows (TUI_SRC_WIN);
116               tui_add_to_source_windows (TUI_DISASM_WIN);
117               break;
118             default:
119               break;
120             }
121         }
122     }
123 }
124
125
126 /* Function to set the layout to SRC_COMMAND, DISASSEM_COMMAND,
127    SRC_DISASSEM_COMMAND, SRC_DATA_COMMAND, or DISASSEM_DATA_COMMAND.
128    If the layout is SRC_DATA_COMMAND, DISASSEM_DATA_COMMAND, or
129    UNDEFINED_LAYOUT, then the data window is populated according to
130    regs_display_type.  */
131 enum tui_status
132 tui_set_layout (enum tui_layout_type layout_type,
133                 enum tui_register_display_type regs_display_type)
134 {
135   enum tui_status status = TUI_SUCCESS;
136
137   if (layout_type != UNDEFINED_LAYOUT || regs_display_type != TUI_UNDEFINED_REGS)
138     {
139       enum tui_layout_type cur_layout = tui_current_layout (), new_layout = UNDEFINED_LAYOUT;
140       int regs_populate = FALSE;
141       CORE_ADDR addr = extract_display_start_addr ();
142       struct tui_win_info * new_win_with_focus = (struct tui_win_info *) NULL;
143       struct tui_win_info * win_with_focus = tui_win_with_focus ();
144       struct tui_layout_def * layout_def = tui_layout_def ();
145
146
147       if (layout_type == UNDEFINED_LAYOUT &&
148           regs_display_type != TUI_UNDEFINED_REGS)
149         {
150           if (cur_layout == SRC_DISASSEM_COMMAND)
151             new_layout = DISASSEM_DATA_COMMAND;
152           else if (cur_layout == SRC_COMMAND || cur_layout == SRC_DATA_COMMAND)
153             new_layout = SRC_DATA_COMMAND;
154           else if (cur_layout == DISASSEM_COMMAND ||
155                    cur_layout == DISASSEM_DATA_COMMAND)
156             new_layout = DISASSEM_DATA_COMMAND;
157         }
158       else
159         new_layout = layout_type;
160
161       regs_populate = (new_layout == SRC_DATA_COMMAND ||
162                       new_layout == DISASSEM_DATA_COMMAND ||
163                       regs_display_type != TUI_UNDEFINED_REGS);
164       if (new_layout != cur_layout || regs_display_type != TUI_UNDEFINED_REGS)
165         {
166           if (new_layout != cur_layout)
167             {
168               show_layout (new_layout);
169               /*
170                  ** Now determine where focus should be
171                */
172               if (win_with_focus != TUI_CMD_WIN)
173                 {
174                   switch (new_layout)
175                     {
176                     case SRC_COMMAND:
177                       tui_set_win_focus_to (TUI_SRC_WIN);
178                       layout_def->display_mode = SRC_WIN;
179                       layout_def->split = FALSE;
180                       break;
181                     case DISASSEM_COMMAND:
182                       /* the previous layout was not showing
183                          ** code. this can happen if there is no
184                          ** source available:
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                       addr = tui_get_begin_asm_address ();
190                       tui_set_win_focus_to (TUI_DISASM_WIN);
191                       layout_def->display_mode = DISASSEM_WIN;
192                       layout_def->split = FALSE;
193                       break;
194                     case SRC_DISASSEM_COMMAND:
195                       /* the previous layout was not showing
196                          ** code. this can happen if there is no
197                          ** source available:
198                          ** 1. if the source file is in another dir OR
199                          ** 2. if target was compiled without -g
200                          ** We still want to show the assembly though!
201                        */
202                       addr = tui_get_begin_asm_address ();
203                       if (win_with_focus == TUI_SRC_WIN)
204                         tui_set_win_focus_to (TUI_SRC_WIN);
205                       else
206                         tui_set_win_focus_to (TUI_DISASM_WIN);
207                       layout_def->split = TRUE;
208                       break;
209                     case SRC_DATA_COMMAND:
210                       if (win_with_focus != TUI_DATA_WIN)
211                         tui_set_win_focus_to (TUI_SRC_WIN);
212                       else
213                         tui_set_win_focus_to (TUI_DATA_WIN);
214                       layout_def->display_mode = SRC_WIN;
215                       layout_def->split = FALSE;
216                       break;
217                     case DISASSEM_DATA_COMMAND:
218                       /* the previous layout was not showing
219                          ** code. this can happen if there is no
220                          ** source available:
221                          ** 1. if the source file is in another dir OR
222                          ** 2. if target was compiled without -g
223                          ** We still want to show the assembly though!
224                        */
225                       addr = tui_get_begin_asm_address ();
226                       if (win_with_focus != TUI_DATA_WIN)
227                         tui_set_win_focus_to (TUI_DISASM_WIN);
228                       else
229                         tui_set_win_focus_to (TUI_DATA_WIN);
230                       layout_def->display_mode = DISASSEM_WIN;
231                       layout_def->split = FALSE;
232                       break;
233                     default:
234                       break;
235                     }
236                 }
237               if (new_win_with_focus != (struct tui_win_info *) NULL)
238                 tui_set_win_focus_to (new_win_with_focus);
239               /*
240                  ** Now update the window content
241                */
242               if (!regs_populate &&
243                   (new_layout == SRC_DATA_COMMAND ||
244                    new_layout == DISASSEM_DATA_COMMAND))
245                 tui_display_all_data ();
246
247               tui_update_source_windows_with_addr (addr);
248             }
249           if (regs_populate)
250             {
251               layout_def->regs_display_type =
252                 (regs_display_type == TUI_UNDEFINED_REGS ?
253                  TUI_GENERAL_REGS : regs_display_type);
254               tui_show_registers (layout_def->regs_display_type);
255             }
256         }
257     }
258   else
259     status = TUI_FAILURE;
260
261   return status;
262 }
263
264 /* Add the specified window to the layout in a logical way.  This
265    means setting up the most logical layout given the window to be
266    added.  */
267 void
268 tui_add_win_to_layout (enum tui_win_type type)
269 {
270   enum tui_layout_type cur_layout = tui_current_layout ();
271
272   switch (type)
273     {
274     case SRC_WIN:
275       if (cur_layout != SRC_COMMAND &&
276           cur_layout != SRC_DISASSEM_COMMAND &&
277           cur_layout != SRC_DATA_COMMAND)
278         {
279           tui_clear_source_windows_detail ();
280           if (cur_layout == DISASSEM_DATA_COMMAND)
281             show_layout (SRC_DATA_COMMAND);
282           else
283             show_layout (SRC_COMMAND);
284         }
285       break;
286     case DISASSEM_WIN:
287       if (cur_layout != DISASSEM_COMMAND &&
288           cur_layout != SRC_DISASSEM_COMMAND &&
289           cur_layout != DISASSEM_DATA_COMMAND)
290         {
291           tui_clear_source_windows_detail ();
292           if (cur_layout == SRC_DATA_COMMAND)
293             show_layout (DISASSEM_DATA_COMMAND);
294           else
295             show_layout (DISASSEM_COMMAND);
296         }
297       break;
298     case DATA_WIN:
299       if (cur_layout != SRC_DATA_COMMAND &&
300           cur_layout != DISASSEM_DATA_COMMAND)
301         {
302           if (cur_layout == DISASSEM_COMMAND)
303             show_layout (DISASSEM_DATA_COMMAND);
304           else
305             show_layout (SRC_DATA_COMMAND);
306         }
307       break;
308     default:
309       break;
310     }
311 }
312
313
314 /* Answer the height of a window.  If it hasn't been created yet,
315    answer what the height of a window would be based upon its type and
316    the layout.  */
317 int
318 tui_default_win_height (enum tui_win_type type, enum tui_layout_type layout)
319 {
320   int h;
321
322   if (tui_win_list[type] != (struct tui_win_info *) NULL)
323     h = tui_win_list[type]->generic.height;
324   else
325     {
326       switch (layout)
327         {
328         case SRC_COMMAND:
329         case DISASSEM_COMMAND:
330           if (TUI_CMD_WIN == NULL)
331             h = tui_term_height () / 2;
332           else
333             h = tui_term_height () - TUI_CMD_WIN->generic.height;
334           break;
335         case SRC_DISASSEM_COMMAND:
336         case SRC_DATA_COMMAND:
337         case DISASSEM_DATA_COMMAND:
338           if (TUI_CMD_WIN == NULL)
339             h = tui_term_height () / 3;
340           else
341             h = (tui_term_height () - TUI_CMD_WIN->generic.height) / 2;
342           break;
343         default:
344           h = 0;
345           break;
346         }
347     }
348
349   return h;
350 }
351
352
353 /* Answer the height of a window.  If it hasn't been created yet,
354    answer what the height of a window would be based upon its type and
355    the layout.  */
356 int
357 tui_default_win_viewport_height (enum tui_win_type type,
358                                  enum tui_layout_type layout)
359 {
360   int h;
361
362   h = tui_default_win_height (type, layout);
363
364   if (tui_win_list[type] == TUI_CMD_WIN)
365     h -= 1;
366   else
367     h -= 2;
368
369   return h;
370 }
371
372
373 /* Function to initialize gdb commands, for tui window layout
374    manipulation.  */
375 void
376 _initialize_tui_layout (void)
377 {
378   add_com ("layout", class_tui, tui_layout_command,
379            "Change the layout of windows.\n\
380 Usage: layout prev | next | <layout_name> \n\
381 Layout names are:\n\
382    src   : Displays source and command windows.\n\
383    asm   : Displays disassembly and command windows.\n\
384    split : Displays source, disassembly and command windows.\n\
385    regs  : Displays register window. If existing layout\n\
386            is source/command or assembly/command, the \n\
387            register window is displayed. If the\n\
388            source/assembly/command (split) is displayed, \n\
389            the register window is displayed with \n\
390            the window that has current logical focus.\n");
391   if (xdb_commands)
392     {
393       add_com ("td", class_tui, tui_toggle_layout_command,
394                "Toggle between Source/Command and Disassembly/Command layouts.\n");
395       add_com ("ts", class_tui, tui_toggle_split_layout_command,
396                "Toggle between Source/Command or Disassembly/Command and \n\
397 Source/Disassembly/Command layouts.\n");
398     }
399 }
400
401
402 /*************************
403 ** STATIC LOCAL FUNCTIONS
404 **************************/
405
406
407 /* Function to set the layout to SRC, ASM, SPLIT, NEXT, PREV, DATA,
408    REGS, $REGS, $GREGS, $FREGS, $SREGS.  */
409 enum tui_status
410 tui_set_layout_for_display_command (const char *layout_name)
411 {
412   enum tui_status status = TUI_SUCCESS;
413
414   if (layout_name != (char *) NULL)
415     {
416       int i;
417       char *buf_ptr;
418       enum tui_layout_type new_layout = UNDEFINED_LAYOUT;
419       enum tui_register_display_type dpy_type = TUI_UNDEFINED_REGS;
420       enum tui_layout_type cur_layout = tui_current_layout ();
421
422       buf_ptr = (char *) xstrdup (layout_name);
423       for (i = 0; (i < strlen (layout_name)); i++)
424         buf_ptr[i] = toupper (buf_ptr[i]);
425
426       /* First check for ambiguous input */
427       if (strlen (buf_ptr) <= 1 && (*buf_ptr == 'S' || *buf_ptr == '$'))
428         {
429           warning ("Ambiguous command input.\n");
430           status = TUI_FAILURE;
431         }
432       else
433         {
434           if (subset_compare (buf_ptr, "SRC"))
435             new_layout = SRC_COMMAND;
436           else if (subset_compare (buf_ptr, "ASM"))
437             new_layout = DISASSEM_COMMAND;
438           else if (subset_compare (buf_ptr, "SPLIT"))
439             new_layout = SRC_DISASSEM_COMMAND;
440           else if (subset_compare (buf_ptr, "REGS") ||
441                    subset_compare (buf_ptr, TUI_GENERAL_SPECIAL_REGS_NAME) ||
442                    subset_compare (buf_ptr, TUI_GENERAL_REGS_NAME) ||
443                    subset_compare (buf_ptr, TUI_FLOAT_REGS_NAME) ||
444                    subset_compare (buf_ptr, TUI_SPECIAL_REGS_NAME))
445             {
446               if (cur_layout == SRC_COMMAND || cur_layout == SRC_DATA_COMMAND)
447                 new_layout = SRC_DATA_COMMAND;
448               else
449                 new_layout = DISASSEM_DATA_COMMAND;
450
451 /* could ifdef out the following code. when compile with -z, there are null 
452    pointer references that cause a core dump if 'layout regs' is the first 
453    layout command issued by the user. HP has asked us to hook up this code 
454    - edie epstein
455  */
456               if (subset_compare (buf_ptr, TUI_FLOAT_REGS_NAME))
457                 {
458                   if (TUI_DATA_WIN->detail.data_display_info.regs_display_type !=
459                       TUI_SFLOAT_REGS &&
460                       TUI_DATA_WIN->detail.data_display_info.regs_display_type !=
461                       TUI_DFLOAT_REGS)
462                     dpy_type = TUI_SFLOAT_REGS;
463                   else
464                     dpy_type =
465                       TUI_DATA_WIN->detail.data_display_info.regs_display_type;
466                 }
467               else if (subset_compare (buf_ptr,
468                                       TUI_GENERAL_SPECIAL_REGS_NAME))
469                 dpy_type = TUI_GENERAL_AND_SPECIAL_REGS;
470               else if (subset_compare (buf_ptr, TUI_GENERAL_REGS_NAME))
471                 dpy_type = TUI_GENERAL_REGS;
472               else if (subset_compare (buf_ptr, TUI_SPECIAL_REGS_NAME))
473                 dpy_type = TUI_SPECIAL_REGS;
474               else if (TUI_DATA_WIN)
475                 {
476                   if (TUI_DATA_WIN->detail.data_display_info.regs_display_type !=
477                       TUI_UNDEFINED_REGS)
478                     dpy_type =
479                       TUI_DATA_WIN->detail.data_display_info.regs_display_type;
480                   else
481                     dpy_type = TUI_GENERAL_REGS;
482                 }
483
484 /* end of potential ifdef 
485  */
486
487 /* if ifdefed out code above, then assume that the user wishes to display the 
488    general purpose registers 
489  */
490
491 /*              dpy_type = TUI_GENERAL_REGS; 
492  */
493             }
494           else if (subset_compare (buf_ptr, "NEXT"))
495             new_layout = next_layout ();
496           else if (subset_compare (buf_ptr, "PREV"))
497             new_layout = prev_layout ();
498           else
499             status = TUI_FAILURE;
500           xfree (buf_ptr);
501
502           tui_set_layout (new_layout, dpy_type);
503         }
504     }
505   else
506     status = TUI_FAILURE;
507
508   return status;
509 }
510
511
512 static CORE_ADDR
513 extract_display_start_addr (void)
514 {
515   enum tui_layout_type cur_layout = tui_current_layout ();
516   CORE_ADDR addr;
517   CORE_ADDR pc;
518   struct symtab_and_line cursal = get_current_source_symtab_and_line ();
519
520   switch (cur_layout)
521     {
522     case SRC_COMMAND:
523     case SRC_DATA_COMMAND:
524       find_line_pc (cursal.symtab,
525                     TUI_SRC_WIN->detail.source_info.start_line_or_addr.line_no,
526                     &pc);
527       addr = pc;
528       break;
529     case DISASSEM_COMMAND:
530     case SRC_DISASSEM_COMMAND:
531     case DISASSEM_DATA_COMMAND:
532       addr = TUI_DISASM_WIN->detail.source_info.start_line_or_addr.addr;
533       break;
534     default:
535       addr = 0;
536       break;
537     }
538
539   return addr;
540 }
541
542
543 static void
544 tui_handle_xdb_layout (struct tui_layout_def * layout_def)
545 {
546   if (layout_def->split)
547     {
548       tui_set_layout (SRC_DISASSEM_COMMAND, TUI_UNDEFINED_REGS);
549       tui_set_win_focus_to (tui_win_list[layout_def->display_mode]);
550     }
551   else
552     {
553       if (layout_def->display_mode == SRC_WIN)
554         tui_set_layout (SRC_COMMAND, TUI_UNDEFINED_REGS);
555       else
556         tui_set_layout (DISASSEM_DATA_COMMAND, layout_def->regs_display_type);
557     }
558 }
559
560
561 static void
562 tui_toggle_layout_command (char *arg, int from_tty)
563 {
564   struct tui_layout_def * layout_def = tui_layout_def ();
565
566   /* Make sure the curses mode is enabled.  */
567   tui_enable ();
568   if (layout_def->display_mode == SRC_WIN)
569     layout_def->display_mode = DISASSEM_WIN;
570   else
571     layout_def->display_mode = SRC_WIN;
572
573   if (!layout_def->split)
574     tui_handle_xdb_layout (layout_def);
575 }
576
577
578 static void
579 tui_toggle_split_layout_command (char *arg, int from_tty)
580 {
581   struct tui_layout_def * layout_def = tui_layout_def ();
582
583   /* Make sure the curses mode is enabled.  */
584   tui_enable ();
585   layout_def->split = (!layout_def->split);
586   tui_handle_xdb_layout (layout_def);
587 }
588
589
590 static void
591 tui_layout_command (char *arg, int from_tty)
592 {
593   /* Make sure the curses mode is enabled.  */
594   tui_enable ();
595
596   /* Switch to the selected layout.  */
597   if (tui_set_layout_for_display_command (arg) != TUI_SUCCESS)
598     warning ("Invalid layout specified.\n%s", LAYOUT_USAGE);
599
600 }
601
602 /* Answer the previous layout to cycle to.  */
603 static enum tui_layout_type
604 next_layout (void)
605 {
606   enum tui_layout_type new_layout;
607
608   new_layout = tui_current_layout ();
609   if (new_layout == UNDEFINED_LAYOUT)
610     new_layout = SRC_COMMAND;
611   else
612     {
613       new_layout++;
614       if (new_layout == UNDEFINED_LAYOUT)
615         new_layout = SRC_COMMAND;
616     }
617
618   return new_layout;
619 }
620
621
622 /* Answer the next layout to cycle to.  */
623 static enum tui_layout_type
624 prev_layout (void)
625 {
626   enum tui_layout_type new_layout;
627
628   new_layout = tui_current_layout ();
629   if (new_layout == SRC_COMMAND)
630     new_layout = DISASSEM_DATA_COMMAND;
631   else
632     {
633       new_layout--;
634       if (new_layout == UNDEFINED_LAYOUT)
635         new_layout = DISASSEM_DATA_COMMAND;
636     }
637
638   return new_layout;
639 }
640
641
642
643 static void
644 make_command_window (struct tui_win_info * * win_info_ptr, int height, int origin_y)
645 {
646   init_and_make_win ((void **) win_info_ptr,
647                    CMD_WIN,
648                    height,
649                    tui_term_width (),
650                    0,
651                    origin_y,
652                    DONT_BOX_WINDOW);
653
654   (*win_info_ptr)->can_highlight = FALSE;
655 }
656
657
658 /*
659    ** make_source_window().
660  */
661 static void
662 make_source_window (struct tui_win_info * * win_info_ptr, int height, int origin_y)
663 {
664   make_source_or_disasm_window (win_info_ptr, SRC_WIN, height, origin_y);
665
666   return;
667 }                               /* make_source_window */
668
669
670 /*
671    ** make_disasm_window().
672  */
673 static void
674 make_disasm_window (struct tui_win_info * * win_info_ptr, int height, int origin_y)
675 {
676   make_source_or_disasm_window (win_info_ptr, DISASSEM_WIN, height, origin_y);
677
678   return;
679 }                               /* make_disasm_window */
680
681
682 static void
683 make_data_window (struct tui_win_info * * win_info_ptr, int height, int origin_y)
684 {
685   init_and_make_win ((void **) win_info_ptr,
686                    DATA_WIN,
687                    height,
688                    tui_term_width (),
689                    0,
690                    origin_y,
691                    BOX_WINDOW);
692 }
693
694
695
696 /* Show the Source/Command layout.  */
697 static void
698 show_source_command (void)
699 {
700   show_source_or_disasm_and_command (SRC_COMMAND);
701 }
702
703
704 /* Show the Dissassem/Command layout.  */
705 static void
706 show_disasm_command (void)
707 {
708   show_source_or_disasm_and_command (DISASSEM_COMMAND);
709 }
710
711
712 /* Show the Source/Disassem/Command layout.  */
713 static void
714 show_source_disasm_command (void)
715 {
716   if (tui_current_layout () != SRC_DISASSEM_COMMAND)
717     {
718       int cmd_height, src_height, asm_height;
719
720       if (TUI_CMD_WIN != NULL)
721         cmd_height = TUI_CMD_WIN->generic.height;
722       else
723         cmd_height = tui_term_height () / 3;
724
725       src_height = (tui_term_height () - cmd_height) / 2;
726       asm_height = tui_term_height () - (src_height + cmd_height);
727
728       if (TUI_SRC_WIN == NULL)
729         make_source_window (&TUI_SRC_WIN, src_height, 0);
730       else
731         {
732           init_gen_win_info (&TUI_SRC_WIN->generic,
733                            TUI_SRC_WIN->generic.type,
734                            src_height,
735                            TUI_SRC_WIN->generic.width,
736                            TUI_SRC_WIN->detail.source_info.execution_info->width,
737                            0);
738           TUI_SRC_WIN->can_highlight = TRUE;
739           init_gen_win_info (TUI_SRC_WIN->detail.source_info.execution_info,
740                            EXEC_INFO_WIN,
741                            src_height,
742                            3,
743                            0,
744                            0);
745           tui_make_visible (&TUI_SRC_WIN->generic);
746           tui_make_visible (TUI_SRC_WIN->detail.source_info.execution_info);
747           TUI_SRC_WIN->detail.source_info.has_locator = FALSE;;
748         }
749       if (TUI_SRC_WIN != NULL)
750         {
751           struct tui_gen_win_info * locator = tui_locator_win_info_ptr ();
752
753           tui_show_source_content (TUI_SRC_WIN);
754           if (TUI_DISASM_WIN == NULL)
755             {
756               make_disasm_window (&TUI_DISASM_WIN, asm_height, src_height - 1);
757               init_and_make_win ((void **) & locator,
758                                LOCATOR_WIN,
759                                2 /* 1 */ ,
760                                tui_term_width (),
761                                0,
762                                (src_height + asm_height) - 1,
763                                DONT_BOX_WINDOW);
764             }
765           else
766             {
767               init_gen_win_info (locator,
768                                LOCATOR_WIN,
769                                2 /* 1 */ ,
770                                tui_term_width (),
771                                0,
772                                (src_height + asm_height) - 1);
773               TUI_DISASM_WIN->detail.source_info.has_locator = TRUE;
774               init_gen_win_info (
775                                 &TUI_DISASM_WIN->generic,
776                                 TUI_DISASM_WIN->generic.type,
777                                 asm_height,
778                                 TUI_DISASM_WIN->generic.width,
779                         TUI_DISASM_WIN->detail.source_info.execution_info->width,
780                                 src_height - 1);
781               init_gen_win_info (TUI_DISASM_WIN->detail.source_info.execution_info,
782                                EXEC_INFO_WIN,
783                                asm_height,
784                                3,
785                                0,
786                                src_height - 1);
787               TUI_DISASM_WIN->can_highlight = TRUE;
788               tui_make_visible (&TUI_DISASM_WIN->generic);
789               tui_make_visible (TUI_DISASM_WIN->detail.source_info.execution_info);
790             }
791           if (TUI_DISASM_WIN != NULL)
792             {
793               TUI_SRC_WIN->detail.source_info.has_locator = FALSE;
794               TUI_DISASM_WIN->detail.source_info.has_locator = TRUE;
795               tui_make_visible (locator);
796               tui_show_locator_content ();
797               tui_show_source_content (TUI_DISASM_WIN);
798
799               if (TUI_CMD_WIN == NULL)
800                 make_command_window (&TUI_CMD_WIN,
801                                     cmd_height,
802                                     tui_term_height () - cmd_height);
803               else
804                 {
805                   init_gen_win_info (&TUI_CMD_WIN->generic,
806                                    TUI_CMD_WIN->generic.type,
807                                    TUI_CMD_WIN->generic.height,
808                                    TUI_CMD_WIN->generic.width,
809                                    0,
810                                    TUI_CMD_WIN->generic.origin.y);
811                   TUI_CMD_WIN->can_highlight = FALSE;
812                   tui_make_visible (&TUI_CMD_WIN->generic);
813                 }
814               if (TUI_CMD_WIN != NULL)
815                 tui_refresh_win (&TUI_CMD_WIN->generic);
816             }
817         }
818       tui_set_current_layout_to (SRC_DISASSEM_COMMAND);
819     }
820 }
821
822
823 /* Show the Source/Data/Command or the Dissassembly/Data/Command
824    layout.  */
825 static void
826 show_data (enum tui_layout_type new_layout)
827 {
828   int total_height = (tui_term_height () - TUI_CMD_WIN->generic.height);
829   int src_height, data_height;
830   enum tui_win_type win_type;
831   struct tui_gen_win_info * locator = tui_locator_win_info_ptr ();
832
833
834   data_height = total_height / 2;
835   src_height = total_height - data_height;
836   tui_make_all_invisible ();
837   tui_make_invisible (locator);
838   make_data_window (&TUI_DATA_WIN, data_height, 0);
839   TUI_DATA_WIN->can_highlight = TRUE;
840   if (new_layout == SRC_DATA_COMMAND)
841     win_type = SRC_WIN;
842   else
843     win_type = DISASSEM_WIN;
844   if (tui_win_list[win_type] == NULL)
845     {
846       if (win_type == SRC_WIN)
847         make_source_window (&tui_win_list[win_type], src_height, data_height - 1);
848       else
849         make_disasm_window (&tui_win_list[win_type], src_height, data_height - 1);
850       init_and_make_win ((void **) & locator,
851                        LOCATOR_WIN,
852                        2 /* 1 */ ,
853                        tui_term_width (),
854                        0,
855                        total_height - 1,
856                        DONT_BOX_WINDOW);
857     }
858   else
859     {
860       init_gen_win_info (&tui_win_list[win_type]->generic,
861                        tui_win_list[win_type]->generic.type,
862                        src_height,
863                        tui_win_list[win_type]->generic.width,
864                    tui_win_list[win_type]->detail.source_info.execution_info->width,
865                        data_height - 1);
866       init_gen_win_info (tui_win_list[win_type]->detail.source_info.execution_info,
867                        EXEC_INFO_WIN,
868                        src_height,
869                        3,
870                        0,
871                        data_height - 1);
872       tui_make_visible (&tui_win_list[win_type]->generic);
873       tui_make_visible (tui_win_list[win_type]->detail.source_info.execution_info);
874       init_gen_win_info (locator,
875                        LOCATOR_WIN,
876                        2 /* 1 */ ,
877                        tui_term_width (),
878                        0,
879                        total_height - 1);
880     }
881   tui_win_list[win_type]->detail.source_info.has_locator = TRUE;
882   tui_make_visible (locator);
883   tui_show_locator_content ();
884   tui_add_to_source_windows (tui_win_list[win_type]);
885   tui_set_current_layout_to (new_layout);
886 }
887
888 /*
889    ** init_gen_win_info().
890  */
891 static void
892 init_gen_win_info (struct tui_gen_win_info * win_info, enum tui_win_type type,
893                  int height, int width, int origin_x, int origin_y)
894 {
895   int h = height;
896
897   win_info->type = type;
898   win_info->width = width;
899   win_info->height = h;
900   if (h > 1)
901     {
902       win_info->viewport_height = h - 1;
903       if (win_info->type != CMD_WIN)
904         win_info->viewport_height--;
905     }
906   else
907     win_info->viewport_height = 1;
908   win_info->origin.x = origin_x;
909   win_info->origin.y = origin_y;
910
911   return;
912 }                               /* init_gen_win_info */
913
914 /*
915    ** init_and_make_win().
916  */
917 static void
918 init_and_make_win (void ** win_info_ptr, enum tui_win_type win_type,
919                  int height, int width, int origin_x, int origin_y, int box_it)
920 {
921   void *opaque_win_info = *win_info_ptr;
922   struct tui_gen_win_info * generic;
923
924   if (opaque_win_info == NULL)
925     {
926       if (tui_win_is_auxillary (win_type))
927         opaque_win_info = (void *) tui_alloc_generic_win_info ();
928       else
929         opaque_win_info = (void *) tui_alloc_win_info (win_type);
930     }
931   if (tui_win_is_auxillary (win_type))
932     generic = (struct tui_gen_win_info *) opaque_win_info;
933   else
934     generic = &((struct tui_win_info *) opaque_win_info)->generic;
935
936   if (opaque_win_info != NULL)
937     {
938       init_gen_win_info (generic, win_type, height, width, origin_x, origin_y);
939       if (!tui_win_is_auxillary (win_type))
940         {
941           if (generic->type == CMD_WIN)
942             ((struct tui_win_info *) opaque_win_info)->can_highlight = FALSE;
943           else
944             ((struct tui_win_info *) opaque_win_info)->can_highlight = TRUE;
945         }
946       tui_make_window (generic, box_it);
947     }
948   *win_info_ptr = opaque_win_info;
949 }
950
951
952 static void
953 make_source_or_disasm_window (struct tui_win_info * * win_info_ptr, enum tui_win_type type,
954                              int height, int origin_y)
955 {
956   struct tui_gen_win_info * execution_info = (struct tui_gen_win_info *) NULL;
957
958   /*
959      ** Create the exeuction info window.
960    */
961   if (type == SRC_WIN)
962     execution_info = tui_source_exec_info_win_ptr ();
963   else
964     execution_info = tui_disassem_exec_info_win_ptr ();
965   init_and_make_win ((void **) & execution_info,
966                    EXEC_INFO_WIN,
967                    height,
968                    3,
969                    0,
970                    origin_y,
971                    DONT_BOX_WINDOW);
972   /*
973      ** Now create the source window.
974    */
975   init_and_make_win ((void **) win_info_ptr,
976                    type,
977                    height,
978                    tui_term_width () - execution_info->width,
979                    execution_info->width,
980                    origin_y,
981                    BOX_WINDOW);
982
983   (*win_info_ptr)->detail.source_info.execution_info = execution_info;
984 }
985
986
987 /* Show the Source/Command or the Disassem layout.   */
988 static void
989 show_source_or_disasm_and_command (enum tui_layout_type layout_type)
990 {
991   if (tui_current_layout () != layout_type)
992     {
993       struct tui_win_info * *win_info_ptr;
994       int src_height, cmd_height;
995       struct tui_gen_win_info * locator = tui_locator_win_info_ptr ();
996
997       if (TUI_CMD_WIN != NULL)
998         cmd_height = TUI_CMD_WIN->generic.height;
999       else
1000         cmd_height = tui_term_height () / 3;
1001       src_height = tui_term_height () - cmd_height;
1002
1003
1004       if (layout_type == SRC_COMMAND)
1005         win_info_ptr = &TUI_SRC_WIN;
1006       else
1007         win_info_ptr = &TUI_DISASM_WIN;
1008
1009       if ((*win_info_ptr) == NULL)
1010         {
1011           if (layout_type == SRC_COMMAND)
1012             make_source_window (win_info_ptr, src_height - 1, 0);
1013           else
1014             make_disasm_window (win_info_ptr, src_height - 1, 0);
1015           init_and_make_win ((void **) & locator,
1016                            LOCATOR_WIN,
1017                            2 /* 1 */ ,
1018                            tui_term_width (),
1019                            0,
1020                            src_height - 1,
1021                            DONT_BOX_WINDOW);
1022         }
1023       else
1024         {
1025           init_gen_win_info (locator,
1026                            LOCATOR_WIN,
1027                            2 /* 1 */ ,
1028                            tui_term_width (),
1029                            0,
1030                            src_height - 1);
1031           (*win_info_ptr)->detail.source_info.has_locator = TRUE;
1032           init_gen_win_info (
1033                             &(*win_info_ptr)->generic,
1034                             (*win_info_ptr)->generic.type,
1035                             src_height - 1,
1036                             (*win_info_ptr)->generic.width,
1037                       (*win_info_ptr)->detail.source_info.execution_info->width,
1038                             0);
1039           init_gen_win_info ((*win_info_ptr)->detail.source_info.execution_info,
1040                            EXEC_INFO_WIN,
1041                            src_height - 1,
1042                            3,
1043                            0,
1044                            0);
1045           (*win_info_ptr)->can_highlight = TRUE;
1046           tui_make_visible (&(*win_info_ptr)->generic);
1047           tui_make_visible ((*win_info_ptr)->detail.source_info.execution_info);
1048         }
1049       if ((*win_info_ptr) != NULL)
1050         {
1051           (*win_info_ptr)->detail.source_info.has_locator = TRUE;
1052           tui_make_visible (locator);
1053           tui_show_locator_content ();
1054           tui_show_source_content (*win_info_ptr);
1055
1056           if (TUI_CMD_WIN == NULL)
1057             {
1058               make_command_window (&TUI_CMD_WIN, cmd_height, src_height);
1059               tui_refresh_win (&TUI_CMD_WIN->generic);
1060             }
1061           else
1062             {
1063               init_gen_win_info (&TUI_CMD_WIN->generic,
1064                                TUI_CMD_WIN->generic.type,
1065                                TUI_CMD_WIN->generic.height,
1066                                TUI_CMD_WIN->generic.width,
1067                                TUI_CMD_WIN->generic.origin.x,
1068                                TUI_CMD_WIN->generic.origin.y);
1069               TUI_CMD_WIN->can_highlight = FALSE;
1070               tui_make_visible (&TUI_CMD_WIN->generic);
1071             }
1072         }
1073       tui_set_current_layout_to (layout_type);
1074     }
1075 }