xref: /haiku/src/apps/cortex/AddOnHost/AddOnHostApp.cpp (revision 71452e98334eaac603bf542d159e24788a46bebb)
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 <Debug.h>
39 #include <MediaRoster.h>
40 
41 #include <cstdlib>
42 #include <cstring>
43 
44 __USE_CORTEX_NAMESPACE
45 using namespace addon_host;
46 
47 
48 App::App()
49 	: BApplication(g_appSignature)
50 {
51 }
52 
53 
54 App::~App()
55 {
56 }
57 
58 
59 bool
60 App::QuitRequested()
61 {
62 	return true;
63 }
64 
65 
66 void
67 App::MessageReceived(BMessage* message)
68 {
69 	status_t err;
70 
71 //	message->PrintToStream();
72 
73 	switch(message->what) {
74 		case M_INSTANTIATE:
75 		{
76 			// fetch node info
77 			dormant_node_info info;
78 			const void *data;
79 			ssize_t dataSize;
80 			err = message->FindData("info", B_RAW_TYPE, &data, &dataSize);
81 			if (err < B_OK) {
82 				PRINT((
83 					"!!! App::MessageReceived(M_INSTANTIATE):\n"
84 					"    missing 'info'\n"));
85 				break;
86 			}
87 			if (dataSize != sizeof(info)) {
88 				PRINT((
89 					"*   App::MessageReceived(M_INSTANTIATE):\n"
90 					"    warning: 'info' size mismatch\n"));
91 				if (dataSize > ssize_t(sizeof(info)))
92 					dataSize = sizeof(info);
93 			}
94 			memcpy(reinterpret_cast<void *>(&info), data, dataSize);
95 
96 			// attempt to instantiate
97 			BMediaRoster* r = BMediaRoster::Roster();
98 			media_node node;
99 			err = r->InstantiateDormantNode(info, &node);
100 
101 //			if(err == B_OK)
102 //				// reference it
103 //				err = r->GetNodeFor(node.node, &node);
104 
105 			// send status
106 			if (err == B_OK) {
107 				BMessage m(M_INSTANTIATE_COMPLETE);
108 				m.AddInt32("node_id", node.node);
109 				message->SendReply(&m);
110 			}
111 			else {
112 				BMessage m(M_INSTANTIATE_FAILED);
113 				m.AddInt32("error", err);
114 				message->SendReply(&m);
115 			}
116 
117 			break;
118 		}
119 
120 		case M_RELEASE:
121 		{
122 			// fetch node info
123 			live_node_info info;
124 			const void *data;
125 			ssize_t dataSize;
126 			err = message->FindData("info", B_RAW_TYPE, &data, &dataSize);
127 			if (err < B_OK) {
128 				PRINT((
129 					"!!! App::MessageReceived(M_RELEASE):\n"
130 					"    missing 'info'\n"));
131 				break;
132 			}
133 			if (dataSize != sizeof(info)) {
134 				PRINT((
135 					"*   App::MessageReceived(M_RELEASE):\n"
136 					"    warning: 'info' size mismatch\n"));
137 				if(dataSize > ssize_t(sizeof(info)))
138 					dataSize = sizeof(info);
139 			}
140 			memcpy(reinterpret_cast<void *>(&info), data, dataSize);
141 
142 			// attempt to release
143 			BMediaRoster* r = BMediaRoster::Roster();
144 			media_node node;
145 			err = r->ReleaseNode(info.node);
146 
147 			// send status
148 			if (err == B_OK) {
149 				BMessage m(M_RELEASE_COMPLETE);
150 				m.AddInt32("node_id", info.node.node);
151 				message->SendReply(&m);
152 			}
153 			else {
154 				BMessage m(M_RELEASE_FAILED);
155 				m.AddInt32("error", err);
156 				message->SendReply(&m);
157 			}
158 
159 			break;
160 		}
161 
162 		default:
163 			_inherited::MessageReceived(message);
164 	}
165 }
166 
167 
168 int
169 main(int argc, char** argv)
170 {
171 	App app;
172 	if (argc < 2 || strcmp(argv[1], "--addon-host") != 0) {
173 		BAlert* alert = new BAlert("Cortex AddOnHost",
174 			"This program runs in the background, and is started automatically "
175 			"by Cortex when necessary.  You probably don't want to start it manually.",
176 			"Continue", "Quit");
177 		alert->SetShortcut(1, B_ESCAPE);
178 		int32 response = alert->Go();
179 
180 		if(response == 1)
181 			return 0;
182 	}
183 	app.Run();
184 	return 0;
185 }
186