;;; -*- Mode: Common-Lisp; Package: User; Base: 10.; Patch-File: T -*-

;;; Reason: moved x11 system's re-definition of tv:mouse-input to window
;;; system mouse file. This corrects window patch 6.29 which
;;; removed the x-enabled hook in tv:mouse-input and made the
;;; mouse on an X-window very slow and sluggish.

;;;                           RESTRICTED RIGHTS LEGEND
;;;
;;; Use, duplication, or disclosure by the Government is subject to
;;; restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
;;; Technical Data and Computer Software clause at 52.227-7013.
;;;
;;;   TEXAS INSTRUMENTS INCORPORATED      
;;;   P.O. BOX 149149, M/S 2151             
;;;   AUSTIN, TEXAS 78714-9149                 
;;;
;;; Copyright (C) 1989 Texas Instruments Incorporated.
;;; All rights reserved.

;;; Written 06/21/90 09:01:33 by MARKY,
;;; while running on LIBRA from band LODA
;;; With SYSTEM 6.41, VIRTUAL-MEMORY 6.3, EH 6.8, MAKE-SYSTEM 6.5, MICRONET 6.0, LOCAL-FILE 6.2,
;;;  BASIC-PATHNAME 6.5, NETWORK-SUPPORT-COLD 6.2, BASIC-NAMESPACE 6.8, NETWORK-NAMESPACE 6.1,
;;;  DISK-IO 6.4, DISK-LABEL 6.1, BASIC-FILE 6.13, MAC-PATHNAME 6.0, NETWORK-PATHNAME 6.2,
;;;  COMPILER 6.18, TV 6.30, DATALINK 6.0, CHAOSNET 6.8, GC 6.4, MEMORY-AUX 6.0, NVRAM 6.4,
;;;  SYSLOG 6.2, STREAMER-TAPE 6.6, UCL 6.1, INPUT-EDITOR 6.1, METER 6.2, ZWEI 6.23,
;;;  DEBUG-TOOLS 6.5, NETWORK-SUPPORT 6.1, NETWORK-SERVICE 6.3, DATALINK-DISPLAYS 6.0,
;;;  FONT-EDITOR 6.1, SERIAL 6.0, PRINTER 6.8, MAC-PRINTER-TYPES 6.2, PRINTER-TYPES 6.2,
;;;  IMAGEN 6.1, SUGGESTIONS 6.1, MAIL-DAEMON 6.6, MAIL-READER 6.8, TELNET 6.1, VT100 6.0,
;;;  NAMESPACE-EDITOR 6.6, PROFILE 6.3, VISIDOC 6.7, TI-CLOS 6.53, CLEH 6.5, IP 3.65,
;;;  Experimental BUG 11.19, Experimental CLX 7.1, X11M 6.31, Experimental CLUE 10.0,
;;;   microcode 483, Band Name: Rel 6.1 *b +SLE 6/18

#!C
; From file MOUSE.LISP#> WINDOW; Hotel:
#10R TV#:
(COMPILER-LET ((*PACKAGE* (FIND-PACKAGE "TV"))
                          (SI:*LISP-MODE* :COMMON-LISP)
                          (*READTABLE* COMMON-LISP-READTABLE)
                          (SI:*READER-SYMBOL-SUBSTITUTIONS* SYS::*COMMON-LISP-SYMBOL-SUBSTITUTIONS*))
  (COMPILER#:PATCH-SOURCE-FILE "SYS: WINDOW; MOUSE.#"


(progn
  (let ((x-symbol (and (find-package 'x11)
		       (intern "X-ENABLED" 'x11))))
    (defvar tv:X-ENABLED (and (boundp x-symbol)
			      (symbol-value x-symbol)))

    (when (and (find-package 'x11)
	       (boundp x-symbol))
      ;; from file : sys: X11M.SERVER; SYSDEPENDENT-DEFS.LISP#>
      (forward-value-cell x-symbol 'tv:x-enabled)
      (setf (documentation 'tv:x-enabled 'variable)
	    (documentation x-symbol 'variable)))))

(DEFUN MOUSE-INPUT (&OPTIONAL (WAIT-FLAG T))
  "Wait until mouse moves or a button goes up or down, then report what happened.
The values are the delta-x, the delta-y (amounts of mouse motion),
buttons-newly-pushed, buttons-newly-raised, and the relevant mouse X and Y (the
current position normally, but if any buttons changed, the position then)."
  ;; Await a change in hardware status from what it was last time
  (COND (WAIT-FLAG
         ;; This sleep makes it reasonable for the mouse process to be high priority.
         ;; It insures that moving the mouse does not lock out lower priority
         ;; processes for a long time.  This constant may want to be tweaked.  A
         ;; value of 1 (1/60 of a second) does not noticably affect mouse response.
         (PROCESS-SLEEP 1)
         (PROCESS-WAIT "MOUSE" #'(LAMBDA ()                                              
				   (IF (mac-system-p)
				       (lispm-or-mac-mouse-wakeup)
				       (AND (NULL X-ENABLED)	;; may 06/21/90 x-enabled is x-server hook
					    (OR MOUSE-WAKEUP MOUSE-RECONSIDER)))))))
  ;; Set some global variables used for mouse transport.  CJJ 05/06/88.
  ;; Added by KJF for CJJ on 08/16/88 for Multiple Monitor (MMON) support.
  (SETF *unglitched-mouse-x-speed* mouse-x-speed
	*unglitched-mouse-y-speed* mouse-y-speed
	;; Remember previous mouse position for mouse transport along mouse trajectory.  CJJ 08/15/88.
	*previous-unglitched-mouse-x* *unglitched-mouse-x*
	*previous-unglitched-mouse-y* *unglitched-mouse-y*
	*unglitched-mouse-x* mouse-x
	*unglitched-mouse-y* mouse-y)
  ;; Clear wakeup flag unless there are buffered mouse button transitions, since we
  ;; might not read all of them before calling MOUSE-INPUT again.
  (SETQ MOUSE-WAKEUP (NOT (= MOUSE-BUTTONS-BUFFER-IN-INDEX MOUSE-BUTTONS-BUFFER-OUT-INDEX)))
  ;; Compute delta-X and delta-Y in screen coordinates
  (LET ((DELTA-X (- MOUSE-X MOUSE-LAST-X))
        (DELTA-Y (- MOUSE-Y MOUSE-LAST-Y))
        (GLITCH-X NIL) (GLITCH-Y NIL) NEW-BUTTONS CHANGED-BUTTONS)
    (INCF MOUSE-LAST-X DELTA-X)
    (INCF MOUSE-LAST-Y DELTA-Y)
    ;; Compute change in button status
    (MULTIPLE-VALUE-SETQ (NEW-BUTTONS MOUSE-LAST-BUTTONS-TIME
                          MOUSE-LAST-BUTTONS-X MOUSE-LAST-BUTTONS-Y)
      (MOUSE-BUTTONS))
    (SETQ CHANGED-BUTTONS (LOGXOR NEW-BUTTONS MOUSE-LAST-BUTTONS)
          MOUSE-LAST-BUTTONS NEW-BUTTONS)
    ;; Force blinker to stay within mouse-sheet.  If the mouse moves during this
    ;; computation, it will glitch back.  So we only SETQ the variables
    ;; if the mouse position actually needs to be changed, rather than using
    ;; MAX and MIN which would be more readable.
    (when (and (sheet-width mouse-sheet) (sheet-height mouse-sheet)) ;; patch 6.29
      (IF (> 0 MOUSE-X)
	  (SETQ GLITCH-X 0))
      (IF (<= (SHEET-WIDTH MOUSE-SHEET) MOUSE-X)
	  (SETQ GLITCH-X (1- (SHEET-WIDTH MOUSE-SHEET))))
      (IF (> 0 MOUSE-Y)
	  (SETQ GLITCH-Y 0))
      (IF (<= (SHEET-HEIGHT MOUSE-SHEET) MOUSE-Y)
	  (SETQ GLITCH-Y (1- (SHEET-HEIGHT MOUSE-SHEET))))
      ;; If mouse blinker needs to be glitched, do so
      (IF (OR GLITCH-X GLITCH-Y)
	  (WITHOUT-INTERRUPTS
	    (%OPEN-MOUSE-CURSOR)
	    (IF GLITCH-X
		(SETQ MOUSE-LAST-X (SETQ MOUSE-X GLITCH-X)))
	    (IF GLITCH-Y
		(SETQ MOUSE-LAST-Y (SETQ MOUSE-Y GLITCH-Y)))
	    (SETQ MOUSE-CURSOR-STATE MOUSE-CURSOR-CLOSED-STATE
		  PREPARED-SHEET NIL))))
    (VALUES DELTA-X
            DELTA-Y
            (LOGAND NEW-BUTTONS CHANGED-BUTTONS)
            (BOOLE 2 NEW-BUTTONS CHANGED-BUTTONS) ;BOOLE 2 is ANDCA
            (IF (ZEROP CHANGED-BUTTONS) MOUSE-LAST-X MOUSE-LAST-BUTTONS-X)
            (IF (ZEROP CHANGED-BUTTONS) MOUSE-LAST-Y MOUSE-LAST-BUTTONS-Y))))
))
