view xaim/initialise.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
line wrap: on
line source
/*
    Copyright (c) Applied Psychology Unit, Medical Research Council. 1993
    ===========================================================================

    Permission to use, copy, modify, and distribute this software without fee 
    is hereby granted for research purposes, provided that this copyright 
    notice appears in all copies and in all supporting documentation, and that 
    the software is not redistributed for any fee (except for a nominal 
    shipping charge). Anyone wanting to incorporate all or part of this 
    software in a commercial product must obtain a license from the Medical 
    Research Council.

    The MRC makes no representations about the suitability of this 
    software for any purpose.  It is provided "as is" without express or 
    implied warranty.
 
    THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 
    THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES 
    OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 
    ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
    SOFTWARE.
*/
/* 
*   initailise.c
*   ---------
*  Initialises lots of X things.
*
*  M. Akeroyd.  July 1993. version 1.10
*  Revisions: MAA: Christmas 1993.
*
*/



#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <stdio.h>

#include "xreview.h"
#include "./xreview.bitmap"
#include <X11/cursorfont.h>



/* General variables ....*/

extern char progname[MAX_STRING_LENGTH];

extern char *data_pointer;
extern long location[MAX_FRAMES];



/* X ....................*/

extern char display_name[MAX_STRING_LENGTH];
extern char fontname[MAX_STRING_LENGTH];

extern Display *display;
extern int screen_num;
extern Screen *screen_ptr;
extern int depth;
extern unsigned int display_width, display_height;

extern toplevelWindow control;
extern toplevelWindow axes;
extern Pixmap stipple_pixmap;
extern XImage *reviewimage;
extern Cursor cursor;

extern GC button_gc;
extern XFontStruct *font_info;
extern int planemask;


/* .ctn ..................*/

extern int no_frames;
extern int width_ctn, height_ctn;
extern int x_ctn, y_ctn;
extern int reviewimage_bytesperline;
extern int reviewimage_bitmap_pad;


/* Command line ..........*/
extern double scale;
extern int new_axes_x, new_axes_y;
extern int new_control_x, new_control_y;

extern int axes_xflag, axes_yflag;
extern int controls_xflag, controls_yflag;
extern int verboseflag;
extern int titleflag;
extern int depthflag;
extern int reversevideoflag;
extern int byteorderflag;



/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/




void initialise_X_screen()
{
  display = XOpenDisplay(display_name);
  if (display == NULL) {
    fprintf(stderr, "xreview : cannot connect to X server %s\n", XDisplayName(display_name));
    exit(-1);}

  screen_num = DefaultScreen(display);
  screen_ptr = DefaultScreenOfDisplay(display);

  display_width = DisplayWidth(display, screen_num);
  display_height = DisplayHeight(display, screen_num);
}  



/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/



void  initialise_axes_window(char *inputfn, char *titlestring, FILE *inputfp, char *argv[], int argc)
{
  char tempstring[MAX_STRING_LENGTH];
  int status;

/*-----------------------------------*/

 /* Set title to either the filename, "stdin", ot the -title option. */
  if (inputfp == stdin){
    strcpy(tempstring, "stdin");
    axes.window_name = tempstring;
    axes.icon_name = tempstring;}
  else {
    axes.window_name = inputfn;
    axes.icon_name = inputfn;}

  if (titleflag == ON){
    axes.window_name = titlestring;
    axes.icon_name = titlestring; }
    
  /* Copy size off .ctn header */
  axes.width = width_ctn;
  axes.height = height_ctn;
 
  /* Copy position off header  */
  axes.x = x_ctn;
  axes.y = y_ctn; 
  if (x_ctn == -1)
    axes.x = display_width/2 - axes.width/2;
  if (y_ctn == -1)
    axes.y = display_height/2 - axes.height/2;

  /* If user requests new position, honour it */
  if (axes_xflag == ON)
    axes.x = new_axes_x;
  if (axes_yflag == ON)
    axes.y = new_axes_y;
   
/*---------------------------------*/

  axes.win = XCreateSimpleWindow(display, RootWindow(display, screen_num), \
				 axes.x, axes.y, axes.width, axes.height,\
				 axes.border_width, \
				 BlackPixel(display, screen_num), \
				 WhitePixel(display, screen_num));
  
  axes.icon_pixmap = XCreateBitmapFromData(display, axes.win, xreview_bits, \
					   xreview_width, xreview_height);
  axes.gc = XCreateGC(display, axes.win, axes.valuemask, &axes.values);

  axes.line_width = LINE_WIDTH_X;
  axes.line_style = LINE_STYLE_X;
  axes.cap_style = CAP_STYLE_X;
  axes.join_style = JOIN_STYLE_X;
  axes.foreground  = BlackPixel(display, screen_num);
  
  XSelectInput(display, axes.win, ExposureMask | KeyPressMask | \
	       ButtonPressMask | StructureNotifyMask);
  XSetFont(display, axes.gc, font_info->fid);
  if (reversevideoflag == OFF) {
    XSetForeground(display, axes.gc, axes.foreground);
    XSetBackground(display, axes.gc, WhitePixel(display, screen_num));}
  else{
    XSetBackground(display, axes.gc, axes.foreground);
    XSetForeground(display, axes.gc, WhitePixel(display, screen_num));}

  XSetLineAttributes(display, axes.gc, axes.line_width, axes.line_style,\
		     axes.cap_style, axes.join_style);
  XSetPlaneMask(display, axes.gc, planemask);
  XDefineCursor(display, axes.win, cursor);

  axes.window_size = BIG_ENOUGH_X;

/*---------------------------------*/  
/* Window Manager bits. Assume R4. */

  axes.size_hints.flags = PPosition | PSize | PMinSize | PMaxSize;
  axes.size_hints.min_width = axes.width;
  axes.size_hints.max_width = axes.width;
  axes.size_hints.min_height = axes.height;
  axes.size_hints.max_height = axes.height;

  axes.wm_hints.initial_state = NormalState;
  axes.wm_hints.input = True;
  axes.wm_hints.icon_pixmap = axes.icon_pixmap;
  axes.wm_hints.flags = StateHint | IconPixmapHint | InputHint;

  axes.class_hints.res_name = progname;
  axes.class_hints.res_class = "Xreview";

  status = XStringListToTextProperty(&axes.window_name, 1, &axes.windowName);
  if (status == 0) {
    if (verboseflag == ON) fprintf(stderr, "\n");
    fprintf(stderr,"xreview : structure allocation for cartoon windowName failed.\n");
    exit(-1); }
  
  status = XStringListToTextProperty(&axes.icon_name, 1, &axes.iconName);
  if (status == 0) {
    if (verboseflag == ON) fprintf(stderr, "\n");
    fprintf(stderr,"xreview : structure allocation for cartoon iconName failed.\n");
    exit(-1); }

  XSetWMProperties(display, axes.win, &axes.windowName, &axes.iconName, \
		   argv, argc, \
		   &axes.size_hints, &axes.wm_hints, &axes.class_hints);

}



/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/



void  initialise_control_window(char *inputfn, char *titlestring, FILE *inputfp,  char *argv[], int argc)
{
  char tempstring[MAX_STRING_LENGTH];
  int status;

/*----------------------------------*/

  control.window_name = "xreview controls";
  control.icon_name = "Controls";

  control.width = (int) CONTROL_WIDTH * scale;
  control.height = (int) CONTROL_HEIGHT * scale;

  control.x = CONTROL_X;
  control.y = CONTROL_Y;

  /* If user requests new position, honour it */
  if (controls_xflag == ON)
    control.x = new_control_x;
  if (controls_yflag == ON)
    control.y = new_control_y;

/*---------------------------------*/
   
  control.win = XCreateSimpleWindow(display, RootWindow(display, screen_num), \
                            control.x, control.y, control.width, \
			    control.height, control.border_width, \
                            BlackPixel(display, screen_num), \
                            WhitePixel(display, screen_num));

  control.icon_pixmap = XCreateBitmapFromData(display, control.win, \
					      xreview_bits, \
					      xreview_width, xreview_height);
  control.gc = XCreateGC(display, control.win, \
			 control.valuemask, &control.values);
  
  control.line_width = LINE_WIDTH_X;
  control.line_style = LINE_STYLE_X;
  control.cap_style = CAP_STYLE_X;
  control.join_style = JOIN_STYLE_X;
  control.foreground  = BlackPixel(display, screen_num);

  XSelectInput(display, control.win, \
	       ExposureMask | KeyPressMask | \
	       ButtonPressMask | StructureNotifyMask);
  XSetFont(display, control.gc, font_info->fid);
  XSetForeground(display, control.gc, control.foreground);
  XSetLineAttributes(display, control.gc, control.line_width, \
		     control.line_style,control.cap_style, control.join_style);
  XDefineCursor(display, control.win, cursor);
  XSetWindowBackgroundPixmap(display, control.win, stipple_pixmap); 

  control.window_size = BIG_ENOUGH_X;

/*---------------------------------*/  
/* Window Manager bits. Assume R4. */

  control.size_hints.flags = PPosition | PSize | PMinSize | PMaxSize;
  control.size_hints.min_width = control.width;
  control.size_hints.min_height = control.height;
  control.size_hints.max_width = control.width;
  control.size_hints.max_height = control.height;

  control.wm_hints.initial_state = NormalState;
  control.wm_hints.input = True;
  control.wm_hints.icon_pixmap = control.icon_pixmap;
  control.wm_hints.flags = StateHint | IconPixmapHint | InputHint;

  control.class_hints.res_name = "xreview controls";
  control.class_hints.res_class = "Xreview";

  status = XStringListToTextProperty(&control.window_name, 1, &control.windowName);
  if (status == 0) {
    if (verboseflag == ON) fprintf(stderr, "\n");
    fprintf(stderr,"xreview : structure allocation for Controls windowName failed.\n");
    exit(-1); }

  status = XStringListToTextProperty(&control.icon_name, 1, &control.iconName);
  if (status == 0) {
    if (verboseflag == ON) fprintf(stderr, "\n");
    fprintf(stderr,"xreview : structure allocation for Controls iconName failed.\n");
    exit(-1); }

  XSetWMProperties(display, control.win,&control.windowName,&control.iconName,\
		   argv, argc, \
		   &control.size_hints,&control.wm_hints,&control.class_hints);

}



/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/



void initialise_image()
{
  int offset = 0;
  
  if (depthflag == MONO)
    reviewimage = XCreateImage(display, DefaultVisual(display, screen_num),\
	 	     depth, XYBitmap, \
		     offset, data_pointer, axes.width, axes.height,\
		     reviewimage_bitmap_pad, reviewimage_bytesperline);
  else
    reviewimage = XCreateImage(display, DefaultVisual(display, screen_num),\
	 	     depth, XYPixmap, \
		     offset, data_pointer, axes.width, axes.height,\
		     reviewimage_bitmap_pad, reviewimage_bytesperline);

  if (byteorderflag == DEC) {
    (*reviewimage).byte_order = LSBFirst;
    (*reviewimage).bitmap_bit_order = LSBFirst;
    (*reviewimage).bitmap_pad = 32;}
  
  if (byteorderflag == SUN) {
    (*reviewimage).byte_order = MSBFirst;
    (*reviewimage).bitmap_bit_order = MSBFirst;
    (*reviewimage).bitmap_pad = 32;}
  
}




/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/




void exit_xreview()
{
  reviewimage->data = (char *) location[1];
  XDestroyImage(reviewimage);
  XUnloadFont(display, font_info->fid);
  XFreeGC(display, axes.gc);
  XFreeGC(display, control.gc);
  XFreeGC(display, button_gc);
  XCloseDisplay(display);
  if (verboseflag==ON) fprintf(stderr, "\n");
  exit(1);
}



/* The end.*/
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/