The Android Open Source Project | 52d4c30 | 2009-03-03 19:29:09 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2005 The Android Open Source Project |
| 3 | // |
| 4 | // Management of the simulated device. |
| 5 | // |
| 6 | |
| 7 | // For compilers that support precompilation, include "wx/wx.h". |
| 8 | #include "wx/wxprec.h" |
| 9 | |
| 10 | // Otherwise, include all standard headers |
| 11 | #ifndef WX_PRECOMP |
| 12 | # include "wx/wx.h" |
| 13 | #endif |
| 14 | #include "wx/image.h" |
| 15 | |
| 16 | #include "ExternalRuntime.h" |
| 17 | #include "MyApp.h" |
| 18 | #include "UserEvent.h" |
| 19 | #include "UserEventMessage.h" |
| 20 | |
| 21 | #include "SimRuntime.h" |
| 22 | #include "LocalBiChannel.h" |
| 23 | #include "utils.h" |
| 24 | |
| 25 | |
| 26 | using namespace android; |
| 27 | |
| 28 | /* |
| 29 | * Destructor. |
| 30 | */ |
| 31 | ExternalRuntime::~ExternalRuntime(void) |
| 32 | { |
| 33 | if (IsRunning()) { |
| 34 | // TODO: cause thread to stop, then Wait for it |
| 35 | } |
| 36 | printf("Sim: in ~ExternalRuntime()\n"); |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Create and run the thread. |
| 41 | */ |
| 42 | bool ExternalRuntime::StartThread(void) |
| 43 | { |
| 44 | if (Create() != wxTHREAD_NO_ERROR) { |
| 45 | fprintf(stderr, "Sim: ERROR: can't create ExternalRuntime thread\n"); |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | Run(); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Thread entry point. |
| 55 | * |
| 56 | * This just sits and waits for a new connection. It hands it off to the |
| 57 | * main thread and then goes back to waiting. |
| 58 | * |
| 59 | * There is currently no "polite" way to shut this down. |
| 60 | */ |
| 61 | void* ExternalRuntime::Entry(void) |
| 62 | { |
| 63 | LocalBiChannel lbic; |
| 64 | Pipe* reader; |
| 65 | Pipe* writer; |
| 66 | |
| 67 | reader = writer = NULL; |
| 68 | |
| 69 | if (!lbic.create(ANDROID_PIPE_NAME)) { |
| 70 | fprintf(stderr, "Sim: failed creating named pipe '%s'\n", |
| 71 | ANDROID_PIPE_NAME); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | while (lbic.listen(&reader, &writer)) { |
| 76 | /* |
| 77 | * Throw it over the wall. |
| 78 | */ |
| 79 | wxWindow* pMainFrame = ((MyApp*)wxTheApp)->GetMainFrame(); |
| 80 | |
| 81 | UserEventMessage* pUem = new UserEventMessage; |
| 82 | pUem->CreateExternalRuntime(reader, writer); |
| 83 | |
| 84 | UserEvent uev(0, (void*) pUem); |
| 85 | pMainFrame->AddPendingEvent(uev); |
| 86 | |
| 87 | reader = writer = NULL; |
| 88 | } |
| 89 | |
| 90 | printf("Sim: ExternalRuntime thread wants to bail\n"); |
| 91 | |
| 92 | return NULL; |
| 93 | } |
| 94 | |