	SUBROUTINE SAO_CURN(NPICN,IXT,IYT,XF,YF,XM,YM,KEY,MESSAGE)
C
C  Note:  This is the UNIX version
C
C       This subroutine reads the position of the cursor on SAOimage.
C  Since only one cursor is used in SAOimage, this subroutine differs
C  somewhat from CURN.FOR and TR_CURN.FOR.
C				Donald H. Gudehus, August, 1993
C				Version 1U, D. Gudehus, September 21, 1994
C				Changed code to work under UNIX
C				Version 2, D. Gudehus, July 25, 2006
C				Incorporated new saofacts.for
C
C	NPICN	(R)	The number of the picture;  useful when several
C			pictures have been loaded at the same time
C	IXT	(R)	The X coordinate of the cursor on the TV screen;
C			it has values between 0 and the maximum allowed by
C			the chosen frame buffer.  Zero is at the left.
C	IYT	(R)	The Y coordinate of the cursor on the TV screen;
C			it has values between 0 and the maximum allowed by
C			the chosen frame buffer.  Zero is at the top.
C	XF	(R)	The X coordinate of the cursor on the frame;  it
C			is independent of zooming and panning.  Fractional
C			values are possible, with the left edge being
C			0.000.
C	YF	(R)	Like XF but in the Y direction;  the top edge is 0.000.
C	XM	(R)	The X coordinate in the map;  it has values
C			beginning at 1.0 and is independent of zooming
C			and panning.
C	YM	(R)	Like XM but in the Y direction.
C	KEY	(R)	The ASCII code of the key that was pressed.
C	MESSAGE	(C)	A logical variable such that when .TRUE., a message
C			will be displayed when the cursor is not in the map.
C
	INTEGER*2 IXT,IYT
	LOGICAL MESSAGE
C
	INCLUDE 'saofacts.for'
C
	INTEGER OF  ! Offset
C
C  Send a command to read the cursor
	IBUFFER_SAO(1)=256 !  imhead.tid,  no name
	IBUFFER_SAO(2)=-3 !  imhead.thingct = 3 words
	IBUFFER_SAO(3)=12 !  imhead.subunit = LOOK_FEEL
        IBUFFER_SAO(4)=0 !  imhead.checksum;  initially set to zero
        IBUFFER_SAO(5)=IOR(0,'100000'O) !  imhead.x
	IBUFFER_SAO(6)=0 !  imhead.y
	IBUFFER_SAO(7)=0 !  imhead.z
	IBUFFER_SAO(8)=3
		!  imhead.t = IOP_MIIPS;  used for control.look_and_feel
	CALL IMTOOLCHECKSUM(IBUFFER_SAO)
	NBYTES=16
	CALL SI_WRITE(BUFFER_SAO,NBYTES)
	IBUFFER_SAO(1)=0
	IBUFFER_SAO(2)=0
	IBUFFER_SAO(3)=0
	NBYTES=6
	CALL SI_WRITE(BUFFER_SAO,NBYTES)
C
	IBUFFER_SAO(1)='100000'O
		!  '100000'O = -32768 = IIS_READ= Reads the cursor
		!   upon trigger (pressing a key)
	IBUFFER_SAO(2)=-160 !  imhead.thingct
	IBUFFER_SAO(3)=IOR('20'O,'100000'O) !  = -32752 imhead.subunit
					    !  IMCURSOR='20'O
        IBUFFER_SAO(4)=0
	IBUFFER_SAO(5)=0  ! x
        IBUFFER_SAO(6)=0  ! y
        IBUFFER_SAO(7)=1  ! Frame number
        IBUFFER_SAO(8)=0
	CALL IMTOOLCHECKSUM(IBUFFER_SAO)
	NBYTES=16
	CALL SI_WRITE(BUFFER_SAO,NBYTES)
	NBYTES=160
  	CALL SI_READ(BUFFER_SAO,NBYTES)
C  The map coordinates returned from SAOimage are correct if only one map was
C  loaded into the frame, or in the case of several loaded maps, if the map
C  chosen with the cursor was the last map loaded.  We therefore need 
C  additional calculation.
	DECODE(10,1010,CBUFFER_SAO(1:10)) XM !  in map
1010	FORMAT(F10.3)
	DECODE(10,1010,CBUFFER_SAO(12:21)) YM
C  Get the ASCII code of the key that was pressed
	IF (BUFFER_SAO(27).GT.32.AND.BUFFER_SAO(28).EQ.32) THEN
			!  Printable character
	  KEY=BUFFER_SAO(27)
	  OF=0
	ELSE IF (BUFFER_SAO(27).EQ.92.AND.BUFFER_SAO(28).NE.32) THEN
			!  Non-printable
C  ASCII 92 = \;  positions 28-30 are ASCII code of octal characters
	  KEY=BUFFER_SAO(30)-48+8*(BUFFER_SAO(29)-48)
	  OF=3
	ELSE
	  CALL ERROR(-1,'Bad ASCII code in SAO_CURN')
	END IF
        DECODE(10,1010,CBUFFER_SAO(29+OF:38+OF)) XT
			!  in desktop window (on TV)
        DECODE(10,1010,CBUFFER_SAO(40+OF:49+OF)) YT
        IXT=XT
        IYT=YT
        DECODE(10,1010,CBUFFER_SAO(51+OF:60+OF)) XF !  in frame
        DECODE(10,1010,CBUFFER_SAO(62+OF:71+OF)) YF
C
C  Find what image the cursor is in
C
	NPICN=0
C  X on the frame increases to the right;  Y on the frame increases downwards
	DO I=1,NPIC_SAO
	  XEND=XLOC_SAO(I)+NPTSX_SAO(I)-1 !  XLOC_SAO is measured on the frame
	  YEND=YLOC_SAO(I)-NPTSY_SAO(I)+1
	  IF (INT(XF).GE.XLOC_SAO(I).AND.INT(YF).LE.YLOC_SAO(I)
	1 .AND.INT(XF).LE.XEND.AND.INT(YF).GE.YEND) THEN
	    NPICN=I
	  END IF
	END DO
	IF (NPICN.EQ.0.AND.MESSAGE) THEN
	  WRITE(6,7500)
	  WRITE(7,7500)
7500	  FORMAT(1X,'Cursor not in a map')
	END IF
C
C  Cursor's position in map
C
	IF (NPICN.NE.0) THEN
	  XM=XF-XLOC_SAO(NPICN)+IMSX_SAO(NFRAME_SAO,NPICN)
	  YM=YLOC_SAO(NPICN)-YF+1.0+IMSY_SAO(NFRAME_SAO,NPICN)
	END IF
 999	RETURN
	END
