1/*
2 * Copyright 2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _TIMED_EVENT_QUEUE_H
6#define _TIMED_EVENT_QUEUE_H
7
8#include <MediaDefs.h>
9
10struct _event_queue_imp;
11
12
13struct media_timed_event {
14								media_timed_event();
15								media_timed_event(bigtime_t inTime,
16									int32 inType);
17								media_timed_event(bigtime_t inTime,
18									int32 inType, void*inPointer,
19									uint32 inCleanup);
20								media_timed_event(
21									bigtime_t inTime, int32 inType,
22									void* inPointer, uint32 inCleanup,
23									int32 inData, int64 inBigdata,
24									const char* inUserData, size_t dataSize = 0);
25
26								media_timed_event(
27									const media_timed_event& other);
28
29								~media_timed_event();
30
31			// TODO: Should this not return "media_timed_event&" ?!
32			void				operator=(const media_timed_event& other);
33
34			bigtime_t			event_time;
35			int32				type;
36			void*				pointer;
37			uint32				cleanup;
38			int32				data;
39			int64				bigdata;
40			char				user_data[64];
41
42			uint32				_reserved_media_timed_event_[8];
43};
44
45
46bool operator==(const media_timed_event& a, const media_timed_event& b);
47bool operator!=(const media_timed_event& a, const media_timed_event& b);
48bool operator<(const media_timed_event& a, const media_timed_event& b);
49bool operator>(const media_timed_event& a, const media_timed_event&b);
50
51
52/*!	A priority queue for holding media_timed_events. Sorts by increasing time,
53	so events near the front of the queue are to occur earlier.
54*/
55class BTimedEventQueue {
56public:
57
58			enum event_type {
59				B_NO_EVENT = -1,	// Pushing this type will always fail.
60				B_ANY_EVENT = 0,	// Pushing this type will always fail.
61				B_START,
62				B_STOP,
63				B_SEEK,
64				B_WARP,
65				B_TIMER,
66				B_HANDLE_BUFFER,
67				B_DATA_STATUS,
68				B_HARDWARE,
69				B_PARAMETER,
70
71				// User defined events are greater than this value.
72				B_USER_EVENT = 0x4000
73			};
74
75			enum cleanup_flag {
76				B_NO_CLEANUP = 0,
77				B_RECYCLE_BUFFER,	// Specifies to recycle buffers handled by
78									// BTimedEventQueue.
79				B_EXPIRE_TIMER,		// Specifies to call TimerExpired() on the
80									// event->data.
81				B_USER_CLEANUP = 0x4000
82			};
83
84			enum time_direction {
85				B_ALWAYS = -1,
86				B_BEFORE_TIME = 0,
87				B_AT_TIME,
88				B_AFTER_TIME
89			};
90
91
92			void*				operator new(size_t size);
93			void				operator delete(void* ptr, size_t size);
94
95								BTimedEventQueue();
96	virtual						~BTimedEventQueue();
97
98			status_t			AddEvent(const media_timed_event& event);
99			status_t			RemoveEvent(const media_timed_event* event);
100			status_t  			RemoveFirstEvent(
101									media_timed_event* _event = NULL);
102
103			bool				HasEvents() const;
104			int32				EventCount() const;
105
106
107			const media_timed_event* FirstEvent() const;
108			bigtime_t			FirstEventTime() const;
109			const media_timed_event* LastEvent() const;
110			bigtime_t			LastEventTime() const;
111
112			const media_timed_event* FindFirstMatch(bigtime_t eventTime,
113								time_direction direction,
114								bool inclusive = true,
115								int32 eventType = B_ANY_EVENT);
116
117
118	// Queue manipulation
119	// Call DoForEach to perform a function on each event in the queue.
120	// Return an appropriate status defining an action to take for that event.
121	// DoForEach is an atomic operation ensuring the consistency of the queue
122	// during the call.
123			enum queue_action {
124				B_DONE = -1,
125				B_NO_ACTION = 0,
126				B_REMOVE_EVENT,
127				B_RESORT_QUEUE
128			};
129			typedef queue_action (*for_each_hook)(media_timed_event* event,
130				void* context);
131
132			status_t			DoForEach(for_each_hook hook, void* context,
133									bigtime_t eventTime = 0,
134									time_direction direction = B_ALWAYS,
135									bool inclusive = true,
136									int32 eventType = B_ANY_EVENT);
137
138
139	// Flushing events
140	// The cleanup hook is called when events are flushed from the queue and
141	// the cleanup is not B_NO_CLEANUP or B_RECYCLE_BUFFER.
142			typedef void (*cleanup_hook)(const media_timed_event* event,
143				void* context);
144			void				SetCleanupHook(cleanup_hook hook,
145									void* context);
146			status_t			FlushEvents(bigtime_t eventTime,
147									time_direction direction,
148									bool inclusive = true,
149									int32 eventType = B_ANY_EVENT);
150
151private:
152	// FBC padding and forbidden methods
153								BTimedEventQueue(
154									const BTimedEventQueue& other);
155			BTimedEventQueue&	operator=(const BTimedEventQueue& other);
156
157	virtual	status_t			_Reserved_BTimedEventQueue_0(void*, ...);
158	virtual	status_t			_Reserved_BTimedEventQueue_1(void*, ...);
159	virtual	status_t			_Reserved_BTimedEventQueue_2(void*, ...);
160	virtual	status_t			_Reserved_BTimedEventQueue_3(void*, ...);
161	virtual	status_t			_Reserved_BTimedEventQueue_4(void*, ...);
162	virtual	status_t			_Reserved_BTimedEventQueue_5(void*, ...);
163	virtual	status_t			_Reserved_BTimedEventQueue_6(void*, ...);
164	virtual	status_t			_Reserved_BTimedEventQueue_7(void*, ...);
165	virtual	status_t			_Reserved_BTimedEventQueue_8(void*, ...);
166	virtual	status_t			_Reserved_BTimedEventQueue_9(void*, ...);
167	virtual	status_t			_Reserved_BTimedEventQueue_10(void*, ...);
168	virtual	status_t			_Reserved_BTimedEventQueue_11(void*, ...);
169	virtual	status_t			_Reserved_BTimedEventQueue_12(void*, ...);
170	virtual	status_t			_Reserved_BTimedEventQueue_13(void*, ...);
171	virtual	status_t			_Reserved_BTimedEventQueue_14(void*, ...);
172	virtual	status_t			_Reserved_BTimedEventQueue_15(void*, ...);
173	virtual	status_t			_Reserved_BTimedEventQueue_16(void*, ...);
174	virtual	status_t			_Reserved_BTimedEventQueue_17(void*, ...);
175	virtual	status_t			_Reserved_BTimedEventQueue_18(void*, ...);
176	virtual	status_t			_Reserved_BTimedEventQueue_19(void*, ...);
177	virtual	status_t			_Reserved_BTimedEventQueue_20(void*, ...);
178	virtual	status_t			_Reserved_BTimedEventQueue_21(void*, ...);
179	virtual	status_t			_Reserved_BTimedEventQueue_22(void*, ...);
180	virtual	status_t			_Reserved_BTimedEventQueue_23(void*, ...);
181
182private:
183			_event_queue_imp* 	fImp;
184
185			uint32 				_reserved_timed_event_queue_[6];
186};
187
188#endif
189