1/*
2 * Copyright 2001-2013, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		DarkWyrm <bpmagic@columbus.rr.com>
7 *		Adrian Oanca <adioanca@cotty.iren.ro>
8 *		Stephan A��mus <superstippi@gmx.de>
9 *		Stefano Ceccherini (burton666@libero.it)
10 *		Axel D��rfler, axeld@pinc-software.de
11 */
12#ifndef SERVER_APP_H
13#define SERVER_APP_H
14
15
16#include "AppFontManager.h"
17#include "ClientMemoryAllocator.h"
18#include "MessageLooper.h"
19#include "ServerFont.h"
20
21#include <ObjectList.h>
22#include <TokenSpace.h>
23
24#include <Messenger.h>
25#include <String.h>
26
27
28class AreaPool;
29class BMessage;
30class BList;
31class Desktop;
32class DrawingEngine;
33class ServerPicture;
34class ServerCursor;
35class ServerBitmap;
36class ServerWindow;
37
38namespace BPrivate {
39	class PortLink;
40};
41
42
43class ServerApp : public MessageLooper {
44public:
45								ServerApp(Desktop* desktop,
46									port_id clientAppPort,
47									port_id clientLooperPort,
48									team_id clientTeamID, int32 handlerID,
49									const char* signature);
50	virtual						~ServerApp();
51
52			status_t			InitCheck();
53
54	virtual	void				Quit();
55			void				Quit(sem_id shutdownSemaphore);
56
57	virtual	port_id				MessagePort() const { return fMessagePort; }
58
59	/*!
60		\brief Determines whether the application is the active one
61		\return true if active, false if not.
62	*/
63			bool				IsActive() const { return fIsActive; }
64			void				Activate(bool value);
65
66			void				SendMessageToClient(BMessage* message) const;
67
68			void				SetCurrentCursor(ServerCursor* cursor);
69			ServerCursor*		CurrentCursor() const;
70
71			team_id				ClientTeam() const { return fClientTeam; }
72
73			const char*			Signature() const
74									{ return fSignature.String(); }
75			const char*			SignatureLeaf() const
76									{ return fSignature.String() + 12; }
77
78			bool				AddWindow(ServerWindow* window);
79			void				RemoveWindow(ServerWindow* window);
80			bool				InWorkspace(int32 index) const;
81			uint32				Workspaces() const;
82			int32				InitialWorkspace() const
83									{ return fInitialWorkspace; }
84
85			ServerBitmap*		GetBitmap(int32 token) const;
86
87			ServerPicture*		CreatePicture(
88									const ServerPicture* original = NULL);
89			ServerPicture*		GetPicture(int32 token) const;
90			bool				AddPicture(ServerPicture* picture);
91			void				RemovePicture(ServerPicture* picture);
92
93			Desktop*			GetDesktop() const { return fDesktop; }
94
95			const ServerFont&	PlainFont() const { return fPlainFont; }
96
97			BPrivate::BTokenSpace& ViewTokens() { return fViewTokens; }
98
99			void				NotifyDeleteClientArea(area_id serverArea);
100			AppFontManager*		FontManager() { return fAppFontManager; }
101
102private:
103	virtual	void				_GetLooperName(char* name, size_t size);
104	virtual	void				_DispatchMessage(int32 code,
105									BPrivate::LinkReceiver& link);
106	virtual	void				_MessageLooper();
107			status_t			_CreateWindow(int32 code,
108									BPrivate::LinkReceiver& link,
109									port_id& clientReplyPort);
110
111			bool				_HasWindowUnderMouse();
112
113			bool				_AddBitmap(ServerBitmap* bitmap);
114			void				_DeleteBitmap(ServerBitmap* bitmap);
115			ServerBitmap*		_FindBitmap(int32 token) const;
116
117			ServerPicture*		_FindPicture(int32 token) const;
118
119private:
120	typedef std::map<int32, BReference<ServerBitmap> > BitmapMap;
121	typedef std::map<int32, BReference<ServerPicture> > PictureMap;
122
123			port_id				fMessagePort;
124			port_id				fClientReplyPort;
125									// our BApplication's event port
126
127			BMessenger			fHandlerMessenger;
128			port_id				fClientLooperPort;
129			int32				fClientToken;
130									// To send a BMessage to the client
131									// (port + token)
132
133			Desktop*			fDesktop;
134			BString				fSignature;
135			team_id				fClientTeam;
136
137			ServerFont			fPlainFont;
138			ServerFont			fBoldFont;
139			ServerFont			fFixedFont;
140
141	mutable	BLocker				fWindowListLock;
142			BObjectList<ServerWindow> fWindowList;
143			BPrivate::BTokenSpace fViewTokens;
144
145			int32				fInitialWorkspace;
146			uint32				fTemporaryDisplayModeChange;
147
148			// NOTE: Bitmaps and Pictures are stored globally, but ServerApps
149			// remember which ones they own so that they can destroy them when
150			// they quit.
151	mutable	BLocker				fMapLocker;
152			BitmapMap			fBitmapMap;
153			PictureMap			fPictureMap;
154
155			BReference<ServerCursor>
156								fAppCursor;
157			BReference<ServerCursor>
158								fViewCursor;
159			int32				fCursorHideLevel;
160									// 0 = cursor visible
161
162			bool				fIsActive;
163
164			BReference<ClientMemoryAllocator> fMemoryAllocator;
165
166			AppFontManager*		fAppFontManager;
167};
168
169
170#endif	// SERVER_APP_H
171