home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 2000 April & May / AMIGA_2000_04.iso / patches / mesa3.1 / mesa-tk.lha / src-tk.aos / awindow.c next >
Encoding:
C/C++ Source or Header  |  1999-07-12  |  26.7 KB  |  884 lines

  1. /*
  2.  * $Id: Awindow.c 3.00 1998/09/21 19:17:47 NielsF Exp NielsF $
  3.  */
  4.  
  5. /*
  6.  * (c) Copyright 1993, Silicon Graphics, Inc.
  7.  * ALL RIGHTS RESERVED
  8.  * Permission to use, copy, modify, and distribute this software for
  9.  * any purpose and without fee is hereby granted, provided that the above
  10.  * copyright notice appear in all copies and that both the copyright notice
  11.  * and this permission notice appear in supporting documentation, and that
  12.  * the name of Silicon Graphics, Inc. not be used in advertising
  13.  * or publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.
  15.  *
  16.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  17.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  18.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  19.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  20.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  21.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  22.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  23.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  24.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  25.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  26.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  27.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  28.  *
  29.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  30.  */
  31.  
  32. /*
  33.  * $Log: Awindow.c $
  34.  * Revision 3.0  1998/09/21  19:17:47  NielsF
  35.  * bumped to mesa 3.0
  36.  * 
  37.  * $Log: Awindow.c $
  38.  * Revision 1.8  1997/06/25  19:17:47  StefanZ
  39.  * bumped to mesa 2.2
  40.  * 
  41.  * Revision 1.5  1996/10/07  00:18:11  StefanZ
  42.  * Mesa 2.0 Fixed
  43.  * 
  44.  * Revision 1.4  1996/08/14  22:53:16  StefanZ
  45.  * rev 1.3 tk fixes was from George 'Wulf' Krämer
  46.  * 
  47.  * Revision 1.3  1996/08/14  22:23:31  StefanZ
  48.  * Modified due to api change in amigaMesa
  49.  * Implemented div. Input modifier and changed windowhandling
  50.  * 
  51.  * Revision 1.2  1996/06/02  00:03:03  StefanZ
  52.  * Started to use RCS to keep track of code.
  53.  * 
  54.  */
  55.  
  56. /*
  57.  * History:
  58.  * 
  59.  * 1.0 960315 Now almost evetything is implemented
  60.  * 1.1 960425 Fixed problem with double closeclicks (Thanx to Daniel Jönsson)
  61.  * 1.2 960731 Modified due to api change in amigaMesa
  62.  * 1.3 Implemented div. Input modifier and changed windowhandling (Georg 'Wulf' Krämer)
  63.  * 
  64.  * TODO:
  65.  * Exposefunc
  66.  * 
  67.  * 
  68.  */
  69.  
  70. #include <signal.h>
  71.  
  72. #include <inline/exec.h>
  73. #include <inline/dos.h>
  74. #include <inline/graphics.h>
  75. #include <inline/layers.h>
  76. #include <inline/intuition.h>
  77.  
  78. #include <devices/inputevent.h>
  79. #include <intuition/intuition.h>
  80.  
  81. #include "awindow.h"
  82.  
  83. /**********************************************************************/
  84.  
  85. struct Library *DOSBase = NULL;
  86. struct Library *GfxBase = NULL;
  87. struct Library *IntuitionBase = NULL;
  88. struct Library *LayersBase = NULL;
  89.  
  90. void tkShutDown(int code) {
  91.   tkCloseWindow();
  92.   exit(0);
  93. }
  94.  
  95. void tkConstructor(void) {
  96.   if(!(DOSBase = OpenLibrary("dos.library", 39))) {
  97.     if ((DOSBase = OpenLibrary("dos.library", 0))) {
  98.       Printf("Can't open dos.library version 39\n");
  99.       CloseLibrary(DOSBase);
  100.     }
  101.     exit(10);
  102.   }
  103.   if(!(IntuitionBase = OpenLibrary("intuition.library", 39))) {
  104.     Printf("Can't open intuition.library version 39\n");
  105.     exit(10);
  106.   }
  107.   if(!(GfxBase = OpenLibrary("graphics.library", 39))) {
  108.     Printf("Can't open graphics.library version 39\n");
  109.     exit(10);
  110.   }
  111.   if(!(LayersBase = OpenLibrary("layers.library", 39))) {
  112.     Printf("Can't open layers.library version 39\n");
  113.     exit(10);
  114.   }
  115.  
  116.   signal(SIGINT, tkShutDown);
  117.   signal(SIGTERM, tkShutDown);
  118. }
  119.  
  120. void tkDestructor(void) {
  121.   if(DOSBase) {
  122.     CloseLibrary(DOSBase);
  123.     DOSBase = 0;
  124.   }
  125.   if(IntuitionBase) {
  126.     CloseLibrary(IntuitionBase);
  127.     IntuitionBase = 0;
  128.   }
  129.   if(GfxBase) {
  130.     CloseLibrary(GfxBase);
  131.     GfxBase = 0;
  132.   }
  133.   if(LayersBase) {
  134.     CloseLibrary(LayersBase);
  135.     LayersBase = 0;
  136.   }
  137. }
  138.  
  139. #if 0
  140. asm ("    .text;     .stabs \"___CTOR_LIST__\",22,0,0,_constructor");
  141. asm ("    .text;     .stabs \"___DTOR_LIST__\",22,0,0,_destructor");
  142. #endif
  143.  
  144. /**********************************************************************/
  145.  
  146. /* Some Bitmasks for amigaevents */
  147. #define ControlMask         (IEQUALIFIER_CONTROL)
  148. #define ShiftMask           (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT)
  149. #define MousePressedMask    (SELECTDOWN | MENUDOWN | MIDDLEDOWN)
  150.  
  151. /**********************************************************************/
  152.  
  153. struct tkWindow actWindow = {
  154.   0, 0, 100, 100,
  155.   TK_INDEX, 0,
  156.   0,
  157.  
  158.   NULL,
  159.   NULL,
  160.   NULL
  161. #ifdef USE_CLIP_LAYER
  162.   ,NULL
  163. #endif
  164. };
  165.  
  166. static GLboolean tkPopupEnable = TRUE;
  167.  
  168. float colorMaps[] = {
  169.     0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 1.000000, 
  170.     0.000000, 1.000000, 0.333333, 0.776471, 0.443137, 0.556863, 
  171.     0.443137, 0.556863, 0.219608, 0.666667, 0.666667, 0.333333, 
  172.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  173.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  174.     0.666667, 0.333333, 0.039216, 0.078431, 0.117647, 0.156863, 
  175.     0.200000, 0.239216, 0.278431, 0.317647, 0.356863, 0.400000, 
  176.     0.439216, 0.478431, 0.517647, 0.556863, 0.600000, 0.639216, 
  177.     0.678431, 0.717647, 0.756863, 0.800000, 0.839216, 0.878431, 
  178.     0.917647, 0.956863, 0.000000, 0.000000, 0.000000, 0.000000, 
  179.     0.000000, 0.000000, 0.000000, 0.000000, 0.247059, 0.247059, 
  180.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  181.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  182.     0.498039, 0.498039, 0.749020, 0.749020, 0.749020, 0.749020, 
  183.     0.749020, 0.749020, 0.749020, 0.749020, 1.000000, 1.000000, 
  184.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  185.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  186.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  187.     0.247059, 0.247059, 0.247059, 0.247059, 0.498039, 0.498039, 
  188.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  189.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  190.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  191.     1.000000, 1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 
  192.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  193.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  194.     0.247059, 0.247059, 0.498039, 0.498039, 0.498039, 0.498039, 
  195.     0.498039, 0.498039, 0.498039, 0.498039, 0.749020, 0.749020, 
  196.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  197.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  198.     1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  199.     0.000000, 0.000000, 0.000000, 0.000000, 0.247059, 0.247059, 
  200.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  201.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  202.     0.498039, 0.498039, 0.749020, 0.749020, 0.749020, 0.749020, 
  203.     0.749020, 0.749020, 0.749020, 0.749020, 1.000000, 1.000000, 
  204.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  205.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  206.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  207.     0.247059, 0.247059, 0.247059, 0.247059, 0.498039, 0.498039, 
  208.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  209.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  210.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  211.     1.000000, 1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 
  212.     1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 1.000000, 
  213.     0.333333, 0.443137, 0.776471, 0.556863, 0.443137, 0.219608, 
  214.     0.556863, 0.666667, 0.666667, 0.333333, 0.666667, 0.333333, 
  215.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  216.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  217.     0.039216, 0.078431, 0.117647, 0.156863, 0.200000, 0.239216, 
  218.     0.278431, 0.317647, 0.356863, 0.400000, 0.439216, 0.478431, 
  219.     0.517647, 0.556863, 0.600000, 0.639216, 0.678431, 0.717647, 
  220.     0.756863, 0.800000, 0.839216, 0.878431, 0.917647, 0.956863, 
  221.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  222.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  223.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  224.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  225.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  226.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  227.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  228.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  229.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  230.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  231.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  232.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  233.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  234.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  235.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  236.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  237.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  238.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  239.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  240.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  241.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  242.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  243.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  244.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  245.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  246.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  247.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  248.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  249.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  250.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  251.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  252.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  253.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  254.     0.854902, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  255.     1.000000, 1.000000, 1.000000, 1.000000, 0.333333, 0.443137, 
  256.     0.443137, 0.219608, 0.776471, 0.556863, 0.556863, 0.666667, 
  257.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  258.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  259.     0.666667, 0.333333, 0.666667, 0.333333, 0.039216, 0.078431, 
  260.     0.117647, 0.156863, 0.200000, 0.239216, 0.278431, 0.317647, 
  261.     0.356863, 0.400000, 0.439216, 0.478431, 0.517647, 0.556863, 
  262.     0.600000, 0.639216, 0.678431, 0.717647, 0.756863, 0.800000, 
  263.     0.839216, 0.878431, 0.917647, 0.956863, 0.000000, 0.000000, 
  264.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  265.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  266.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  267.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  268.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  269.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  270.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  271.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  272.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  273.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  274.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  275.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  276.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  277.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  278.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  279.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  280.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  281.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  282.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  283.     0.498039, 0.498039, 0.498039, 0.498039, 0.749020, 0.749020, 
  284.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  285.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  286.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  287.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  288.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  289.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  290.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  291.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  292.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  293.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  294.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  295.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  296.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
  297. };
  298.  
  299. float tkRGBMap[8][3] =
  300. {
  301.   {0, 0, 0},
  302.   {1, 0, 0},
  303.   {0, 1, 0},
  304.   {1, 1, 0},
  305.   {0, 0, 1},
  306.   {1, 0, 1},
  307.   {0, 1, 1},
  308.   {1, 1, 1}
  309. };
  310.  
  311. /**********************************************************************/
  312. /* Local prototypes */
  313.  
  314. #ifdef USE_CLIP_LAYER
  315. static struct Region *clipWindow(struct Window *win, LONG minX, LONG minY, LONG maxX, LONG maxY);
  316. static struct Region *clipWindowToBorders(struct Window *win);
  317. #endif
  318.  
  319. static void (*ExposeFunc) (int, int) = NULL;
  320. static void (*ReshapeFunc) (GLsizei, GLsizei) = NULL;
  321. static void (*DisplayFunc) (void) = NULL;
  322. static GLenum(*KeyDownFunc) (int, GLenum) = NULL;
  323. static GLenum(*MouseDownFunc) (int, int, GLenum) = NULL;
  324. static GLenum(*MouseUpFunc) (int, int, GLenum) = NULL;
  325. static GLenum(*MouseMoveFunc) (int, int, GLenum) = NULL;
  326. static void (*IdleFunc) (void) = NULL;
  327.  
  328. /**********************************************************************/
  329.  
  330. #ifdef USE_CLIP_LAYER
  331. /*
  332.  * clipWindow()
  333.  * Clip a window to a specified rectangle (given by upper left and
  334.  * lower right corner.)  the removed region is returned so that it
  335.  * may be re-installed later.
  336.  */
  337. static struct Region *clipWindow(struct Window *win, LONG minX, LONG minY, LONG maxX, LONG maxY)
  338. {
  339.   struct Region *new_region;
  340.   struct Rectangle my_rectangle;
  341.  
  342.   /* set up the limits for the clip */
  343.   my_rectangle.MinX = minX;
  344.   my_rectangle.MinY = minY;
  345.   my_rectangle.MaxX = maxX;
  346.   my_rectangle.MaxY = maxY;
  347.  
  348.   /* get a new region and OR in the limits. */
  349.   if ((new_region = NewRegion())) {
  350.     if (!OrRectRegion(new_region, &my_rectangle)) {
  351.       DisposeRegion(new_region);
  352.       new_region = NULL;
  353.     }
  354.   }
  355.  
  356.   /*
  357.    * Install the new region, and return any existing region.
  358.    * If the above allocation and region processing failed, then
  359.    * new_region will be NULL and no clip region will be installed.
  360.    */
  361.   return (InstallClipRegion(win->WLayer, new_region));
  362. }
  363.  
  364. /*
  365.  * clipWindowToBorders()
  366.  * clip a window to its borders.
  367.  * The removed region is returned so that it may be re-installed later.
  368.  */
  369. struct Region *clipWindowToBorders(struct Window *win)
  370. {
  371.   return (clipWindow(win, win->BorderLeft, win->BorderTop, win->Width - win->BorderRight - 1, win->Height - win->BorderBottom - 1));
  372. }
  373. #endif
  374.  
  375. /******************************************************************************/
  376.  
  377. ULONG atk_setIDCMPs(void)
  378. {
  379.   ULONG mask;
  380.  
  381.   mask = IDCMP_CLOSEWINDOW | IDCMP_NEWSIZE;
  382.   if (ReshapeFunc)
  383.     mask |= IDCMP_NEWSIZE;
  384.   if (KeyDownFunc)
  385.     mask |= (IDCMP_RAWKEY | IDCMP_VANILLAKEY);
  386.   if (MouseDownFunc || MouseUpFunc)
  387.     mask |= IDCMP_MOUSEBUTTONS;
  388.   if (MouseMoveFunc)
  389.     mask |= IDCMP_MOUSEMOVE;
  390.  
  391.   return (mask);
  392. }
  393.  
  394. /* TODO (*ExposeFunc)(int, int) */
  395.  
  396. void atk_modifyIDCMP(void)
  397. {
  398.   if (actWindow.window)
  399.     ModifyIDCMP(actWindow.window, atk_setIDCMPs());
  400. }
  401.  
  402. int atk_FixKeyRAW(char c)
  403. {
  404.   int key;
  405.  
  406.   switch (c) {
  407.     case CURSORLEFT:
  408.       key = TK_LEFT;
  409.       break;
  410.     case CURSORUP:
  411.       key = TK_UP;
  412.       break;
  413.     case CURSORRIGHT:
  414.       key = TK_RIGHT;
  415.       break;
  416.     case CURSORDOWN:
  417.       key = TK_DOWN;
  418.       break;
  419.     default:
  420.       key = GL_FALSE;
  421.       break;
  422.   }
  423.   return (key);
  424. }
  425.  
  426. /******************************************************************************/
  427. /* cursor.c                                      */
  428. /******************************************************************************/
  429.  
  430. void tkNewCursor(GLint id, GLubyte * shapeBuf, GLubyte * maskBuf, GLenum fgColor,
  431.          GLenum bgColor, GLint hotX, GLint hotY) {}
  432. void tkSetCursor(GLint id) {}
  433.  
  434. /******************************************************************************/
  435. /*                                          */
  436. /******************************************************************************/
  437.  
  438. void tkErrorPopups(GLboolean bEnable)
  439. {
  440.   tkPopupEnable = bEnable;
  441. }
  442.  
  443. /******************************************************************************/
  444. /* window.c                                      */
  445. /******************************************************************************/
  446.  
  447. void tkCloseWindow(void)
  448. {
  449.   if (actWindow.context) {
  450.     amigaMesaDestroyContext(actWindow.context);
  451.     actWindow.context = NULL;
  452.   }
  453. #ifdef USE_CLIP_LAYER
  454.   if (actWindow.clipreg) {
  455.     actWindow.clipreg = InstallClipRegion(actWindow.window->WLayer, actWindow.clipreg);
  456.     DisposeRegion(actWindow.clipreg);
  457.     actWindow.clipreg = NULL;
  458.   }
  459. #endif
  460.   if (actWindow.window) {
  461.     CloseWindow(actWindow.window);
  462.     actWindow.window = NULL;
  463.   }
  464. }
  465.  
  466. void tkInitDisplayModePolicy(GLenum type)
  467. {
  468.   actWindow.dmPolicy = type;
  469. }
  470.  
  471. GLenum tkInitDisplay(void)
  472. {
  473.   return GL_TRUE;
  474. }
  475.  
  476. void tkInitDisplayMode(GLenum type)
  477. {
  478.   actWindow.type = type;
  479. }
  480.  
  481. GLenum tkInitDisplayModeID(GLint ID)
  482. {
  483.   if(!actWindow.window)
  484.     return GL_FALSE;
  485.   else {
  486.     actWindow.ID = ID;
  487.     return GL_TRUE;
  488.   }
  489. }
  490.  
  491. void tkInitPosition(int x, int y, int width, int height)
  492. {
  493.   actWindow.left = x;
  494.   actWindow.top = y;
  495.   actWindow.width = width;
  496.   actWindow.height = height;
  497. }
  498.  
  499. GLenum tkInitWindow(char *title)
  500. {
  501.   GLenum Result = GL_FALSE, RGB_Flag = GL_TRUE, DB_Flag = GL_FALSE;
  502.  
  503.   if (!(actWindow.screen = LockPubScreen("Mesa")))
  504.     actWindow.screen = LockPubScreen(NULL);
  505.   if (actWindow.screen) {
  506.     if (actWindow.type & TK_INDEX)
  507.       RGB_Flag = GL_FALSE;
  508.     if (actWindow.type & TK_DOUBLE)
  509.       DB_Flag = GL_TRUE;
  510.  
  511.     /* open the window on the public actWindow.screen */
  512.     actWindow.window = OpenWindowTags(NULL,
  513.                       WA_Left, actWindow.left,
  514.                       WA_Top, actWindow.left,
  515.                       WA_InnerWidth, actWindow.width,
  516.                       WA_InnerHeight, actWindow.height,
  517.                       WA_ReportMouse, TRUE,
  518.                       WA_RMBTrap, TRUE,                       /* Detect right mouse events, no Menus */
  519.                       WA_MinWidth, 100, WA_MinHeight, 30,
  520.                       WA_MaxWidth, -1, WA_MaxHeight, -1,
  521.                       WA_IDCMP, atk_setIDCMPs(),
  522.                       WA_Flags, WFLG_SIZEGADGET | WFLG_DRAGBAR | WFLG_REPORTMOUSE | WFLG_CLOSEGADGET | WFLG_DEPTHGADGET | WFLG_SIZEBBOTTOM | ((DB_Flag) ? WFLG_SIMPLE_REFRESH : WFLG_NOCAREREFRESH),
  523.                       WA_Title, (ULONG)title,
  524.                       WA_PubScreen, (ULONG)actWindow.screen,
  525.                       TAG_END);
  526.     /*
  527.      * Unlock the actWindow.screen.  The window now acts as a lock on
  528.      * the actWindow.screen, and we do not need the actWindow.screen after the
  529.      * window has been closed.  
  530.      */
  531.     UnlockPubScreen(NULL, actWindow.screen);
  532.  
  533.     if (actWindow.window) {
  534.       amigaMesaBuffer buffer = NULL;
  535.  
  536. #ifdef USE_CLIP_LAYER
  537.       actWindow.clipreg = clipWindowToBorders(actWindow.window);
  538. #endif
  539.  
  540.       actWindow.context = amigaMesaCreateContextTags(AMA_Window, (ULONG)actWindow.window,
  541.                              AMA_RastPort, (ULONG)actWindow.window->RPort,
  542.                                  AMA_Screen, (ULONG)actWindow.window->WScreen,
  543.                              AMA_DoubleBuffer, DB_Flag,
  544.                              AMA_RGBMode, RGB_Flag,
  545.                              AMA_Left, actWindow.window->BorderLeft,
  546.                              AMA_Bottom, actWindow.window->BorderBottom,
  547.                              AMA_Width, actWindow.width,
  548.                              AMA_Height, actWindow.height,
  549.                              TAG_DONE, 0);
  550.       amigaMesaGetContextTags(actWindow.context, AMA_Buffer, &buffer, TAG_DONE, 0);
  551.       if (buffer)
  552.         amigaMesaMakeCurrent(actWindow.context, buffer);
  553.  
  554.       tkSetRGBMap(256, colorMaps);
  555.  
  556.       Result = GL_TRUE;
  557.     }
  558.     else
  559.       Printf("Failed to open a tkWindow.\n");
  560.   }
  561.  
  562.   return Result;
  563. }
  564.  
  565. /*
  566.  * You cannot just call DestroyWindow() here.  The programs do not expect
  567.  * tkQuit() to return;  DestroyWindow() just sends a WM_DESTROY message
  568.  */
  569.  
  570. void tkQuit(void)
  571. {
  572.   tkCloseWindow();
  573.   exit(0);
  574. }
  575.  
  576. void tkSwapBuffers(void)
  577. {
  578.   amigaMesaSwapBuffers(actWindow.context);
  579. }
  580.  
  581. /******************************************************************************/
  582. /* events.c                                      */
  583. /******************************************************************************/
  584.  
  585. void tkExec(void)
  586. {
  587.   struct IntuiMessage *msg, cmsg;
  588.   BOOL wanttoquit = FALSE;
  589.   BOOL press = FALSE;
  590.   int key, id;
  591.  
  592.   /* Redraw handling changed by Wulf 11. Aug. 96 to ensure the
  593.    * same handling under all systems 
  594.    */
  595.   GLenum Redraw = GL_FALSE;
  596.  
  597.   if (ReshapeFunc)
  598.     (*ReshapeFunc) (InnerWidth(actWindow.window), InnerHeight(actWindow.window));
  599.  
  600.   if (DisplayFunc)
  601.     (*DisplayFunc) ();
  602.  
  603.   while (!wanttoquit) {
  604.     if ((msg = (struct IntuiMessage *)GetMsg(actWindow.window->UserPort))) {
  605.       cmsg = *msg;
  606.  
  607.       ReplyMsg((struct Message *)msg);
  608.  
  609.       /* after ReplyMsg, you are nolonger allowed to use msg ! */
  610.       switch (cmsg.Class) {
  611.     case IDCMP_NEWSIZE:
  612.       /* Sizes should be adjusted by border, but painting isn't adjusted :-( */
  613.       actWindow.width = InnerWidth(cmsg.IDCMPWindow);
  614.       actWindow.height = InnerHeight(cmsg.IDCMPWindow);
  615. #ifdef USE_CLIP_LAYER
  616.       if (actWindow.clipreg)                        /* Get rid of last clipping region */
  617.         DisposeRegion(InstallClipRegion(cmsg.IDCMPWindow->WLayer, NULL));
  618.       clipWindowToBorders(cmsg.IDCMPWindow);
  619. #endif
  620.       if (ReshapeFunc)
  621.         (*ReshapeFunc) (actWindow.width, actWindow.height);
  622.       Redraw = GL_TRUE;                            /* very likely to make everything new */
  623.       break;
  624.     case IDCMP_RAWKEY:
  625.       key = atk_FixKeyRAW(cmsg.Code);
  626.       if (key && KeyDownFunc) {
  627.         GLenum mask;
  628.  
  629.         mask = 0;
  630.         if (cmsg.Qualifier & ControlMask)
  631.           mask |= TK_CONTROL;
  632.         if (cmsg.Qualifier & ShiftMask)
  633.           mask |= TK_SHIFT;
  634.         Redraw = (*KeyDownFunc) (key, mask);
  635.       }
  636.       break;
  637.     case IDCMP_VANILLAKEY:
  638.       if (KeyDownFunc) {
  639.         GLenum mask;
  640.  
  641.         mask = 0;
  642.         if (cmsg.Qualifier & ControlMask)
  643.           mask |= TK_CONTROL;
  644.         if (cmsg.Qualifier & ShiftMask)
  645.           mask |= TK_SHIFT;
  646.         key = cmsg.Code;
  647.         Redraw = (*KeyDownFunc) (key, mask);
  648.       }
  649.       break;
  650.     case IDCMP_MOUSEMOVE:
  651.       if (MouseMoveFunc) {
  652.         int x = cmsg.MouseX, y = cmsg.MouseY;
  653.  
  654.         Redraw = (*MouseMoveFunc) (x, y, press);
  655.       }
  656.       break;
  657.     case IDCMP_MOUSEBUTTONS:
  658.       /* Buttonhandling changed Wulf 11.08.96 */
  659.       {
  660.         GLenum mask = 0;
  661.         int x = cmsg.MouseX, y = cmsg.MouseY;
  662.  
  663.         switch (cmsg.Code) {
  664.           case SELECTDOWN:
  665.           case SELECTUP:
  666.         mask |= TK_LEFTBUTTON;
  667.         break;
  668.           case MIDDLEDOWN:
  669.           case MIDDLEUP:
  670.         mask |= TK_MIDDLEBUTTON;
  671.         break;
  672.           case MENUDOWN:
  673.           case MENUUP:
  674.         mask |= TK_RIGHTBUTTON;
  675.         break;
  676.         }
  677.         if (cmsg.Qualifier & ControlMask)
  678.           mask |= TK_CONTROL;
  679.         if (cmsg.Qualifier & ShiftMask)
  680.           mask |= TK_SHIFT;
  681.         if ((cmsg.Code & MousePressedMask)) {
  682.           press = mask;
  683.           if (MouseDownFunc)
  684.         Redraw = (*MouseDownFunc) (x, y, mask);
  685.         }
  686.         else {
  687.           if (MouseUpFunc)
  688.         Redraw = (*MouseUpFunc) (x, y, mask);
  689.           press = 0;
  690.         }
  691.       }
  692.       break;
  693.     case IDCMP_REFRESHWINDOW:                        /* received only in DB-mode, non-DB-modes are NoCareRefresh */
  694.       BeginRefresh(cmsg.IDCMPWindow);
  695.       tkSwapBuffers();                            /* put current contents into damaged region */
  696.       EndRefresh(cmsg.IDCMPWindow, TRUE);
  697.       if (ExposeFunc)
  698.         (*ExposeFunc) (actWindow.width, actWindow.height);
  699.       break;
  700.     case IDCMP_CLOSEWINDOW:
  701.       wanttoquit = TRUE;
  702.       break;
  703.     default:
  704.       break;
  705.       }
  706.     }
  707.     /* TODO Fill this with tests and call apropriate functions */
  708.     if (IdleFunc) {
  709.       (*IdleFunc) ();
  710.       Redraw = GL_TRUE;
  711.     }
  712.     if ((Redraw == GL_TRUE) && DisplayFunc) {                       /* Redraw handling changed by Wulf 11. Aug. 96 */
  713.       (*DisplayFunc) ();
  714.       Redraw = GL_FALSE;
  715.     }
  716.     if (!wanttoquit && !IdleFunc) {
  717.       WaitPort(actWindow.window->UserPort);
  718.     }
  719.   }
  720.   tkQuit();                                       /* do not return after quit */
  721. }
  722.  
  723. void tkExposeFunc(void (*Func) (int, int))
  724. {
  725.   ExposeFunc = Func;
  726.   atk_modifyIDCMP();
  727. }
  728.  
  729. void tkReshapeFunc(void (*Func) (GLsizei, GLsizei))
  730. {
  731.   ReshapeFunc = Func;
  732.   atk_modifyIDCMP();
  733. }
  734.  
  735. void tkDisplayFunc(void (*Func) (void))
  736. {
  737.   DisplayFunc = Func;
  738. }
  739.  
  740. void tkKeyDownFunc(GLenum(*Func) (int, GLenum))
  741. {
  742.   KeyDownFunc = Func;
  743.   atk_modifyIDCMP();
  744. }
  745.  
  746. void tkMouseDownFunc(GLenum(*Func) (int, int, GLenum))
  747. {
  748.   MouseDownFunc = Func;
  749.   atk_modifyIDCMP();
  750. }
  751.  
  752. void tkMouseUpFunc(GLenum(*Func) (int, int, GLenum))
  753. {
  754.   MouseUpFunc = Func;
  755.   atk_modifyIDCMP();
  756. }
  757.  
  758. void tkMouseMoveFunc(GLenum(*Func) (int, int, GLenum))
  759. {
  760.   MouseMoveFunc = Func;
  761.   atk_modifyIDCMP();
  762. }
  763.  
  764. void tkIdleFunc(void (*Func) (void))
  765. {
  766.   IdleFunc = Func;
  767. }
  768.  
  769. /******************************************************************************/
  770. /* getset.c                                       */
  771. /******************************************************************************/
  772.  
  773. GLint tkGetColorMapSize(void)
  774. {
  775.   /*
  776.    * AmigaMesa simulates 8bit-Index in any case,
  777.    * so we have 256 colors allways
  778.    */
  779.   return (256);
  780. }
  781.  
  782. void tkGetMouseLoc(int *x, int *y)
  783. {
  784.   *x = actWindow.window->MouseX;
  785.   *y = actWindow.window->MouseY;
  786. }
  787.  
  788. void tkGetSystem(TKenum type, void *ptr)
  789. {
  790.   switch (type) {
  791.     case TK_AOS_SCREEN:
  792.       *((struct Screen **)ptr) = actWindow.screen;
  793.       break;
  794.     case TK_AOS_WINDOW:
  795.       *((struct Window **)ptr) = actWindow.window;
  796.       break;
  797.     case TK_AOS_CONTEXT:
  798.       *((struct amigamesa_context **)ptr) = actWindow.context;
  799.       break;
  800.     default:
  801.       *((int **)ptr) = 0;
  802.       break;
  803.   }
  804. }
  805.  
  806. void tkSetFogRamp(int density, int startIndex)
  807. {
  808.   int fogValues, colorValues;
  809.   int i, j, k;
  810.   float intensity;
  811.  
  812.   fogValues = 1 << density;
  813.   colorValues = 1 << startIndex;
  814.   for (i = 0; i < colorValues; i++) {
  815.     for (j = 0; j < fogValues; j++) {
  816.       k = i * fogValues + j;
  817.       intensity = (i * fogValues + j * colorValues) / 255.0;
  818.       tkSetOneColor(k, intensity, intensity, intensity);
  819.     }
  820.   }
  821. }
  822.  
  823. void tkSetGreyRamp(void)
  824. {
  825.   int i;
  826.   float intensity;
  827.  
  828.   for (i = 0; i < 255; i++) {
  829.     intensity = i / 255.0;
  830.     tkSetOneColor(i, intensity, intensity, intensity);
  831.   }
  832. }
  833.  
  834. void tkSetOneColor(int index, float r, float g, float b)
  835. {
  836.   amigaMesaSetOneColor(actWindow.context, index, r, g, b);
  837. }
  838.  
  839. void tkSetOverlayMap(int size, float *rgb)
  840. {
  841.   Printf("tkSetOverlayMap(%d,%f) TODO\n", size, *rgb);
  842. }
  843.  
  844. void tkSetRGBMap(int Size, float *Values)
  845. {
  846.   int i;
  847.  
  848.   if (Size > 256)
  849.     Size = 256;
  850.  
  851.   for (i = 0; i < Size; i++) {
  852.     tkSetOneColor(i, Values[           i],
  853.              Values[Size     + i],
  854.              Values[Size * 2 + i]);
  855.   }
  856. }
  857.  
  858. GLenum tkSetWindowLevel(GLenum level)
  859. {
  860.   Printf("tkSetWindowLevel(%d) TODO\n", level);
  861.   return GL_FALSE;
  862. }
  863.  
  864. /******************************************************************************/
  865. /*                                          */
  866. /******************************************************************************/
  867.  
  868. GLint tkGetDisplayModeID(void)
  869. {
  870.   return actWindow.ID;
  871. }
  872.  
  873. GLint tkGetDisplayModePolicy(void)
  874. {
  875.   return actWindow.dmPolicy;
  876. }
  877.  
  878. GLenum tkGetDisplayMode(void)
  879. {
  880.   return actWindow.type;
  881. }
  882.  
  883. #undef USE_CLIP_LAYER
  884.