1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc.
22 // System.Windows.Forms.XEventQueue
25 // Jackson Harper (jackson@ximian.com)
26 // Peter Dennis Bartok (pbartok@novell.com)
30 using System
.Threading
;
31 using System
.Collections
;
33 namespace System
.Windows
.Forms
{
35 internal class XEventQueue
{
37 private XQueue xqueue
;
38 private XQueue lqueue
; // Events inserted from threads other then the main X thread
39 private PaintQueue paint
; // Paint-only queue
40 internal ArrayList timer_list
;
41 private Thread thread
;
42 private bool quit_posted
;
43 private bool dispatch_idle
;
45 private static readonly int InitialXEventSize
= 100;
46 private static readonly int InitialLXEventSize
= 10;
47 private static readonly int InitialPaintSize
= 50;
49 public XEventQueue (Thread thread
) {
50 xqueue
= new XQueue (InitialXEventSize
);
51 lqueue
= new XQueue (InitialLXEventSize
);
52 paint
= new PaintQueue(InitialPaintSize
);
53 timer_list
= new ArrayList ();
55 this.quit_posted
= false;
56 this.dispatch_idle
= true;
62 return xqueue
.Count
+ lqueue
.Count
;
67 public PaintQueue Paint
{
73 public Thread Thread
{
79 public void Enqueue (XEvent xevent
)
81 if (Thread
.CurrentThread
!= thread
) {
82 Console
.WriteLine ("Hwnd.Queue.Enqueue called from a different thread without locking.");
83 Console
.WriteLine (Environment
.StackTrace
);
86 xqueue
.Enqueue (xevent
);
89 public void EnqueueLocked (XEvent xevent
)
92 lqueue
.Enqueue (xevent
);
96 public XEvent
Dequeue ()
98 if (Thread
.CurrentThread
!= thread
) {
99 Console
.WriteLine ("Hwnd.Queue.Dequeue called from a different thread without locking.");
100 Console
.WriteLine (Environment
.StackTrace
);
103 if (xqueue
.Count
== 0) {
105 return lqueue
.Dequeue ();
108 return xqueue
.Dequeue ();
113 if (Thread
.CurrentThread
!= thread
) {
114 Console
.WriteLine ("Hwnd.Queue.Peek called from a different thread without locking.");
115 Console
.WriteLine (Environment
.StackTrace
);
118 if (xqueue
.Count
== 0) {
120 return lqueue
.Peek ();
123 return xqueue
.Peek();
126 public bool DispatchIdle
{
128 return dispatch_idle
;
131 dispatch_idle
= value;
135 public bool PostQuitState
{
145 public class PaintQueue
{
147 private ArrayList hwnds
;
148 private XEvent xevent
;
150 public PaintQueue (int size
) {
151 hwnds
= new ArrayList(size
);
152 xevent
= new XEvent();
153 xevent
.AnyEvent
.type
= XEventName
.Expose
;
157 get { return hwnds.Count; }
160 public void Enqueue (Hwnd hwnd
) {
164 public void Remove(Hwnd hwnd
) {
165 if (!hwnd
.expose_pending
&& !hwnd
.nc_expose_pending
) {
170 public XEvent
Dequeue () {
174 if (hwnds
.Count
== 0) {
175 xevent
.ExposeEvent
.window
= IntPtr
.Zero
;
179 next
= hwnds
.GetEnumerator();
181 hwnd
= (Hwnd
)next
.Current
;
183 // We only remove the event from the queue if we have one expose left since
184 // a single 'entry in our queue may be for both NC and Client exposed
185 if ( !(hwnd
.nc_expose_pending
&& hwnd
.expose_pending
)) {
188 if (hwnd
.expose_pending
) {
189 xevent
.ExposeEvent
.window
= hwnd
.client_window
;
191 xevent
.ExposeEvent
.x
= hwnd
.invalid
.X
;
192 xevent
.ExposeEvent
.y
= hwnd
.invalid
.Y
;
193 xevent
.ExposeEvent
.width
= hwnd
.invalid
.Width
;
194 xevent
.ExposeEvent
.height
= hwnd
.invalid
.Height
;
198 xevent
.ExposeEvent
.window
= hwnd
.whole_window
;
199 xevent
.ExposeEvent
.x
= hwnd
.nc_invalid
.X
;
200 xevent
.ExposeEvent
.y
= hwnd
.nc_invalid
.Y
;
201 xevent
.ExposeEvent
.width
= hwnd
.nc_invalid
.Width
;
202 xevent
.ExposeEvent
.height
= hwnd
.nc_invalid
.Height
;
208 private class XQueue
{
210 private XEvent
[] xevents
;
215 public XQueue (int size
)
217 xevents
= new XEvent
[size
];
224 public void Enqueue (XEvent xevent
)
226 if (size
== xevents
.Length
)
229 xevents
[tail
] = xevent
;
230 tail
= (tail
+ 1) % xevents
.Length
;
234 public XEvent
Dequeue ()
237 throw new Exception ("Attempt to dequeue empty queue.");
238 XEvent res
= xevents
[head
];
239 head
= (head
+ 1) % xevents
.Length
;
244 public XEvent
Peek() {
246 throw new Exception ("Attempt to peek at empty queue");
248 return xevents
[head
];
253 int newcap
= (xevents
.Length
* 2);
254 XEvent
[] na
= new XEvent
[newcap
];
255 xevents
.CopyTo (na
, 0);