root/rat/trunk/main_ui.c @ 3056

Revision 3056, 4.5 KB (checked in by ucaccsp, 14 years ago)

Remove spurious debugging code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * FILE:    main_ui.c
3 * PROGRAM: RAT
4 * AUTHORS: Colin Perkins
5 *
6 * $Revision$
7 * $Date$
8 *
9 * Copyright (c) 1999-2000 University College London
10 * All rights reserved.
11 *
12 */
13
14#include "config_unix.h"
15#include "config_win32.h"
16#include "debug.h"
17#include "mbus.h"
18#include "mbus_ui.h"
19#include "tcl.h"
20#include "tk.h"
21#include "tcltk.h"
22
23char    *e_addr = NULL;
24char     m_addr[100];
25char    *c_addr, *token, *token_e;
26
27int      ui_active   = FALSE;
28int      should_exit = FALSE;
29
30static void parse_args(int argc, char *argv[])
31{
32        int     i;
33
34        if (argc != 5) {
35                printf("Usage: %s -ctrl <addr> -token <token>\n", argv[0]);
36                exit(1);
37        }
38        for (i = 1; i < argc; i++) {
39                if (strcmp(argv[i], "-ctrl") == 0) {
40                        c_addr = xstrdup(argv[++i]);
41                } else if (strcmp(argv[i], "-token") == 0) {
42                        token   = xstrdup(argv[++i]);
43                        token_e = mbus_encode_str(token);
44                } else {
45                        printf("Unknown argument \"%s\"\n", argv[i]);
46                        abort();
47                }
48        }
49}
50
51#ifdef WIN32
52extern HINSTANCE hAppInstance;
53extern int       TkWinXInit(HINSTANCE);
54extern void      TkWinXCleanup(HINSTANCE);
55#endif
56
57static void 
58mbus_error_handler(int seqnum, int reason)
59{
60        debug_msg("mbus message failed (%d:%d)\n", seqnum, reason);
61        if (should_exit == FALSE) {
62                abort();
63        }
64        UNUSED(seqnum);
65        UNUSED(reason);
66        /* Ignore error we're closing down anyway */
67}
68
69int main(int argc, char *argv[])
70{
71        struct mbus     *m;
72        struct timeval   timeout;
73
74#ifdef WIN32
75        HANDLE     hWakeUpEvent;
76        TkWinXInit(hAppInstance);
77        hWakeUpEvent = CreateEvent(NULL, FALSE, FALSE, "Local\\RAT UI WakeUp Event");
78#endif
79
80        debug_set_core_dir(argv[0]);
81
82        debug_msg("rat-ui started argc=%d\n", argc);
83        parse_args(argc, argv);
84        tcl_init1(argc, argv);
85
86        m = mbus_init(mbus_ui_rx, mbus_error_handler);
87        sprintf(m_addr, "(media:audio module:ui app:rat instance:%lu)", (unsigned long) getpid());
88        mbus_addr(m, m_addr);
89
90        /* The first stage is to wait until we hear from our controller. The address of the */
91        /* controller is passed to us via a command line parameter, and we just wait until  */
92        /* we get an mbus.hello() from that address.                                        */
93        debug_msg("Waiting to validate address %s\n", c_addr);
94        while (!mbus_addr_valid(m, c_addr)) {
95                timeout.tv_sec  = 0;
96                timeout.tv_usec = 250000;
97                mbus_recv(m, NULL, &timeout);
98                mbus_send(m);
99                mbus_heartbeat(m, 1);
100                mbus_retransmit(m);
101        }
102        debug_msg("Address %s is valid\n", c_addr);
103
104        /* Next, we signal to the controller that we are ready to go. It should be sending  */
105        /* us an mbus.waiting(foo) where "foo" is the same as the "-token" argument we were */
106        /* passed on startup. We respond with mbus.go(foo) sent reliably to the controller. */
107        debug_msg("Waiting for mbus.waiting(%s) from controller...\n", token);
108        mbus_rendezvous_go(m, token, (void *) m);
109        debug_msg("...got it\n");
110
111        /* At this point we know the mbus address of our controller, and have conducted   */
112        /* a successful rendezvous with it. It will now send us configuration commands.   */
113        /* We do mbus.waiting(foo) where "foo" is the original token. The controller will */
114        /* eventually respond with mbus.go(foo) when it has finished sending us commands. */
115        debug_msg("Waiting for mbus.go(%s) from controller...\n", token);
116        mbus_rendezvous_waiting(m, c_addr, token, (void *) m);
117        debug_msg("...got it\n");
118
119        /* At this point we should know (at least) the address of the media engine. */
120        /* We may also have been given other information too...                     */
121        assert(e_addr != NULL);
122
123        ui_active = TRUE;
124        tcl_init2(m, e_addr);
125        while (!should_exit) {
126                timeout.tv_sec  = 0;
127                timeout.tv_usec = 20000;
128                mbus_recv(m, (void *)m, &timeout);
129                mbus_heartbeat(m, 1);
130                mbus_retransmit(m);
131                mbus_send(m);
132                while (Tcl_DoOneEvent(TCL_DONT_WAIT | TCL_ALL_EVENTS)) {
133                        /* Process Tcl/Tk events... */
134                }
135                if (Tk_GetNumMainWindows() == 0) {
136                        should_exit = TRUE;
137                }
138                /* Throttle CPU usage */
139#ifdef WIN32
140                /* Just timeout waiting for event that never happens */
141                WaitForSingleObject(hWakeUpEvent, 10);
142#else
143                timeout.tv_sec  = 0;
144                timeout.tv_usec = 10000;
145                select(0, NULL, NULL, NULL, &timeout);
146#endif
147        }
148
149        /* Close things down nicely... */
150        mbus_qmsgf(m, "()", FALSE, "mbus.bye", "");
151        do {
152                mbus_send(m);
153                mbus_retransmit(m);
154                timeout.tv_sec  = 0;
155                timeout.tv_usec = 20000;
156                mbus_recv(m, NULL, &timeout);
157        } while (!mbus_sent_all(m));
158        mbus_exit(m);
159       
160#ifdef WIN32
161        TkWinXCleanup(hAppInstance);
162#endif
163        return 0;
164}
Note: See TracBrowser for help on using the browser.