summaryrefslogtreecommitdiffstats
path: root/src/VBox/Additions/haiku/VBoxTray/VBoxGuestDeskbarView.cpp
blob: aaba2392616ca473269b09c4fa9ba6e1130d3725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* $Id: VBoxGuestDeskbarView.cpp $ */
/** @file
 * VBoxGuestDeskbarView, Haiku Guest Additions, implementation.
 */

/*
 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

/*
 * This code is based on:
 *
 * VirtualBox Guest Additions for Haiku.
 * Copyright (c) 2011 Mike Smith <mike@scgtrp.net>
 *                    François Revol <revol@free.fr>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <errno.h>
#include <Alert.h>
#include <Roster.h>
#include <Debug.h>
#include <Deskbar.h>
#include <File.h>
#include <MenuItem.h>
#include <Path.h>
#include <PopUpMenu.h>
#include <Resources.h>
#include <String.h>
#include <TranslationUtils.h>

#include "VBoxGuestDeskbarView.h"
#include "VBoxGuestApplication.h"

#define VIEWNAME        "VBoxGuestDeskbarView"

static status_t
our_image(image_info& image)
{
    /** @todo r=ramshankar: find a way to do this without annoying the compiler, probably uintptr_t? */
    int32 cookie = 0;
    while (get_next_image_info(B_CURRENT_TEAM, &cookie, &image) == B_OK)
    {
        if ((char *)our_image >= (char *)image.text
            && (char *)our_image <= (char *)image.text + image.text_size)
            return B_OK;
    }

    return B_ERROR;
}


VBoxGuestDeskbarView::VBoxGuestDeskbarView()
     : BView(BRect(0, 0, 15, 15), VIEWNAME, B_FOLLOW_NONE,
             B_WILL_DRAW | B_NAVIGABLE),
       fIcon(NULL), fClipboardService(NULL), fDisplayService(NULL)
{
    _Init();
}


VBoxGuestDeskbarView::VBoxGuestDeskbarView(BMessage *archive)
     : BView(archive),
       fIcon(NULL)
{
    archive->PrintToStream();
    _Init(archive);
}


VBoxGuestDeskbarView::~VBoxGuestDeskbarView()
{
    delete fIcon;
    if (fClipboardService)
    {
        fClipboardService->Disconnect();
        delete fClipboardService;
    }
    VbglR3Term();
}


BArchivable* VBoxGuestDeskbarView::Instantiate(BMessage *data)
{
    if (!validate_instantiation(data, VIEWNAME))
        return NULL;

    return new VBoxGuestDeskbarView(data);
}


status_t VBoxGuestDeskbarView::Archive(BMessage *data, bool deep) const
{
    status_t err;

    err = BView::Archive(data, false);
    if (err < B_OK)
    {
        LogRel(("VBoxGuestDeskbarView::Archive failed.rc=%08lx\n", err));
        return err;
    }
    data->AddString("add_on", VBOX_GUEST_APP_SIG);
    data->AddString("class", "VBoxGuestDeskbarView");
    return B_OK;
}


void VBoxGuestDeskbarView::Draw(BRect rect)
{
    SetDrawingMode(B_OP_ALPHA);
    DrawBitmap(fIcon);
}


void VBoxGuestDeskbarView::AttachedToWindow()
{
    BView::AttachedToWindow();
    if (Parent())
    {
        SetViewColor(Parent()->ViewColor());
        SetLowColor(Parent()->LowColor());
    }

    if (fClipboardService) /* Don't repeatedly crash deskbar if vboxdev not loaded */
    {
        Looper()->AddHandler(fClipboardService);
        fClipboardService->Connect();
    }

    if (fDisplayService)
        fDisplayService->Start();
}


void VBoxGuestDeskbarView::DetachedFromWindow()
{
    BMessage message(B_QUIT_REQUESTED);
    fClipboardService->MessageReceived(&message);
    fDisplayService->MessageReceived(&message);
}


void VBoxGuestDeskbarView::MouseDown(BPoint point)
{
    int32 buttons = B_PRIMARY_MOUSE_BUTTON;
    if (Looper() != NULL && Looper()->CurrentMessage() != NULL)
        Looper()->CurrentMessage()->FindInt32("buttons", &buttons);

    BPoint where = ConvertToScreen(point);

    if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0)
    {
        BPopUpMenu *menu = new BPopUpMenu(B_EMPTY_STRING, false, false);
        menu->SetAsyncAutoDestruct(true);
        menu->SetFont(be_plain_font);

        menu->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED)));
        menu->SetTargetForItems(this);

        menu->Go(where, true, true, true);
    }
}


void VBoxGuestDeskbarView::MessageReceived(BMessage *message)
{
    if (message->what == B_QUIT_REQUESTED)
        RemoveFromDeskbar();
    else
        BHandler::MessageReceived(message);
}


status_t VBoxGuestDeskbarView::AddToDeskbar(bool force)
{
    BDeskbar deskbar;
    status_t err;

    if (force)
        RemoveFromDeskbar();
    else if (deskbar.HasItem(VIEWNAME))
        return B_OK;

    app_info info;
    err = be_app->GetAppInfo(&info);
    if (err < B_OK)
        return err;

    BPath p(&info.ref);
    return deskbar.AddItem(&info.ref);
}


status_t VBoxGuestDeskbarView::RemoveFromDeskbar()
{
    BDeskbar deskbar;
    return deskbar.RemoveItem(VIEWNAME);
}


status_t VBoxGuestDeskbarView::_Init(BMessage *archive)
{
    BString toolTipText;
    toolTipText << VBOX_PRODUCT << " Guest Additions ";
    toolTipText << VBOX_VERSION_MAJOR << "." << VBOX_VERSION_MINOR << "." << VBOX_VERSION_BUILD;
    toolTipText << "r" << VBOX_SVN_REV;

    SetToolTip(toolTipText.String());

    image_info info;
    if (our_image(info) != B_OK)
        return B_ERROR;

    BFile file(info.name, B_READ_ONLY);
    if (file.InitCheck() < B_OK)
        return B_ERROR;

    BResources resources(&file);
    if (resources.InitCheck() < B_OK)
        return B_ERROR;

    const void *data = NULL;
    size_t size;
    //data = resources.LoadResource(B_VECTOR_ICON_TYPE,
    //    kNetworkStatusNoDevice + i, &size);
    data = resources.LoadResource('data', 400, &size);
    if (data != NULL)
    {
        BMemoryIO mem(data, size);
        fIcon = BTranslationUtils::GetBitmap(&mem);
    }

    int rc = RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
    if (RT_SUCCESS(rc))
    {
        rc = VbglR3Init();
        if (RT_SUCCESS(rc))
        {
            fClipboardService = new VBoxShClService();
            fDisplayService = new VBoxDisplayService();
        }
        else
            LogRel(("VBoxGuestDeskbarView::_init VbglR3Init failed. rc=%d\n", rc));
    }
    else
        LogRel(("VBoxGuestDeskbarView::_init RTR3InitDll failed. rc=%d\n", rc));
    return RTErrConvertToErrno(rc);
}


RTDECL(BView*) instantiate_deskbar_item()
{
    return new VBoxGuestDeskbarView();
}