BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / switcher / CaptureWindow.cpp
blob93e2c22f62ae7323b154d609be107ef506bf3796
1 /*
2 * Copyright 2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "CaptureWindow.h"
9 #include <stdio.h>
11 #include <Roster.h>
12 #include <Screen.h>
14 #include "Switcher.h"
17 static const bigtime_t kUpdateDelay = 50000;
18 // 50 ms
21 class CaptureView : public BView {
22 public:
23 CaptureView();
24 virtual ~CaptureView();
26 void AddMoveOutNotification(BMessage* message);
28 virtual void MouseMoved(BPoint point, uint32 transit,
29 const BMessage* dragMessage);
30 virtual void KeyDown(const char* bytes, int32 numBytes);
32 private:
33 void _UpdateLast(const BPoint& point);
34 void _Notify(uint32 location, team_id team);
35 void _SendMovedOutNotification();
37 static team_id _CurrentTeam();
39 private:
40 uint32 fModifierMask;
41 BPoint fLastPoint;
42 team_id fLastTeam;
43 bigtime_t fLastEvent;
45 uint32 fMovedOutWhat;
46 BMessenger fMovedOutMessenger;
47 BRect fMovedOutFrame;
51 CaptureView::CaptureView()
53 BView("main", 0),
54 fModifierMask(B_CONTROL_KEY),
55 fMovedOutWhat(0)
57 SetEventMask(B_POINTER_EVENTS | B_KEYBOARD_EVENTS, B_NO_POINTER_HISTORY);
58 _UpdateLast(BPoint(-1, -1));
62 CaptureView::~CaptureView()
67 void
68 CaptureView::AddMoveOutNotification(BMessage* message)
70 if (fMovedOutWhat != 0)
71 _SendMovedOutNotification();
73 if (message->FindMessenger("target", &fMovedOutMessenger) == B_OK
74 && message->FindRect("frame", &fMovedOutFrame) == B_OK)
75 fMovedOutWhat = (uint32)message->FindInt32("what");
79 void
80 CaptureView::MouseMoved(BPoint point, uint32 transit,
81 const BMessage* dragMessage)
83 ConvertToScreen(&point);
85 if (fMovedOutWhat != 0 && !fMovedOutFrame.Contains(point))
86 _SendMovedOutNotification();
88 uint32 modifiers = ::modifiers();
89 if ((modifiers & fModifierMask) == 0) {
90 _UpdateLast(point);
91 return;
94 // TODO: we will need to iterate over all existing screens to find the
95 // right one!
96 BScreen screen;
97 BRect screenFrame = screen.Frame();
99 uint32 location = kNowhere;
100 if (point.x <= screenFrame.left && fLastPoint.x > screenFrame.left)
101 location = kLeftEdge;
102 else if (point.x >= screenFrame.right && fLastPoint.x < screenFrame.right)
103 location = kRightEdge;
104 else if (point.y <= screenFrame.top && fLastPoint.y > screenFrame.top)
105 location = kTopEdge;
106 else if (point.y >= screenFrame.bottom && fLastPoint.y < screenFrame.bottom)
107 location = kBottomEdge;
109 if (location != kNowhere)
110 _Notify(location, fLastTeam);
112 _UpdateLast(point);
116 void
117 CaptureView::KeyDown(const char* bytes, int32 numBytes)
119 if ((::modifiers() & (B_CONTROL_KEY | B_SHIFT_KEY | B_OPTION_KEY
120 | B_COMMAND_KEY)) != (B_COMMAND_KEY | B_CONTROL_KEY))
121 return;
123 uint32 location = kNowhere;
125 switch (bytes[0]) {
126 case '1':
127 location = kLeftEdge;
128 break;
129 case '2':
130 location = kRightEdge;
131 break;
132 case '3':
133 location = kTopEdge;
134 break;
135 case '4':
136 location = kBottomEdge;
137 break;
140 if (location != kNowhere)
141 _Notify(location, _CurrentTeam());
145 void
146 CaptureView::_UpdateLast(const BPoint& point)
148 fLastPoint = point;
150 bigtime_t now = system_time();
152 // We update the currently active application only, if the mouse did
153 // not move over it for a certain time - this is necessary only for
154 // focus follow mouse.
155 if (now > fLastEvent + kUpdateDelay)
156 fLastTeam = _CurrentTeam();
158 fLastEvent = now;
162 void
163 CaptureView::_Notify(uint32 location, team_id team)
165 if (location == kNowhere)
166 return;
168 BMessage message(kMsgLocationTrigger);
169 message.AddInt32("location", location);
170 message.AddInt32("team", team);
172 be_app->PostMessage(&message);
176 void
177 CaptureView::_SendMovedOutNotification()
179 BMessage movedOut(fMovedOutWhat);
180 fMovedOutMessenger.SendMessage(&movedOut);
181 fMovedOutWhat = 0;
185 /*static*/ team_id
186 CaptureView::_CurrentTeam()
188 app_info appInfo;
189 status_t status = be_roster->GetActiveAppInfo(&appInfo);
190 if (status == B_OK)
191 return appInfo.team;
193 return status;
197 // #pragma mark -
200 CaptureWindow::CaptureWindow()
202 BWindow(BRect(0, 0, 100, 100), "mouse capture", B_NO_BORDER_WINDOW_LOOK,
203 B_NORMAL_WINDOW_FEEL, B_ASYNCHRONOUS_CONTROLS, B_ALL_WORKSPACES)
205 fCaptureView = new CaptureView();
206 AddChild(fCaptureView);
210 CaptureWindow::~CaptureWindow()
215 void
216 CaptureWindow::MessageReceived(BMessage* message)
218 switch (message->what) {
219 case kMsgHideWhenMouseMovedOut:
220 fCaptureView->AddMoveOutNotification(message);
221 break;
222 default:
223 BWindow::MessageReceived(message);
224 break;