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 dispatch_idle
;
44 private static readonly int InitialXEventSize
= 100;
45 private static readonly int InitialLXEventSize
= 10;
46 private static readonly int InitialPaintSize
= 50;
48 public XEventQueue (Thread thread
) {
49 xqueue
= new XQueue (InitialXEventSize
);
50 lqueue
= new XQueue (InitialLXEventSize
);
51 paint
= new PaintQueue(InitialPaintSize
);
52 timer_list
= new ArrayList ();
54 this.dispatch_idle
= true;
60 return xqueue
.Count
+ lqueue
.Count
;
65 public PaintQueue Paint
{
71 public Thread Thread
{
77 public void Enqueue (XEvent xevent
)
79 if (Thread
.CurrentThread
!= thread
) {
80 Console
.WriteLine ("Hwnd.Queue.Enqueue called from a different thread without locking.");
81 Console
.WriteLine (Environment
.StackTrace
);
84 xqueue
.Enqueue (xevent
);
87 public void EnqueueLocked (XEvent xevent
)
90 lqueue
.Enqueue (xevent
);
94 public XEvent
Dequeue ()
96 if (Thread
.CurrentThread
!= thread
) {
97 Console
.WriteLine ("Hwnd.Queue.Dequeue called from a different thread without locking.");
98 Console
.WriteLine (Environment
.StackTrace
);
101 if (xqueue
.Count
== 0) {
103 return lqueue
.Dequeue ();
106 return xqueue
.Dequeue ();
111 if (Thread
.CurrentThread
!= thread
) {
112 Console
.WriteLine ("Hwnd.Queue.Peek called from a different thread without locking.");
113 Console
.WriteLine (Environment
.StackTrace
);
116 if (xqueue
.Count
== 0) {
118 return lqueue
.Peek ();
121 return xqueue
.Peek();
124 public bool DispatchIdle
{
126 return dispatch_idle
;
129 dispatch_idle
= value;
133 public class PaintQueue
{
135 private ArrayList hwnds
;
136 private XEvent xevent
;
138 public PaintQueue (int size
) {
139 hwnds
= new ArrayList(size
);
140 xevent
= new XEvent();
141 xevent
.AnyEvent
.type
= XEventName
.Expose
;
145 get { return hwnds.Count; }
148 public void Enqueue (Hwnd hwnd
) {
152 public void Remove(Hwnd hwnd
) {
153 if (!hwnd
.expose_pending
&& !hwnd
.nc_expose_pending
) {
158 public XEvent
Dequeue () {
162 if (hwnds
.Count
== 0) {
163 xevent
.ExposeEvent
.window
= IntPtr
.Zero
;
167 next
= hwnds
.GetEnumerator();
169 hwnd
= (Hwnd
)next
.Current
;
171 // We only remove the event from the queue if we have one expose left since
172 // a single 'entry in our queue may be for both NC and Client exposed
173 if ( !(hwnd
.nc_expose_pending
&& hwnd
.expose_pending
)) {
176 if (hwnd
.expose_pending
) {
177 xevent
.ExposeEvent
.window
= hwnd
.client_window
;
179 xevent
.ExposeEvent
.x
= hwnd
.invalid
.X
;
180 xevent
.ExposeEvent
.y
= hwnd
.invalid
.Y
;
181 xevent
.ExposeEvent
.width
= hwnd
.invalid
.Width
;
182 xevent
.ExposeEvent
.height
= hwnd
.invalid
.Height
;
186 xevent
.ExposeEvent
.window
= hwnd
.whole_window
;
187 xevent
.ExposeEvent
.x
= hwnd
.nc_invalid
.X
;
188 xevent
.ExposeEvent
.y
= hwnd
.nc_invalid
.Y
;
189 xevent
.ExposeEvent
.width
= hwnd
.nc_invalid
.Width
;
190 xevent
.ExposeEvent
.height
= hwnd
.nc_invalid
.Height
;
196 private class XQueue
{
198 private XEvent
[] xevents
;
203 public XQueue (int size
)
205 xevents
= new XEvent
[size
];
212 public void Enqueue (XEvent xevent
)
214 if (size
== xevents
.Length
)
217 xevents
[tail
] = xevent
;
218 tail
= (tail
+ 1) % xevents
.Length
;
222 public XEvent
Dequeue ()
225 throw new Exception ("Attempt to dequeue empty queue.");
226 XEvent res
= xevents
[head
];
227 head
= (head
+ 1) % xevents
.Length
;
232 public XEvent
Peek() {
234 throw new Exception ("Attempt to peek at empty queue");
236 return xevents
[head
];
241 int newcap
= (xevents
.Length
* 2);
242 XEvent
[] na
= new XEvent
[newcap
];
243 xevents
.CopyTo (na
, 0);