Skip to content
Snippets Groups Projects
doc.lisp 137 KiB
Newer Older
dan's avatar
dan committed
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
  (card16 x y width height count))

(declare-event :graphics-exposure
  (card16 sequence)
  (drawable (drawable event-window))
  (card16 x y width height count)
  (card8 major)
  (card16 minor))

(declare-event :no-exposure
  (card16 sequence)
  (drawable (drawable event-window))
  (card8 major)
  (card16 minor))

(declare-event :visibility-notify
  (card16 sequence)
  (window (window event-window))
  ((member :unobscured :partially-obscured :fully-obscured) state))

(declare-event :create-notify
  (card16 sequence)
  (window window (parent event-window))
  (int16 x y)
  (card16 width height border-width)
  (boolean override-redirect-p))

(declare-event :destroy-notify
  (card16 sequence)
  (window event-window window))

(declare-event :unmap-notify
  (card16 sequence)
  (window event-window window)
  (boolean configure-p))

(declare-event :map-notify
  (card16 sequence)
  (window event-window window)
  (boolean override-redirect-p))

(declare-event :map-request
  (card16 sequence)
  (window (parent event-window) window))

(declare-event :reparent-notify
  (card16 sequence)
  (window event-window window parent)
  (int16 x y)
  (boolean override-redirect-p))

(declare-event :configure-notify
  (card16 sequence)
  (window event-window window)
  (int16 x y)
  (card16 width height border-width)
  ((or null window) above-sibling)
  (boolean override-redirect-p))

(declare-event :gravity-notify
  (card16 sequence)
  (window event-window window)
  (int16 x y))

(declare-event :resize-request
  (card16 sequence)
  (window (window event-window))
  (card16 width height))

(declare-event :configure-request
  (card16 sequence)
  (window (parent event-window) window)
  (int16 x y)
  (card16 width height border-width)
  ((member :above :below :top-if :bottom-if :opposite) stack-mode)
  ((or null window) above-sibling)
  (mask16 value-mask))

(declare-event :circulate-notify
  (card16 sequence)
  (window event-window window)
  ((member :top :bottom) place))

(declare-event :circulate-request
  (card16 sequence)
  (window (parent event-window) window)
  ((member :top :bottom) place))

(declare-event :property-notify
  (card16 sequence)
  (window (window event-window))
  (keyword atom)
  ((member :new-value :deleted) state)
  ((or null card32) time))

(declare-event :selection-clear
  (card16 sequence)
  (window (window event-window))
  (keyword selection)
  ((or null card32) time))

(declare-event :selection-request
  (card16 sequence)
  (window (window event-window) requestor)
  (keyword selection target)
  ((or null keyword) property)
  ((or null card32) time))

(declare-event :selection-notify
  (card16 sequence)
  (window (window event-window))
  (keyword selection target)
  ((or null keyword) property)
  ((or null card32) time))

(declare-event :colormap-notify
  (card16 sequence)
  (window (window event-window))
  ((or null colormap) colormap)
  (boolean new-p installed-p))

(declare-event :mapping-notify
  (card16 sequence)
  ((member :modifier :keyboard :pointer) request)
  (card8 start count))

(declare-event :client-message
  (card16 sequence)
  (window (window event-window))
  ((member 8 16 32) format)
  (sequence data))

(defun queue-event (display event-key &rest args &key append-p &allow-other-keys)
  ;; The event is put at the head of the queue if append-p is nil, else the tail.
  ;; Additional arguments depend on event-key, and are as specified above with
  ;; declare-event, except that both resource-ids and resource objects are accepted
  ;; in the event components.
  (declare (type display display)
	   (type event-key event-key)
	   (type boolean append-p)))



;;; From here on, there has been less coherent review of the interface:

;;;-----------------------------------------------------------------------------
;;; Window Manager Property functions

(defun wm-name (window)
  (declare (type window window)
	   (clx-values string)))

(defsetf wm-name (window) (name))

(defun wm-icon-name (window)
  (declare (type window window)
	   (clx-values string)))

(defsetf wm-icon-name (window) (name))

(defun get-wm-class (window)
  (declare (type window window)
	   (clx-values (or null name-string) (or null class-string))))

(defun set-wm-class (window resource-name resource-class)
  (declare (type window window)
	   (type (or null stringable) resource-name resource-class)))

(defun wm-command (window)
  ;; Returns a list whose car is a command string and 
  ;; whose cdr is the list of argument strings.
  (declare (type window window)
	   (clx-values (clx-list string))))

(defsetf wm-command (window) (command)
  ;; Uses PRIN1 inside the ANSI common lisp form WITH-STANDARD-IO-SYNTAX (or
  ;; equivalent), with elements of command separated by NULL characters.  This
  ;; enables 
  ;;   (with-standard-io-syntax (mapcar #'read-from-string (wm-command window)))
  ;; to recover a lisp command.
  (declare (type window window)
	   (type (clx-list stringable) command)))

(defun wm-client-machine (window)
  ;; Returns a list whose car is a command string and 
  ;; whose cdr is the list of argument strings.
  (declare (type window window)
	   (clx-values string)))

(defsetf wm-client-machine (window) (string)
  (declare (type window window)
	   (type stringable string)))

(defstruct wm-hints
  (input nil :type (or null (member :off :on)))
  (initial-state nil :type (or null (member :normal :iconic)))
  (icon-pixmap nil :type (or null pixmap))
  (icon-window nil :type (or null window))
  (icon-x nil :type (or null card16))
  (icon-y nil :type (or null card16))
  (icon-mask nil :type (or null pixmap))
  (window-group nil :type (or null resource-id))
  (flags 0 :type card32)    ;; Extension-hook.  Exclusive-Or'ed with the FLAGS field
  ;; may be extended in the future
  )

(defun wm-hints (window)
  (declare (type window window)
	   (clx-values wm-hints)))

(defsetf wm-hints (window) (wm-hints))


(defstruct wm-size-hints
  ;; Defaulted T to put the burden of remembering these on widget programmers.
  (user-specified-position-p t :type boolean) ;; True when user specified x y
  (user-specified-size-p t :type boolean)     ;; True when user specified width height
  (x nil :type (or null int16))		      ;; Obsolete
  (y nil :type (or null int16))		      ;; Obsolete
  (width nil :type (or null card16))	      ;; Obsolete
  (height nil :type (or null card16))	      ;; Obsolete
  (min-width nil :type (or null card16))
  (min-height nil :type (or null card16))
  (max-width nil :type (or null card16))
  (max-height nil :type (or null card16))
  (width-inc nil :type (or null card16))
  (height-inc nil :type (or null card16))
  (min-aspect nil :type (or null number))
  (max-aspect nil :type (or null number))
  (base-width nil :type (or null card16))
  (base-height nil :type (or null card16))
  (win-gravity nil :type (or null win-gravity)))

(defun wm-normal-hints (window)
  (declare (type window window)
	   (clx-values wm-size-hints)))

(defsetf wm-normal-hints (window) (wm-size-hints))

;; ICON-SIZES uses the SIZE-HINTS structure

(defun icon-sizes (window)
  (declare (type window window)
	   (clx-values wm-size-hints)))
  
(defsetf icon-sizes (window) (wm-size-hints))

(defun wm-protocols (window)
  (declare (type window window)
	   (clx-values protocols)))
  
(defsetf wm-protocols (window) (protocols)
  (declare (type window window)
	   (type (clx-list keyword) protocols)))

(defun wm-colormap-windows (window)
  (declare (type window window)
	   (clx-values windows)))
  
(defsetf wm-colormap-windows (window) (windows)
  (declare (type window window)
	   (type (clx-list window) windows)))

(defun transient-for (window)
  (declare (type window window)
	   (clx-values window)))

(defsetf transient-for (window) (transient)
  (declare (type window window transient)))

(defun set-wm-properties (window &rest options &key 
			  name icon-name resource-name resource-class command
			  hints normal-hints
			  ;; the following are used for wm-normal-hints
			  user-specified-position-p user-specified-size-p
			  program-specified-position-p program-specified-size-p
			  min-width min-height max-width max-height
			  width-inc height-inc min-aspect max-aspect
			  base-width base-height win-gravity
			  ;; the following are used for wm-hints
			  input initial-state icon-pixmap icon-window
			  icon-x icon-y icon-mask window-group)
  ;; Set properties for WINDOW.
  (declare (type window window)
	   (type (or null stringable) name icoin-name resource-name resource-class)
	   (type (or null list) command)
	   (type (or null wm-hints) hints)
	   (type (or null wm-size-hints) normal-hints)
	   (type boolean user-specified-position-p user-specified-size-p)
	   (type boolean program-specified-position-p program-specified-size-p)
	   (type (or null card16) min-width min-height max-width max-height width-inc height-inc base-width base-height win-gravity)
	   (type (or null number) min-aspect max-aspect)
	   (type (or null (member :off :on)) input)
	   (type (or null (member :normal :iconic)) initial-state)
	   (type (or null pixmap) icon-pixmap icon-mask)
	   (type (or null window) icon-window)
	   (type (or null card16) icon-x icon-y)
	   (type (or null resource-id) window-group)))

(defun iconify-window (window)
  (declare (type window window)))

(defun withdraw-window (window)
  (declare (type window window)))
 
(defstruct standard-colormap
  (colormap nil :type (or null colormap))
  (base-pixel 0 :type pixel)
  (max-color nil :type (or null color))
  (mult-color nil :type (or null color))
  (visual nil :type (or null visual-info))
  (kill nil :type (or (member nil :release-by-freeing-colormap)
		      drawable gcontext cursor colormap font)))

(defun rgb-colormaps (window property)
  (declare (type window window)
	   (type (member :rgb_default_map :rgb_best_map :rgb_red_map
			 :rgb_green_map :rgb_blue_map) property)
	   (clx-values (clx-list standard-colormap))))

(defsetf rgb-colormaps (window property) (standard-colormaps)
  (declare (type window window)
	   (type (member :rgb_default_map :rgb_best_map :rgb_red_map
			 :rgb_green_map :rgb_blue_map) property)
	   (type (clx-list standard-colormap) standard-colormaps)))

(defun cut-buffer (display &key (buffer 0) (type :string) (result-type 'string)
		                (transform #'card8->char) (start 0) end)
  ;; Return the contents of cut-buffer BUFFER
  (declare (type display display)
	   (type (integer 0 7) buffer)
	   (type xatom type)
	   (type array-index start)
	   (type (or null array-index) end)
	   (type t result-type)			;a sequence type
	   (type (or null (function (integer) t)) transform)
	   (clx-values sequence type format bytes-after)))

(defsetf cut-buffer (display buffer &key (type :string) (format 8)
			     (transform #'char->card8) (start 0) end) (data))

(defun rotate-cut-buffers (display &optional (delta 1) (careful-p t))
  ;; Positive rotates left, negative rotates right (opposite of actual
  ;; protocol request).  When careful-p, ensure all cut-buffer
  ;; properties are defined, to prevent errors.
  (declare (type display display)
	   (type int16 delta)
	   (type boolean careful-p)))

;;;-----------------------------------------------------------------------------
;;; Keycode mapping

(defun define-keysym-set (set first-keysym last-keysym)
  ;; Define all keysyms from first-keysym up to and including
  ;; last-keysym to be in SET (returned from the keysym-set function).
  ;; Signals an error if the keysym range overlaps an existing set.
 (declare (type keyword set)
	  (type keysym first-keysym last-keysym)))

(defun keysym-set (keysym)
  ;; Return the character code set name of keysym
  ;; Note that the keyboard set (255) has been broken up into its parts.
  (declare (type keysym keysym)
	   (clx-values keyword)))

(defun define-keysym (object keysym &key lowercase translate modifiers mask display)	              
  ;; Define the translation from keysym/modifiers to a (usually
  ;; character) object.  ANy previous keysym definition with
  ;; KEYSYM and MODIFIERS is deleted before adding the new definition.
  ;;
  ;; MODIFIERS is either a modifier-mask or list containing intermixed
  ;; keysyms and state-mask-keys specifying when to use this
  ;; keysym-translation.  The default is NIL.
  ;;
  ;; MASK is either a modifier-mask or list containing intermixed
  ;; keysyms and state-mask-keys specifying which modifiers to look at
  ;; (i.e.  modifiers not specified are don't-cares).
  ;; If mask is :MODIFIERS then the mask is the same as the modifiers
  ;; (i.e.  modifiers not specified by modifiers are don't cares)
  ;; The default mask is *default-keysym-translate-mask*
  ;;
  ;; If DISPLAY is specified, the translation will be local to DISPLAY,
  ;; otherwise it will be the default translation for all displays.
  ;;
  ;; LOWERCASE is used for uppercase alphabetic keysyms.  The value
  ;; is the associated lowercase keysym.  This information is used
  ;; by the keysym-both-case-p predicate (for caps-lock computations)
  ;; and by the keysym-downcase function.
  ;;
  ;; TRANSLATE will be called with parameters (display state OBJECT)
  ;; when translating KEYSYM and modifiers and mask are satisfied.
  ;; [e.g (zerop (logxor (logand state (or mask *default-keysym-translate-mask*))
  ;;                     (or modifiers 0)))
  ;;      when mask and modifiers aren't lists of keysyms]
  ;; The default is #'default-keysym-translate
  ;;
  (declare (type (or base-char t) object)
	   (type keysym keysym)
	   (type (or null mask16 (clx-list (or keysym state-mask-key)))
	         modifiers)
	   (type (or null (member :modifiers) mask16 (clx-list (or keysym state-mask-key)))
	         mask)
	   (type (or null display) display)
           (type (or null keysym) lowercase)
	   (type (function (display card16 t) t) translate)))

(defvar *default-keysym-translate-mask*
	(the (or (member :modifiers) mask16 (clx-list (or keysym state-mask-key)))
	     (logand #xff (lognot (make-state-mask :lock))))
  "Default keysym state mask to use during keysym-translation.")

(defun undefine-keysym (object keysym &key display modifiers &allow-other-keys)	              
  ;; Undefine the keysym-translation translating KEYSYM to OBJECT with MODIFIERS.
  ;; If DISPLAY is non-nil, undefine the translation for DISPLAY if it exists.
  (declare (type (or base-char t) object)
	   (type keysym keysym)
	   (type (or null mask16 (clx-list (or keysym state-mask-key)))
	         modifiers)
	   (type (or null display) display)))

(defun default-keysym-translate (display state object)
  ;; If object is a character, char-bits are set from state.
  ;; If object is a list, it is an alist with entries:
  ;; (base-char [modifiers] [mask-modifiers)
  ;; When MODIFIERS are specified, this character translation
  ;; will only take effect when the specified modifiers are pressed.
  ;; MASK-MODIFIERS can be used to specify a set of modifiers to ignore.
  ;; When MASK-MODIFIERS is missing, all other modifiers are ignored.
  ;; In ambiguous cases, the most specific translation is used.
  (declare (type display display)
	   (type card16 state)
	   (type t object)
	   (clx-values t))) ;; Object returned by keycode->character
   
(defmacro keysym (keysym &rest bytes)
  ;; Build a keysym.
  ;; If KEYSYM is an integer, it is used as the most significant bits of
  ;; the keysym, and BYTES are used to specify low order bytes. The last
  ;; parameter is always byte4 of the keysym.  If KEYSYM is not an
  ;; integer, the keysym associated with KEYSYM is returned.
  ;;
  ;; This is a macro and not a function macro to promote compile-time
  ;; lookup. All arguments are evaluated.
  (declare (type t keysym)
	   (type (clx-list card8) bytes)
	   (clx-values keysym)))

(defun character->keysyms (character &optional display)
  ;; Given a character, return a list of all matching keysyms.
  ;; If DISPLAY is given, translations specific to DISPLAY are used,
  ;; otherwise only global translations are used.
  ;; Implementation dependent function.
  ;; May be slow [i.e. do a linear search over all known keysyms]
  (declare (type t character)
	   (type (or null display) display)
	   (clx-values (clx-list keysym))))

(defun keycode->keysym (display keycode keysym-index)
  (declare (type display display)
	   (type card8 code)
	   (type card16 state)
	   (type card8 keysym-index)
	   (clx-values keysym)))

(defun keysym->keycodes (display keysym)
  ;; Return keycodes for keysym, as multiple values
  (declare (type display display)
	   (type keysym keysym)
	   (clx-values (or null keycode) (or null keycode) (or null keycode)))
  )

(defun keysym->character (display keysym &optional state)
  ;; Find the character associated with a keysym.
  ;; STATE is used for adding char-bits to character as follows:
  ;;    control -> char-control-bit
  ;;    mod-1 -> char-meta-bit
  ;;    mod-2 -> char-super-bit
  ;;    mod-3 -> char-hyper-bit
  ;; Implementation dependent function.
  (declare (type display display)
	   (type keysym keysym)
	   (type (or null card16) state)
	   (clx-values (or null character))))

(defun keycode->character (display keycode state &key keysym-index
	                   (keysym-index-function #'default-keysym-index))
  ;; keysym-index defaults to the result of keysym-index-function which
  ;; is called with the following parameters:
  ;; (char0 state caps-lock-p keysyms-per-keycode)
  ;; where char0 is the "character" object associated with keysym-index 0 and
  ;; caps-lock-p is non-nil when the keysym associated with the lock
  ;; modifier is for caps-lock.
  ;; STATE is also used for setting char-bits:
  ;;    control -> char-control-bit
  ;;    mod-1 -> char-meta-bit
  ;;    mod-2 -> char-super-bit
  ;;    mod-3 -> char-hyper-bit
  ;; Implementation dependent function.
  (declare (type display display)
	   (type card8 keycode)
	   (type card16 state)
	   (type (or null card8) keysym-index)
	   (type (or null (function (char0 state caps-lock-p keysyms-per-keycode) card8))
		 keysym-index-function)
	   (clx-values (or null character))))

(defun default-keysym-index (display keycode state)
  ;; Returns a keysym-index for use with keycode->character
  (declare (clx-values card8))
)

;;; default-keysym-index implements the following tables:
;;;
;;; control shift caps-lock character               character
;;;   0       0       0       #\a                      #\8
;;;   0       0       1       #\A                      #\8
;;;   0       1       0       #\A                      #\*
;;;   0       1       1       #\A                      #\*
;;;   1       0       0       #\control-A              #\control-8
;;;   1       0       1       #\control-A              #\control-8
;;;   1       1       0       #\control-shift-a        #\control-*
;;;   1       1       1       #\control-shift-a        #\control-*
;;;
;;; control shift shift-lock character               character
;;;   0       0       0       #\a                      #\8
;;;   0       0       1       #\A                      #\*
;;;   0       1       0       #\A                      #\*
;;;   0       1       1       #\A                      #\8
;;;   1       0       0       #\control-A              #\control-8
;;;   1       0       1       #\control-A              #\control-*
;;;   1       1       0       #\control-shift-a        #\control-*
;;;   1       1       1       #\control-shift-a        #\control-8

(defun state-keysymp (display state keysym)
  ;; Returns T when a modifier key associated with KEYSYM is on in STATE
  (declare (type display display)
	   (type card16 state)
	   (type keysym keysym)
	   (clx-values boolean)))

(defun mapping-notify (display request start count)
  ;; Called on a mapping-notify event to update
  ;; the keyboard-mapping cache in DISPLAY
  (declare (type display display)
	   (type (member :modifier :keyboard :pointer) request)
	   (type card8 start count)))

(defun keysym-in-map-p (display keysym keymap)
  ;; Returns T if keysym is found in keymap
  (declare (type display display)
	   (type keysym keysym)
	   (type (bit-vector 256) keymap)
	   (value boolean)))

(defun character-in-map-p (display character keymap)
  ;; Implementation dependent function.
  ;; Returns T if character is found in keymap
  (declare (type display display)
	   (type t character)
	   (type (bit-vector 256) keymap)
	   (value boolean)))

;;;-----------------------------------------------------------------------------
;;; Extensions

(defmacro define-extension (name &key events errors)
  ;; Define extension NAME with EVENTS and ERRORS.
  ;; Note: The case of NAME is important.
  ;; To define the request, Use:
  ;;     (with-buffer-request (display (extension-opcode ,name)) ,@body)
  ;;     See the REQUESTS file for lots of examples.
  ;; To define event handlers, use declare-event.
  ;; To define error handlers, use declare-error and define-condition.
  (declare (type stringable name)
	   (type (clx-list symbol) events errors)))

(defmacro extension-opcode (display name)
  ;; Returns the major opcode for extension NAME.
  ;; This is a macro to enable NAME to be interned for fast run-time
  ;; retrieval. 
  ;; Note: The case of NAME is important.
  (declare (type display display)
	   (type stringable name)
	   (clx-values card8)))

(defmacro define-error (error-key function)
  ;; Associate a function with ERROR-KEY which will be called with
  ;; parameters DISPLAY and REPLY-BUFFER and returns a plist of
  ;; keyword/value pairs which will be passed on to the error handler.
  ;; A compiler warning is printed when ERROR-KEY is not defined in a
  ;; preceding DEFINE-EXTENSION.
  ;; Note: REPLY-BUFFER may used with the READING-EVENT and READ-type
  ;;       macros for getting error fields. See DECODE-CORE-ERROR for
  ;        an example.
  (declare (type symbol error-key)
	   (type function function)))

;; All core errors use this, so we make it available to extensions.
(defun decode-core-error (display event &optional arg)
  ;; All core errors have the following keyword/argument pairs:
  ;;    :major integer
  ;;    :minor integer
  ;;    :sequence integer
  ;;    :current-sequence integer
  ;; In addition, many have an additional argument that comes from the
  ;; same place in the event, but is named differently.  When the ARG
  ;; argument is specified, the keyword ARG with card32 value starting
  ;; at byte 4 of the event is returned with the other keyword/argument
  ;; pairs.
  (declare (type display display)
	   (type reply-buffer event)
	   (type (or null keyword) arg)
	   (clx-values keyword/arg-plist)))

;; This isn't new, just extended.
(defmacro declare-event (event-codes &body declares)
  ;; Used to indicate the keyword arguments for handler functions in
  ;; process-event and event-case.
  ;; Generates functions used in SEND-EVENT.
  ;; A compiler warning is printed when all of EVENT-CODES are not
  ;; defined by a preceding DEFINE-EXTENSION.
  ;; See the INPUT file for lots of examples.
  (declare (type (or keyword (clx-list keywords)) event-codes)
	   (type (alist (field-type symbol) (field-names (clx-list symbol)))
                 declares)))

(defmacro define-gcontext-accessor (name &key default set-function copy-function)
  ;; This will define a new gcontext accessor called NAME.
  ;; Defines the gcontext-NAME accessor function and its defsetf.
  ;; Gcontext's will cache DEFAULT-VALUE and the last value SETF'ed when
  ;; gcontext-cache-p is true.  The NAME keyword will be allowed in
  ;; CREATE-GCONTEXT, WITH-GCONTEXT, and COPY-GCONTEXT-COMPONENTS.
  ;; SET-FUNCTION will be called with parameters (GCONTEXT NEW-VALUE)
  ;; from create-gcontext, and force-gcontext-changes.
  ;; COPY-FUNCTION will be called with parameters (src-gc dst-gc src-value)
  ;; from copy-gcontext and copy-gcontext-components.
  ;; The copy-function defaults to:
  ;; (lambda (ignore dst-gc value)
  ;;    (if value
  ;;	    (,set-function dst-gc value)
  ;;	  (error "Can't copy unknown GContext component ~a" ',name)))
  (declare (type symbol name)
	   (type t default)
	   (type symbol set-function) ;; required
	   (type symbol copy-function)))


;; To aid extension implementors in attaching additional information to
;; clx data structures, the following accessors (with SETF's) are
;; defined.  GETF can be used on these to extend the structures.

display-plist
screen-plist
visual-info-plist
gcontext-plist
font-plist
drawable-plist



;;; These have had perhaps even less review.

;;; Add some of the functionality provided by the C XLIB library.
;;;
;;; LaMott G. Oren, Texas Instruments  10/87
;;; 
;;; Design Contributors:
;;;	Robert W. Scheifler, MIT

;;;-----------------------------------------------------------------------------
;;; Regions (not yet implemented)

;;; Regions are arbitrary collections of pixels.  This is represented
;;; in the region structure as either a list of rectangles or a bitmap.

(defun make-region (&optional x y width height)
  ;; With no parameters, returns an empty region
  ;; If some parameters are given, all must be given.
  (declare (type (or null int16) x y width height)
	   (clx-values region)))

(defun region-p (thing))

(defun copy-region (region))

(defun region-empty-p (region)
  (declare (type region region)
	   (clx-values boolean)))

(defun region-clip-box (region)
  ;; Returns a region which is the smallest enclosing rectangle
  ;; enclosing REGION
  (declare (type region region)
	   (clx-values region)))

;; Accessors that return the boundaries of a region
(defun region-x (region))
(defun region-y (region))
(defun region-width (region))
(defun region-height (region))

(defsetf region-x (region) (x))
(defsetf region-y (region) (y))
;; Setting a region's X/Y translates the region

(defun region-intersection (&rest regions)
  "Returns a region which is the intersection of one or more REGIONS.
Returns an empty region if the intersection is empty.
If there are no regions given, return a very large region."
  (declare (type (clx-list region) regions)
	   (clx-values region)))

(defun region-union (&rest regions)
  "Returns a region which is the union of a number of REGIONS
 (i.e. the smallest region that can contain all the other regions)
 Returns the empty region if no regions are given."
  (declare (type (clx-list region) regions)
	   (clx-values region)))

(defun region-subtract (region subtract)
  "Returns a region containing the points that are in REGION but not in SUBTRACT"
  (declare (type region region subtract)
	   (clx-values region)))

(defun point-in-region-p (region x y)
  ;; Returns T when X/Y are a point within REGION.
  (declare (type region region)
	   (type int16 x y)
	   (clx-values boolean)))

(defun region-equal (a b)
  ;; Returns T when regions a and b contain the same points.
  ;; That is, return t when for every X/Y (point-in-region-p a x y)
  ;; equals (point-in-region-p b x y)
  (declare (type region a b)
	   (clx-values boolean)))

(defun subregion-p (large small)
  "Returns T if SMALL is within LARGE.
 That is, return T when for every X/Y (point-in-region-p small X Y)
 implies (point-in-region-p large X Y)."
  (declare (type region large small)
	   (clx-values boolean)))

(defun region-intersect-p (a b)
  "Returns T if A intersects B.
 That is, return T when there is some point common to regions A and B."
  (declare (type region a b)
	   (clx-values boolean)))

(defun map-region (region function &rest args)
  ;; Calls function with arguments (x y . args) for every point in REGION.
  (declare (type region region)
	   (type (function x y &rest args) function)))

;;   Why isn't it better to augment
;;   gcontext-clip-mask to deal with
;;   	(or null (member :none) pixmap rect-seq region)
;;   and force conversions on the caller?
;; Good idea.

;;(defun gcontext-clip-region (gcontext)
;;  ;; If the clip-mask of GCONTEXT is known, return it as a region.
;;  (declare (type gcontext gcontext)
;;	   (clx-values (or null region))))

;;(defsetf gcontext-clip-region (gcontext) (region)
;;  ;; Set the clip-rectangles or clip-mask for for GCONTEXT to include
;;  ;; only the pixels within REGION.
;;  (declare (type gcontext gcontext)
;;	   (type region region)))

(defun image->region (image)
  ;; Returns a region containing the 1 bits of a depth-1 image
  ;; Signals an error if image isn't of depth 1.
  (declare (type image image)
	   (clx-values region)))

(defun region->image (region)
  ;; Returns a depth-1 image containg 1 bits for every pixel in REGION.
  (declare (type region region)
	   (clx-values image)))

(defun polygon-region (points &optional (fill-rule :even-odd))
  (declare (type sequence points) ;(repeat-seq (integer x) (integer y))
	   (type (member :even-odd :winding) fill-rule)
	   (clx-values region)))

;;;-----------------------------------------------------------------------------
;;; IMAGE functions


(deftype bitmap () '(array bit (* *)))
(deftype pixarray () '(array pixel (* *)))

(defconstant *lisp-byte-lsb-first-p* #+lispm t #-lispm nil
	     "Byte order in pixel arrays")

(defstruct image
  ;; Public structure
  (width 0 :type card16 :read-only t)
  (height 0 :type card16 :read-only t)
  (depth 1 :type card8 :read-only t)
  (plist nil :type list))

;; Image-Plist accessors:
(defun image-name (image))
(defun image-x-hot (image))
(defun image-y-hot (image))
(defun image-red-mask (image))
(defun image-blue-mask (image))
(defun image-green-mask (image))

(defsetf image-name (image) (name))
(defsetf image-x-hot (image) (x))
(defsetf image-y-hot (image) (y))
(defsetf image-red-mask (image) (mask))
(defsetf image-blue-mask (image) (mask))
(defsetf image-green-mask (image) (mask))

(defstruct (image-x (:include image))
  ;; Use this format for shoveling image data
  ;; Private structure. Accessors for these NOT exported.
  (format :z-pixmap :type (member :bitmap :xy-pixmap :z-pixmap))
  (bytes-per-line 0 :type card16)
  (scanline-pad 32 :type (member 8 16 32))
  (bits-per-pixel 0 :type (member 1 4 8 16 24 32))
  (bit-lsb-first-p nil :type boolean)		; Bit order
  (byte-lsb-first-p nil :type boolean)		; Byte order
  (data #() :type (array card8 (*))))		; row-major

(defstruct (image-xy (:include image))
  ;; Public structure
  ;; Use this format for image processing
  (bitmap-list nil :type (clx-list bitmap)))

(defstruct (image-z (:include image))
  ;; Public structure
  ;; Use this format for image processing
  (bits-per-pixel 0 :type (member 1 4 8 16 24 32))
  (pixarray #() :type pixarray))

(defun create-image (&key (width (required-arg width))
		          (height (required-arg height))
		     depth data plist name x-hot y-hot
		     red-mask blue-mask green-mask
		     bits-per-pixel format scanline-pad bytes-per-line
		     byte-lsb-first-p bit-lsb-first-p )
  ;; Returns an image-x image-xy or image-z structure, depending on the
  ;; type of the :DATA parameter.
  (declare
    (type card16 width height)			; Required
    (type (or null card8) depth)		; Defualts to 1
    (type (or (array card8 (*))			;Returns image-x
	      (clx-list bitmap)			;Returns image-xy
	      pixarray) data)			;Returns image-z
    (type list plist)
    (type (or null stringable) name)
    (type (or null card16) x-hot y-hot)
    (type (or null pixel) red-mask blue-mask green-mask)
    (type (or null (member 1 4 8 16 24 32)) bits-per-pixel)

    ;; The following parameters are ignored for image-xy and image-z:
    (type (or null (member :bitmap :xy-pixmap :z-pixmap))
	  format)				; defaults to :z-pixmap
    (type (or null (member 8 16 32)) scanline-pad)
    (type (or null card16) bytes-per-line) ;default from width and scanline-pad
    (type boolean byte-lsb-first-p bit-lsb-first-p)
    (clx-values image)))

(defun get-image (drawable &key 
		  (x (required-arg x))
		  (y (required-arg y))
		  (width (required-arg width))
		  (height (required-arg height))
		  plane-mask format result-type)
  ;; Get an image from the server.
  ;; Format defaults to :z-pixmap.  Result-Type defaults from Format,
  ;; image-z for :z-pixmap, and image-xy for :xy-pixmap.
  ;; Plane-mask defaults to #xFFFFFFFF.
  ;; Returns an image-x image-xy or image-z structure, depending on the
  ;; result-type parameter.
  (declare (type drawable drawable)
	   (type int16 x y) ;; required
	   (type card16 width height) ;; required
	   (type (or null pixel) plane-mask)
	   (type (or null (member :xy-pixmap :z-pixmap)) format)
	   (type (or null (member image-x image-xy image-z)) result-type)
	   (clx-values image)))

(defun put-image (drawable gcontext image &key
		  (src-x 0) (src-y 0)
		  (x (required-arg x))
		  (y (required-arg y))
		  width height
		  bitmap-p)
  ;; When BITMAP-P, force format to be :bitmap when depth=1
  ;; This causes gcontext to supply foreground & background pixels.
  (declare (type drawable drawable)
	   (type gcontext gcontext)
	   (type image image)
	   (type int16 x y) ;; required
	   (type (or null card16) width height)
	   (type boolean bitmap-p)))

(defun copy-image (image &key (x 0) (y 0) width height result-type)
  ;; Copy with optional sub-imaging and format conversion.
  ;; result-type defaults to (type-of image)
  (declare (type image image)
	   (type card16 x y)
	   (type (or null card16) width height) ;; Default from image
	   (type (or null (member image-x image-xy image-z)) result-type)
	   (clx-values image)))

(defun read-bitmap-file (pathname)
  ;; Creates an image from a C include file in standard X11 format
  (declare (type (or pathname string stream) pathname)
	   (clx-values image)))

(defun write-bitmap-file (pathname image &optional name)
  ;; Writes an image to a C include file in standard X11 format
  ;; NAME argument used for variable prefixes.  Defaults to "image"
  (declare (type (or pathname string stream) pathname)
	   (type image image)
	   (type (or null stringable) name)))

;;;-----------------------------------------------------------------------------
;;; Resource data-base


(defun make-resource-database ()
  ;; Returns an empty resource data-base
  (declare (clx-values resource-database)))

(defun get-resource (database value-name value-class full-name full-class)
  ;; Return the value of the resource in DATABASE whose partial name
  ;; most closely matches (append full-name (list value-name)) and
  ;;                      (append full-class (list value-class)).
  (declare (type resource-database database)
	   (type stringable value-name value-class)
	   (type (clx-list stringable) full-name full-class)
	   (clx-values value)))

(defun add-resource (database name-list value)
  ;; name-list is a list of either strings or symbols. If a symbol, 
  ;; case-insensitive comparisons will be used, if a string,
  ;; case-sensitive comparisons will be used.  The symbol '* or
  ;; string "*" are used as wildcards, matching anything or nothing.
  (declare (type resource-database database)
	   (type (clx-list stringable) name-list)
	   (type t value)))

(defun delete-resource (database name-list)
  (declare (type resource-database database)
	   (type (clx-list stringable) name-list)))

(defun map-resource (database function &rest args)
  ;; Call FUNCTION on each resource in DATABASE.
  ;; FUNCTION is called with arguments (name-list value . args)
  (declare (type resource-database database)
	   (type (function ((clx-list stringable) t &rest t) t) function)
	   (clx-values nil)))

(defun merge-resources (database with-database)
  (declare (type resource-database database with-database)
	   (clx-values resource-database))
  (map-resource #'add-resource database with-database)
  with-database)

;; Note: with-input-from-string can be used with read-resources to define
;;       default resources in a program file.

(defun read-resources (database pathname &key key test test-not)
  ;; Merges resources from a file in standard X11 format with DATABASE.
  ;; KEY is a function used for converting value-strings, the default is
  ;; identity.  TEST and TEST-NOT are predicates used for filtering
  ;; which resources to include in the database.  They are called with
  ;; the name and results of the KEY function.
  (declare (type resource-database database)
	   (type (or pathname string stream) pathname)
	   (type (or null (function (string) t)) key)
	   (type (or null (function ((clx-list string) t) boolean))
                 test test-not)
	   (clx-values resource-database)))

(defun write-resources (database pathname &key write test test-not)
  ;; Write resources to PATHNAME in the standard X11 format.
  ;; WRITE is a function used for writing values, the default is #'princ
  ;; TEST and TEST-NOT are predicates used for filtering which resources
  ;; to include in the database.  They are called with the name and value.
  (declare (type resource-database database)
	   (type (or pathname string stream) pathname)
	   (type (or null (function (string stream) t)) write)
	   (type (or null (function ((clx-list string) t) boolean))
                 test test-not)))

(defun root-resources (screen &key database key test test-not)
  "Returns a resource database containing the contents of the root window
   RESOURCE_MANAGER property for the given SCREEN. If SCREEN is a display,
   then its default screen is used. If an existing DATABASE is given, then