-
Notifications
You must be signed in to change notification settings - Fork 175
/
X11GLNative.cs
764 lines (649 loc) · 26.9 KB
/
X11GLNative.cs
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// 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.
//
#endregion
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using OpenTK.Graphics;
using OpenTK.Input;
namespace OpenTK.Platform.X11 {
/// \internal
/// <summary> Drives GameWindow on X11.
/// This class supports OpenTK, and is not intended for use by OpenTK programs. </summary>
internal sealed class X11GLNative : INativeWindow, IDisposable {
// TODO: Disable screensaver.
// TODO: What happens if we can't disable decorations through motif?
// TODO: Mouse/keyboard grabbing/wrapping.
const int _min_width = 30, _min_height = 30;
X11WindowInfo window = new X11WindowInfo();
// The Atom class from Mono might be useful to avoid calling XInternAtom by hand (somewhat error prone).
IntPtr wm_destroy;
IntPtr net_wm_state;
IntPtr net_wm_state_minimized;
IntPtr net_wm_state_fullscreen;
IntPtr net_wm_state_maximized_horizontal;
IntPtr net_wm_state_maximized_vertical;
IntPtr net_wm_icon;
IntPtr net_frame_extents;
static readonly IntPtr xa_cardinal = (IntPtr)6;
static readonly IntPtr _remove = (IntPtr)0;
static readonly IntPtr _add = (IntPtr)1;
static readonly IntPtr _toggle = (IntPtr)2;
Rectangle bounds, client_rectangle;
int borderLeft, borderRight, borderTop, borderBottom;
Icon icon;
bool has_focus;
bool visible;
// Used for event loop.
XEvent e = new XEvent();
bool disposed, exists, isExiting;
// Keyboard input
readonly byte[] ascii = new byte[16];
readonly char[] chars = new char[16];
readonly KeyPressEventArgs KPEventArgs = new KeyPressEventArgs();
KeyboardDevice keyboard = new KeyboardDevice();
MouseDevice mouse = new MouseDevice();
X11KeyMap keymap = new X11KeyMap();
int firstKeyCode, lastKeyCode; // The smallest and largest KeyCode supported by the X server.
int keysyms_per_keycode; // The number of KeySyms for each KeyCode.
IntPtr[] keysyms;
public X11GLNative(int x, int y, int width, int height, string title, GraphicsMode mode, DisplayDevice device) {
if (width <= 0)
throw new ArgumentOutOfRangeException("width", "Must be higher than zero.");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", "Must be higher than zero.");
Debug.Print("Creating X11GLNative window.");
// Open a display connection to the X server, and obtain the screen and root window.
window.Display = API.DefaultDisplay;
window.Screen = API.XDefaultScreen(window.Display); //API.DefaultScreen;
window.RootWindow = API.XRootWindow(window.Display, window.Screen); // API.RootWindow;
Debug.Print("Display: {0}, Screen {1}, Root window: {2}", window.Display, window.Screen, window.RootWindow);
RegisterAtoms(window);
XVisualInfo info = new XVisualInfo();
mode = X11GLContext.SelectGraphicsMode( mode, out info );
window.VisualInfo = info;
// Create a window on this display using the visual above
Debug.Print("Opening render window... ");
XSetWindowAttributes attributes = new XSetWindowAttributes();
attributes.background_pixel = IntPtr.Zero;
attributes.border_pixel = IntPtr.Zero;
attributes.colormap = API.XCreateColormap(window.Display, window.RootWindow, window.VisualInfo.Visual, 0/*AllocNone*/);
window.EventMask = EventMask.StructureNotifyMask /*| EventMask.SubstructureNotifyMask*/ | EventMask.ExposureMask |
EventMask.KeyReleaseMask | EventMask.KeyPressMask | EventMask.KeymapStateMask |
EventMask.PointerMotionMask | EventMask.FocusChangeMask |
EventMask.ButtonPressMask | EventMask.ButtonReleaseMask |
EventMask.EnterWindowMask | EventMask.LeaveWindowMask |
EventMask.PropertyChangeMask;
attributes.event_mask = (IntPtr)window.EventMask;
uint mask = (uint)SetWindowValuemask.ColorMap | (uint)SetWindowValuemask.EventMask |
(uint)SetWindowValuemask.BackPixel | (uint)SetWindowValuemask.BorderPixel;
window.WindowHandle = API.XCreateWindow(window.Display, window.RootWindow,
x, y, width, height, 0, window.VisualInfo.Depth/*(int)CreateWindowArgs.CopyFromParent*/,
(int)CreateWindowArgs.InputOutput, window.VisualInfo.Visual, (IntPtr)mask, ref attributes);
if (window.WindowHandle == IntPtr.Zero)
throw new ApplicationException("XCreateWindow call failed (returned 0).");
if (title != null)
API.XStoreName(window.Display, window.WindowHandle, title);
// Set the window hints
SetWindowMinMax(_min_width, _min_height, -1, -1);
XSizeHints hints = new XSizeHints();
hints.base_width = width;
hints.base_height = height;
hints.flags = (IntPtr)(XSizeHintsFlags.PSize | XSizeHintsFlags.PPosition);
API.XSetWMNormalHints(window.Display, window.WindowHandle, ref hints);
// Register for window destroy notification
API.XSetWMProtocols(window.Display, window.WindowHandle, new IntPtr[] { wm_destroy }, 1);
// Set the initial window size to ensure X, Y, Width, Height and the rest
// return the correct values inside the constructor and the Load event.
XEvent e = new XEvent();
e.ConfigureEvent.x = x;
e.ConfigureEvent.y = y;
e.ConfigureEvent.width = width;
e.ConfigureEvent.height = height;
RefreshWindowBounds(ref e);
Debug.Print("X11GLNative window created successfully (id: {0}).", Handle);
SetupInput();
exists = true;
}
void SetupInput() {
Debug.Print("Initalizing X11 input driver.");
// Init keyboard
API.XDisplayKeycodes(window.Display, ref firstKeyCode, ref lastKeyCode);
Debug.Print("First keycode: {0}, last {1}", firstKeyCode, lastKeyCode);
IntPtr keysym_ptr = API.XGetKeyboardMapping(window.Display, (byte)firstKeyCode,
lastKeyCode - firstKeyCode + 1, ref keysyms_per_keycode);
Debug.Print("{0} keysyms per keycode.", keysyms_per_keycode);
keysyms = new IntPtr[(lastKeyCode - firstKeyCode + 1) * keysyms_per_keycode];
Marshal.PtrToStructure(keysym_ptr, keysyms);
API.XFree(keysym_ptr);
// Request that auto-repeat is only set on devices that support it physically.
// This typically means that it's turned off for keyboards (which is what we want).
// We prefer this method over XAutoRepeatOff/On, because the latter needs to
// be reset before the program exits.
bool supported;
API.XkbSetDetectableAutoRepeat(window.Display, true, out supported);
}
/// <summary> Registers the necessary atoms for GameWindow. </summary>
void RegisterAtoms(X11WindowInfo window) {
wm_destroy = API.XInternAtom(window.Display, "WM_DELETE_WINDOW", true);
net_wm_state = API.XInternAtom(window.Display, "_NET_WM_STATE", false);
net_wm_state_minimized = API.XInternAtom(window.Display, "_NET_WM_STATE_MINIMIZED", false);
net_wm_state_fullscreen = API.XInternAtom(window.Display, "_NET_WM_STATE_FULLSCREEN", false);
net_wm_state_maximized_horizontal = API.XInternAtom(window.Display, "_NET_WM_STATE_MAXIMIZED_HORZ", false);
net_wm_state_maximized_vertical = API.XInternAtom(window.Display, "_NET_WM_STATE_MAXIMIZED_VERT", false);
net_wm_icon = API.XInternAtom(window.Display, "_NEW_WM_ICON", false);
net_frame_extents = API.XInternAtom(window.Display, "_NET_FRAME_EXTENTS", false);
}
void SetWindowMinMax(short min_width, short min_height, short max_width, short max_height) {
IntPtr dummy;
XSizeHints hints = new XSizeHints();
API.XGetWMNormalHints(window.Display, window.WindowHandle, ref hints, out dummy);
if (min_width > 0 || min_height > 0) {
hints.flags = (IntPtr)((int)hints.flags | (int)XSizeHintsFlags.PMinSize);
hints.min_width = min_width;
hints.min_height = min_height;
} else
hints.flags = (IntPtr)((int)hints.flags & ~(int)XSizeHintsFlags.PMinSize);
if (max_width > 0 || max_height > 0) {
hints.flags = (IntPtr)((int)hints.flags | (int)XSizeHintsFlags.PMaxSize);
hints.max_width = max_width;
hints.max_height = max_height;
} else
hints.flags = (IntPtr)((int)hints.flags & ~(int)XSizeHintsFlags.PMaxSize);
if (hints.flags != IntPtr.Zero) {
// The Metacity team has decided that they won't care about this when clicking the maximize
// icon, will maximize the window to fill the screen/parent no matter what.
// http://bugzilla.ximian.com/show_bug.cgi?id=80021
API.XSetWMNormalHints(window.Display, window.WindowHandle, ref hints);
}
}
void ToggleKey( ref XKeyEvent keyEvent, bool pressed ) {
IntPtr keysym = API.XLookupKeysym(ref keyEvent, 0);
IntPtr keysym2 = API.XLookupKeysym(ref keyEvent, 1);
if (keymap.ContainsKey((XKey)keysym))
keyboard[keymap[(XKey)keysym]] = pressed;
else if (keymap.ContainsKey((XKey)keysym2))
keyboard[keymap[(XKey)keysym2]] = pressed;
else
Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.KeyEvent.keycode, (XKey)keysym, (XKey)keysym2);
}
static void DeleteIconPixmaps(IntPtr display, IntPtr window) {
IntPtr wmHints_ptr = API.XGetWMHints(display, window);
if (wmHints_ptr != IntPtr.Zero) {
XWMHints wmHints = (XWMHints)Marshal.PtrToStructure(wmHints_ptr, typeof(XWMHints));
XWMHintsFlags flags = (XWMHintsFlags)wmHints.flags.ToInt32();
if ((flags & XWMHintsFlags.IconPixmapHint) != 0) {
wmHints.flags = new IntPtr((int)(flags & ~XWMHintsFlags.IconPixmapHint));
API.XFreePixmap(display, wmHints.icon_pixmap);
}
if ((flags & XWMHintsFlags.IconMaskHint) != 0) {
wmHints.flags = new IntPtr((int)(flags & ~XWMHintsFlags.IconMaskHint));
API.XFreePixmap(display, wmHints.icon_mask);
}
API.XSetWMHints(display, window, ref wmHints);
API.XFree(wmHints_ptr);
}
}
void RefreshWindowBorders() {
IntPtr atom, nitems, bytes_after, prop = IntPtr.Zero;
int format;
API.XGetWindowProperty(window.Display, window.WindowHandle,
net_frame_extents, IntPtr.Zero, new IntPtr(16), false,
(IntPtr)Atom.XA_CARDINAL, out atom, out format, out nitems, out bytes_after, ref prop);
if (prop != IntPtr.Zero) {
if ((long)nitems == 4) {
borderLeft = Marshal.ReadIntPtr(prop, 0).ToInt32();
borderRight = Marshal.ReadIntPtr(prop, IntPtr.Size).ToInt32();
borderTop = Marshal.ReadIntPtr(prop, IntPtr.Size * 2).ToInt32();
borderBottom = Marshal.ReadIntPtr(prop, IntPtr.Size * 3).ToInt32();
}
API.XFree(prop);
}
}
void RefreshWindowBounds(ref XEvent e) {
RefreshWindowBorders();
Point newLoc = new Point(e.ConfigureEvent.x - borderLeft, e.ConfigureEvent.y - borderTop);
if (Location != newLoc) {
bounds.Location = newLoc;
if (Move != null)
Move(this, EventArgs.Empty);
}
// Note: width and height denote the internal (client) size.
// To get the external (window) size, we need to add the border size.
Size newSize = new Size(
e.ConfigureEvent.width + borderLeft + borderRight,
e.ConfigureEvent.height + borderTop + borderBottom);
if (bounds.Size != newSize) {
bounds.Size = newSize;
client_rectangle.Size = new Size(e.ConfigureEvent.width, e.ConfigureEvent.height);
if (this.Resize != null) {
Resize(this, EventArgs.Empty);
}
}
}
public void ProcessEvents() {
// Process all pending events
while (Exists && window != null) {
if (!API.XCheckWindowEvent(window.Display, window.WindowHandle, window.EventMask, ref e) &&
!API.XCheckTypedWindowEvent(window.Display, window.WindowHandle, XEventName.ClientMessage, ref e))
break;
// Respond to the event e
switch (e.type) {
case XEventName.MapNotify:
case XEventName.UnmapNotify:
{
bool previous_visible = visible;
visible = e.type == XEventName.MapNotify;
if (visible != previous_visible)
if (VisibleChanged != null)
VisibleChanged(this, EventArgs.Empty);
} break;
case XEventName.CreateNotify:
// A child was was created - nothing to do
break;
case XEventName.ClientMessage:
if (!isExiting && e.ClientMessageEvent.ptr1 == wm_destroy) {
Debug.Print("Exit message received.");
CancelEventArgs ce = new CancelEventArgs();
if (Closing != null)
Closing(this, ce);
if (!ce.Cancel) {
isExiting = true;
DestroyWindow();
if (Closed != null)
Closed(this, EventArgs.Empty);
}
} break;
case XEventName.DestroyNotify:
Debug.Print("Window destroyed");
exists = false;
break;
case XEventName.ConfigureNotify:
RefreshWindowBounds(ref e);
break;
case XEventName.KeyPress:
ToggleKey( ref e.KeyEvent, true );
int status = API.XLookupString(ref e.KeyEvent, ascii, ascii.Length, null, IntPtr.Zero);
Encoding.Default.GetChars(ascii, 0, status, chars, 0);
EventHandler<KeyPressEventArgs> key_press = KeyPress;
if (key_press != null) {
for (int i = 0; i < status; i++) {
// ignore NULL char after non-ASCII input char, like ä or å on Finnish keyboard layout
if (chars[i] == '\0') continue;
KPEventArgs.KeyChar = chars[i];
key_press(this, KPEventArgs);
}
}
break;
case XEventName.KeyRelease:
// Todo: raise KeyPress event. Use code from
// http://anonsvn.mono-project.com/viewvc/trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/X11Keyboard.cs?view=markup
ToggleKey( ref e.KeyEvent, false );
break;
case XEventName.ButtonPress:
if (e.ButtonEvent.button == 1) mouse[MouseButton.Left] = true;
else if (e.ButtonEvent.button == 2) mouse[MouseButton.Middle] = true;
else if (e.ButtonEvent.button == 3) mouse[MouseButton.Right] = true;
else if (e.ButtonEvent.button == 4) mouse.Wheel++;
else if (e.ButtonEvent.button == 5) mouse.Wheel--;
else if (e.ButtonEvent.button == 6) keyboard[Key.XButton1] = true;
else if (e.ButtonEvent.button == 7) keyboard[Key.XButton2] = true;
break;
case XEventName.ButtonRelease:
if (e.ButtonEvent.button == 1) mouse[MouseButton.Left] = false;
else if (e.ButtonEvent.button == 2) mouse[MouseButton.Middle] = false;
else if (e.ButtonEvent.button == 3) mouse[MouseButton.Right] = false;
else if (e.ButtonEvent.button == 6) keyboard[Key.XButton1] = false;
else if (e.ButtonEvent.button == 7) keyboard[Key.XButton2] = false;
break;
case XEventName.MotionNotify:
mouse.Position = new Point(e.MotionEvent.x, e.MotionEvent.y);
break;
case XEventName.FocusIn:
case XEventName.FocusOut:
{
bool previous_focus = has_focus;
has_focus = e.type == XEventName.FocusIn;
if (has_focus != previous_focus && FocusedChanged != null)
FocusedChanged(this, EventArgs.Empty);
} break;
case XEventName.MappingNotify:
// 0 == MappingModifier, 1 == MappingKeyboard
if (e.MappingEvent.request == 0 || e.MappingEvent.request == 1) {
Debug.Print("keybard mapping refreshed");
API.XRefreshKeyboardMapping(ref e.MappingEvent);
}
break;
case XEventName.PropertyNotify:
if (e.PropertyEvent.atom == net_wm_state) {
if (WindowStateChanged != null)
WindowStateChanged(this, EventArgs.Empty);
}
//if (e.PropertyEvent.atom == net_frame_extents) {
// RefreshWindowBorders();
//}
break;
default:
//Debug.WriteLine(String.Format("{0} event was not handled", e.type));
break;
}
}
}
public string GetClipboardText() { return ""; }
public void SetClipboardText( string value ) { }
public Rectangle Bounds {
get { return bounds; }
set {
API.XMoveResizeWindow(
window.Display, window.WindowHandle, value.X, value.Y,
value.Width - borderLeft - borderRight, value.Height - borderTop - borderBottom);
ProcessEvents();
}
}
public Point Location {
get { return Bounds.Location; }
set {
API.XMoveWindow(window.Display, window.WindowHandle, value.X, value.Y);
ProcessEvents();
}
}
public Size Size {
get { return Bounds.Size; }
set {
int width = value.Width - borderLeft - borderRight;
int height = value.Height - borderTop - borderBottom;
width = width <= 0 ? 1 : width;
height = height <= 0 ? 1 : height;
API.XResizeWindow(window.Display, window.WindowHandle, width, height);
ProcessEvents();
}
}
public Rectangle ClientRectangle {
get {
if (client_rectangle.Width == 0)
client_rectangle.Width = 1;
if (client_rectangle.Height == 0)
client_rectangle.Height = 1;
return client_rectangle;
}
set {
API.XResizeWindow(window.Display, window.WindowHandle, value.Width, value.Height);
ProcessEvents();
}
}
public Size ClientSize {
get { return ClientRectangle.Size; }
set { ClientRectangle = new Rectangle(Point.Empty, value); }
}
public Icon Icon {
get { return icon; }
set {
if (value == icon)
return;
// Note: it seems that Gnome/Metacity does not respect the _NET_WM_ICON hint.
// For this reason, we'll also set the icon using XSetWMHints.
if (value == null) {
API.XDeleteProperty(window.Display, window.WindowHandle, net_wm_icon);
DeleteIconPixmaps(window.Display, window.WindowHandle);
} else {
// Set _NET_WM_ICON
System.Drawing.Bitmap bitmap = value.ToBitmap();
int size = bitmap.Width * bitmap.Height + 2;
IntPtr[] data = new IntPtr[size];
int index = 0;
data[index++] = (IntPtr)bitmap.Width;
data[index++] = (IntPtr)bitmap.Height;
for (int y = 0; y < bitmap.Height; y++)
for (int x = 0; x < bitmap.Width; x++)
data[index++] = (IntPtr)bitmap.GetPixel(x, y).ToArgb();
API.XChangeProperty(window.Display, window.WindowHandle,
net_wm_icon, xa_cardinal, 32,
PropertyMode.Replace, data, size);
// Set XWMHints
DeleteIconPixmaps(window.Display, window.WindowHandle);
IntPtr wmHints_ptr = API.XGetWMHints(window.Display, window.WindowHandle);
if (wmHints_ptr == IntPtr.Zero)
wmHints_ptr = API.XAllocWMHints();
XWMHints wmHints = (XWMHints)Marshal.PtrToStructure(wmHints_ptr, typeof(XWMHints));
wmHints.flags = new IntPtr(wmHints.flags.ToInt32() | (int)(XWMHintsFlags.IconPixmapHint | XWMHintsFlags.IconMaskHint));
wmHints.icon_pixmap = API.CreatePixmapFromImage(window.Display, bitmap);
wmHints.icon_mask = API.CreateMaskFromImage(window.Display, bitmap);
API.XSetWMHints(window.Display, window.WindowHandle, ref wmHints);
API.XFree(wmHints_ptr);
API.XSync(window.Display, false);
}
icon = value;
}
}
public bool Focused {
get { return has_focus; }
}
public WindowState WindowState {
get {
IntPtr actual_atom, nitems, bytes_after, prop = IntPtr.Zero;
int actual_format;
bool fullscreen = false, minimised = false;
int maximised = 0;
API.XGetWindowProperty(window.Display, window.WindowHandle,
net_wm_state, IntPtr.Zero, new IntPtr(256), false,
new IntPtr(4) /*XA_ATOM*/, out actual_atom, out actual_format,
out nitems, out bytes_after, ref prop);
if ((long)nitems > 0 && prop != IntPtr.Zero) {
for (int i = 0; i < (long)nitems; i++) {
IntPtr atom = (IntPtr)Marshal.ReadIntPtr(prop, i * IntPtr.Size);
if (atom == net_wm_state_maximized_horizontal ||
atom == net_wm_state_maximized_vertical)
maximised++;
else if (atom == net_wm_state_minimized)
minimised = true;
else if (atom == net_wm_state_fullscreen)
fullscreen = true;
}
API.XFree(prop);
}
if (minimised)
return OpenTK.WindowState.Minimized;
else if (maximised == 2)
return OpenTK.WindowState.Maximized;
else if (fullscreen)
return OpenTK.WindowState.Fullscreen;
/*
attributes = new XWindowAttributes();
Functions.XGetWindowAttributes(window.Display, window.WindowHandle, ref attributes);
if (attributes.map_state == MapState.IsUnmapped)
return (OpenTK.WindowState)(-1);
*/
return WindowState.Normal;
}
set {
WindowState current_state = this.WindowState;
if (current_state == value)
return;
Debug.Print("GameWindow {0} changing WindowState from {1} to {2}.", window.WindowHandle.ToString(),
current_state.ToString(), value.ToString());
// Reset the current window state
if (current_state == OpenTK.WindowState.Minimized)
API.XMapWindow(window.Display, window.WindowHandle);
else if (current_state == OpenTK.WindowState.Fullscreen)
API.SendNetWMMessage(window, net_wm_state, _remove,
net_wm_state_fullscreen,
IntPtr.Zero);
else if (current_state == OpenTK.WindowState.Maximized)
API.SendNetWMMessage(window, net_wm_state, _toggle,
net_wm_state_maximized_horizontal,
net_wm_state_maximized_vertical);
API.XSync(window.Display, false);
switch (value) {
case OpenTK.WindowState.Normal:
API.XRaiseWindow(window.Display, window.WindowHandle);
break;
case OpenTK.WindowState.Maximized:
API.SendNetWMMessage(window, net_wm_state, _add,
net_wm_state_maximized_horizontal,
net_wm_state_maximized_vertical);
API.XRaiseWindow(window.Display, window.WindowHandle);
break;
case OpenTK.WindowState.Minimized:
// Todo: multiscreen support
API.XIconifyWindow(window.Display, window.WindowHandle, window.Screen);
break;
case OpenTK.WindowState.Fullscreen:
API.SendNetWMMessage(window, net_wm_state, _add,
net_wm_state_fullscreen, IntPtr.Zero);
API.XRaiseWindow(window.Display, window.WindowHandle);
break;
}
ProcessEvents();
}
}
public event EventHandler Load;
public event EventHandler Unload;
public event EventHandler Move;
public event EventHandler Resize;
public event EventHandler<CancelEventArgs> Closing;
public event EventHandler Closed;
public event EventHandler Disposed;
public event EventHandler VisibleChanged;
public event EventHandler FocusedChanged;
public event EventHandler WindowStateChanged;
public event EventHandler<KeyPressEventArgs> KeyPress;
public event EventHandler MouseEnter;
public event EventHandler MouseLeave;
public KeyboardDevice Keyboard {
get { return keyboard; }
}
public MouseDevice Mouse {
get { return mouse; }
}
public Point DesktopCursorPos {
get {
IntPtr root, child;
int rootX, rootY, childX, childY, mask;
API.XQueryPointer( window.Display, window.RootWindow, out root, out child, out rootX, out rootY, out childX, out childY, out mask );
return new Point( rootX, rootY );
}
set {
API.XWarpPointer( window.Display, IntPtr.Zero, window.RootWindow, 0, 0, 0, 0, value.X, value.Y );
API.XFlush( window.Display ); // TODO: not sure if XFlush call is necessary
}
}
bool cursorVisible = true;
public bool CursorVisible {
get { return cursorVisible; }
set {
cursorVisible = value;
if( value ) {
API.XUndefineCursor( window.Display, window.WindowHandle );
} else {
if( blankCursor == IntPtr.Zero )
MakeBlankCursor();
API.XDefineCursor( window.Display, window.WindowHandle, blankCursor );
}
}
}
IntPtr blankCursor;
void MakeBlankCursor() {
XColor color = default( XColor );
IntPtr pixmap = API.XCreatePixmap( window.Display, window.RootWindow, 1, 1, 1 );
blankCursor = API.XCreatePixmapCursor( window.Display, pixmap, pixmap, ref color, ref color, 0, 0 );
API.XFreePixmap( window.Display, pixmap );
}
/// <summary> Returns true if a render window/context exists. </summary>
public bool Exists {
get { return exists; }
}
/// <summary> Gets the current window handle. </summary>
public IntPtr Handle {
get { return window.WindowHandle; }
}
public bool Visible {
get { return visible; }
set {
if (value && !visible) {
API.XMapWindow(window.Display, window.WindowHandle);
} else if (!value && visible) {
API.XUnmapWindow(window.Display, window.WindowHandle);
}
}
}
public IWindowInfo WindowInfo {
get { return window; }
}
public void Close() {
XEvent ev = new XEvent();
ev.type = XEventName.ClientMessage;
ev.ClientMessageEvent.format = 32;
ev.ClientMessageEvent.display = window.Display;
ev.ClientMessageEvent.window = window.WindowHandle;
ev.ClientMessageEvent.ptr1 = wm_destroy;
API.XSendEvent(window.Display, window.WindowHandle, false,
EventMask.NoEventMask, ref ev);
API.XFlush(window.Display);
}
public void DestroyWindow() {
Debug.Print("X11GLNative shutdown sequence initiated.");
API.XSync(window.Display, true);
API.XDestroyWindow(window.Display, window.WindowHandle);
exists = false;
}
public Point PointToClient(Point point) {
int ox, oy;
IntPtr child;
API.XTranslateCoordinates(window.Display, window.RootWindow, window.WindowHandle, point.X, point.Y, out ox, out oy, out child);
return new Point( ox, oy );
}
public Point PointToScreen(Point point) {
int ox, oy;
IntPtr child;
API.XTranslateCoordinates(window.Display, window.WindowHandle, window.RootWindow, point.X, point.Y, out ox, out oy, out child);
return new Point( ox, oy );
}
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool manuallyCalled) {
if (disposed) return;
if (manuallyCalled) {
if (window != null && window.WindowHandle != IntPtr.Zero) {
if (Exists) {
DestroyWindow();
}
window.Dispose();
window = null;
}
} else {
Debug.Print("[Warning] {0} leaked.", this.GetType().Name);
}
disposed = true;
}
~X11GLNative() {
this.Dispose(false);
}
}
}