xref: /haiku/src/servers/app/InputManager.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2005, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 // TODO: introduce means to define event stream features (like local vs. net)
10 // TODO: introduce the possibility to identify a stream by a unique name
11 
12 
13 #include "EventStream.h"
14 #include "InputManager.h"
15 
16 #include <Autolock.h>
17 
18 
19 InputManager* gInputManager;
20 	// the global input manager will be created by the AppServer
21 
22 
23 InputManager::InputManager()
24 	: BLocker("input manager"),
25 	fFreeStreams(2, true),
26 	fUsedStreams(2, true)
27 {
28 }
29 
30 
31 InputManager::~InputManager()
32 {
33 }
34 
35 
36 bool
37 InputManager::AddStream(EventStream* stream)
38 {
39 	BAutolock _(this);
40 	return fFreeStreams.AddItem(stream);
41 }
42 
43 
44 void
45 InputManager::RemoveStream(EventStream* stream)
46 {
47 	BAutolock _(this);
48 	fFreeStreams.RemoveItem(stream);
49 }
50 
51 
52 EventStream*
53 InputManager::GetStream()
54 {
55 	BAutolock _(this);
56 
57 	EventStream* stream = NULL;
58 	do {
59 		delete stream;
60 			// this deletes the previous invalid stream
61 
62 		stream = fFreeStreams.RemoveItemAt(0);
63 	} while (stream != NULL && !stream->IsValid());
64 
65 	if (stream == NULL)
66 		return NULL;
67 
68 	fUsedStreams.AddItem(stream);
69 	return stream;
70 }
71 
72 
73 void
74 InputManager::PutStream(EventStream* stream)
75 {
76 	if (stream == NULL)
77 		return;
78 
79 	BAutolock _(this);
80 
81 	fUsedStreams.RemoveItem(stream, false);
82 	if (stream->IsValid())
83 		fFreeStreams.AddItem(stream);
84 	else
85 		delete stream;
86 }
87 
88 
89 void
90 InputManager::UpdateScreenBounds(BRect bounds)
91 {
92 	BAutolock _(this);
93 
94 	for (int32 i = fUsedStreams.CountItems(); i-- > 0;) {
95 		fUsedStreams.ItemAt(i)->UpdateScreenBounds(bounds);
96 	}
97 
98 	for (int32 i = fFreeStreams.CountItems(); i-- > 0;) {
99 		fFreeStreams.ItemAt(i)->UpdateScreenBounds(bounds);
100 	}
101 }
102 
103 
104