1 | /* $Id: VBoxGuestDeskbarView.cpp 43364 2012-09-20 12:12:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestDeskbarView, Haiku Guest Additions, implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * This code is based on:
|
---|
20 | *
|
---|
21 | * VirtualBox Guest Additions for Haiku.
|
---|
22 | * Copyright (c) 2011 Mike Smith <[email protected]>
|
---|
23 | * François Revol <[email protected]>
|
---|
24 | *
|
---|
25 | * Permission is hereby granted, free of charge, to any person
|
---|
26 | * obtaining a copy of this software and associated documentation
|
---|
27 | * files (the "Software"), to deal in the Software without
|
---|
28 | * restriction, including without limitation the rights to use,
|
---|
29 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
30 | * copies of the Software, and to permit persons to whom the
|
---|
31 | * Software is furnished to do so, subject to the following
|
---|
32 | * conditions:
|
---|
33 | *
|
---|
34 | * The above copyright notice and this permission notice shall be
|
---|
35 | * included in all copies or substantial portions of the Software.
|
---|
36 | *
|
---|
37 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
38 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
39 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
40 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
41 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
42 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
43 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
44 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
45 | */
|
---|
46 | /*******************************************************************************
|
---|
47 | * Header Files *
|
---|
48 | *******************************************************************************/
|
---|
49 | #include <errno.h>
|
---|
50 | #define DEBUG 1
|
---|
51 | #include <Alert.h>
|
---|
52 | #include <Roster.h>
|
---|
53 | #include <Debug.h>
|
---|
54 | #include <Deskbar.h>
|
---|
55 | #include <File.h>
|
---|
56 | #include <MenuItem.h>
|
---|
57 | #include <Path.h>
|
---|
58 | #include <PopUpMenu.h>
|
---|
59 | #include <Resources.h>
|
---|
60 | #include <String.h>
|
---|
61 | #include <TranslationUtils.h>
|
---|
62 |
|
---|
63 | #include "VBoxGuestDeskbarView.h"
|
---|
64 | #include "VBoxGuestApplication.h"
|
---|
65 |
|
---|
66 | #define VIEWNAME "VBoxGuestDeskbarView"
|
---|
67 |
|
---|
68 | static status_t
|
---|
69 | our_image(image_info& image)
|
---|
70 | {
|
---|
71 | /** @todo r=ramshankar: find a way to do this without annoying the compiler, probably uintptr_t? */
|
---|
72 | int32 cookie = 0;
|
---|
73 | while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK)
|
---|
74 | {
|
---|
75 | if ((char *)our_image >= (char *)image.text
|
---|
76 | && (char *)our_image <= (char *)image.text + image.text_size)
|
---|
77 | return B_OK;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return B_ERROR;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | VBoxGuestDeskbarView::VBoxGuestDeskbarView()
|
---|
85 | : BView(BRect(0, 0, 15, 15), VIEWNAME, B_FOLLOW_NONE,
|
---|
86 | B_WILL_DRAW | B_NAVIGABLE),
|
---|
87 | fIcon(NULL), fClipboardService(NULL), fDisplayService(NULL)
|
---|
88 | {
|
---|
89 | PRINT(("%s()\n", __FUNCTION__));
|
---|
90 | _Init();
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | VBoxGuestDeskbarView::VBoxGuestDeskbarView(BMessage *archive)
|
---|
95 | : BView(archive),
|
---|
96 | fIcon(NULL)
|
---|
97 | {
|
---|
98 | PRINT(("%s()\n", __FUNCTION__));
|
---|
99 | archive->PrintToStream();
|
---|
100 |
|
---|
101 | _Init(archive);
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | VBoxGuestDeskbarView::~VBoxGuestDeskbarView()
|
---|
106 | {
|
---|
107 | PRINT(("%s()\n", __FUNCTION__));
|
---|
108 | delete fIcon;
|
---|
109 | if (fClipboardService)
|
---|
110 | {
|
---|
111 | fClipboardService->Disconnect();
|
---|
112 | delete fClipboardService;
|
---|
113 | }
|
---|
114 | VbglR3Term();
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | BArchivable* VBoxGuestDeskbarView::Instantiate(BMessage *data)
|
---|
119 | {
|
---|
120 | PRINT(("%s()\n", __FUNCTION__));
|
---|
121 | if (!validate_instantiation(data, VIEWNAME))
|
---|
122 | return NULL;
|
---|
123 |
|
---|
124 | return new VBoxGuestDeskbarView(data);
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | status_t VBoxGuestDeskbarView::Archive(BMessage *data, bool deep) const
|
---|
129 | {
|
---|
130 | status_t err;
|
---|
131 | PRINT(("%s()\n", __FUNCTION__));
|
---|
132 |
|
---|
133 | err = BView::Archive(data, false);
|
---|
134 | if (err < B_OK)
|
---|
135 | return err;
|
---|
136 | data->AddString("add_on", VBOX_GUEST_APP_SIG);
|
---|
137 | data->AddString("class", "VBoxGuestDeskbarView");
|
---|
138 | return B_OK;
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | void VBoxGuestDeskbarView::Draw(BRect rect)
|
---|
143 | {
|
---|
144 | SetDrawingMode(B_OP_ALPHA);
|
---|
145 | DrawBitmap(fIcon);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | void VBoxGuestDeskbarView::AttachedToWindow()
|
---|
150 | {
|
---|
151 | BView::AttachedToWindow();
|
---|
152 | if (Parent())
|
---|
153 | {
|
---|
154 | SetViewColor(Parent()->ViewColor());
|
---|
155 | SetLowColor(Parent()->LowColor());
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (fClipboardService) // don't repeatedly crash deskbar if vboxdev not loaded
|
---|
159 | {
|
---|
160 | Looper()->AddHandler(fClipboardService);
|
---|
161 | fClipboardService->Connect();
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (fDisplayService)
|
---|
165 | {
|
---|
166 | fDisplayService->Start();
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | void VBoxGuestDeskbarView::DetachedFromWindow()
|
---|
172 | {
|
---|
173 | BMessage message(B_QUIT_REQUESTED);
|
---|
174 | fClipboardService->MessageReceived(&message);
|
---|
175 | fDisplayService->MessageReceived(&message);
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | void VBoxGuestDeskbarView::MouseDown(BPoint point)
|
---|
180 | {
|
---|
181 | printf("MouseDown\n");
|
---|
182 | int32 buttons = B_PRIMARY_MOUSE_BUTTON;
|
---|
183 | if (Looper() != NULL && Looper()->CurrentMessage() != NULL)
|
---|
184 | Looper()->CurrentMessage()->FindInt32("buttons", &buttons);
|
---|
185 |
|
---|
186 | BPoint where = ConvertToScreen(point);
|
---|
187 |
|
---|
188 | if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0)
|
---|
189 | {
|
---|
190 | BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
|
---|
191 | menu->SetAsyncAutoDestruct(true);
|
---|
192 | menu->SetFont(be_plain_font);
|
---|
193 |
|
---|
194 | menu->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED)));
|
---|
195 | menu->SetTargetForItems(this);
|
---|
196 |
|
---|
197 | menu->Go(where, true, true, true);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 | void VBoxGuestDeskbarView::MessageReceived(BMessage *message)
|
---|
203 | {
|
---|
204 | if (message->what == B_QUIT_REQUESTED)
|
---|
205 | RemoveFromDeskbar();
|
---|
206 | else
|
---|
207 | BHandler::MessageReceived(message);
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | status_t VBoxGuestDeskbarView::AddToDeskbar(bool force)
|
---|
212 | {
|
---|
213 | BDeskbar deskbar;
|
---|
214 | status_t err;
|
---|
215 | PRINT(("%s()\n", __FUNCTION__));
|
---|
216 |
|
---|
217 | if (force)
|
---|
218 | RemoveFromDeskbar();
|
---|
219 | else if (deskbar.HasItem(VIEWNAME))
|
---|
220 | return B_OK;
|
---|
221 |
|
---|
222 | app_info info;
|
---|
223 | err = be_app->GetAppInfo(&info);
|
---|
224 | PRINT(("%s: be_app->GetAppInfo: 0x%08lx\n", __FUNCTION__, err));
|
---|
225 | if (err < B_OK)
|
---|
226 | return err;
|
---|
227 |
|
---|
228 | BPath p(&info.ref);
|
---|
229 | PRINT(("%s: app path: '%s'\n", __FUNCTION__, p.Path()));
|
---|
230 |
|
---|
231 | return deskbar.AddItem(&info.ref);
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | status_t VBoxGuestDeskbarView::RemoveFromDeskbar()
|
---|
236 | {
|
---|
237 | BDeskbar deskbar;
|
---|
238 | PRINT(("%s()\n", __FUNCTION__));
|
---|
239 |
|
---|
240 | return deskbar.RemoveItem(VIEWNAME);
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | status_t VBoxGuestDeskbarView::_Init(BMessage *archive)
|
---|
245 | {
|
---|
246 | BString toolTipText;
|
---|
247 | toolTipText << VBOX_PRODUCT << " Guest Additions ";
|
---|
248 | toolTipText << VBOX_VERSION_MAJOR << "." << VBOX_VERSION_MINOR << "." << VBOX_VERSION_BUILD;
|
---|
249 | toolTipText << "r" << VBOX_SVN_REV;
|
---|
250 |
|
---|
251 | SetToolTip(toolTipText.String());
|
---|
252 |
|
---|
253 | image_info info;
|
---|
254 | if (our_image(info) != B_OK)
|
---|
255 | return B_ERROR;
|
---|
256 |
|
---|
257 | BFile file(info.name, B_READ_ONLY);
|
---|
258 | if (file.InitCheck() < B_OK)
|
---|
259 | return B_ERROR;
|
---|
260 |
|
---|
261 | BResources resources(&file);
|
---|
262 | if (resources.InitCheck() < B_OK)
|
---|
263 | return B_ERROR;
|
---|
264 |
|
---|
265 | const void *data = NULL;
|
---|
266 | size_t size;
|
---|
267 | //data = resources.LoadResource(B_VECTOR_ICON_TYPE,
|
---|
268 | // kNetworkStatusNoDevice + i, &size);
|
---|
269 | data = resources.LoadResource('data', 400, &size);
|
---|
270 | if (data != NULL)
|
---|
271 | {
|
---|
272 | BMemoryIO mem(data, size);
|
---|
273 | fIcon = BTranslationUtils::GetBitmap(&mem);
|
---|
274 | }
|
---|
275 |
|
---|
276 | int rc = RTR3InitDll(0);
|
---|
277 | printf("%d\n", rc);
|
---|
278 | if (RT_SUCCESS(rc))
|
---|
279 | {
|
---|
280 | rc = VbglR3Init();
|
---|
281 | }
|
---|
282 | printf("%d\n", rc);
|
---|
283 | if (RT_SUCCESS(rc))
|
---|
284 | {
|
---|
285 | fClipboardService = new VBoxClipboardService();
|
---|
286 | fDisplayService = new VBoxDisplayService();
|
---|
287 | }
|
---|
288 |
|
---|
289 | return RTErrConvertToErrno(rc);
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | RTDECL(BView*) instantiate_deskbar_item()
|
---|
294 | {
|
---|
295 | PRINT(("%s()\n", __FUNCTION__));
|
---|
296 | return new VBoxGuestDeskbarView();
|
---|
297 | }
|
---|
298 |
|
---|