1 | /* $Id: VBoxGuestDeskbarView.cpp 46593 2013-06-17 14:32:51Z 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 <mike@scgtrp.net>
|
---|
23 | * François Revol <revol@free.fr>
|
---|
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 | #include <Alert.h>
|
---|
51 | #include <Roster.h>
|
---|
52 | #include <Debug.h>
|
---|
53 | #include <Deskbar.h>
|
---|
54 | #include <File.h>
|
---|
55 | #include <MenuItem.h>
|
---|
56 | #include <Path.h>
|
---|
57 | #include <PopUpMenu.h>
|
---|
58 | #include <Resources.h>
|
---|
59 | #include <String.h>
|
---|
60 | #include <TranslationUtils.h>
|
---|
61 |
|
---|
62 | #include "VBoxGuestDeskbarView.h"
|
---|
63 | #include "VBoxGuestApplication.h"
|
---|
64 |
|
---|
65 | #define VIEWNAME "VBoxGuestDeskbarView"
|
---|
66 |
|
---|
67 | static status_t
|
---|
68 | our_image(image_info& image)
|
---|
69 | {
|
---|
70 | /** @todo r=ramshankar: find a way to do this without annoying the compiler, probably uintptr_t? */
|
---|
71 | int32 cookie = 0;
|
---|
72 | while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK)
|
---|
73 | {
|
---|
74 | if ((char *)our_image >= (char *)image.text
|
---|
75 | && (char *)our_image <= (char *)image.text + image.text_size)
|
---|
76 | return B_OK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return B_ERROR;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | VBoxGuestDeskbarView::VBoxGuestDeskbarView()
|
---|
84 | : BView(BRect(0, 0, 15, 15), VIEWNAME, B_FOLLOW_NONE,
|
---|
85 | B_WILL_DRAW | B_NAVIGABLE),
|
---|
86 | fIcon(NULL), fClipboardService(NULL), fDisplayService(NULL)
|
---|
87 | {
|
---|
88 | _Init();
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | VBoxGuestDeskbarView::VBoxGuestDeskbarView(BMessage *archive)
|
---|
93 | : BView(archive),
|
---|
94 | fIcon(NULL)
|
---|
95 | {
|
---|
96 | archive->PrintToStream();
|
---|
97 | _Init(archive);
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | VBoxGuestDeskbarView::~VBoxGuestDeskbarView()
|
---|
102 | {
|
---|
103 | delete fIcon;
|
---|
104 | if (fClipboardService)
|
---|
105 | {
|
---|
106 | fClipboardService->Disconnect();
|
---|
107 | delete fClipboardService;
|
---|
108 | }
|
---|
109 | VbglR3Term();
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | BArchivable* VBoxGuestDeskbarView::Instantiate(BMessage *data)
|
---|
114 | {
|
---|
115 | if (!validate_instantiation(data, VIEWNAME))
|
---|
116 | return NULL;
|
---|
117 |
|
---|
118 | return new VBoxGuestDeskbarView(data);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | status_t VBoxGuestDeskbarView::Archive(BMessage *data, bool deep) const
|
---|
123 | {
|
---|
124 | status_t err;
|
---|
125 |
|
---|
126 | err = BView::Archive(data, false);
|
---|
127 | if (err < B_OK)
|
---|
128 | {
|
---|
129 | LogRel(("VBoxGuestDeskbarView::Archive failed.rc=%08lx\n", err));
|
---|
130 | return err;
|
---|
131 | }
|
---|
132 | data->AddString("add_on", VBOX_GUEST_APP_SIG);
|
---|
133 | data->AddString("class", "VBoxGuestDeskbarView");
|
---|
134 | return B_OK;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | void VBoxGuestDeskbarView::Draw(BRect rect)
|
---|
139 | {
|
---|
140 | SetDrawingMode(B_OP_ALPHA);
|
---|
141 | DrawBitmap(fIcon);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | void VBoxGuestDeskbarView::AttachedToWindow()
|
---|
146 | {
|
---|
147 | BView::AttachedToWindow();
|
---|
148 | if (Parent())
|
---|
149 | {
|
---|
150 | SetViewColor(Parent()->ViewColor());
|
---|
151 | SetLowColor(Parent()->LowColor());
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (fClipboardService) /* Don't repeatedly crash deskbar if vboxdev not loaded */
|
---|
155 | {
|
---|
156 | Looper()->AddHandler(fClipboardService);
|
---|
157 | fClipboardService->Connect();
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (fDisplayService)
|
---|
161 | fDisplayService->Start();
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | void VBoxGuestDeskbarView::DetachedFromWindow()
|
---|
166 | {
|
---|
167 | BMessage message(B_QUIT_REQUESTED);
|
---|
168 | fClipboardService->MessageReceived(&message);
|
---|
169 | fDisplayService->MessageReceived(&message);
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | void VBoxGuestDeskbarView::MouseDown(BPoint point)
|
---|
174 | {
|
---|
175 | int32 buttons = B_PRIMARY_MOUSE_BUTTON;
|
---|
176 | if (Looper() != NULL && Looper()->CurrentMessage() != NULL)
|
---|
177 | Looper()->CurrentMessage()->FindInt32("buttons", &buttons);
|
---|
178 |
|
---|
179 | BPoint where = ConvertToScreen(point);
|
---|
180 |
|
---|
181 | if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0)
|
---|
182 | {
|
---|
183 | BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
|
---|
184 | menu->SetAsyncAutoDestruct(true);
|
---|
185 | menu->SetFont(be_plain_font);
|
---|
186 |
|
---|
187 | menu->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED)));
|
---|
188 | menu->SetTargetForItems(this);
|
---|
189 |
|
---|
190 | menu->Go(where, true, true, true);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | void VBoxGuestDeskbarView::MessageReceived(BMessage *message)
|
---|
196 | {
|
---|
197 | if (message->what == B_QUIT_REQUESTED)
|
---|
198 | RemoveFromDeskbar();
|
---|
199 | else
|
---|
200 | BHandler::MessageReceived(message);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | status_t VBoxGuestDeskbarView::AddToDeskbar(bool force)
|
---|
205 | {
|
---|
206 | BDeskbar deskbar;
|
---|
207 | status_t err;
|
---|
208 |
|
---|
209 | if (force)
|
---|
210 | RemoveFromDeskbar();
|
---|
211 | else if (deskbar.HasItem(VIEWNAME))
|
---|
212 | return B_OK;
|
---|
213 |
|
---|
214 | app_info info;
|
---|
215 | err = be_app->GetAppInfo(&info);
|
---|
216 | if (err < B_OK)
|
---|
217 | return err;
|
---|
218 |
|
---|
219 | BPath p(&info.ref);
|
---|
220 | return deskbar.AddItem(&info.ref);
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | status_t VBoxGuestDeskbarView::RemoveFromDeskbar()
|
---|
225 | {
|
---|
226 | BDeskbar deskbar;
|
---|
227 | return deskbar.RemoveItem(VIEWNAME);
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | status_t VBoxGuestDeskbarView::_Init(BMessage *archive)
|
---|
232 | {
|
---|
233 | BString toolTipText;
|
---|
234 | toolTipText << VBOX_PRODUCT << " Guest Additions ";
|
---|
235 | toolTipText << VBOX_VERSION_MAJOR << "." << VBOX_VERSION_MINOR << "." << VBOX_VERSION_BUILD;
|
---|
236 | toolTipText << "r" << VBOX_SVN_REV;
|
---|
237 |
|
---|
238 | SetToolTip(toolTipText.String());
|
---|
239 |
|
---|
240 | image_info info;
|
---|
241 | if (our_image(info) != B_OK)
|
---|
242 | return B_ERROR;
|
---|
243 |
|
---|
244 | BFile file(info.name, B_READ_ONLY);
|
---|
245 | if (file.InitCheck() < B_OK)
|
---|
246 | return B_ERROR;
|
---|
247 |
|
---|
248 | BResources resources(&file);
|
---|
249 | if (resources.InitCheck() < B_OK)
|
---|
250 | return B_ERROR;
|
---|
251 |
|
---|
252 | const void *data = NULL;
|
---|
253 | size_t size;
|
---|
254 | //data = resources.LoadResource(B_VECTOR_ICON_TYPE,
|
---|
255 | // kNetworkStatusNoDevice + i, &size);
|
---|
256 | data = resources.LoadResource('data', 400, &size);
|
---|
257 | if (data != NULL)
|
---|
258 | {
|
---|
259 | BMemoryIO mem(data, size);
|
---|
260 | fIcon = BTranslationUtils::GetBitmap(&mem);
|
---|
261 | }
|
---|
262 |
|
---|
263 | int rc = RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
|
---|
264 | if (RT_SUCCESS(rc))
|
---|
265 | {
|
---|
266 | rc = VbglR3Init();
|
---|
267 | if (RT_SUCCESS(rc))
|
---|
268 | {
|
---|
269 | fClipboardService = new VBoxClipboardService();
|
---|
270 | fDisplayService = new VBoxDisplayService();
|
---|
271 | }
|
---|
272 | else
|
---|
273 | LogRel(("VBoxGuestDeskbarView::_init VbglR3Init failed. rc=%d\n", rc));
|
---|
274 | }
|
---|
275 | else
|
---|
276 | LogRel(("VBoxGuestDeskbarView::_init RTR3InitDll failed. rc=%d\n", rc));
|
---|
277 | return RTErrConvertToErrno(rc);
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | RTDECL(BView*) instantiate_deskbar_item()
|
---|
282 | {
|
---|
283 | return new VBoxGuestDeskbarView();
|
---|
284 | }
|
---|
285 |
|
---|