Annotation of Amaya/amaya/AHTBridge.c, revision 1.84

1.9       cvs         1: /*
                      2:  *
1.84    ! vatton      3:  *  (c) COPYRIGHT INRIA and W3C, 1996-2003
1.9       cvs         4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.75      cvs         7:   
1.14      cvs         8: /*
                      9:  * AHTBridge.c : This module implements the callback setup and
                     10:  * handlers between the Xt, libwww, and Amaya procedures. It's responsible
                     11:  * for assigning, modifying, and supressing Xt events to the active
                     12:  * requests.
                     13:  *
                     14:  * Author: J Kahan
1.67      cvs        15:  *         J. K./R. Guetari. Windows NT/95 routines
1.79      cvs        16:  *         J. K./S. Gully GTK routines
1.14      cvs        17:  *
                     18:  */
1.68      cvs        19: #ifdef _GTK
                     20: #include <glib.h>
                     21: #endif /* _GTK */
                     22: 
1.16      cvs        23: #define THOT_EXPORT extern
1.4       cvs        24: #include "amaya.h"
1.10      cvs        25: #include "AHTBridge_f.h"
                     26: #include "AHTFWrite_f.h"
                     27: #include "query_f.h"
                     28: #include "answer_f.h"
1.64      cvs        29: #include "HTEvtLst.h"
1.4       cvs        30: 
1.58      cvs        31: #if 0
                     32: #define DEBUG_LIBWWW
                     33: #define THD_TRACE 1
                     34: #endif
                     35: 
1.22      cvs        36: #ifndef _WINDOWS
1.79      cvs        37: #ifndef _GTK
1.4       cvs        38: /* Amaya's X appcontext */
1.16      cvs        39: extern ThotAppContext app_cont;
1.79      cvs        40: #endif /* !_GTK */
1.4       cvs        41: 
1.32      cvs        42: /* Private functions */
1.58      cvs        43: static void         RequestRegisterReadXtevent (SOCKET);
                     44: static void         RequestKillReadXtevent (SOCKET);
                     45: static void         RequestRegisterWriteXtevent ( SOCKET);
                     46: static void         RequestKillWriteXtevent (SOCKET);
                     47: static void         RequestRegisterExceptXtevent ( SOCKET);
                     48: static void         RequestKillExceptXtevent (SOCKET);
1.59      cvs        49: #endif /* !_WINDOWS */
1.24      cvs        50: 
1.32      cvs        51: /* Private variables */
1.4       cvs        52: 
                     53: /*
1.58      cvs        54:  * this set of HTEventType map our WinSock "socket event HTEventType" into 
1.4       cvs        55:  * our read and write sets. Note that under the canonical Unix model,
                     56:  * a non-blocking socket passed to an accept() call will appear as readable, 
                     57:  * whilst a non-blocking call to connect() will appear as writeable. In add.
                     58:  * if the connection has been closed, the socket will appear readable under
                     59:  * BSD Unix semantics 
                     60:  */
1.32      cvs        61: 
1.58      cvs        62: static const HTEventType ReadBits = HTEvent_READ | HTEvent_ACCEPT | HTEvent_CLOSE;
                     63: static const HTEventType WriteBits = HTEvent_WRITE | HTEvent_CONNECT;
                     64: static const HTEventType ExceptBits = HTEvent_OOB;
                     65: 
1.79      cvs        66: #ifndef _GTK
1.58      cvs        67: typedef struct sStatus {
1.79      cvs        68:   XtInputId read;             /* the different Xt Id's */
1.58      cvs        69:   XtInputId write;
                     70:   XtInputId except;
                     71: } SocketStatus;
1.79      cvs        72: #endif /*! _GTK */
                     73: 
                     74: #ifdef _GTK
                     75: typedef struct sStatus {
                     76:   gint read;             /* the different GTK Id's */
                     77:   gint write;
                     78:   gint except;
                     79: } SocketStatus;
                     80: #endif /* _GTK */
1.4       cvs        81: 
1.38      cvs        82: #define SOCK_TABLE_SIZE 67
                     83: #define HASH(s) ((s) % SOCK_TABLE_SIZE)
1.58      cvs        84: static SocketStatus persSockets[SOCK_TABLE_SIZE];
1.4       cvs        85: 
1.10      cvs        86: /*--------------------------------------------------------------------
                     87:   AHTCallback_bridge
1.11      cvs        88:   this function acts as a bridge between Xt and libwww. From the Xt
                     89:   point of view, this function is the callback handler whenever there's
                     90:   any activity on the sockets associated with the active requests. From
                     91:   the libwww point of view, this is the function that takes the initiative
                     92:   to invoke the callback function associated with an active request,
                     93:   whenever there's an activity in the socket associated to it.
                     94:   In this latter  aspect, this function is similar to the library's
                     95:   own __DoCallback()  function.
                     96:   Upon activation, the function looks up the request associated with the
                     97:   active socket and then looks up the cbf associated with that request.
                     98:   Upon completion of the execution of the request's cbf, it verifies
                     99:   the state of the request and, if it's an asynchronous request, deletes
                    100:   the memory allocated to it.
1.10      cvs       101:   -------------------------------------------------------------------*/
1.37      cvs       102: #ifndef _WINDOWS
1.68      cvs       103: #ifndef _GTK
1.80      kahan     104: void *AHTCallback_bridge (caddr_t cd, int *s, XtInputId * id)
1.4       cvs       105: {
1.11      cvs       106:    int                 status;  /* the status result of the libwwww call */
1.58      cvs       107:    HTEventType         type  = HTEvent_ALL;    
1.38      cvs       108:    int                 v;
1.40      cvs       109:    int                        socket = *s;
1.58      cvs       110:    ms_t                now = HTGetTimeInMillis();
                    111:    
                    112:    v = HASH (socket);
1.4       cvs       113: 
1.58      cvs       114:    /* convert the FD into an HTEventType which will allow us to find the
                    115:       request associated with the socket */
1.47      cvs       116: 
1.58      cvs       117:    /* I could send some other data here, like the event itself, right */
                    118:    switch ((XtInputId) cd) 
1.47      cvs       119:      {
1.58      cvs       120:      case XtInputReadMask:
                    121:        type = HTEvent_READ;
                    122:        break;
                    123:      case XtInputWriteMask:
                    124:        type = HTEvent_WRITE;
                    125:        break;
                    126:      case XtInputExceptMask:
                    127:        type = HTEvent_OOB;
                    128:        break;
                    129:      default:
                    130:        type = HTEvent_ALL; 
                    131:        break;
                    132:      } /* switch */
1.47      cvs       133:    
1.32      cvs       134:    /* Invokes the callback associated to the requests */
                    135:    
1.66      cvs       136:    /**   CanDoStop_set (FALSE); **/
1.58      cvs       137:    if ((status = HTEventList_dispatch (socket, type, now)) != HT_OK)
                    138:      {
1.36      cvs       139: #ifdef DEBUG_LIBWWW
1.58      cvs       140:      HTTrace ("Callback.... returned a value != HT_OK");
1.36      cvs       141: #endif
1.58      cvs       142:      }
1.41      cvs       143: 
1.66      cvs       144:    /***   CanDoStop_set (TRUE); **/
1.38      cvs       145: 
1.7       cvs       146:    return (0);
1.4       cvs       147: }
1.79      cvs       148: #else /* _GTK */
                    149: static void AHTCallback_bridgeGTK (gpointer data,  gint source, GdkInputCondition condition)
                    150: {
                    151:    int                 status;  /* the status result of the libwwww call */
                    152:    HTEventType         type  = HTEvent_ALL;    
                    153:    int                 v;
                    154:    int                        socket;
                    155:    ms_t                now = HTGetTimeInMillis();
                    156:    
                    157:    socket = (SOCKET) data;
                    158:    v = HASH (socket);
                    159: 
                    160:    /* convert the FD into an HTEventType which will allow us to find the
                    161:       request associated with the socket */
                    162: 
                    163:    /* I could send some other data here, like the event itself, right */
                    164:    switch (condition) 
                    165:      {
                    166:      case GDK_INPUT_READ:
                    167:        type = HTEvent_READ;
                    168:        break;
                    169:      case GDK_INPUT_WRITE:
                    170:        type = HTEvent_WRITE;
                    171:        break;
                    172:      case GDK_INPUT_EXCEPTION:
                    173:        type = HTEvent_OOB;
                    174:        break;
                    175:      default:
                    176:        type = HTEvent_ALL; 
                    177:        break;
                    178:      } /* switch */
                    179:    
                    180:    /* Invokes the callback associated to the requests */
                    181:    
                    182:    /**   CanDoStop_set (FALSE); **/
                    183:    if ((status = HTEventList_dispatch (socket, type, now)) != HT_OK)
                    184:      {
                    185: #ifdef DEBUG_LIBWWW
                    186:      HTTrace ("Callback.... returned a value != HT_OK");
                    187: #endif
                    188:      }
                    189:    /***   CanDoStop_set (TRUE); **/
                    190: }
                    191: #endif /* !_GTK */
1.37      cvs       192: #endif /* !_WINDOWS */
1.41      cvs       193: 
                    194: /*--------------------------------------------------------------------
                    195:   ProcessTerminateRequest
                    196:   This function is called whenever a request has ended. If the requested
                    197:   ended normally, the function will call any callback associated to the
                    198:   request. Otherwise, it will just mark the request as over.
                    199:   -------------------------------------------------------------------*/
1.82      vatton    200: void  ProcessTerminateRequest (HTRequest *request, HTResponse *response,
                    201:                               void *param, int status)
1.41      cvs       202: {   
1.58      cvs       203:   AHTReqContext *me = HTRequest_context (request);
1.56      cvs       204: 
1.58      cvs       205:   /* choose a correct treatment in function of the request's
1.41      cvs       206:      being associated with an error, with an interruption, or with a
                    207:      succesful completion */
1.64      cvs       208: 
                    209: #ifdef DEBUG_LIBWWW  
                    210:   if (THD_TRACE)
1.82      vatton    211:     fprintf (stderr,"ProcessTerminateRequest: processing req %p, url %s,\
                    212:  status %d\n", me, me->urlName, me->reqStatus);  
1.64      cvs       213: #endif /* DEBUG_LIBWWW */
1.83      kahan     214:   if (me->reqStatus == HT_END)
1.41      cvs       215:     {
1.58      cvs       216:       if (AmayaIsAlive ()  && me->terminate_cbf)
1.82      vatton    217:        (*me->terminate_cbf) (me->docid, 0, me->urlName, me->outputfile,
                    218:                              &(me->http_headers), me->context_tcbf);
1.58      cvs       219: 
1.41      cvs       220:     }
1.42      cvs       221:   else if (me->reqStatus == HT_ABORT)
1.41      cvs       222:     /* either the application ended or the user pressed the stop 
                    223:        button. We erase the incoming file, if it exists */
                    224:     {
1.52      cvs       225:       if (AmayaIsAlive () && me->terminate_cbf)
1.82      vatton    226:        (*me->terminate_cbf) (me->docid, -1, me->urlName, me->outputfile,
                    227:                              &(me->http_headers), me->context_tcbf);
1.78      cvs       228:       if (me->outputfile && me->outputfile[0] != EOS)
1.41      cvs       229:        {
1.70      cvs       230:          TtaFileUnlink (me->outputfile);
1.78      cvs       231:          me->outputfile[0] = EOS; 
1.70      cvs       232:        } 
1.41      cvs       233:     }
1.83      kahan     234:   else if (me->reqStatus == HT_ERR)
1.41      cvs       235:     {
                    236:       /* there was an error */
1.58      cvs       237:       if (AmayaIsAlive && me->terminate_cbf)
1.82      vatton    238:        (*me->terminate_cbf) (me->docid, -2, me->urlName, me->outputfile,
                    239:                              &(me->http_headers), me->context_tcbf);
1.41      cvs       240:       
1.78      cvs       241:       if (me->outputfile && me->outputfile[0] != EOS)
1.41      cvs       242:        {
1.70      cvs       243:          TtaFileUnlink (me->outputfile);
1.78      cvs       244:          me->outputfile[0] = EOS;
1.41      cvs       245:        }
                    246:     }
1.58      cvs       247: 
1.41      cvs       248:    /* we erase the context if we're dealing with an asynchronous request */
1.61      cvs       249:   if ((me->mode & AMAYA_ASYNC)
                    250:       || (me->mode & AMAYA_IASYNC))
1.58      cvs       251:     {
1.63      cvs       252:       Document doc = me->docid;
1.58      cvs       253:       me->reqStatus = HT_END;
                    254:       /*** @@@ do we need this? yes!! **/
                    255:       AHTLoadTerminate_handler (request, response, param, status);
                    256:       AHTReqContext_delete (me);
1.63      cvs       257:       AHTPrintPendingRequestStatus (doc, YES);
1.58      cvs       258:     }
                    259: }
1.4       cvs       260: 
1.32      cvs       261: #ifdef _WINDOWS
1.29      cvs       262: /*----------------------------------------------------------------
1.32      cvs       263:   WIN_Activate_Request
1.29      cvs       264:   when there are more open requests than available sockets, the 
                    265:   requests are put in a "pending state." When a socket becomes
                    266:   available, libwww associates it with a pending request and then
                    267:   calls this callback function. This function is responsible for
                    268:   opening the temporary file where the GET and POST  results
                    269:   will be stored. The function is also responsible for 
                    270:   registering the socket with the Xt event loop.
                    271:   Consult the libwww manual for more details on the signature
                    272:   of this function.
                    273:   ----------------------------------------------------------------*/
1.79      cvs       274: int WIN_Activate_Request (HTRequest * request, HTAlertOpcode op, int msgnum, const char *dfault, void *input, HTAlertPar * reply)
1.29      cvs       275: {
                    276:    AHTReqContext      *me = HTRequest_context (request);
                    277: 
1.58      cvs       278:    if (me->reqStatus == HT_NEW) 
                    279:      {
                    280:        if (!(me->output)
                    281:           && (me->output != stdout) 
                    282:           && me->outputfile
1.78      cvs       283:           &&  (me->output = fopen (me->outputfile, "wb")) == NULL) {
1.58      cvs       284:         /* the request is associated with a file */
1.78      cvs       285:         me->outputfile[0] = EOS;       /* file could not be opened */
1.70      cvs       286:         TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE), me->outputfile);
1.58      cvs       287:         me->reqStatus = HT_ERR;
                    288: 
                    289:         if (me->error_html)
                    290:           DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR; /* so we can show the error message */
                    291:        } 
                    292:        else
                    293:         {
                    294: #ifdef DEBUG_LIBWWW
                    295:           if (THD_TRACE)
                    296:             fprintf (stderr, "WIN_Activate_Request: Activating pending %s. "
                    297:                      "Open fd %d\n", me->urlName, (int) me->output);
                    298: #endif /* DEBUG_LIBWWW */
                    299:           HTRequest_setOutputStream (me->request, AHTFWriter_new (me->request, me->output, YES));    
                    300:           /*change the status of the request */
                    301:           me->reqStatus = HT_WAITING;
                    302:         }
                    303:      } 
                    304:    else if (me->reqStatus == HT_WAITING)
                    305:      {
                    306:        /*change the status of the request */
                    307:        rewind (me->output);
                    308:        if (HTRequest_outputStream (me->request) == NULL)
                    309:         HTRequest_setOutputStream (me->request, AHTFWriter_new (me->request, me->output, YES));
                    310:      } 
1.32      cvs       311:    else {
1.36      cvs       312:      me->reqStatus = HT_ERR;
1.32      cvs       313:    }
                    314:    
1.36      cvs       315:    return ((me->reqStatus != HT_ERR) ? HT_OK : HT_ERROR);
1.32      cvs       316: }
                    317: 
1.60      cvs       318: #endif /* WINDOWS but should be an else */
                    319: /* #else  */ /* _WINDOWS */
                    320: 
                    321: #ifndef _WINDOWS
1.11      cvs       322: /*----------------------------------------------------------------------
                    323:   AHTEvent_register
                    324:   callback called by libwww whenever a socket is open and associated
                    325:   to a request. It sets the pertinent Xt events so that the Xt Event
                    326:   loops gets an interruption whenever there's action of the socket. 
                    327:   In addition, it registers the request with libwww.
                    328:   ----------------------------------------------------------------------*/
1.58      cvs       329: int AHTEvent_register (SOCKET sock, HTEventType type, HTEvent *event)
1.4       cvs       330: {
1.58      cvs       331:   int  status;
1.7       cvs       332: 
1.40      cvs       333:   if (sock == INVSOC)
                    334:     {
1.36      cvs       335: #ifdef DEBUG_LIBWWW
1.58      cvs       336:       fprintf(stderr, "AHTEvent_register: sock = INVSOC\n");
1.37      cvs       337: #endif /* DEBUG_LIBWWW */
1.58      cvs       338:       return (0);
                    339:     }
1.43      cvs       340: 
1.33      cvs       341: #ifndef _WINDOWS
1.58      cvs       342:        /* need something special for HTEvent_CLOSE */
                    343:   if (type & ReadBits)
                    344:     RequestRegisterReadXtevent (sock);
                    345:   
                    346:   if (type & WriteBits)
                    347:     RequestRegisterWriteXtevent (sock);
                    348:   
                    349:   if (type & ExceptBits)
                    350:     RequestRegisterExceptXtevent (sock);
1.33      cvs       351: #endif  /* !_WINDOWS */
1.58      cvs       352:   
1.51      cvs       353: #ifdef _WINDOWS   
1.50      cvs       354:   /* under windows, libwww requires an explicit FD_CLOSE registration 
1.58      cvs       355:      to detect HTTP responses not having a Content-Length header */
                    356:   status = HTEventList_register (sock, type | HTEvent_CLOSE , event);
1.50      cvs       357: #else
1.58      cvs       358:   status = HTEventList_register (sock, type, event);
1.51      cvs       359: #endif /* _WINDOWS */
1.58      cvs       360:   
                    361:   return (status);
                    362: }
1.68      cvs       363: #endif /* _WINDOWS */
1.50      cvs       364: 
1.34      cvs       365: /*----------------------------------------------------------------------
                    366:   AHTEvent_unregister
                    367:   callback called by libwww each time a request is unregistered. This
                    368:   function takes care of unregistering the pertinent Xt events
                    369:   associated with the request's socket. In addition, it unregisters
                    370:   the request from libwww.
                    371:   ----------------------------------------------------------------------*/
1.58      cvs       372: int AHTEvent_unregister (SOCKET sock, HTEventType type)
1.34      cvs       373: {
1.58      cvs       374:   int    status;
1.76      kahan     375:   
                    376:   if (sock == INVSOC)
                    377:     return HT_OK;
1.38      cvs       378: 
                    379: #ifndef _WINDOWS   
1.58      cvs       380:    /* remove the Xt event hooks */
                    381:    if (type & ReadBits) 
                    382:      RequestKillReadXtevent (sock);
                    383:    
                    384:    if (type & WriteBits)
                    385:      RequestKillWriteXtevent (sock);
                    386:    
                    387:    if (type & ExceptBits) 
                    388:      RequestKillExceptXtevent (sock);
1.36      cvs       389: #endif /* !_WINDOWS */
1.35      cvs       390: 
1.58      cvs       391:    /* @@@ if this is the default for windows, no need to have AHTEvent_..
                    392:       in windows!! */
1.35      cvs       393: 
1.58      cvs       394:    /* call libwww's default routine */
                    395:    status = HTEventList_unregister (sock, type);
                    396:    
1.7       cvs       397:    return (status);
1.4       cvs       398: }
1.35      cvs       399: 
                    400: #ifndef _WINDOWS
1.58      cvs       401: /* Private functions */
                    402: 
1.11      cvs       403: /*----------------------------------------------------------------------
                    404:   RequestKillAllXtevents
                    405:   front-end for kill all Xt events associated with the request pointed
                    406:   to by "me".
                    407:   ----------------------------------------------------------------------*/
1.79      cvs       408: void RequestKillAllXtevents (AHTReqContext * me)
1.4       cvs       409: {
1.58      cvs       410:   int sock = INVSOC;
                    411: 
                    412:   return;
                    413: 
                    414:   /* @@@ what to do with this one? @@@ */
                    415:   if (me->read_sock != INVSOC)
                    416:     sock = me->read_sock;
                    417:   else
                    418:     if (me->write_sock != INVSOC)
                    419:           sock = me->write_sock;
                    420:   else
                    421:     if (me->except_sock != INVSOC)
                    422:           sock = me->except_sock;
                    423: 
                    424: #ifdef DEBUG_LIBWWW
1.7       cvs       425:    if (THD_TRACE)
1.58      cvs       426:       fprintf (stderr, "RequestKillAllXtEvents: Clearing XtInputs\n");
                    427: #endif /* DEBUG_LIBWWW */
1.4       cvs       428: 
1.58      cvs       429: 
                    430:    RequestKillReadXtevent (sock);
                    431:    RequestKillWriteXtevent (sock);
                    432:    RequestKillExceptXtevent (sock);
1.4       cvs       433: }
                    434: 
1.11      cvs       435: /*----------------------------------------------------------------------
1.18      cvs       436:   RequestRegisterReadXtevent
                    437:   Registers with Xt the read events associated with socket sock
                    438:   ----------------------------------------------------------------------*/
1.79      cvs       439: static void RequestRegisterReadXtevent (SOCKET sock)
1.18      cvs       440: {
1.58      cvs       441:   int v;
                    442: 
                    443:   v = HASH (sock);
                    444: 
                    445:   if (!persSockets[v].read)
1.18      cvs       446:     {
1.79      cvs       447: #ifndef _GTK
1.58      cvs       448:       persSockets[v].read  =
                    449:        XtAppAddInput (app_cont,
                    450:                       sock,
                    451:                       (XtPointer) XtInputReadMask,
                    452:                       (XtInputCallbackProc) AHTCallback_bridge,
                    453:                       (XtPointer) XtInputReadMask);
1.79      cvs       454: #else  /* _GTK */
                    455:      persSockets[v].read  =
                    456:        gdk_input_add ((gint) sock,
                    457:                      GDK_INPUT_READ,
                    458:                      AHTCallback_bridgeGTK,
                    459:                      (gpointer) sock);
                    460: #endif /* !_GTK */
                    461: 
1.58      cvs       462: #ifdef DEBUG_LIBWWW
1.18      cvs       463:       if (THD_TRACE)
1.58      cvs       464:        fprintf (stderr, "RegisterReadXtEvent: adding XtInput %lu Socket %d\n",
                    465:                 persSockets[v].read, sock);
                    466: #endif /* DEBUG_LIBWWW */
1.18      cvs       467:     }
                    468: 
                    469: }
                    470: 
                    471: /*----------------------------------------------------------------------
1.11      cvs       472:   RequestKillReadXtevent
                    473:   kills any read Xt event associated with the request pointed to by "me".
                    474:   ----------------------------------------------------------------------*/
1.79      cvs       475: static void RequestKillReadXtevent (SOCKET sock)
1.4       cvs       476: {
1.58      cvs       477:   int v;
                    478: 
                    479:   v = HASH (sock);
                    480: 
                    481:   if (persSockets[v].read)
                    482:     {
                    483: #ifdef DEBUG_LIBWWW
                    484:       if (THD_TRACE)
                    485:        fprintf (stderr, "UnregisterReadXtEvent: Clearing XtInput %lu\n",
                    486:                 persSockets[v].read);
                    487: #endif /* DEBUG_LIBWWW */
1.79      cvs       488: #ifndef _GTK
1.58      cvs       489:       XtRemoveInput (persSockets[v].read);
                    490:       persSockets[v].read = (XtInputId) NULL;
1.79      cvs       491: #else  /* _GTK */
                    492:       gdk_input_remove (persSockets[v].read);
                    493:       persSockets[v].read = (gint) 0;
                    494: #endif /* !_GTK */
1.58      cvs       495:     }
1.18      cvs       496: }
                    497: 
                    498: /*----------------------------------------------------------------------
                    499:   RequestRegisterWriteXtevent
                    500:   Registers with Xt the write events associated with socket sock
                    501:   ----------------------------------------------------------------------*/
1.79      cvs       502: static void RequestRegisterWriteXtevent (SOCKET sock)
1.18      cvs       503: {
1.58      cvs       504:   int v;
                    505:   v = HASH (sock);
1.18      cvs       506: 
1.58      cvs       507:   if (!persSockets[v].write)
                    508:     {   
1.79      cvs       509: #ifndef _GTK
1.58      cvs       510:       persSockets[v].write =
                    511:        XtAppAddInput (app_cont,
                    512:                       sock,
1.18      cvs       513:                   (XtPointer) XtInputWriteMask,
                    514:                   (XtInputCallbackProc) AHTCallback_bridge,
                    515:                   (XtPointer) XtInputWriteMask);
1.79      cvs       516: #else  /* _GTK */
                    517:      persSockets[v].write  =
                    518:        gdk_input_add ((gint) sock,
                    519:                      GDK_INPUT_WRITE,
                    520:                      AHTCallback_bridgeGTK,
                    521:                      (gpointer) sock);
                    522: #endif /* !_GTK */
                    523: 
1.58      cvs       524: #ifdef DEBUG_LIBWWW   
1.18      cvs       525:   if (THD_TRACE)
1.58      cvs       526:     fprintf (stderr, "RegisterWriteXtEvent: Adding XtInput %lu Socket %d\n",
                    527:             persSockets[v].write, sock);
                    528: #endif /* DEBUG_LIBWWW */
                    529:   
                    530:     }
1.4       cvs       531: }
                    532: 
1.11      cvs       533: /*----------------------------------------------------------------------
                    534:   RequestKillWriteXtevent
                    535:   kills any write Xt event associated with the request pointed to
                    536:   by "me".
                    537:   ----------------------------------------------------------------------*/
1.79      cvs       538: static void RequestKillWriteXtevent (SOCKET sock)
1.4       cvs       539: {
1.58      cvs       540:   int v;
                    541: 
                    542:   v = HASH (sock);
                    543: 
                    544:   if (persSockets[v].write)
                    545:     {
                    546: #ifdef DEBUG_LIBWWW   
                    547:       if (THD_TRACE)
                    548:        fprintf (stderr, "UnRegisterWriteXtEvent: Clearing Write XtInputs "
                    549:                 "%lu\n",
                    550:                 persSockets[v].write);
                    551: #endif /* DEBUG_LIBWWW */
1.79      cvs       552: #ifndef _GTK
1.58      cvs       553:       XtRemoveInput (persSockets[v].write);
                    554:       persSockets[v].write =  (XtInputId) NULL;
1.79      cvs       555: #else  /* _GTK */
                    556:       gdk_input_remove (persSockets[v].write);
                    557:       persSockets[v].write = (gint) 0;
                    558: #endif /* !_GTK */
1.58      cvs       559:     }
1.18      cvs       560: }
                    561: 
                    562: /*----------------------------------------------------------------------
                    563:   RequestRegisterExceptXtevent
                    564:   Registers with Xt the except events associated with socket sock
                    565:   ----------------------------------------------------------------------*/
1.79      cvs       566: static void RequestRegisterExceptXtevent (SOCKET sock)
1.18      cvs       567: {
1.58      cvs       568:   int v;
                    569: 
                    570:   v = HASH (sock);
                    571: 
                    572:    if (!persSockets[v].except)
1.18      cvs       573:      {
1.79      cvs       574: #ifndef _GTK   
1.58      cvs       575:        persSockets[v].except =
                    576:         XtAppAddInput (app_cont,
                    577:                        sock,
                    578:                        (XtPointer) XtInputExceptMask,
                    579:                        (XtInputCallbackProc) AHTCallback_bridge,
                    580:                        (XtPointer) XtInputExceptMask);
1.79      cvs       581: #else  /* _GTK */
                    582:      persSockets[v].except  =
                    583:        gdk_input_add ((gint) sock,
                    584:                      GDK_INPUT_EXCEPTION,
                    585:                      AHTCallback_bridgeGTK,
                    586:                      (gpointer) sock);
                    587: #endif /* !_GTK */
1.58      cvs       588: #ifdef DEBUG_LIBWWW      
                    589:    if (THD_TRACE)
                    590:      fprintf (stderr, "RegisterExceptXtEvent: adding XtInput %lu Socket %d\n",
                    591:              persSockets[v].except, sock);
                    592: #endif /* DEBUG_LIBWWW */
1.18      cvs       593:      }
1.4       cvs       594: }
                    595: 
1.11      cvs       596: /*----------------------------------------------------------------------
                    597:   RequestKillExceptXtevent
                    598:   kills any exception Xt event associated with the request pointed to
                    599:   by "me".
                    600:   ----------------------------------------------------------------------*/
1.79      cvs       601: static void RequestKillExceptXtevent (SOCKET sock)
1.4       cvs       602: {
1.58      cvs       603:   int v;
                    604: 
                    605:   v = HASH (sock);
                    606:   if (persSockets[v].except)
                    607:     {
                    608: #ifdef DEBUG_LIBWWW   
                    609:       if (THD_TRACE)
                    610:        fprintf (stderr, "UnregisterExceptXtEvent: Clearing Except XtInputs "
                    611:                 "%lu\n", persSockets[v].except);
                    612: #endif /* DEBUG_LIBWWW */
1.79      cvs       613: #ifndef _GTK
1.58      cvs       614:       XtRemoveInput (persSockets[v].except);
                    615:       persSockets[v].except = (XtInputId) NULL;
1.79      cvs       616: #else  /* _GTK */
                    617:       gdk_input_remove (persSockets[v].except);
                    618:       persSockets[v].except = (gint) 0;
                    619: #endif /* !_GTK */
1.58      cvs       620:     }
                    621: }
                    622: 
                    623: /*----------------------------------------------------------------------
                    624:   Xt Timer functions 
                    625:   ----------------------------------------------------------------------*/
                    626: 
                    627: struct _HTTimer {
                    628:     ms_t        millis;         /* Relative value in millis */
                    629:     ms_t        expires;        /* Absolute value in millis */
                    630:     BOOL        relative;
                    631:     BOOL        repetitive;
                    632:     void *      param;          /* Client supplied context */
                    633:     HTTimerCallback * cbf;
                    634: };
                    635: 
                    636: struct _AmayaTimer {
                    637:   HTTimer *libwww_timer;
1.79      cvs       638: #ifndef _GTK
1.58      cvs       639:   XtIntervalId xt_timer;
1.79      cvs       640: #else  /* _GTK */
                    641:   guint  xt_timer;
                    642: #endif /* !_GTK */
1.58      cvs       643: };
                    644: 
                    645: typedef struct _AmayaTimer AmayaTimer;
                    646: 
                    647: static HTList *Timers = NULL;
                    648: 
                    649: /*----------------------------------------------------------------------
1.79      cvs       650:   TimerCallback
                    651:   called by the system event loop. Timers shouldn't be restarted
                    652:   on exiting.
1.58      cvs       653:   ----------------------------------------------------------------------*/
1.79      cvs       654: #ifndef _GTK
1.58      cvs       655: void *TimerCallback (XtPointer cdata, XtIntervalId *id)
                    656: {
                    657:   HTList *cur, *last;
                    658:   AmayaTimer *me;
                    659:   HTTimer *libwww_timer;
                    660: 
                    661:   if (!AmayaIsAlive () 
                    662:       || Timers == NULL)
                    663:     return (0);
                    664: 
                    665:   /* find the timer from the uid */
                    666:   last = cur = Timers;
                    667:   while ((me = (AmayaTimer * ) HTList_nextObject (cur)))
                    668:     {
                    669:       if (me->xt_timer == *id)
                    670:        break;
                    671:       last = cur;
                    672:     }
                    673: 
                    674:   if (me)
                    675:     {
                    676:       libwww_timer = me->libwww_timer;
                    677:       /* remove the element from the list @@@ can be optimized later */
                    678:       HTList_quickRemoveElement(cur, last);
                    679:       TtaFreeMemory (me);
                    680:       HTTimer_dispatch (libwww_timer);
                    681:     }
                    682: 
                    683:   return (0);
                    684: }
                    685: 
1.79      cvs       686: #else  /* _GTK */
                    687: /*----------------------------------------------------------------------
                    688:   TimerCallbackGTK
1.81      kahan     689:   The callback returns FALSE to destroy the timer that called it.
1.79      cvs       690:   ----------------------------------------------------------------------*/
                    691: gboolean TimerCallbackGTK (gpointer id)
                    692: {
                    693:   HTList *cur, *last;
                    694:   AmayaTimer *me;
                    695:   HTTimer *libwww_timer;
                    696:   AmayaTimer *data;
                    697: 
                    698:   data = (AmayaTimer *) id;
                    699: 
                    700:   if (!AmayaIsAlive () 
                    701:       || Timers == NULL)
1.81      kahan     702:     return (FALSE);
1.79      cvs       703: 
                    704:   /* find the timer from the uid */
                    705:   last = cur = Timers;
                    706:   while ((me = (AmayaTimer * ) HTList_nextObject (cur)))
                    707:     {
                    708:       if (me == data)
                    709:        break;
                    710:       last = cur;
                    711:     }
                    712: 
                    713:   if (me)
                    714:     {
                    715:       libwww_timer = me->libwww_timer;
                    716:       /* remove the element from the list @@@ can be optimized later */
                    717:       HTList_quickRemoveElement(cur, last);
                    718:       TtaFreeMemory (me);
                    719:       HTTimer_dispatch (libwww_timer);
                    720:     }
                    721: 
                    722:   return (FALSE);
                    723: }
                    724: #endif /* !_GTK */
                    725: 
1.58      cvs       726: /*----------------------------------------------------------------------
1.59      cvs       727:   KillAllTimers
1.58      cvs       728:   ----------------------------------------------------------------------*/
1.59      cvs       729: void KillAllTimers (void)
1.58      cvs       730: {
                    731:   /* @@@ maybe add something else to kill the Xt things */
                    732:   if (Timers)
                    733:     HTList_delete (Timers);
1.65      cvs       734:   Timers = NULL;
1.58      cvs       735: }
                    736: 
                    737: /*----------------------------------------------------------------------
1.59      cvs       738:  AMAYA_SetTimer
                    739:  ----------------------------------------------------------------------*/
                    740: void AMAYA_SetTimer (HTTimer *libwww_timer)
1.58      cvs       741: {
                    742:   HTList *cur, *last;
                    743:   AmayaTimer *me;
                    744: 
                    745:   if (!AmayaIsAlive 
                    746:       || libwww_timer == NULL
                    747:       || libwww_timer->expires == 0)
                    748:     return;
                    749: 
                    750:   if (Timers == NULL)
                    751:     Timers = HTList_new ();
                    752: 
                    753:   /* see if this timer existed already */
                    754:   last = cur = Timers;
                    755:   while ((me = (AmayaTimer * ) HTList_nextObject (cur)))
                    756:     {
                    757:       if (me->libwww_timer == libwww_timer)
                    758:        break;
                    759:       last = cur;
                    760:     }
                    761: 
                    762:   if (me)
                    763:     {
                    764:     /* remove the old timer */
                    765:       if (me->xt_timer) 
                    766:        {
1.79      cvs       767: #ifndef _GTK
1.58      cvs       768:          XtRemoveTimeOut (me->xt_timer);
1.59      cvs       769:          me->xt_timer = (XtIntervalId) NULL;
1.79      cvs       770: #else  /* _GTK */
                    771:          gtk_timeout_remove (me->xt_timer);
                    772:          me->xt_timer = (guint) 0;
                    773: #endif /* !_GTK */
1.58      cvs       774:        }
                    775:     }
                    776:   else
                    777:     {
                    778:       /* create a new element */
                    779:       me = TtaGetMemory (sizeof (AmayaTimer));
                    780:       /* and add it to the list */
                    781:       HTList_addObject(last, (void *) me);
                    782:       me->libwww_timer = libwww_timer;
                    783:     }
                    784: 
                    785:   /* add a new time out */
1.79      cvs       786: #ifndef _GTK
1.58      cvs       787:   me->xt_timer = XtAppAddTimeOut (app_cont,
                    788:                                 me->libwww_timer->millis,
                    789:                                 (XtTimerCallbackProc) TimerCallback,
                    790:                                 (XtPointer *) (void *) me);
1.79      cvs       791: #else  /* _GTK */
                    792:   me->xt_timer = gtk_timeout_add ((guint32) me->libwww_timer->millis,
                    793:                                  (GtkFunction) TimerCallbackGTK,
                    794:                                  (gpointer) me);
                    795: #endif /* !_GTK */
1.58      cvs       796: }
                    797: 
                    798: /*----------------------------------------------------------------------
1.59      cvs       799:   AMAYA_DeleteTimer
1.58      cvs       800:   ----------------------------------------------------------------------*/
1.59      cvs       801: void AMAYA_DeleteTimer (HTTimer *libwww_timer)
1.58      cvs       802: {
                    803:   HTList *cur, *last;
                    804:   AmayaTimer *me;
                    805: 
1.59      cvs       806:   if (Timers == NULL || libwww_timer == NULL)
1.58      cvs       807:     return;
                    808: 
                    809:   /* find the id */
                    810:   last = cur = Timers;
                    811:   while ((me = (AmayaTimer * ) HTList_nextObject (cur)))
                    812:     {
1.59      cvs       813:       if (me->libwww_timer == libwww_timer)
1.58      cvs       814:        break;
                    815:       last = cur;
                    816:     }
                    817: 
                    818:   if (me)
                    819:     {
                    820:       /* remove the Xt timer */
1.79      cvs       821: #ifndef _GTK
1.58      cvs       822:       XtRemoveTimeOut (me->xt_timer);
1.79      cvs       823: #else  /* _GTK */
                    824:       gtk_timeout_remove (me->xt_timer);
                    825: #endif /* !_GTK */
1.58      cvs       826:       /* and the element from the list */
1.65      cvs       827:       HTList_removeObject (Timers, me);
1.58      cvs       828:       TtaFreeMemory (me);
                    829:     }
1.31      cvs       830: }
1.32      cvs       831: #endif /* !_WINDOWS */

Webmaster