xref: /haiku/src/apps/cortex/AddOnHost/AddOnHostApp.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 // AddOnHostApp.cpp
2 
3 #include "AddOnHostApp.h"
4 #include "AddOnHostProtocol.h"
5 
6 #include <Alert.h>
7 #include <Debug.h>
8 #include <MediaRoster.h>
9 
10 #include <cstdlib>
11 #include <cstring>
12 
13 __USE_CORTEX_NAMESPACE
14 using namespace addon_host;
15 
16 // -------------------------------------------------------- //
17 // *** implementation
18 // -------------------------------------------------------- //
19 
20 int main(int argc, char** argv) {
21 	App app;
22 	if(argc < 2 || strcmp(argv[1], "--addon-host") != 0)
23 	{
24 		int32 response = (new BAlert(
25 			"Cortex AddOnHost",
26 			"This program runs in the background, and is started automatically "
27 			"by Cortex when necessary.  You probably don't want to start it manually.",
28 			"Continue", "Quit"))->Go();
29 		if(response == 1)
30 			return 0;
31 	}
32 	app.Run();
33 	return 0;
34 }
35 
36 App::~App() {}
37 App::App() :
38 	BApplication(g_appSignature) {}
39 
40 // -------------------------------------------------------- //
41 // *** BLooper
42 // -------------------------------------------------------- //
43 
44 bool App::QuitRequested() {
45 	return true;
46 }
47 
48 // -------------------------------------------------------- //
49 // *** BHandler
50 // -------------------------------------------------------- //
51 
52 void App::MessageReceived(
53 	BMessage*								message) {
54 
55 	status_t err;
56 
57 //	message->PrintToStream();
58 
59 	switch(message->what) {
60 		case M_INSTANTIATE: {
61 			// fetch node info
62 			dormant_node_info info;
63 			const void *data;
64 			ssize_t dataSize;
65 			err = message->FindData("info", B_RAW_TYPE, &data, &dataSize);
66 			if(err < B_OK) {
67 				PRINT((
68 					"!!! App::MessageReceived(M_INSTANTIATE):\n"
69 					"    missing 'info'\n"));
70 				break;
71 			}
72 			if(dataSize != sizeof(info)) {
73 				PRINT((
74 					"*   App::MessageReceived(M_INSTANTIATE):\n"
75 					"    warning: 'info' size mismatch\n"));
76 				if(dataSize > ssize_t(sizeof(info)))
77 					dataSize = sizeof(info);
78 			}
79 			memcpy(reinterpret_cast<void *>(&info), data, dataSize);
80 
81 			// attempt to instantiate
82 			BMediaRoster* r = BMediaRoster::Roster();
83 			media_node node;
84 			err = r->InstantiateDormantNode(
85 				info,
86 				&node);
87 
88 //			if(err == B_OK)
89 //				// reference it
90 //				err = r->GetNodeFor(node.node, &node);
91 
92 			// send status
93 			if(err == B_OK) {
94 				BMessage m(M_INSTANTIATE_COMPLETE);
95 				m.AddInt32("node_id", node.node);
96 				message->SendReply(&m);
97 			}
98 			else {
99 				BMessage m(M_INSTANTIATE_FAILED);
100 				m.AddInt32("error", err);
101 				message->SendReply(&m);
102 			}
103 		}
104 		break;
105 
106 		case M_RELEASE: {
107 			// fetch node info
108 			live_node_info info;
109 			const void *data;
110 			ssize_t dataSize;
111 			err = message->FindData("info", B_RAW_TYPE, &data, &dataSize);
112 			if(err < B_OK) {
113 				PRINT((
114 					"!!! App::MessageReceived(M_RELEASE):\n"
115 					"    missing 'info'\n"));
116 				break;
117 			}
118 			if(dataSize != sizeof(info)) {
119 				PRINT((
120 					"*   App::MessageReceived(M_RELEASE):\n"
121 					"    warning: 'info' size mismatch\n"));
122 				if(dataSize > ssize_t(sizeof(info)))
123 					dataSize = sizeof(info);
124 			}
125 			memcpy(reinterpret_cast<void *>(&info), data, dataSize);
126 
127 			// attempt to release
128 			BMediaRoster* r = BMediaRoster::Roster();
129 			media_node node;
130 			err = r->ReleaseNode(info.node);
131 
132 			// send status
133 			if(err == B_OK) {
134 				BMessage m(M_RELEASE_COMPLETE);
135 				m.AddInt32("node_id", info.node.node);
136 				message->SendReply(&m);
137 			}
138 			else {
139 				BMessage m(M_RELEASE_FAILED);
140 				m.AddInt32("error", err);
141 				message->SendReply(&m);
142 			}
143 		}
144 		break;
145 
146 		default:
147 			_inherited::MessageReceived(message);
148 	}
149 }
150 
151 // END -- AddOnHostApp.cpp --
152 
153