aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/barrierc/MSWindowsClientTaskBarReceiver.cpp
blob: 8d17900cedafd27da5a4cb8d7839698b3cac37dd (plain) (blame)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2003 Chris Schoeneman
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 * 
 * This package 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 <http://www.gnu.org/licenses/>.
 */

#include "MSWindowsClientTaskBarReceiver.h"

#include "resource.h"
#include "client/Client.h"
#include "platform/MSWindowsClipboard.h"
#include "platform/MSWindowsScreen.h"
#include "arch/win32/ArchTaskBarWindows.h"
#include "arch/win32/ArchMiscWindows.h"
#include "arch/Arch.h"
#include "base/EventQueue.h"
#include "base/log_outputters.h"
#include "base/EventTypes.h"

//
// MSWindowsClientTaskBarReceiver
//

const UINT MSWindowsClientTaskBarReceiver::s_stateToIconID[kMaxState] =
{
    IDI_TASKBAR_NOT_RUNNING,
    IDI_TASKBAR_NOT_WORKING,
    IDI_TASKBAR_NOT_CONNECTED,
    IDI_TASKBAR_NOT_CONNECTED,
    IDI_TASKBAR_CONNECTED
};

MSWindowsClientTaskBarReceiver::MSWindowsClientTaskBarReceiver(
                HINSTANCE appInstance, const BufferedLogOutputter* logBuffer, IEventQueue* events) :
    ClientTaskBarReceiver(events),
    m_appInstance(appInstance),
    m_window(NULL),
    m_logBuffer(logBuffer)
{
    for (UInt32 i = 0; i < kMaxState; ++i) {
        m_icon[i] = loadIcon(s_stateToIconID[i]);
    }
    m_menu = LoadMenu(m_appInstance, MAKEINTRESOURCE(IDR_TASKBAR));

    // don't create the window yet.  we'll create it on demand.  this
    // has the side benefit of being created in the thread used for
    // the task bar.  that's good because it means the existence of
    // the window won't prevent changing the main thread's desktop.

    // add ourself to the task bar
    ARCH->addReceiver(this);
}

MSWindowsClientTaskBarReceiver::~MSWindowsClientTaskBarReceiver()
{
    cleanup();
}

void
MSWindowsClientTaskBarReceiver::cleanup()
{
    ARCH->removeReceiver(this);
    for (UInt32 i = 0; i < kMaxState; ++i) {
        deleteIcon(m_icon[i]);
    }
    DestroyMenu(m_menu);
    destroyWindow();
}

void
MSWindowsClientTaskBarReceiver::showStatus()
{
    // create the window
    createWindow();

    // lock self while getting status
    lock();

    // get the current status
    std::string status = getToolTip();

    // done getting status
    unlock();

    // update dialog
    HWND child = GetDlgItem(m_window, IDC_TASKBAR_STATUS_STATUS);
    SendMessage(child, WM_SETTEXT, 0, (LPARAM)status.c_str());

    if (!IsWindowVisible(m_window)) {
        // position it by the mouse
        POINT cursorPos;
        GetCursorPos(&cursorPos);
        RECT windowRect;
        GetWindowRect(m_window, &windowRect);
        int  x = cursorPos.x;
        int  y = cursorPos.y;
        int fw = GetSystemMetrics(SM_CXDLGFRAME);
        int fh = GetSystemMetrics(SM_CYDLGFRAME);
        int ww = windowRect.right  - windowRect.left;
        int wh = windowRect.bottom - windowRect.top;
        int sw = GetSystemMetrics(SM_CXFULLSCREEN);
        int sh = GetSystemMetrics(SM_CYFULLSCREEN);
        if (fw < 1) {
            fw = 1;
        }
        if (fh < 1) {
            fh = 1;
        }
        if (x + ww - fw > sw) {
            x -= ww - fw;
        }
        else {
            x -= fw;
        }
        if (x < 0) {
            x = 0;
        }
        if (y + wh - fh > sh) {
            y -= wh - fh;
        }
        else {
            y -= fh;
        }
        if (y < 0) {
            y = 0;
        }
        SetWindowPos(m_window, HWND_TOPMOST, x, y, ww, wh,
                            SWP_SHOWWINDOW);
    }
}

void
MSWindowsClientTaskBarReceiver::runMenu(int x, int y)
{
    // do popup menu.  we need a window to pass to TrackPopupMenu().
    // the SetForegroundWindow() and SendMessage() calls around
    // TrackPopupMenu() are to get the menu to be dismissed when
    // another window gets activated and are just one of those
    // win32 weirdnesses.
    createWindow();
    SetForegroundWindow(m_window);
    HMENU menu = GetSubMenu(m_menu, 0);
    SetMenuDefaultItem(menu, IDC_TASKBAR_STATUS, FALSE);
    HMENU logLevelMenu = GetSubMenu(menu, 3);
    CheckMenuRadioItem(logLevelMenu, 0, 6,
                            CLOG->getFilter() - kERROR, MF_BYPOSITION);
    int n = TrackPopupMenu(menu,
                            TPM_NONOTIFY |
                            TPM_RETURNCMD |
                            TPM_LEFTBUTTON |
                            TPM_RIGHTBUTTON,
                            x, y, 0, m_window, NULL);
    SendMessage(m_window, WM_NULL, 0, 0);

    // perform the requested operation
    switch (n) {
    case IDC_TASKBAR_STATUS:
        showStatus();
        break;

    case IDC_TASKBAR_LOG:
        copyLog();
        break;

    case IDC_TASKBAR_SHOW_LOG:
        ARCH->showConsole(true);
        break;

    case IDC_TASKBAR_LOG_LEVEL_ERROR:
        CLOG->setFilter(kERROR);
        break;

    case IDC_TASKBAR_LOG_LEVEL_WARNING:
        CLOG->setFilter(kWARNING);
        break;

    case IDC_TASKBAR_LOG_LEVEL_NOTE:
        CLOG->setFilter(kNOTE);
        break;

    case IDC_TASKBAR_LOG_LEVEL_INFO:
        CLOG->setFilter(kINFO);
        break;

    case IDC_TASKBAR_LOG_LEVEL_DEBUG:
        CLOG->setFilter(kDEBUG);
        break;

    case IDC_TASKBAR_LOG_LEVEL_DEBUG1:
        CLOG->setFilter(kDEBUG1);
        break;

    case IDC_TASKBAR_LOG_LEVEL_DEBUG2:
        CLOG->setFilter(kDEBUG2);
        break;

    case IDC_TASKBAR_QUIT:
        quit();
        break;
    }
}

void
MSWindowsClientTaskBarReceiver::primaryAction()
{
    showStatus();
}

const IArchTaskBarReceiver::Icon
MSWindowsClientTaskBarReceiver::getIcon() const
{
    return static_cast<Icon>(m_icon[getStatus()]);
}

void
MSWindowsClientTaskBarReceiver::copyLog() const
{
    if (m_logBuffer != NULL) {
        // collect log buffer
        String data;
        for (BufferedLogOutputter::const_iterator index = m_logBuffer->begin();
                                index != m_logBuffer->end(); ++index) {
            data += *index;
            data += "\n";
        }

        // copy log to clipboard
        if (!data.empty()) {
            MSWindowsClipboard clipboard(m_window);
            clipboard.open(0);
            clipboard.emptyUnowned();
            clipboard.add(IClipboard::kText, data);
            clipboard.close();
        }
    }
}

void
MSWindowsClientTaskBarReceiver::onStatusChanged()
{
    if (IsWindowVisible(m_window)) {
        showStatus();
    }
}

HICON
MSWindowsClientTaskBarReceiver::loadIcon(UINT id)
{
    HANDLE icon = LoadImage(m_appInstance,
                            MAKEINTRESOURCE(id),
                            IMAGE_ICON,
                            0, 0,
                            LR_DEFAULTCOLOR);
    return static_cast<HICON>(icon);
}

void
MSWindowsClientTaskBarReceiver::deleteIcon(HICON icon)
{
    if (icon != NULL) {
        DestroyIcon(icon);
    }
}

void
MSWindowsClientTaskBarReceiver::createWindow()
{
    // ignore if already created
    if (m_window != NULL) {
        return;
    }

    // get the status dialog
    m_window = CreateDialogParam(m_appInstance,
                            MAKEINTRESOURCE(IDD_TASKBAR_STATUS),
                            NULL,
                            (DLGPROC)&MSWindowsClientTaskBarReceiver::staticDlgProc,
                            reinterpret_cast<LPARAM>(
                                static_cast<void*>(this)));

    // window should appear on top of everything, including (especially)
    // the task bar.
    LONG_PTR style = GetWindowLongPtr(m_window, GWL_EXSTYLE);
    style |= WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
    SetWindowLongPtr(m_window, GWL_EXSTYLE, style);

    // tell the task bar about this dialog
    ArchTaskBarWindows::addDialog(m_window);
}

void
MSWindowsClientTaskBarReceiver::destroyWindow()
{
    if (m_window != NULL) {
        ArchTaskBarWindows::removeDialog(m_window);
        DestroyWindow(m_window);
        m_window = NULL;
    }
}

BOOL
MSWindowsClientTaskBarReceiver::dlgProc(HWND hwnd,
                            UINT msg, WPARAM wParam, LPARAM)
{
    switch (msg) {
    case WM_INITDIALOG:
        // use default focus
        return TRUE;

    case WM_ACTIVATE:
        // hide when another window is activated
        if (LOWORD(wParam) == WA_INACTIVE) {
            ShowWindow(hwnd, SW_HIDE);
        }
        break;
    }
    return FALSE;
}

BOOL CALLBACK
MSWindowsClientTaskBarReceiver::staticDlgProc(HWND hwnd,
                            UINT msg, WPARAM wParam, LPARAM lParam)
{
    // if msg is WM_INITDIALOG, extract the MSWindowsClientTaskBarReceiver*
    // and put it in the extra window data then forward the call.
    MSWindowsClientTaskBarReceiver* self = NULL;
    if (msg == WM_INITDIALOG) {
        self = static_cast<MSWindowsClientTaskBarReceiver*>(
                            reinterpret_cast<void*>(lParam));
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) lParam);
    }
    else {
        // get the extra window data and forward the call
        LONG_PTR data = GetWindowLongPtr(hwnd, GWLP_USERDATA);
        if (data != 0) {
            self = (MSWindowsClientTaskBarReceiver*) data;
        }
    }

    // forward the message
    if (self != NULL) {
        return self->dlgProc(hwnd, msg, wParam, lParam);
    }
    else {
        return (msg == WM_INITDIALOG) ? TRUE : FALSE;
    }
}

IArchTaskBarReceiver*
createTaskBarReceiver(const BufferedLogOutputter* logBuffer, IEventQueue* events)
{
    ArchMiscWindows::setIcons(
        (HICON)LoadImage(ArchMiscWindows::instanceWin32(),
        MAKEINTRESOURCE(IDI_BARRIER),
        IMAGE_ICON,
        32, 32, LR_SHARED),
        (HICON)LoadImage(ArchMiscWindows::instanceWin32(),
        MAKEINTRESOURCE(IDI_BARRIER),
        IMAGE_ICON,
        16, 16, LR_SHARED));

    return new MSWindowsClientTaskBarReceiver(
        MSWindowsScreen::getWindowInstance(), logBuffer, events);
}