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

;;; Reason: Return events in the correct order for event handling functions. 

;;;                           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 2909, M/S 2151             
;;;   AUSTIN, TEXAS 78769                 
;;;
;;; Copyright (C) 1989 Texas Instruments Incorporated.
;;; All rights reserved.

;;; Patch file for MACTOOLBOX version 2.2
;;; Written 06/14/89 16:45:11 by jones,
;;; while running on HOBBS from band LOD1
;;; With SYSTEM 6.5, GC 6.2, VIRTUAL-MEMORY 6.1, MICRONET 6.0, MICRONET-COMM 6.1,
;;;  DISK-IO 6.0, DISK-LABEL 6.0, BASIC-PATHNAME 6.0, MAC-PATHNAME 6.0, NETWORK-SUPPORT-COLD 6.0,
;;;  BASIC-NAMESPACE 6.0, BASIC-FILE 6.2, RPC 6.1, NFS 6.0, EH 6.2, MAKE-SYSTEM 6.0,
;;;  MEMORY-AUX 6.0, COMPILER 6.2, TV 6.6, NVRAM 6.0, UCL 6.0, INPUT-EDITOR 6.0, MACTOOLBOX 2.0,
;;;  METER 6.0, ZWEI 6.1, DEBUG-TOOLS 6.0, WINDOW-MX 6.2, PRINTER 6.1, MAC-PRINTER-TYPES 6.1,
;;;  CLIPBOARD 6.0, NETWORK-PATHNAME 6.0, NETWORK-NAMESPACE 6.0, DATALINK 6.0, CHAOSNET 6.0,
;;;  NETWORK-SUPPORT 6.0, NETWORK-SERVICE 6.0, DATALINK-DISPLAYS 6.0, MX-DATALINK 6.0,
;;;  NAMESPACE-EDITOR 6.0, IP 3.46, NFS-SERVER 6.0, MX-SERIAL 6.0, PRINTER-TYPES 6.0,
;;;  IMAGEN 6.0, MAIL-DAEMON 6.2, MAIL-READER 6.0, TELNET 6.0, VT100 6.0, STREAMER-TAPE 6.2,
;;;  DECNET 1.67, VISIDOC 6.1, PROFILE 6.1, TI-CLOS 6.5, CLEH 6.3, CLX 6.0, CLUE 6.0,
;;;  Experimental BUG 11.5, Experimental ACTION 2.0,  microcode 137, Band Name: p526*1

#!C
; From file MAC-APPLICATIONS.LISP#> TOOLBOX-INTERFACE; MR-X:
#10R MACTOOLBOX#:
(COMPILER-LET ((*PACKAGE* (FIND-PACKAGE "MACTOOLBOX"))
                          (SI:*LISP-MODE* :Common-lisp)
                          (*READTABLE* Sys:Common-lisp-readtable)
                          (SI:*READER-SYMBOL-SUBSTITUTIONS* Sys::*common-lisp-symbol-substitutions*))
  (COMPILER#:PATCH-SOURCE-FILE "SYS: TOOLBOX-INTERFACE; MAC-APPLICATIONS.#"


(defmethod (mac-application-internal :dequeue-event) (event-mask user-event &optional leave-in-queue mouse-moved-region &aux evt)
  "Return an event from applications event queue for event-mask."
  (block dequeue-event

      ;; Return mouse moved events only if a region was specified and we have seen the mouse move.
      (when (and mouse-moved-region
		 (not (zerop (send mouse-moved-region :handle)))
		 (not (= last-mouse-moved-time last-mouse-moved-event-time)))
	
	(without-interrupts
	  (send *temp-point* := last-mouse-v last-mouse-h)
	  (when (not (!PtInRgn *temp-point* mouse-moved-region))	  
	    
	    (setf last-mouse-moved-event-time  last-mouse-moved-time)
	    (setf (send user-event :what)      !app4Evt)
	    (setf (send user-event :message)   (dpb  *mouse-moved-event-opcode* (byte 8 24) 0))
	    (setf (send user-event :when)      last-mouse-moved-time)
	    (setf (send user-event :h)         last-mouse-h)
	    (setf (send user-event :v)         last-mouse-v)
	    (setf (send user-event :modifiers) last-modifiers)
	    (return-from dequeue-event t))))

      (dolist (mask `(,(logior !app4Evt !activateEvt)
		      ,(logior !mouseDown !mouseUp !keyDown !keyUp !diskEvt
			       !networkEvt !driverEvt !app1Evt !app2Evt !app3Evt)
		      !autoKey
		      !updateEvt))
	(when (setf evt (find (logand mask event-mask)
			      event-list
			      :test #'(lambda (a b) (logand (ash 1 a) b)
					      :key #'(lambda (el) (send el :what))))))
	  (return))
      (when evt
	
	(when (not leave-in-queue)
	  (without-interrupts (setf event-list (delete evt event-list :test #'eq :count 1))))
	
	(setf (send user-event :what)      (send evt :what))
	(setf (send user-event :message)   (send evt :message))
	(setf (send user-event :when)      (send evt :when))
	(setf (send user-event :modifiers) (send evt :modifiers))
	(setf (send user-event :h)         (send evt :h))
	(setf (send user-event :v)         (send evt :v))
	
	(deallocate-event evt)
	(return-from dequeue-event t))
    
      ;; No events available. return a null event.    
      (setf (send user-event :what)      0)
      (setf (send user-event :message)   0)
      ;; A crude way to generate :when for null events.
      (setf (send user-event :when)      (+ (time-difference (time) last-event-arrival-time) last-when))
      (setf (send user-event :h)         last-mouse-h)
      (setf (send user-event :v)         last-mouse-v)
      (setf (send user-event :modifiers) last-modifiers)
      (return-from dequeue-event nil)))

(compile-flavor-methods mac-application mac-application-internal)

))
