Annotation of Amaya/amaya/AHTEvntrg.c, revision 1.15

1.1       cvs         1: /*                                                                 AHTEvntrg.c
1.2       cvs         2: **     Amaya EVENT MANAGER
1.1       cvs         3: **
                      4: */
                      5: 
1.2       cvs         6: /* Amaya includes  */
                      7: #define THOT_EXPORT extern
                      8: 
                      9: /* Implementation dependent include files */
                     10: #include "amaya.h"
                     11: #include "AHTBridge_f.h"
1.1       cvs        12: #include <assert.h>                    /* @@@ Should be in sysdep.h @@@ */
1.2       cvs        13: #include <fcntl.h>
1.1       cvs        14: 
                     15: static HWND HTSocketWin;
                     16: static unsigned long HTwinMsg;
                     17: 
                     18: /* Type definitions and global variables etc. local to this module */
                     19: 
                     20: PRIVATE int HTEndLoop = 0;                    /* If !0 then exit event loop */
                     21: typedef unsigned long DWORD;
                     22: 
                     23: /*
                     24: ** our internal structure to hold a socket, it's request 
                     25: ** and our action. For version 1, we allow one action per socket
                     26: */
                     27: 
                     28: typedef struct rq_t RQ;
                     29: 
                     30: /*
                     31: ** an action consists of a request, a set of requested operations 
                     32: ** a HTEventCallback function, and a priority (priority is not used in this
                     33: ** version)
                     34: */
                     35: 
                     36: #ifndef WWW_MSWINDOWS 
                     37: typedef void * HANDLE ;
                     38: #endif
                     39: 
1.2       cvs        40: 
1.1       cvs        41: /* ------------------------------------------------------------------------- */
                     42: 
                     43: /*  HTEventrg_loop
                     44: **  ------------
                     45: **  event loop: that is, we wait for activity from one of our registered 
                     46: **  channels, and dispatch on that.
                     47: **
                     48: **  There are now two versions of the event loop. The first is if you want
                     49: **  to use async I/O on windows, and the other is if you want to use normal
                     50: **  Unix setup with sockets
                     51: */
1.2       cvs        52: 
1.1       cvs        53: /* only responsible for WM_TIMER and WSA_AsyncSelect */        
1.2       cvs        54: PUBLIC LRESULT CALLBACK AmayaAsyncWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1.1       cvs        55: {
                     56:     WORD event;
                     57:     SOCKET sock;
1.2       cvs        58:     HTEventCallback    *cbf;
                     59:        HTRequest *rqp;
1.1       cvs        60: 
                     61:     if (uMsg != HTwinMsg)      /* not our async message */
                     62:        return (DefWindowProc(hwnd, uMsg, wParam, lParam));
                     63: 
                     64:     event = LOWORD(lParam);
                     65:     sock = (SOCKET)wParam;
1.2       cvs        66: 
                     67:     cbf = (HTEventCallback *) __RetrieveCBF (sock, FD_WRITE, &rqp);
                     68: 
1.4       cvs        69:     if (event & FD_CLOSE) {
                     70:                /* close the socket and unregister it from the Windows environment */
                     71:        if (HTEventrg_dispatch((int)sock, FD_READ) != HT_OK)
                     72:                HTEndLoop = -1;
                     73:            WSAAsyncSelect(sock, HTSocketWin, 0, 0);
                     74:                return 0;
                     75:        }
1.1       cvs        76:     if (event & (FD_READ | FD_ACCEPT))
                     77:        if (HTEventrg_dispatch((int)sock, FD_READ) != HT_OK) {
                     78:            HTEndLoop = -1;
                     79:            return 0;
                     80:        }
                     81:     if (event & (FD_WRITE | FD_CONNECT))
                     82:        if (HTEventrg_dispatch((int)sock, FD_WRITE) != HT_OK) {
                     83:            HTEndLoop = -1;
                     84:            return 0;
                     85:        }
                     86:     if (event & FD_OOB)
                     87:        if (HTEventrg_dispatch((int)sock, FD_OOB) != HT_OK) {
                     88:            HTEndLoop = -1;
                     89:            return 0;
1.2       cvs        90:        }
1.3       cvs        91:                
1.1       cvs        92:     return (0);
                     93: }
                     94: 
                     95: 
1.2       cvs        96: 
1.11      cvs        97: ThotBool AHTEventInit (void)
1.1       cvs        98: {
                     99:     /*
                    100:     ** We are here starting a hidden window to take care of events from
                    101:     **  the async select() call in the async version of the event loop in
                    102:     ** the Internal event manager (HTEvntrg.c)
                    103:     */
1.14      cvs       104:     static char className[] = "AsyncWindowClass";
1.1       cvs       105:     WNDCLASS wc;
                    106:     OSVERSIONINFO osInfo;
                    107:     
                    108:     wc.style=0;
                    109:     wc.lpfnWndProc=(WNDPROC)AmayaAsyncWindowProc;
                    110:     wc.cbClsExtra=0;
                    111:     wc.cbWndExtra=0;
                    112:     wc.hIcon=0;
                    113:     wc.hCursor=0;
                    114:     wc.hbrBackground=0;
                    115:     wc.lpszMenuName=(LPSTR)0;
                    116:     wc.lpszClassName=className;
                    117: 
                    118:     osInfo.dwOSVersionInfoSize = sizeof(osInfo);
                    119:     GetVersionEx(&osInfo);
                    120:     if (osInfo.dwPlatformId == VER_PLATFORM_WIN32s || osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
                    121:        wc.hInstance=GetModuleHandle(NULL); /* 95 and non threaded platforms */
                    122:     else
                    123:        wc.hInstance=GetCurrentProcess(); /* NT and hopefully everything following */
                    124:     if (!RegisterClass(&wc)) {
                    125:        HTTrace("HTLibInit.. Can't RegisterClass \"%s\"\n", className);
                    126:            return NO;
                    127:     }
                    128:     if (!(HTSocketWin = CreateWindow(className, "WWW_WIN_ASYNC", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 
                    129:                                      CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance,0))) {
1.15    ! cvs       130:        char *space = TtaGetMemory (50);
1.6       cvs       131:        HTTrace("HTLibInit.. Can't CreateWindow \"WWW_WIN_ASYNC\" - error:");
                    132:        sprintf(space, "%ld\n", GetLastError());
                    133:        HTTrace(space);
                    134:           TtaFreeMemory (space);
                    135:        return NO;
1.1       cvs       136:     }
                    137:     HTwinMsg = WM_USER;  /* use first available message since app uses none */
1.2       cvs       138:     HTEventrg_setWinHandle  (HTSocketWin, HTwinMsg);
1.1       cvs       139: #ifdef _WINSOCKAPI_
                    140:     /*
                    141:     ** Initialise WinSock DLL. This must also be shut down! PMH
                    142:     */
                    143:     {
                    144:         WSADATA            wsadata;
                    145:        if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
                    146:            if (WWWTRACE)
                    147:                HTTrace("HTEventInit. Can't initialize WinSoc\n");
                    148:             WSACleanup();
                    149:             return NO;
                    150:         }
                    151:         if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
                    152:             if (WWWTRACE)
                    153:                HTTrace("HTEventInit. Bad version of WinSoc\n");
                    154:             WSACleanup();
                    155:             return NO;
                    156:         }
                    157:        if (APP_TRACE)
                    158:            HTTrace("HTEventInit. Using WinSoc version \"%s\".\n", 
                    159:                    wsadata.szDescription);
                    160:     }
                    161: #endif /* _WINSOCKAPI_ */
                    162: 
1.2       cvs       163:     HTEvent_setRegisterCallback(AHTEvent_register);
                    164:        HTEvent_setUnregisterCallback (AHTEvent_unregister);
1.1       cvs       165:     return YES;
                    166: }
                    167: 
                    168: PUBLIC BOOL AHTEventTerminate (void)
                    169: {
                    170: #ifdef _WINSOCKAPI_
                    171:     WSACleanup();
                    172: #endif
                    173: 
                    174:     DestroyWindow(HTSocketWin);
                    175:     return YES;
                    176: }

Webmaster