# -Q2.C.7- mix interactions between Xt and Tk/Tcl?

    From: -II-  Tk Questions and Answers - How can I:

    A2.C.7.

    [From YIP Chi Lap [Beta] <h9118101@hkuxa.hku.hk> ]
    	I have come up with a CPU hogging polling loop that
    works (till now) for my application which not much
    direct interaction between a Tk window and an Xt window is made.
    (e.g., it won't draw something on a Tk window by a Xt-dispatched routine)

      XEvent xevent;
      for (;;)
      {
        if (XtAppPending(appcontext))
        {
          XtAppNextEvent(appcontext,&xevent);
          XtDispatchEvent(&xevent);
        }
        Tk_DoOneEvent(TK_DONT_WAIT);
      }

    [ From David C Mudie <mudie@radon.eecs.berkeley.edu> ]
    	We use the main event loop below.  The basic idea
    is to watch for events arriving from either the Xt server
    connection or the Tk server connection and then call
    the library dispatchers.

    	The code fragment below will need to be cleaned up for
    your usage; there are some missing include statements
    and global declarations buried elsewhere in the file.

    /* Allow Tcl/Tk and Xt to work at the same time. */
    void tkGo(void)
    {
      extern Widget topLevel;
      int width = ulimit(4, -1);
      fd_set readfds;
      struct timeval timeout;
      int nfds;
      int tkfd = ConnectionNumber(Tk_Display(mainWindow));
      int xtfd = ConnectionNumber(XtDisplay(topLevel));

      Tcl_VarEval(interp, "update", NULL);
      while (1) {

        /* Select on X server connections to wait for event. */
        /* Timeout every half second to allow processing of non-X events */

        FD_ZERO(&readfds);
        FD_SET(tkfd, &readfds);
        FD_SET(xtfd, &readfds);
        timeout.tv_sec = 0;
        timeout.tv_usec = 500000;

        nfds = select(width, &readfds, NULL, NULL, &timeout);
        if (nfds < 0 && errno != EINTR) {
          perror("select");
        } else if (nfds == 0) {
          /* printf("timeout\n"); */
        }

        while (XtPending()) {
          XtProcessEvent(XtIMAll);
        }

        while (Tk_DoOneEvent(1)) {
          /* do nothing */
        }

        if (mainWindow == NULL) {
    //      Tcl_DeleteInterp(interp);
    //      Tcl_DStringFree(&command);
          Tcl_Eval(interp, "exit");
          return;
        }
      }
    }

    [ From Davide Frisoni <frisoni@faw.uni-ulm.de> ]
    	One may use Xt properties to communicate between different
    applications.

Parent document is top of "FAQ: comp.lang.tcl Tk Toolkit Usage Questions And Answers (1/1)"
Previous document is "-Q2.C.6- bind and to a frame containing other widgets?"
Next document is "-Q2.C.8- bind the ResizeRequest event?"