root/rat/trunk/main_ui.c @ 3054

Revision 3054, 4.5 KB (checked in by ucaccsp, 15 years ago)

Lots of debugging code - don't even think of playing with this version...

  • 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        Sleep(10000);
81
82        debug_set_core_dir(argv[0]);
83
84        debug_msg("rat-ui started argc=%d\n", argc);
85        parse_args(argc, argv);
86        tcl_init1(argc, argv);
87
88        m = mbus_init(mbus_ui_rx, mbus_error_handler);
89        sprintf(m_addr, "(media:audio module:ui app:rat instance:%lu)", (unsigned long) getpid());
90        mbus_addr(m, m_addr);
91
92        /* The first stage is to wait until we hear from our controller. The address of the */
93        /* controller is passed to us via a command line parameter, and we just wait until  */
94        /* we get an mbus.hello() from that address.                                        */
95        debug_msg("Waiting to validate address %s\n", c_addr);
96        while (!mbus_addr_valid(m, c_addr)) {
97                timeout.tv_sec  = 0;
98                timeout.tv_usec = 250000;
99                mbus_recv(m, NULL, &timeout);
100                mbus_send(m);
101                mbus_heartbeat(m, 1);
102                mbus_retransmit(m);
103        }
104        debug_msg("Address %s is valid\n", c_addr);
105
106        /* Next, we signal to the controller that we are ready to go. It should be sending  */
107        /* us an mbus.waiting(foo) where "foo" is the same as the "-token" argument we were */
108        /* passed on startup. We respond with mbus.go(foo) sent reliably to the controller. */
109        debug_msg("Waiting for mbus.waiting(%s) from controller...\n", token);
110        mbus_rendezvous_go(m, token, (void *) m);
111        debug_msg("...got it\n");
112
113        /* At this point we know the mbus address of our controller, and have conducted   */
114        /* a successful rendezvous with it. It will now send us configuration commands.   */
115        /* We do mbus.waiting(foo) where "foo" is the original token. The controller will */
116        /* eventually respond with mbus.go(foo) when it has finished sending us commands. */
117        debug_msg("Waiting for mbus.go(%s) from controller...\n", token);
118        mbus_rendezvous_waiting(m, c_addr, token, (void *) m);
119        debug_msg("...got it\n");
120
121        /* At this point we should know (at least) the address of the media engine. */
122        /* We may also have been given other information too...                     */
123        assert(e_addr != NULL);
124
125        ui_active = TRUE;
126        tcl_init2(m, e_addr);
127        while (!should_exit) {
128                timeout.tv_sec  = 0;
129                timeout.tv_usec = 20000;
130                mbus_recv(m, (void *)m, &timeout);
131                mbus_heartbeat(m, 1);
132                mbus_retransmit(m);
133                mbus_send(m);
134                while (Tcl_DoOneEvent(TCL_DONT_WAIT | TCL_ALL_EVENTS)) {
135                        /* Process Tcl/Tk events... */
136                }
137                if (Tk_GetNumMainWindows() == 0) {
138                        should_exit = TRUE;
139                }
140                /* Throttle CPU usage */
141#ifdef WIN32
142                /* Just timeout waiting for event that never happens */
143                WaitForSingleObject(hWakeUpEvent, 10);
144#else
145                timeout.tv_sec  = 0;
146                timeout.tv_usec = 10000;
147                select(0, NULL, NULL, NULL, &timeout);
148#endif
149        }
150
151        /* Close things down nicely... */
152        mbus_qmsgf(m, "()", FALSE, "mbus.bye", "");
153        do {
154                mbus_send(m);
155                mbus_retransmit(m);
156                timeout.tv_sec  = 0;
157                timeout.tv_usec = 20000;
158                mbus_recv(m, NULL, &timeout);
159        } while (!mbus_sent_all(m));
160        mbus_exit(m);
161       
162#ifdef WIN32
163        TkWinXCleanup(hAppInstance);
164#endif
165        return 0;
166}
Note: See TracBrowser for help on using the browser.