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