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;
152 public void Enqueue (Hwnd hwnd) {
158 public void Remove(Hwnd hwnd) {
159 if (!hwnd.expose_pending && !hwnd.nc_expose_pending) {
166 public XEvent Dequeue () {
171 if (hwnds.Count == 0) {
172 xevent.ExposeEvent.window = IntPtr.Zero;
176 next = hwnds.GetEnumerator ();
178 hwnd = (Hwnd)next.Current;
180 // We only remove the event from the queue if we have one expose left since
181 // a single 'entry in our queue may be for both NC and Client exposed
182 if (!(hwnd.nc_expose_pending && hwnd.expose_pending)) {
185 if (hwnd.expose_pending) {
186 xevent.ExposeEvent.window = hwnd.client_window;
188 xevent.ExposeEvent.x = hwnd.invalid.X;
189 xevent.ExposeEvent.y = hwnd.invalid.Y;
190 xevent.ExposeEvent.width = hwnd.invalid.Width;
191 xevent.ExposeEvent.height = hwnd.invalid.Height;
195 xevent.ExposeEvent.window = hwnd.whole_window;
196 xevent.ExposeEvent.x = hwnd.nc_invalid.X;
197 xevent.ExposeEvent.y = hwnd.nc_invalid.Y;
198 xevent.ExposeEvent.width = hwnd.nc_invalid.Width;
199 xevent.ExposeEvent.height = hwnd.nc_invalid.Height;
206 private class XQueue {
208 private XEvent [] xevents;
213 public XQueue (int size)
215 xevents = new XEvent [size];
222 public void Enqueue (XEvent xevent)
224 if (size == xevents.Length)
227 xevents [tail] = xevent;
228 tail = (tail + 1) % xevents.Length;
232 public XEvent Dequeue ()
235 throw new Exception ("Attempt to dequeue empty queue.");
236 XEvent res = xevents [head];
237 head = (head + 1) % xevents.Length;
242 public XEvent Peek() {
244 throw new Exception ("Attempt to peek at empty queue");
246 return xevents[head];
251 int newcap = (xevents.Length * 2);
252 XEvent [] na = new XEvent [newcap];
253 xevents.CopyTo (na, 0);