VirtualBox

source: vbox/trunk/src/VBox/Additions/haiku/VBoxTray/VBoxGuestDeskbarView.cpp

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use