Common Desktop Environment: ToolTalk Messaging Overview

The CoEd.C File

The CoEd.C file, shown in Example B-2 , shows the ToolTalk code that needs to be included in every application to initialize the toolkit, join a ToolTalk session and registering patterns, and add the ToolTalk service to its event loop.


Note -

This file also contains ToolTalk code that is specific to CoEd in its role as an editor application. This code includes declaring a ptype and processing the start message.



Example B-2 The CoEd.C File

/*
  * CoEd.cc
  *
  * Copyright (c) 1991,1993 by Sun Microsystems.
  */  

#include <stdlib.h> 
#include <desktop/tttk.h>             // Include the ToolTalk messaging toolkit 
#include <CoEd.h> 
#include "CoEditor.h" 
#include "CoEdTextBuffer.h"  

XtAppContext               myContext; 
Widget                     myTopWidget      = 0; 
Display	       *myDpy; 
int          abortCode = 0; 
Tt_pattern     *sessPats = 0;               // Patterns returned when session joined 
int                timeOutFactor            = 1000; 
int                maxBuffers               = 1000; 
int            *pArgc; 
char          **globalArgv;  

const char     *ToolName = "CoEd"; 
const char     *usage 		= 
"Usage: CoEd [-p01] [-w n] [-t n] [file]\n" 
" -p      print ToolTalk procid\n" 
" -0      do not open an initial composition window\n" 
" -1      be a single-buffer editor\n" 
" -w      sleep for n seconds before coming up\n" 
" -t      use n as timeout factor, in milliseconds (default: 1000)\n" 
;  

void main(
    int    argc,
    char **argv 
) 
{
    static const char *here = "main()";
    int   delay   = 0;    int   printid = 0;
    int   compose = 1;    char *file   = 0;
     OlToolkitInitialize( 0 );
    XtToolkitInitialize();
    myContext = XtCreateApplicationContext();
    //
    // This display may get closed, and another opened, inside
    // CoEditor::_init(), if e.g. our parent is on a different screen
    //
    pArgc = &argc;
    globalArgv = argv;
    myDpy = XtOpenDisplay( myContext, 0, 0, "CoEd", 0, 0, &argc, argv );
    int c;
    while ((c = getopt( argc, argv, "p01w:t:" )) != -1) {
           switch (c) {
               case `p':
           printid = 1;
           break;
            case `0':
           compose = 0;
          break;
            case `1':
           maxBuffers = 1;
           break;
            case `w':
           delay = atoi( optarg );
           break;
            case `t':
           timeOutFactor = atoi( optarg );
           break;
            default:
           fputs( usage, stderr );
           exit( 1 );
           }    
}
if (optind < argc) {
           file = argv[ optind ];    
}
while (delay > 0) {
           sleep( 1 );
           delay--;   
}  int myTtFd;                            // Obtain process identifier 
// Initialize toolkit and create a ToolTalk communication endpoint 
char *myProcID = ttdt_open( &myTtFd, ToolName, "SunSoft", "%I", 1 );  

// Declare ptype ttmedia_ptype_declare( "DT_CoEd", 0, CoEditor::loadISOLatin1_,
               (void *)&myTopWidget, 1 ); 

// Process the message that started us, if any 
tttk_Xt_input_handler( 0, 0, 0 ); 
if (abortCode != 0) {
        // Error in message that caused us to start.
        exit( abortCode ); 
}  

if (CoEditor::numEditors == 0) {
        // started by hand, not by ToolTalk
        if (file == 0) {
        if (compose) {
           new CoEditor( &myTopWidget );
        }
        } else {
           new CoEditor( &myTopWidget, file );
        }        
} 
// 
// If sessPats is unset, then we have not joined the desktop 
// session yet.  So join it. 
// 
if (sessPats == 0) {
        Widget session_shell = CoEditor::editors[0]->shell;
        if (maxBuffers > 1) {
           //
           // In multi-window mode, no single window is the
           // distinguished window.
           //
           session_shell = myTopWidget;
        }
        sessPats = ttdt_session_join( 0, 0, session_shell, 0, 1 );
   }
        XtAppAddInput( myContext, myTtFd,(XtPointer)XtInputReadMask,
             tttk_Xt_input_handler, myProcID );
     XtAppMainLoop( myContext );   
}