File: X11ThreadQueue.cs

package info (click to toggle)
mono 1.9.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 173,128 kB
  • ctags: 310,695
  • sloc: cs: 1,855,117; ansic: 276,741; sh: 21,695; xml: 15,360; makefile: 6,139; perl: 1,508; asm: 689; yacc: 288; sql: 81
file content (502 lines) | stat: -rw-r--r-- 11,299 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004-2006 Novell, Inc.
//
// Authors:
//  Jackson Harper (jackson@ximian.com)
//  Peter Dennis Bartok (pbartok@novell.com)
//  Chris Toshok (toshok@ximian.com)
//

using System;
using System.Threading;
using System.Collections;

namespace System.Windows.Forms.X11Internal {

	internal class X11ThreadQueue {

		XEventQueue xqueue;
		PaintQueue paint_queue;
		ConfigureQueue configure_queue;
		ArrayList timer_list;
		Thread thread;
		bool quit_posted;
		bool dispatch_idle;
		bool need_dispatch_idle = true;
		object lockobj = new object ();

		static readonly int InitialXEventQueueSize = 128;
		static readonly int InitialHwndQueueSize = 50;

		public X11ThreadQueue (Thread thread)
		{
			xqueue = new XEventQueue (InitialXEventQueueSize);
			paint_queue = new PaintQueue (InitialHwndQueueSize);
			configure_queue = new ConfigureQueue (InitialHwndQueueSize);
			timer_list = new ArrayList ();
			this.thread = thread;
			this.quit_posted = false;
			this.dispatch_idle = true;
		}

		public int CountUnlocked {
			get { return xqueue.Count + paint_queue.Count; }
		}

		public Thread Thread {
			get { return thread; }
		}

		public void EnqueueUnlocked (XEvent xevent)
		{
			switch (xevent.type) {
			case XEventName.KeyPress:
			case XEventName.KeyRelease:
			case XEventName.ButtonPress:
			case XEventName.ButtonRelease:
				NeedDispatchIdle = true;
				break;
			case XEventName.MotionNotify:
				if (xqueue.Count > 0) {
					XEvent peek = xqueue.Peek ();
					if (peek.AnyEvent.type == XEventName.MotionNotify)
						return; // we've already got a pending motion notify.
				}

				// otherwise fall through and enqueue
				// the event.
				break;
			}

			xqueue.Enqueue (xevent);
			// wake up any thread blocking in DequeueUnlocked
			Monitor.PulseAll (lockobj);
		}

		public void Enqueue (XEvent xevent)
		{
			lock (lockobj) {
				EnqueueUnlocked (xevent);
			}
		}

		public bool Dequeue (out XEvent xevent)
		{
		StartOver:
			bool got_xevent = false;

			lock (lockobj) {
				if (xqueue.Count > 0) {
					got_xevent = true;
					xevent = xqueue.Dequeue ();
				}
				else
					xevent = new XEvent (); /* not strictly needed, but mcs complains */
			}

			if (got_xevent) {
				if (xevent.AnyEvent.type == XEventName.Expose) {
#if spew
					Console.Write ("E");
					Console.Out.Flush ();
#endif
					X11Hwnd hwnd = (X11Hwnd)Hwnd.GetObjectFromWindow (xevent.AnyEvent.window);
					hwnd.AddExpose (xevent.AnyEvent.window == hwnd.ClientWindow,
							xevent.ExposeEvent.x, xevent.ExposeEvent.y,
							xevent.ExposeEvent.width, xevent.ExposeEvent.height);
					goto StartOver;
				}
				else if (xevent.AnyEvent.type == XEventName.ConfigureNotify) {
#if spew
					Console.Write ("C");
					Console.Out.Flush ();
#endif
					X11Hwnd hwnd = (X11Hwnd)Hwnd.GetObjectFromWindow (xevent.AnyEvent.window);
					hwnd.AddConfigureNotify (xevent);
					goto StartOver;
				}
				else {
#if spew
					Console.Write ("X");
					Console.Out.Flush ();
#endif
					/* it was an event we can deal with directly, return it */
					return true;
				}
			}
			else {
				if (paint_queue.Count > 0) {
					xevent = paint_queue.Dequeue ();
#if spew
					Console.Write ("e");
					Console.Out.Flush ();
#endif
					return true;
				}
				else if (configure_queue.Count > 0) {
					xevent = configure_queue.Dequeue ();
#if spew
					Console.Write ("c");
					Console.Out.Flush ();
#endif
					return true;
				}
			}

			if (dispatch_idle && need_dispatch_idle) {
				OnIdle (EventArgs.Empty);
				need_dispatch_idle = false;
			}

			lock (lockobj) {
				if (CountUnlocked > 0)
					goto StartOver;

				if (Monitor.Wait (lockobj, NextTimeout (), true)) {
					// the lock was reaquired before the
					// timeout.  meaning an event was
					// enqueued by X11Display.XEventThread.
					goto StartOver;
				}
				else {
					CheckTimers ();
					return false;
				}
			}
		}

		public void RemovePaint (Hwnd hwnd)
		{
			paint_queue.Remove (hwnd);
		}

		public void AddPaint (Hwnd hwnd)
		{
			paint_queue.Enqueue (hwnd);
		}

		public void AddConfigure (Hwnd hwnd)
		{
			configure_queue.Enqueue (hwnd);
		}

		public ConfigureQueue Configure {
			get { return configure_queue; }
		}

		public PaintQueue Paint {
			get { return paint_queue; }
		}

		public void Lock ()
		{
			Monitor.Enter (lockobj);
		}

		public void Unlock ()
		{
			Monitor.Exit (lockobj);
		}

		private int NextTimeout ()
		{
			int timeout = Int32.MaxValue; 
			DateTime now = DateTime.UtcNow;

			foreach (Timer timer in timer_list) {
				int next = (int) (timer.Expires - now).TotalMilliseconds;
				if (next < 0)
					return 0; // Have a timer that has already expired

				if (next < timeout)
					timeout = next;
			}

			if (timeout < Timer.Minimum) {
				timeout = Timer.Minimum;
			}

			if (timeout == Int32.MaxValue)
				timeout = Timeout.Infinite;

			return timeout;
		}

		public void CheckTimers ()
		{
			int count;
			DateTime now = DateTime.UtcNow;

			count = timer_list.Count;

			if (count == 0)
				return;

			for (int i = 0; i < timer_list.Count; i++) {
				Timer timer;

				timer = (Timer) timer_list [i];

				if (timer.Enabled && timer.Expires <= now) {
					timer.Update (now);
					timer.FireTick ();
				}
			}
		}

		public void SetTimer (Timer timer)
		{
			lock (lockobj) {
				timer_list.Add (timer);

				// we need to wake up any thread waiting in DequeueUnlocked,
				// since it might need to wait for a different amount of time.
				Monitor.PulseAll (lockobj);
			}

		}

		public void KillTimer (Timer timer)
		{
			lock (lockobj) {
				timer_list.Remove (timer);

				// we need to wake up any thread waiting in DequeueUnlocked,
				// since it might need to wait for a different amount of time.
				Monitor.PulseAll (lockobj);
			}
		}

		public event EventHandler Idle;
		public void OnIdle (EventArgs e)
		{
			if (Idle != null)
				Idle (thread, e);
		}

		public bool NeedDispatchIdle {
			get { return need_dispatch_idle; }
			set { need_dispatch_idle = value; }
		}

		public bool DispatchIdle {
			get { return dispatch_idle; }
			set { dispatch_idle = value; }
		}

		public bool PostQuitState {
			get { return quit_posted; }
			set { quit_posted = value; }
		}

		public abstract class HwndEventQueue {
			protected ArrayList hwnds;
#if DebugHwndEventQueue
			protected ArrayList stacks;
#endif
			public HwndEventQueue (int size)
			{
				hwnds = new ArrayList (size);
#if DebugHwndEventQueue
				stacks = new ArrayList (size);
#endif
			}

			public int Count {
				get { return hwnds.Count; }
			}

			public void Enqueue (Hwnd hwnd)
			{
				if (hwnds.Contains (hwnd)) {
#if DebugHwndEventQueue
					Console.WriteLine ("hwnds can only appear in the queue once.");
					Console.WriteLine (Environment.StackTrace);
					Console.WriteLine ("originally added here:");
					Console.WriteLine (stacks[hwnds.IndexOf (hwnd)]);
#endif

					return;
				}
				hwnds.Add(hwnd);
#if DebugHwndEventQueue
				stacks.Add(Environment.StackTrace);
#endif
			}

			public void Remove(Hwnd hwnd)
			{
#if DebugHwndEventQueue
				int index = hwnds.IndexOf(hwnd);
				if (index != -1)
					stacks.RemoveAt(index);
#endif
				hwnds.Remove(hwnd);
			}

			protected abstract XEvent Peek ();

			public virtual XEvent Dequeue ()
			{
				if (hwnds.Count == 0)
					throw new Exception ("Attempt to dequeue empty queue.");

				return Peek ();
			}
		}


		public class ConfigureQueue : HwndEventQueue
		{
			public ConfigureQueue (int size) : base (size)
			{
			}

			protected override XEvent Peek ()
			{
				X11Hwnd hwnd = (X11Hwnd)hwnds[0];

				XEvent xevent = new XEvent ();
				xevent.AnyEvent.type = XEventName.ConfigureNotify;

				xevent.ConfigureEvent.window = hwnd.ClientWindow;
				xevent.ConfigureEvent.x = hwnd.X;
				xevent.ConfigureEvent.y = hwnd.Y;
				xevent.ConfigureEvent.width = hwnd.Width;
				xevent.ConfigureEvent.height = hwnd.Height;
				
				return xevent;
			}

			public override XEvent Dequeue ()
			{
				XEvent xev = base.Dequeue ();


				hwnds.RemoveAt(0);
#if DebugHwndEventQueue
				stacks.RemoveAt(0);
#endif

				return xev;
			}
		}

		public class PaintQueue : HwndEventQueue
		{
			public PaintQueue (int size) : base (size)
			{
			}

			protected override XEvent Peek ()
			{
				X11Hwnd hwnd = (X11Hwnd)hwnds[0];

				XEvent xevent = new XEvent ();

				xevent.AnyEvent.type = XEventName.Expose;

				if (hwnd.PendingExpose) {
					xevent.ExposeEvent.window = hwnd.ClientWindow;
				} else {
					xevent.ExposeEvent.window = hwnd.WholeWindow;
					xevent.ExposeEvent.x = hwnd.nc_invalid.X;
					xevent.ExposeEvent.y = hwnd.nc_invalid.Y;
					xevent.ExposeEvent.width = hwnd.nc_invalid.Width;
					xevent.ExposeEvent.height = hwnd.nc_invalid.Height;
				}

				return xevent;
			}

			// don't override Dequeue like ConfigureQueue does.
		}

		/* a circular queue for holding X events for processing by GetMessage */
		private class XEventQueue {

			XEvent[] xevents;
			int head;
			int tail;
			int size;
			
			public XEventQueue (int initial_size)
			{
				if (initial_size % 2 != 0)
					throw new Exception ("XEventQueue must be a power of 2 size");

				xevents = new XEvent [initial_size];
			}

			public int Count {
				get { return size; }
			}

			public void Enqueue (XEvent xevent)
			{
				if (size == xevents.Length)
					Grow ();

				xevents [tail] = xevent;
				tail = (tail + 1) & (xevents.Length - 1);
				size++;
			}

			public XEvent Dequeue ()
			{
				if (size < 1)
					throw new Exception ("Attempt to dequeue empty queue.");

				XEvent res = xevents [head];
				head = (head + 1) & (xevents.Length - 1);
				size--;
				return res;
			}

			public XEvent Peek()
			{
				if (size < 1)
					throw new Exception ("Attempt to peek at empty queue.");

				return xevents[head];
			}

			private void Grow ()
			{
				int newcap = (xevents.Length * 2);
				XEvent [] na = new XEvent [newcap];

				if (head + size > xevents.Length) {
					Array.Copy (xevents, head, na, 0, xevents.Length - head);
					Array.Copy (xevents, 0, na, xevents.Length - head, head + size - xevents.Length);
				}
				else {
					Array.Copy (xevents, head, na, 0, size);
				}

				xevents = na;
				head = 0;
				tail = head + size;
			}
		}
	}
}