xref: /haiku/src/apps/cortex/AddOnHost/AddOnHostApp.cpp (revision a127b88ecbfab58f64944c98aa47722a18e363b2)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // AddOnHostApp.cpp
33 
34 #include "AddOnHostApp.h"
35 #include "AddOnHostProtocol.h"
36 
37 #include <Alert.h>
38 #include <Catalog.h>
39 #include <Debug.h>
40 #include <MediaRoster.h>
41 
42 #include <cstdlib>
43 #include <cstring>
44 
45 #undef B_TRANSLATION_CONTEXT
46 #define B_TRANSLATION_CONTEXT "CortexAddOnHost"
47 
48 __USE_CORTEX_NAMESPACE
49 using namespace addon_host;
50 
51 
52 App::App()
53 	: BApplication(g_appSignature)
54 {
55 }
56 
57 
58 App::~App()
59 {
60 }
61 
62 
63 bool
64 App::QuitRequested()
65 {
66 	return true;
67 }
68 
69 
70 void
71 App::MessageReceived(BMessage* message)
72 {
73 	status_t err;
74 
75 //	message->PrintToStream();
76 
77 	switch(message->what) {
78 		case M_INSTANTIATE:
79 		{
80 			// fetch node info
81 			dormant_node_info info;
82 			const void *data;
83 			ssize_t dataSize;
84 			err = message->FindData("info", B_RAW_TYPE, &data, &dataSize);
85 			if (err < B_OK) {
86 				PRINT((
87 					"!!! App::MessageReceived(M_INSTANTIATE):\n"
88 					"    missing 'info'\n"));
89 				break;
90 			}
91 			if (dataSize != sizeof(info)) {
92 				PRINT((
93 					"*   App::MessageReceived(M_INSTANTIATE):\n"
94 					"    warning: 'info' size mismatch\n"));
95 				if (dataSize > ssize_t(sizeof(info)))
96 					dataSize = sizeof(info);
97 			}
98 			memcpy(reinterpret_cast<void *>(&info), data, dataSize);
99 
100 			// attempt to instantiate
101 			BMediaRoster* r = BMediaRoster::Roster();
102 			media_node node;
103 			err = r->InstantiateDormantNode(info, &node);
104 
105 //			if(err == B_OK)
106 //				// reference it
107 //				err = r->GetNodeFor(node.node, &node);
108 
109 			// send status
110 			if (err == B_OK) {
111 				BMessage m(M_INSTANTIATE_COMPLETE);
112 				m.AddInt32("node_id", node.node);
113 				message->SendReply(&m);
114 			}
115 			else {
116 				BMessage m(M_INSTANTIATE_FAILED);
117 				m.AddInt32("error", err);
118 				message->SendReply(&m);
119 			}
120 
121 			break;
122 		}
123 
124 		case M_RELEASE:
125 		{
126 			// fetch node info
127 			live_node_info info;
128 			const void *data;
129 			ssize_t dataSize;
130 			err = message->FindData("info", B_RAW_TYPE, &data, &dataSize);
131 			if (err < B_OK) {
132 				PRINT((
133 					"!!! App::MessageReceived(M_RELEASE):\n"
134 					"    missing 'info'\n"));
135 				break;
136 			}
137 			if (dataSize != sizeof(info)) {
138 				PRINT((
139 					"*   App::MessageReceived(M_RELEASE):\n"
140 					"    warning: 'info' size mismatch\n"));
141 				if(dataSize > ssize_t(sizeof(info)))
142 					dataSize = sizeof(info);
143 			}
144 			memcpy(reinterpret_cast<void *>(&info), data, dataSize);
145 
146 			// attempt to release
147 			BMediaRoster* r = BMediaRoster::Roster();
148 			media_node node;
149 			err = r->ReleaseNode(info.node);
150 
151 			// send status
152 			if (err == B_OK) {
153 				BMessage m(M_RELEASE_COMPLETE);
154 				m.AddInt32("node_id", info.node.node);
155 				message->SendReply(&m);
156 			}
157 			else {
158 				BMessage m(M_RELEASE_FAILED);
159 				m.AddInt32("error", err);
160 				message->SendReply(&m);
161 			}
162 
163 			break;
164 		}
165 
166 		default:
167 			_inherited::MessageReceived(message);
168 	}
169 }
170 
171 
172 int
173 main(int argc, char** argv)
174 {
175 	App app;
176 	if (argc < 2 || strcmp(argv[1], "--addon-host") != 0) {
177 		BAlert* alert = new BAlert(B_TRANSLATE("Cortex AddOnHost"),
178 			B_TRANSLATE("This program runs in the background, and is started "
179 				"automatically by Cortex when necessary. You probably don't "
180 				"want to start it manually."),
181 			B_TRANSLATE("Continue"), B_TRANSLATE("Quit"));
182 		alert->SetShortcut(1, B_ESCAPE);
183 		int32 response = alert->Go();
184 
185 		if(response == 1)
186 			return 0;
187 	}
188 	app.Run();
189 	return 0;
190 }
191