home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / maininit.c < prev    next >
C/C++ Source or Header  |  1992-10-09  |  8KB  |  275 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    maininit.c (Main Initialize)
  6.  * Purpose:    Start of the saoimage package
  7.  * Program:    main()
  8.  * Subroutine:    crash_on_error()
  9.  * Xlib calls:    XSetErrorHandler(), XCloseDisplay(), XGetErrorText()
  10.  * UNIX calls:    setrlimit(), ieee_handler(), abrupt_underflow_()
  11.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  12.  *        You may do anything you like with this file except remove
  13.  *        this copyright.  The Smithsonian Astrophysical Observatory
  14.  *        makes no representations about the suitability of this
  15.  *        software for any purpose.  It is provided "as is" without
  16.  *        express or implied warranty.
  17.  * Modified:    {0} Michael VanHilst    initial version           9 May 1989
  18.  *              {1} MVH BSDonly strings.h compatability           19 Feb 1990
  19.  *        {n} <who> -- <does what> -- <when>
  20.  */
  21.  
  22. #include <stdio.h>        /* stderr, FILE, NULL, etc. */
  23.  
  24. #ifndef VMS
  25. #ifdef SYSV
  26. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  27. #else
  28. #include <strings.h>        /* strlen, etc. for unenlightened BSD's */
  29. #endif
  30. #else
  31. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  32. #endif
  33.  
  34. #include <sys/time.h>        /* struct timeval used in <sys/resource.h> */
  35. #include <X11/Xlib.h>        /* X window stuff */
  36. #include <X11/Xutil.h>        /* X window manager stuff */
  37. #include "hfiles/constant.h"    /* define codes */
  38. #include "hfiles/define.h"    /* YES, NO, MIN, MAX and more */
  39. #include "hfiles/struct.h"    /* declare structure types */
  40. #ifdef DEBUG
  41. #ifdef SUN            /* DEBUG and SUN(OS4.xx) */
  42. #include <floatingpoint.h>    /* SIGFPE_ABORT ieee exception switch */
  43. #endif
  44. #else
  45. #ifndef VMS            /* NOT DEBUG AND NOT VMS */
  46. #ifndef SYSV
  47. #include <sys/resource.h>    /* rlimit, RLIMIT_CORE for setrlimit */
  48. #endif
  49. #endif
  50. #endif
  51.  
  52. char *RevMsg = "release 1.07     9 October 1992";
  53. int init_done = 0;
  54.  
  55. /* declare and initialize parameter structs */
  56. #include "defs/control.def"
  57. #include "defs/color.def"
  58. #include "defs/image.def"
  59. #include "defs/dispbox.def"
  60. #include "defs/magnibox.def"
  61. #include "defs/colorbox.def"
  62. #include "defs/graphbox.def"
  63. #include "defs/panbox.def"
  64. #include "defs/btnbox.def"
  65. #include "defs/desktop.def"
  66. #include "defs/cursor.def"
  67.  
  68. /* declare uninitialized structs */
  69. struct bufferRec buffer;
  70. struct coordRec coord;
  71. Display *display;        /* display connection */
  72.  
  73. /*
  74.  * Program:    main
  75.  * Purpose:    Get things started, then call event_loop
  76.  * Xlib calls:    XSetErrorHandler()
  77.  * UNIX calls:    setrlimit()
  78.  */
  79. main(argc, argv)
  80.   int argc;
  81.   char **argv;
  82. {
  83.   static void init_params(), init_packages();
  84.   void crash_on_error(), control_event_loop();
  85.   void say_goodbye();
  86.   
  87. #ifdef DEBUG
  88.   /* install special error handler to force crash so we can trace problem */
  89.   XSetErrorHandler(crash_on_error);
  90. #ifdef SUN
  91.   /* Under SunOS4 we can force floating point exceptions to report errors */
  92.   ieee_handler("set", "common", SIGFPE_ABORT);
  93.   abrupt_underflow_();
  94. #endif
  95. #else
  96. #ifndef VMS
  97. #ifndef SYSV
  98.   {
  99.     struct rlimit rlp;    /* l: UNIX resourse limit structure */
  100.     /* disable core dumping */
  101.     rlp.rlim_cur = 0;
  102.     rlp.rlim_max = 0;
  103.     setrlimit(RLIMIT_CORE, &rlp);
  104.   }
  105. #endif
  106. #endif
  107. #endif
  108.   /* set up, read, and check parameters */
  109.   init_params(argc, argv);
  110.   if( control.verbose ) {
  111.     (void)printf("\n  Smithsonian Astrophysical Observatory image utility\n");
  112.     (void)printf("  %s\n\n", RevMsg);
  113.   }
  114.   init_packages(argc, argv);
  115.   /* CALL MAIN EVENT LOOP (uses extern.h) */
  116.   init_done = 1;
  117.   control_event_loop();
  118.   say_goodbye(0);
  119. }
  120.  
  121. /*
  122.  * Subroutine:    say_goodbye
  123.  * Purpose:    Free server resources before exiting
  124.  * Xlib calls:    XCloseDisplay()
  125.  */
  126. void say_goodbye ( code )
  127.      int code;        /* i: code to identify exit */
  128. {
  129.   void free_gc();
  130.  
  131.   if( desktop.display != NULL ) {
  132.     free_gc();
  133.     XCloseDisplay(desktop.display);
  134.   }
  135.   exit(code);
  136. }
  137.  
  138. /*
  139.  * Subroutine:    init_params
  140.  * Purpose:    Initialize parameters in the records
  141.  * Note:    Resource or default file not yet used
  142.  */
  143. static void init_params ( argc, argv )
  144.      int argc;
  145.      char **argv;
  146. {
  147.   char *name;        /* l: both flag for init and return display name */
  148.   int parse_stat;
  149.   static void init_server();
  150.   int parse_cmdline(), check_image();
  151.   void say_goodbye(), init_connections();
  152.  
  153.   name = NULL;
  154.   /* get parameters from the command line (uses extern.h) */
  155.   if( (parse_stat = parse_cmdline(argc, argv, &name)) < 0 )
  156.     say_goodbye(1);
  157.   /* check image parameters for consistency (uses extern.h) */
  158.   if( check_image(&img, parse_stat) )
  159.     img.file_type = SOP_Logo;
  160.   /* connect to the display server */
  161.   init_server(name, &desktop, &color);
  162.   init_connections();
  163. }
  164.  
  165. /*
  166.  * Subroutine:    init_packages
  167.  * Purpose:    Initialize all of the subroutine packages and windows
  168.  */
  169. static void init_packages ( argc, argv )
  170.      int argc;
  171.      char **argv;
  172. {
  173.   GC gc;
  174.   int init_image();
  175.   GC set_gc_with_background();
  176.   void init_windows1(), init_windows2(), init_panbox_coords(), disp_dispbox();
  177.   void disp_panbox(), init_imaging_buffers(), init_mousepointers(), init_gc();
  178.   void init_display_buffers(), init_region_draw(), init_color(), new_display();
  179.   void init_software_cursors(), init_buttonmenu(), init_buttonbox_settings();
  180.   void init_magnifier(), set_magnifier(), redraw_magnifier(), init_colorbox();
  181.   void say_goodbye(), mount_buttonmenu(), new_panimage(), init_cgraph_text();
  182.  
  183.   /* get the image dimensions (need file name, type), exit if failure */
  184.   if( init_image() == 0 ) {
  185.     (void)fprintf(stderr, "No image given and no input device connected.\n");
  186.     say_goodbye(1);
  187.   }
  188.   /* get the colors ready (need display) */
  189.   init_color(&color, 1);
  190.   /* map the desktop(need image dimensions, color) */
  191.   init_windows1(argc, argv);
  192.   /* set up the mouse pointer icons (need display, hardcolors) */
  193.   init_mousepointers(desktop.display, desktop.display);
  194.   /* initialize color bar and graph labeling text font and parameters */
  195.   init_gc(desktop.display, desktop.ID);
  196.   init_cgraph_text();
  197.   /* set up the image buffer (need image) */
  198.   init_imaging_buffers();
  199.   /* get desktop dimensions and create windows (need colors, mouse cursors) */
  200.   init_windows2();
  201.   /* get the soft cursors ready(need colors, img coords) */
  202.   init_software_cursors();
  203.   /* set up color bar */
  204.   init_colorbox();
  205.   /* set up display buffers (need window dimensions) */
  206.   init_display_buffers();
  207.   /* set params and get image data (need colors, buffers, scope, imageinit) */
  208.   init_panbox_coords();
  209.   init_magnifier(control.magni_track, control.coord_track);
  210.   new_display(0, 1, 1, 1);
  211.   /* move image data into the panbox's buffer */
  212.   new_panimage();
  213.   /* set up the button menu */
  214.   gc = set_gc_with_background(&color.gcset.menu, color.gcset.menu.background);
  215.   init_buttonmenu(&btnbox, gc, color.visual,
  216.           (unsigned long)color.hard.std_black,
  217.           (unsigned long)color.hard.std_white);
  218.   init_buttonbox_settings();
  219.   /* set up region action flags and drawing stuff (draw and label on) */
  220.   init_region_draw(1, 1);
  221.   mount_buttonmenu();
  222. }
  223.  
  224. #ifdef DEBUG
  225. /*
  226.  * Subroutine:    crash_on_error
  227.  * Purpose:    Special error handler to force crash on error
  228.  * Xlib calls:    XGetErrorText()
  229.  */
  230. void crash_on_error ( dt_display, error )
  231.      Display *dt_display;
  232.      XErrorEvent *error;
  233. {
  234.   int *foo;
  235.   char msg[SZ_LINE];
  236.  
  237.   (void)fprintf(stderr,"XError Reported: type code: %d\n",error->error_code);
  238.   XGetErrorText(dt_display, (int)error->error_code, msg, SZ_LINE);
  239.   (void)fprintf(stderr, "%s\n", msg);
  240.   /* cause fatal core dump */
  241.   foo = 0;
  242.   *foo = 0;
  243.   exit( 0 );
  244. }
  245. #endif
  246.  
  247. /*
  248.  * Subroutine:    init_server
  249.  * Purpose:    Open connection with chosen or default display server
  250.  */
  251. static void init_server ( displayname, desktop, color )
  252.      char *displayname;
  253.      struct windowRec *desktop;
  254.      struct colorRec *color;
  255. {
  256.   Display *dt_display;
  257.   int dt_screen;
  258.   char errmsg[SZ_FNAME];
  259.   void exit_errmsg();
  260.  
  261.   /* make contact with the display server */
  262.   if( !(dt_display = XOpenDisplay(displayname)) ) {
  263.     (void)strcpy(errmsg, "Could not connect to display: ");
  264.     if( displayname != NULL )
  265.       (void)strcat(errmsg, displayname);
  266.     exit_errmsg(errmsg);
  267.   }
  268.   desktop->display = dt_display;
  269.   color->display = dt_display;
  270.   dt_screen = DefaultScreen(dt_display);
  271.   desktop->screen = dt_screen;
  272.   color->screen = dt_screen;
  273.   display = dt_display;
  274. }
  275.