Desktop Integration Guide
  Cerca solo questo libro
Scarica il manuale in formato PDF

ToolTalk Example Program for XView Toolkit

D

This appendix presents two code examples (ttreceive.c and ttsend.c) and a header file (ttdig.h) which illustrate the use of ToolTalk service with the XView toolkit.
The source and header files, and the Makefile for this example can be found online at $OPENWINHOME/share/src/dig_samples/tooltalk_simple.

D.1 ttreceive.c

Code Example D-1 ttreceive.c (1 of 3)

   /* ttreceive - show receiving tooltalk message based on pattern.  
   * This simple example program is the counterpart to ttsend.  It registers  
   * a pattern which describes the message it is interested in, and then  
   * waits for them. */  
  
  #include <xview/xview.h>  
  #include <xview/panel.h>  
  #include <xview/tt_c.h>  
  
  #include "ttdig.h"  
  
  Frame base_frame;  
  Panel_item controls;  
  Panel_item gauge;  
  
  char *my_procid;  
  
  voidreceive_tt_message();  
  voidcreate_ui_components();  
  
  void  
  main(argc, argv)  
  int argc;  
  char **argv;  
  {  
    int ttfd;  
    Tt_pattern pat;  
  
    /* Initialize XView. */  
    xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, 0);  
    create_ui_components();  
  
    /* Initialize ToolTalk and obtain file descriptor for incoming messages. */  
    my_procid = tt_open();  
    ttfd = tt_fd();  
  
    /* Tell XView to call my receive procedure when there are messages. */  
    notify_set_input_func(base_frame,  
                (Notify_func)receive_tt_message, ttfd);  
    /*  
     * Create and register the pattern we are interested in.  We are  

Code Example D-1 ttreceive.c (2 of 3)

     * registering as an observer; all observers will receive a message  
     * destined for them (try a few ttreceives).  If we had registered  
     * as a TT_HANDLE, we would be the one to handle the message.  
     */  
    pat = tt_pattern_create();  
    tt_pattern_category_set(pat, TT_OBSERVE);  
    tt_pattern_scope_add(pat, TT_SESSION);  
    tt_pattern_op_add(pat, RECEIVE_PATTERN);  
    tt_pattern_register(pat);  
  
    /* Join the default session to get messages. */  
    tt_session_join(tt_default_session());  
    xv_main_loop(base_frame);  
  
    /* Clean up ToolTalk on exit. */  
    tt_close();  
    exit(0);  
  }  
  
  /*  
   * receive_tt_message is the procedure that gets called by the XView  
   * notifier when my tooltalk file descriptor becomes active with a message.  
   */  
  void  
  receive_tt_message()  
  {  
    Tt_message msg_in;  
    int mark;  
    int val_in;  
  
    /*  
     * Pull in my message handle.  If it is null, we became active even  
     * though there wasn't a real message for us.  
     */  
    msg_in = tt_message_receive();  
    if (msg_in == NULL) return;  
  
    /*  
     * Get a storage mark so we can free storage that tt obtains for  
     * our message contents.  
     */  
    mark = tt_mark();  

Code Example D-1 ttreceive.c (3 of 3)

    /* If the message pattern matches our interest, fetch the value.  */  
    if (0==strcmp(RECEIVE_PATTERN, tt_message_op(msg_in))) {  
      tt_message_arg_ival(msg_in, 0, &val_in);  
      xv_set(gauge, PANEL_VALUE, val_in, NULL);  
    }  
  
    tt_message_destroy(msg_in);  
    tt_release(mark);  
    return;  
  }  
  
  /*  
   * create_ui_components is the procedure called to set up the panel.  
   */  
  void  
  create_ui_components()  
  {  
    base_frame = xv_create(NULL, FRAME,  
                 XV_LABEL, "TT Receiver Example",  
                 FRAME_SHOW_RESIZE_CORNER, FALSE,  
                 NULL);  
    controls = xv_create(base_frame, PANEL,  
                  WIN_BORDER, FALSE,  
                  NULL);  
    gauge = xv_create(controls, PANEL_GAUGE,  
               PANEL_LABEL_STRING, "Received:",  
               PANEL_MIN_VALUE, RECEIVE_MIN,  
               PANEL_MAX_VALUE, RECEIVE_MAX,  
               PANEL_SHOW_RANGE, FALSE,  
               NULL);  
    window_fit(controls);  
    window_fit(base_frame);  
  }  

D.2 ttsend.c

Code Example D-2 ttsend.c (1 of 3)

  /* ttsend - Demonstrate sending a message with a particular pattern.  
   *  
   * This simple program is the counterpart to ttreceive.  It sends  

Code Example D-2 ttsend.c (2 of 3)

   * a message with a particular pattern that all receivers that are  
   * listening will receive.  
   */  
  
  #include <xview/xview.h>  
  #include <xview/panel.h>  
  #include <xview/tt_c.h>  
  
  #include "ttdig.h"  
  
  Frame base_frame;  
  Panel_item controls;  
  Panel_item slider;  
  
  char *my_procid;  
  
  voidbroadcast_value();  
  voidcreate_ui_components();  
  
  void  
  main(argc, argv)  
  int argc;  
  char **argv;  
  {  
  
    /* Initialize XView and Tooltalk; enter XView main loop.  */  
    xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, 0);  
    create_ui_components();  
    my_procid = tt_open();  
    xv_main_loop(base_frame);  
  
    /* Clean up ToolTalk on exit.  */  
    tt_close();  
    exit(0);  
  }  
  
  /*  
   * broadcast_value is the procedure that gets called when you  
   * release the slider.  It gets the current slider  
   * value and broadcasts it with ToolTalk.  
   */  
  void  

Code Example D-2 ttsend.c (3 of 3)

  broadcast_value(item, value, event)  
  Panel_item item;  
  int value;  
  Event *event;  
  {  
    Tt_message msg_out;  
  
    /* Create and send ToolTalk msg.  */  
    msg_out = tt_pnotice_create(TT_SESSION, RECEIVE_PATTERN);  
    tt_message_arg_add(msg_out, TT_IN, "integer", NULL);  
    tt_message_arg_ival_set(msg_out, 0, value);  
    tt_message_send(msg_out);  
  
    /* Destroy the handle since we don't expect a reply.  */  
    tt_message_destroy(msg_out);  
  }  
  
  /*  
   * create_ui_components is the procedure called to set up the panel.  
   */  
  void  
  create_ui_components()  
  {  
    base_frame = xv_create(NULL, FRAME,  
                 XV_LABEL, "TT Send Example",  
                 FRAME_SHOW_RESIZE_CORNER, FALSE,  
                 NULL);  
    controls = xv_create(base_frame, PANEL,  
                  WIN_BORDER, FALSE,  
                  NULL);  
    slider = xv_create(controls, PANEL_SLIDER,  
                PANEL_LABEL_STRING, "Send:",  
                PANEL_SLIDER_END_BOXES, FALSE,  
                PANEL_SHOW_RANGE, FALSE,  
                PANEL_SHOW_VALUE, FALSE,  
                PANEL_MIN_VALUE, RECEIVE_MIN,  
                PANEL_MAX_VALUE, RECEIVE_MAX,  
                PANEL_TICKS, 0,  
                PANEL_NOTIFY_PROC, broadcast_value,  
                NULL);  
    window_fit(controls);  
    window_fit(base_frame);  
  }  

D.3 ttdig.h

Code Example D-3 ttdig.h

  /*  
   * RECEIVE_PATTERN is the message identifier for our tooltalk messages.  
   * It is prefixed with Sun_ as a simple mechanism to avoid namespace  
   * conflicts with other apps in the default session.  
   */  
  #define RECEIVE_PATTERN "Sun_ttexample_pattern"  
  
  /*  
   * RECEIVE_MIN and _MAX is our slider/gauge range.  
   */  
  #define RECEIVE_MIN 0  
  #define RECEIVE_MAX 100