An Introduction To Embedded Tk (page 18 of 32)

[Previous Page][Next Page][Table of Contents]

9.1 Global C Variables Created By ET

Perhaps the most useful of the global variables available in ET is Et_Interp. This variable is a pointer to the Tcl/Tk interpreter, the one created by Et_Init() and used to execute all Tcl/Tk commands within the program. The Et_Interp variable has the same value as the interp formal parameter found in every ET_PROC() function.

The Et_Interp variable is useful because you may often want to call C routines in the Tcl/Tk library, and most of these routines require a pointer to the interpreter as their first parameter. For instance, suppose in the initialization code you want to create a link between the global C variable nClients and a Tcl/Tk variable by the same name. Using the Et_Interp variable as the first parameter to the Tcl function Tcl_LinkVar(), you could write:

   Tcl_LinkVar(Et_Interp,"nClients",(char*)&nClients,TCL_LINK_INT);
Having done this, any changes to the C nClients variable will be reflected in the Tcl/Tk variable, and vice versa.

Perhaps the second most useful global varible is Et_Display. This variable contains the Display pointer required as the first argument to most Xlib routines. It is used by daring, down-to-the-bare-metal programmers who like to call Xlib directly.

Here's an example. Suppose you want to create a new Tcl/Tk command, PitchedBell, that makes the X terminal emit a beep with a pitch specified by its sole argument. Once such a command is implemented, then the following Tcl/Tk code would emit a single tone at the pitch of concert A:

   PitchedBell 440
Here a short piece of Tcl/Tk code that plays the opening bar of Beethoven's Fifth Symphony:
   foreach pitch {784 784 784 659} {
      PitchedBell $pitch
      after 200
   }
You probably get the idea. Here's the code that implements the PitchedBell command:
   #include <tk.h>   /* Will also pickup <Xlib.h> */

   ET_PROC( PitchedBell ){
      XKeyboardControl ctrl;   /* For changing the bell pitch */

      if( argc!=2 ){
        interp->result = 
          "Wrong # args.  Should be: ``PitchedBell PITCH''";
        return ET_ERROR;
      }
      ctrl.bell_pitch = atoi( argv[1] );
      XChangeKeyboardControl(Et_Display,KBBellPitch,&ctrl);
      XBell(Et_Display,0);
      XFlush(Et_Display);
      return ET_OK;
   }
After checking to make sure it has exactly one argument, the PitchedBell command uses the XChangeKeyboardControl() function of Xlib to change the bell pitch. It then rings the bell using the XBell() Xlib function, and finally flushes the Xlib message queue using XFlush() to force the bell to be rung immediately. All three of these Xlib functions require a Display pointer as their first argument, a role that is perfectly filled by the Et_Display global variable.

The third and final global C variable in ET is Et_MainWindow. This variable is a pointer to the Tcl/Tk structure that defines the application's main window. Back in the days of Tk3.6, several Tcl/Tk library functions required this value as a parameter. But the Tcl/Tk library interface changed in the move to Tk4.0, so that the main window pointer is no longer required. Hence, the Et_MainWindow variable isn't used much any more. It has been kept around as an historical artifact.

[Next Page][Table of Contents]