summaryrefslogtreecommitdiffstats
path: root/simulator/app/ExternalRuntime.cpp
blob: 9f2cc5e3144fd94c4df783f4989aa10c32ba23ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Copyright 2005 The Android Open Source Project
//  
// Management of the simulated device.
//  
    
// For compilers that support precompilation, include "wx/wx.h".
#include "wx/wxprec.h"
    
// Otherwise, include all standard headers
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
#include "wx/image.h"
    
#include "ExternalRuntime.h"
#include "MyApp.h"
#include "UserEvent.h"
#include "UserEventMessage.h"

#include "SimRuntime.h"
#include "LocalBiChannel.h"
#include "utils.h"


using namespace android;

/*
 * Destructor.
 */
ExternalRuntime::~ExternalRuntime(void)
{
    if (IsRunning()) {
        // TODO: cause thread to stop, then Wait for it
    }
    printf("Sim: in ~ExternalRuntime()\n");
}

/*
 * Create and run the thread.
 */
bool ExternalRuntime::StartThread(void)
{
    if (Create() != wxTHREAD_NO_ERROR) {
        fprintf(stderr, "Sim: ERROR: can't create ExternalRuntime thread\n");
        return false;
    }

    Run();
    return true;
}

/*
 * Thread entry point.
 *
 * This just sits and waits for a new connection.  It hands it off to the
 * main thread and then goes back to waiting.
 *
 * There is currently no "polite" way to shut this down.
 */
void* ExternalRuntime::Entry(void)
{
    LocalBiChannel lbic;
    Pipe* reader;
    Pipe* writer;

    reader = writer = NULL;

    if (!lbic.create(ANDROID_PIPE_NAME)) {
        fprintf(stderr, "Sim: failed creating named pipe '%s'\n",
            ANDROID_PIPE_NAME);
        return NULL;
    }

    while (lbic.listen(&reader, &writer)) {
        /*
         * Throw it over the wall.
         */
        wxWindow* pMainFrame = ((MyApp*)wxTheApp)->GetMainFrame();

        UserEventMessage* pUem = new UserEventMessage;
        pUem->CreateExternalRuntime(reader, writer);

        UserEvent uev(0, (void*) pUem);
        pMainFrame->AddPendingEvent(uev);

        reader = writer = NULL;
    }

    printf("Sim: ExternalRuntime thread wants to bail\n");

    return NULL;
}