1 /* 2 * Copyright 2001-2015, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Adrian Oanca <adioanca@cotty.iren.ro> 7 * Stephan Aßmus <superstippi@gmx.de> 8 * Axel Dörfler <axeld@pinc-software.de> 9 * Andrej Spielmann, <andrej.spielmann@seh.ox.ac.uk> 10 * Brecht Machiels <brecht@mos6581.org> 11 * Clemens Zeidler <haiku@clemens-zeidler.de> 12 * Joseph Groover <looncraz@looncraz.net> 13 */ 14 #ifndef DESKTOP_H 15 #define DESKTOP_H 16 17 18 #include <Autolock.h> 19 #include <InterfaceDefs.h> 20 #include <List.h> 21 #include <Menu.h> 22 #include <ObjectList.h> 23 #include <Region.h> 24 #include <Window.h> 25 26 #include <ServerProtocolStructs.h> 27 28 #include "CursorManager.h" 29 #include "DelayedMessage.h" 30 #include "DesktopListener.h" 31 #include "DesktopSettings.h" 32 #include "EventDispatcher.h" 33 #include "MessageLooper.h" 34 #include "MultiLocker.h" 35 #include "Screen.h" 36 #include "ScreenManager.h" 37 #include "ServerCursor.h" 38 #include "StackAndTile.h" 39 #include "VirtualScreen.h" 40 #include "WindowList.h" 41 #include "Workspace.h" 42 #include "WorkspacePrivate.h" 43 44 45 class BMessage; 46 47 class DecorAddOn; 48 class DrawingEngine; 49 class HWInterface; 50 class ServerApp; 51 class Window; 52 class WorkspacesView; 53 struct server_read_only_memory; 54 55 namespace BPrivate { 56 class LinkSender; 57 }; 58 59 60 class Desktop : public DesktopObservable, public MessageLooper, 61 public ScreenOwner { 62 public: 63 Desktop(uid_t userID, 64 const char* targetScreen); 65 virtual ~Desktop(); 66 67 void RegisterListener(DesktopListener* listener); 68 69 status_t Init(); 70 71 uid_t UserID() const { return fUserID; } 72 const char* TargetScreen() { return fTargetScreen; } 73 virtual port_id MessagePort() const { return fMessagePort; } 74 area_id SharedReadOnlyArea() const 75 { return fSharedReadOnlyArea; } 76 77 ::EventDispatcher& EventDispatcher() { return fEventDispatcher; } 78 79 void BroadcastToAllApps(int32 code); 80 void BroadcastToAllWindows(int32 code); 81 82 int32 GetAllWindowTargets(DelayedMessage& message); 83 int32 GetAllAppTargets(DelayedMessage& message); 84 85 filter_result KeyEvent(uint32 what, int32 key, 86 int32 modifiers); 87 // Locking 88 bool LockSingleWindow() 89 { return fWindowLock.ReadLock(); } 90 void UnlockSingleWindow() 91 { fWindowLock.ReadUnlock(); } 92 93 bool LockAllWindows() 94 { return fWindowLock.WriteLock(); } 95 void UnlockAllWindows() 96 { fWindowLock.WriteUnlock(); } 97 98 const MultiLocker& WindowLocker() { return fWindowLock; } 99 100 // Mouse and cursor methods 101 102 void SetCursor(ServerCursor* cursor); 103 ServerCursorReference Cursor() const; 104 void SetManagementCursor(ServerCursor* newCursor); 105 106 void SetLastMouseState(const BPoint& position, 107 int32 buttons, Window* windowUnderMouse); 108 // for use by the mouse filter only 109 // both mouse position calls require 110 // the Desktop object to be locked 111 // already 112 void GetLastMouseState(BPoint* position, 113 int32* buttons) const; 114 // for use by ServerWindow 115 116 CursorManager& GetCursorManager() { return fCursorManager; } 117 118 // Screen and drawing related methods 119 120 status_t SetScreenMode(int32 workspace, int32 id, 121 const display_mode& mode, bool makeDefault); 122 status_t GetScreenMode(int32 workspace, int32 id, 123 display_mode& mode); 124 status_t GetScreenFrame(int32 workspace, int32 id, 125 BRect& frame); 126 void RevertScreenModes(uint32 workspaces); 127 128 MultiLocker& ScreenLocker() { return fScreenLock; } 129 130 status_t LockDirectScreen(team_id team); 131 status_t UnlockDirectScreen(team_id team); 132 133 const ::VirtualScreen& VirtualScreen() const 134 { return fVirtualScreen; } 135 DrawingEngine* GetDrawingEngine() const 136 { return fVirtualScreen.DrawingEngine(); } 137 ::HWInterface* HWInterface() const 138 { return fVirtualScreen.HWInterface(); } 139 140 void RebuildAndRedrawAfterWindowChange( 141 Window* window, BRegion& dirty); 142 // the window lock must be held when calling 143 // this function 144 145 // ScreenOwner implementation 146 virtual void ScreenRemoved(Screen* screen) {} 147 virtual void ScreenAdded(Screen* screen) {} 148 virtual bool ReleaseScreen(Screen* screen) { return false; } 149 150 // Workspace methods 151 152 void SetWorkspaceAsync(int32 index, 153 bool moveFocusWindow = false); 154 void SetWorkspace(int32 index, 155 bool moveFocusWindow = false); 156 int32 CurrentWorkspace() 157 { return fCurrentWorkspace; } 158 Workspace::Private& WorkspaceAt(int32 index) 159 { return fWorkspaces[index]; } 160 status_t SetWorkspacesLayout(int32 columns, int32 rows); 161 BRect WorkspaceFrame(int32 index) const; 162 163 void StoreWorkspaceConfiguration(int32 index); 164 165 void AddWorkspacesView(WorkspacesView* view); 166 void RemoveWorkspacesView(WorkspacesView* view); 167 168 // Window methods 169 170 void SelectWindow(Window* window); 171 void ActivateWindow(Window* window); 172 void SendWindowBehind(Window* window, 173 Window* behindOf = NULL, 174 bool sendStack = true); 175 176 void ShowWindow(Window* window); 177 void HideWindow(Window* window, 178 bool fromMinimize = false); 179 void MinimizeWindow(Window* window, bool minimize); 180 181 void MoveWindowBy(Window* window, float x, float y, 182 int32 workspace = -1); 183 void ResizeWindowBy(Window* window, float x, 184 float y); 185 bool SetWindowTabLocation(Window* window, 186 float location, bool isShifting); 187 bool SetWindowDecoratorSettings(Window* window, 188 const BMessage& settings); 189 190 void SetWindowWorkspaces(Window* window, 191 uint32 workspaces); 192 193 void AddWindow(Window* window); 194 void RemoveWindow(Window* window); 195 196 bool AddWindowToSubset(Window* subset, 197 Window* window); 198 void RemoveWindowFromSubset(Window* subset, 199 Window* window); 200 201 void FontsChanged(Window* window); 202 void ColorUpdated(Window* window, color_which which, 203 rgb_color color); 204 205 void SetWindowLook(Window* window, window_look look); 206 void SetWindowFeel(Window* window, window_feel feel); 207 void SetWindowFlags(Window* window, uint32 flags); 208 void SetWindowTitle(Window* window, 209 const char* title); 210 211 Window* FocusWindow() const { return fFocus; } 212 Window* FrontWindow() const { return fFront; } 213 Window* BackWindow() const { return fBack; } 214 215 Window* WindowAt(BPoint where); 216 217 Window* MouseEventWindow() const 218 { return fMouseEventWindow; } 219 void SetMouseEventWindow(Window* window); 220 221 void SetViewUnderMouse(const Window* window, 222 int32 viewToken); 223 int32 ViewUnderMouse(const Window* window); 224 225 EventTarget* KeyboardEventTarget(); 226 227 void SetFocusWindow(Window* window = NULL); 228 void SetFocusLocked(const Window* window); 229 230 Window* FindWindowByClientToken(int32 token, 231 team_id teamID); 232 EventTarget* FindTarget(BMessenger& messenger); 233 234 void MarkDirty(BRegion& region); 235 void Redraw(); 236 void RedrawBackground(); 237 238 bool ReloadDecor(DecorAddOn* oldDecor); 239 240 BRegion& BackgroundRegion() 241 { return fBackgroundRegion; } 242 243 void MinimizeApplication(team_id team); 244 void BringApplicationToFront(team_id team); 245 void WindowAction(int32 windowToken, int32 action); 246 247 void WriteWindowList(team_id team, 248 BPrivate::LinkSender& sender); 249 void WriteWindowInfo(int32 serverToken, 250 BPrivate::LinkSender& sender); 251 void WriteApplicationOrder(int32 workspace, 252 BPrivate::LinkSender& sender); 253 void WriteWindowOrder(int32 workspace, 254 BPrivate::LinkSender& sender); 255 256 //! The window lock must be held when accessing a window list! 257 WindowList& CurrentWindows(); 258 WindowList& AllWindows(); 259 260 Window* WindowForClientLooperPort(port_id port); 261 262 StackAndTile* GetStackAndTile() { return &fStackAndTile; } 263 private: 264 WindowList& _Windows(int32 index); 265 266 void _FlushPendingColors(); 267 268 void _LaunchInputServer(); 269 void _GetLooperName(char* name, size_t size); 270 void _PrepareQuit(); 271 void _DispatchMessage(int32 code, 272 BPrivate::LinkReceiver &link); 273 274 void _UpdateFloating(int32 previousWorkspace = -1, 275 int32 nextWorkspace = -1, 276 Window* mouseEventWindow = NULL); 277 void _UpdateBack(); 278 void _UpdateFront(bool updateFloating = true); 279 void _UpdateFronts(bool updateFloating = true); 280 bool _WindowHasModal(Window* window) const; 281 bool _WindowCanHaveFocus(Window* window) const; 282 283 void _WindowChanged(Window* window); 284 void _WindowRemoved(Window* window); 285 286 void _ShowWindow(Window* window, 287 bool affectsOtherWindows = true); 288 void _HideWindow(Window* window); 289 290 void _UpdateSubsetWorkspaces(Window* window, 291 int32 previousIndex = -1, 292 int32 newIndex = -1); 293 void _ChangeWindowWorkspaces(Window* window, 294 uint32 oldWorkspaces, uint32 newWorkspaces); 295 void _BringWindowsToFront(WindowList& windows, 296 int32 list, bool wereVisible); 297 Window* _LastFocusSubsetWindow(Window* window); 298 bool _CheckSendFakeMouseMoved( 299 const Window* lastWindowUnderMouse); 300 void _SendFakeMouseMoved(Window* window = NULL); 301 302 Screen* _DetermineScreenFor(BRect frame); 303 void _RebuildClippingForAllWindows( 304 BRegion& stillAvailableOnScreen); 305 void _TriggerWindowRedrawing( 306 BRegion& newDirtyRegion); 307 void _SetBackground(BRegion& background); 308 309 status_t _ActivateApp(team_id team); 310 311 void _SuspendDirectFrameBufferAccess(); 312 void _ResumeDirectFrameBufferAccess(); 313 314 void _ScreenChanged(Screen* screen); 315 void _SetCurrentWorkspaceConfiguration(); 316 void _SetWorkspace(int32 index, 317 bool moveFocusWindow = false); 318 319 private: 320 friend class DesktopSettings; 321 friend class LockedDesktopSettings; 322 323 uid_t fUserID; 324 char* fTargetScreen; 325 ::VirtualScreen fVirtualScreen; 326 DesktopSettingsPrivate* fSettings; 327 port_id fMessagePort; 328 ::EventDispatcher fEventDispatcher; 329 area_id fSharedReadOnlyArea; 330 server_read_only_memory* fServerReadOnlyMemory; 331 332 BLocker fApplicationsLock; 333 BObjectList<ServerApp> fApplications; 334 335 sem_id fShutdownSemaphore; 336 int32 fShutdownCount; 337 338 ::Workspace::Private fWorkspaces[kMaxWorkspaces]; 339 MultiLocker fScreenLock; 340 BLocker fDirectScreenLock; 341 team_id fDirectScreenTeam; 342 int32 fCurrentWorkspace; 343 int32 fPreviousWorkspace; 344 345 WindowList fAllWindows; 346 WindowList fSubsetWindows; 347 WindowList fFocusList; 348 Window* fLastWorkspaceFocus[kMaxWorkspaces]; 349 350 BObjectList<WorkspacesView> fWorkspacesViews; 351 BLocker fWorkspacesLock; 352 353 CursorManager fCursorManager; 354 ServerCursorReference fCursor; 355 ServerCursorReference fManagementCursor; 356 357 MultiLocker fWindowLock; 358 359 BRegion fBackgroundRegion; 360 BRegion fScreenRegion; 361 362 Window* fMouseEventWindow; 363 const Window* fWindowUnderMouse; 364 const Window* fLockedFocusWindow; 365 int32 fViewUnderMouse; 366 BPoint fLastMousePosition; 367 int32 fLastMouseButtons; 368 369 Window* fFocus; 370 Window* fFront; 371 Window* fBack; 372 373 StackAndTile fStackAndTile; 374 375 BMessage fPendingColors; 376 }; 377 378 #endif // DESKTOP_H 379