To use the ToolTalk service, your application calls ToolTalk functions from the ToolTalk API library. To modify your application to use the ToolTalk service, you must first include the ToolTalk API header file in your program. After you have initialized the ToolTalk service and joined a session, you can join files and additional user sessions. When your process is ready to quit, you unregister your message patterns and leave your ToolTalk session.
The following code sample shows how a program includes this file.
#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <desktop/tt_c.h>
The ToolTalk functions you need to register with the ToolTalk service are shown in Table 7-1.
Table 7-1 Registering with the ToolTalk Service
The following code sample first initializes and registers the sample program with the ToolTalk service, and then obtains the corresponding file descriptor.
int ttfd;
char *my_procid;
/*
* Initialize ToolTalk, using the initial default session
*/
my_procid = tt_open();
/*
* obtain the file descriptor that will become active whenever
* ToolTalk has a message for this process.
*/
ttfd = tt_fd();
tt_open returns the procid for your process and sets it as the default procid; tt_fd returns a file descriptor for your current procid that will become active when a message arrives for your application.
Caution: Your application must call tt_open before other tt_ calls are made; otherwise, errors may occur. However, there are two exceptions: tt_default_session_set and tt_X_session can be called before tt_open.
When tt_open is the first call made to the ToolTalk service, it sets the initial session as the default session. The default session identifier (sessid) is important to the delivery of ToolTalk messages. The ToolTalk service automatically fills in the default sessid if an application does not explicitly set the session message attribute. If the message is scoped to TT_SESSION, the message will be delivered to all applications in the default session that have registered interest in this type of message.
The following code sample shows how to join an X session named somehost:0 that is not your initial session.
char *my_session;
char *my_procid;
my_session = tt_X_session("somehost:0");
tt_default_session_set(my_session);
my_procid = tt_open();
ttfd = tt_fd();
This call retrieves the name of the session associated with an X display server. tt_X_session() takes the argument
char *xdisplay_name
where xdisplay_name is the name of an X display server (in this example, somehost:0, :0).
This call sets the new session as the default session.
This call returns the procid for your process and sets it as the default procid.
This call returns a file descriptor for your current procid.
The following code sample shows how to connect procid to sessid1, and procid2 to sessid2.
tt_default_session_set(sessid1);
my_procid1 = tt_open();
tt_default_session_set(sessid2);
my_procid2 = tt_open();
tt_fd2 = tt_fd();
You can then use tt_default_procid_set to switch between the sessions.For example, a program that uses the XView notifier, through the xv_main_loop or notify_start calls, can have a callback function invoked when the file descriptor becomes active. The following code sample invokes notify_set_input_func with the handle for the message object as a parameter.
/*
* Arrange for XView to call receive_tt_message when the
* ToolTalk file descriptor becomes active.
*/
notify_set_input_func(base_frame,
Notify_func)receive_tt_message,
tfd);
Table 7-2 describes various window toolkits and the call used to watch for arriving messages.
Table 7-2 Code Used to Watch for Arriving Messages
One workaround is to put a refcount on the message. To do this, use the tt_message_user[_set]() function.
Another workaround is to destroy the message in the receiver only if the sender is not the current procid; for example:
Tt_callback_action
my_pattern_callback(Tt_message m, Tt_pattern p)
{
/* normal message processing goes here */
if (0!=strcmp(tt_message_sender(m),tt_default_procid()) {
tt_message_destroy(m);
}
return TT_CALLBACK_PROCESSED;
}
defines _TT_SESSION in that cmdtool and any ToolTalk client you run with the environment variable $_TT_SESSION set to its value will join the session owned by this ttsession.
ttsession -c cmdtool
/*
* Before leaving, allow ToolTalk to clean up.
*/
tt_close();
exit(0);
}
tt_close returns Tt_status and closes the current default procid.