;;; -*- Mode:COMMON-LISP; Package:X11; Base:10.; Fonts:(MEDFNB hl12B hl12bi) -*-

;;;                           RESTRICTED RIGHTS LEGEND

;;;Use, duplication, or disclosure by the Government is subject to
;;;restrictions as set forth in subdivision (b)(3)(ii) of the Rights in
;;;Technical Data and Computer Software clause at 52.227-7013.
;;;
;;;                     TEXAS INSTRUMENTS INCORPORATED.
;;;                              P.O. BOX 2909
;;;                           AUSTIN, TEXAS 78769
;;;                                 MS 2151
;;;
;;; Copyright (C) 1987, Texas Instruments Incorporated. All rights reserved.

;;; Change history:
;;;
;;;  Date       Author	Description
;;; -------------------------------------------------------------------------------------
;;; 10/05/88    DAN     Changed SEND-EVENT to use READ-EVENT-FROM-CLIENT.
;;;  6/06/88    TWE	Fixed SEND-EVENT to handle its BOOL argument correctly.
;;;  5/16/88    DAN     Removed ALLOW-SOME after discovering it already exists in EVENTS file.
;;;  5/13/88    DAN     Added code for ALLOW-EVENTS request and helping function ALLOW-SOME.
;;;  5/12/88    DAN     Added SEND-EVENT code.
;;;   4/12/88  KDB	Added Client-Message event1.*
;;;  2/23/88    TWE	Moved Get-Motion-Events to the mouse-requests file.  Added more C
;;;			code.
;;;  2/18/88    TWE	Moved the C code from the /server/dix/events.c file for the
;;;			allow-events request to here.
;;; 12/15/87    TWE	Moved requests from the REQUESTS file.
(defreq Allow-Events ((mode (CARD8 Async-Pointer-Mode Sync-Pointer-Mode
                                   Replay-Pointer-Mode Async-Keyboard-Mode
                                   Sync-Keyboard-Mode Replay-Keyboard-Mode
                                   Async-Both-Mode Sync-Both-Mode))
		      (time TIMESTAMP))
  (allow-events state mode time))

(DEFUN ALLOW-EVENTS (CLIENT mode time)
  (DECLARE (TYPE STATE CLIENT)
           (TYPE integer mode)
           (TYPE time-stamp time)
           (VALUES INTEGER))
  (EVENT-TRACE-ENTERING "ALLOW-EVENTS")
  (LET ((mouse (input-info.pointer input-info))
        (keybd (input-info.keyboard input-info)))
    (SETQ time (client-time-to-server-time time))
    (SELECTOR mode eql
      (Replay-Pointer-Mode  (Allow-Some client time mouse keybd NOT-GRABBED))
      (Sync-Pointer-Mode    (Allow-Some client time mouse keybd FREEZE-NEXT-EVENT))
      (Async-Pointer-Mode   (Allow-Some client time mouse keybd THAWED))
      (Replay-Keyboard-Mode (Allow-Some client time keybd mouse NOT-GRABBED))
      (Sync-Keyboard-Mode   (Allow-Some client time keybd mouse FREEZE-NEXT-EVENT))
      (Async-Keyboard-Mode  (Allow-Some client time keybd mouse THAWED))
      (Sync-Both-Mode       (Allow-Some client time keybd mouse FREEZE-BOTH-NEXT-EVENT))
      (Async-Both-Mode      (Allow-Some client time keybd mouse THAWED-BOTH))))
  (EVENT-TRACE-LEAVING "ALLOW-EVENTS"))
    


(defreq Send-Event ((propagate BOOL)
		    (destination (ONEOF WINDOW Pointer-Window Input-Focus))
		    (event-mask CARD32)
		    (:byte 32)) ;1; event*
  (setq propagate (= propagate true))
  (if (= length 1) ;1; only one multiple of 32 bytes allowed*
      (send-event state destination propagate event-mask byte-offset)
    (bad-length)))

(DEFUN SEND-EVENT (CLIENT destination propagate event-mask offset)
  (DECLARE (TYPE STATE CLIENT)
           (TYPE (OR window integer) destination)
           (TYPE boolean propagate)
           (TYPE integer event-mask)
           (VALUES INTEGER))
  (EVENT-TRACE-ENTERING "SEND-EVENT")
  (LET ((event (read-event-from-client client offset)))
    (when event
      (let* ((event-type (event-record.type event))
	     (sprite-win (sprite.window sprite))
	     (exit-with-success nil)
	     (effective-focus nil)   ;1only set if destination = Input-Focus*
	     (win destination))
	;1;The client's event type must be a core event type or one defined by an extension.*
	(COND ((AND (>= event-type LAST-EVENT)
		    (OR (> EXTENSION-EVENT-BASE event-type)
			(>= event-type NEXT-EXTENSION-EVENT)))
	       (bad-value event-type))
	      (t    ;1;;ELSE*
	       (SELECTOR win eql
		 (Pointer-Window (SETQ win sprite-win))
		 (Input-Focus
		  (LET ((inputfocus (device.focus-window (input-info.keyboard input-info))))
		    (COND ((NULL inputfocus)
			   (SETQ exit-with-success t))
			  ((EQ inputfocus Pointer-Root-Window)
			   (SETQ inputfocus (ROOT))))
		    (UNLESS exit-with-success
		      (COND ((is-parent inputfocus sprite-win)
			     (SETQ effective-focus inputfocus
				   win sprite-win))
			    (t
			     (SETQ effective-focus inputfocus
				   win inputfocus)))))))
	       (UNLESS exit-with-success
		 (SETF (event-record.type event) (LOGIOR event-type #x80))
		 (COND (propagate
			(LOOP for win first win then (window.parent win)
			      while (AND win
					 (ZEROP (deliver-events-to-window win event 1 event-mask nil))
					 (NEQ win effective-focus))
			      do (SETQ event-mask
				       (LOGAND event-mask
					       (LOGNOT (window.do-not-propagate-mask win))))))
		       (t
			(deliver-events-to-window win event 1 event-mask nil)))))))))
  (EVENT-TRACE-LEAVING "SEND-EVENT"))


1(defun  CLIENT-MESSAGE (*STATE 1WINDOW TYPE FORMAT DATA)
  "        *window: WINDOW
	type: ATOM
	format: {8, 16, 32}
	data: LISTofINT8 or LISTofINT16 or LISTofINT32

	This event is only generated by clients using Send-Event .  The type
	specifies how the data is to be interpreted by the receiving client;
	the server places no interpretation on the type or the data.  The
	format specifies whether the data should be viewed as a list of 8-bit,
	16-bit, or 32-bit quantities, so that the server can correctly
	byte-swap as necessary.  The data always consists of either 20 8-bit
	values or 10 16-bit values or 5 32-bit values, although particular
	message types might not make use of all of these values.1"
  (LET ((EVENT (MAKE-EVENT-CLIENT-MESSAGE*
	1       :WINDOW      WINDOW*
	1       :DATA-TYPE    TYPE*
	1       :FORMAT       FORMAT*
	1       :DATA          DATA))*
      (EVENT-COUNT     ))
   (WRITE-TO-CLIENT STATE EVENT-COUNT EVENT)))



