Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] New: Detect display scaling, cleanup in X11 API #1324

Open
wants to merge 32 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
611dd43
Change: Remove unused code from DisplayDevice.cs
leezer3 Sep 5, 2021
1f85448
New: Add DPI detection to Windows, SDL2 and OS-X backends
leezer3 Sep 5, 2021
d12f983
Fix: Convert SDL DPI to scale factor
leezer3 Sep 6, 2021
c06c09b
Change: Various fixes / changes in OS-X calculations
leezer3 Sep 6, 2021
53757be
swapping out the values refrenced for width and height for between
andylin2004 Sep 6, 2021
7d07dc8
Merge pull request #1 from andylin2004/DisplayScaling
leezer3 Sep 6, 2021
f126c1c
Change: Minor cleaning in MacOS display device driver
leezer3 Sep 7, 2021
b9aefc6
Change: Assorted cleaning in X11 display device driver
leezer3 Sep 7, 2021
6dba93a
Change: Work on X11 display device drivers
leezer3 Sep 7, 2021
1aa63de
Screen scaling value is now dynamically calculated based off of curre…
andylin2004 Sep 10, 2021
89667c9
Change: More minor cleaning
leezer3 Sep 22, 2021
ba2eca1
Change: Implement basic X11 scale factor
leezer3 Sep 22, 2021
cea70ba
Change: Fix typo and improve, add some XML comments
leezer3 Sep 23, 2021
de7b5e2
Change: Clean out some unused duplicate X11 bits
leezer3 Sep 23, 2021
ea6b602
Change: Review comments
leezer3 Sep 30, 2021
a88ead0
Change: Remove another unused junk method
leezer3 Sep 30, 2021
3fd372d
Change: Remove unused / disabled filesystem notify class
leezer3 Sep 30, 2021
7ed18a5
Change: Minor junk removal
leezer3 Sep 30, 2021
5027de0
Change: Remove some more odd unused bits from X11 API
leezer3 Oct 11, 2021
cca1899
Change: Convert some comments to XML-doc
leezer3 Oct 11, 2021
f888461
Change: Add XML-doc to some more X functions, little more cleaning
leezer3 Oct 11, 2021
3fd9fd6
Change: Remove junk comment
leezer3 Oct 11, 2021
3ca519d
Change: More minor junk removal
leezer3 Oct 11, 2021
8b659af
Change: Add more XML-doc to X11 interop functions
leezer3 Oct 12, 2021
12c055e
Change: More X11 xml-doc
leezer3 Oct 14, 2021
3cfc0ac
Change: More X11 XML-doc, minor cleaning
leezer3 Oct 16, 2021
6c5a11f
Change: Fix some compiler warnings
leezer3 Oct 16, 2021
ccbbebb
Fix: SDL2 not recognising KP_PERIOD
leezer3 Dec 22, 2021
865aa26
Fix: Make WINE work, remove duplicate code
leezer3 Dec 28, 2021
79a7e44
Change: Style tweak
leezer3 Dec 28, 2021
2df7072
Change: Update DLLMap for FreeBSD
leezer3 Dec 28, 2021
dd83ed7
Change: Add DE specific keys to Linux keymap
leezer3 Nov 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/OpenTK/Platform/X11/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ internal static partial class Functions

[DllImport("libX11", EntryPoint = "XOpenDisplay")]
private extern static IntPtr sys_XOpenDisplay(IntPtr display);

/// <summary>Opens an X11 display</summary>
/// <param name="display">The display to open</param>
public static IntPtr XOpenDisplay(IntPtr display)
{
lock (Lock)
Expand All @@ -61,6 +64,7 @@ public static IntPtr XOpenDisplay(IntPtr display)

[DllImport("libX11", EntryPoint = "XCloseDisplay")]
public extern static int XCloseDisplay(IntPtr display);

[DllImport("libX11", EntryPoint = "XSynchronize")]
public extern static IntPtr XSynchronize(IntPtr display, bool onoff);

Expand All @@ -77,7 +81,6 @@ public static IntPtr XOpenDisplay(IntPtr display)
/// <param name="visual">The visual type</param>
/// <param name="valuemask">Specifies which window attributs are defined in the attributes argument</param>
/// <param name="attributes">The window's attributes</param>
/// <returns></returns>
[DllImport("libX11", EntryPoint = "XCreateWindow")]
public unsafe extern static IntPtr XCreateWindow(IntPtr display, IntPtr parent, int x, int y, int width, int height, int border_width, int depth, int xclass, IntPtr visual, IntPtr valuemask, XSetWindowAttributes* attributes);

Expand Down Expand Up @@ -112,8 +115,22 @@ public static IntPtr XOpenDisplay(IntPtr display)
public extern static IntPtr XNextEvent(IntPtr display, ref XEvent xevent);
[DllImport("libX11")]
public extern static Bool XWindowEvent(Display display, Window w, EventMask event_mask, ref XEvent event_return);

/// <summary>Searches the event queue for an event matching the specified window and event mask</summary>
/// <param name="display">The display connection to the XServer</param>
/// <param name="w">The window to match</param>
/// <param name="event_mask">The event mask</param>
/// <param name="event_return">The returned event</param>
/// <returns>TRUE if a matching event is found, FALSE otherwise</returns>
[DllImport("libX11")]
public extern static Bool XCheckWindowEvent(Display display, Window w, EventMask event_mask, ref XEvent event_return);

/// <summary>Searches the event queue for an event matching the specified window and event name</summary>
/// <param name="display">The display connection to the XServer</param>
/// <param name="w">The window to match</param>
/// <param name="event_type">The event name</param>
/// <param name="event_return">The returned event</param>
/// <returns>TRUE if a matching event is found, FALSE otherwise</returns>
[DllImport("libX11")]
public extern static Bool XCheckTypedWindowEvent(Display display, Window w, XEventName event_type, ref XEvent event_return);
[DllImport("libX11")]
Expand Down Expand Up @@ -144,12 +161,30 @@ public static IntPtr XOpenDisplay(IntPtr display)
[DllImport("libX11", EntryPoint = "XReparentWindow")]
public extern static int XReparentWindow(IntPtr display, IntPtr window, IntPtr parent, int x, int y);

/// <summary>Changes the size and location of the specified window without raising it</summary>
/// <param name="display">The display connection to the XServer</param>
/// <param name="window">The window to modify</param>
/// <param name="x">The new X co-ordinate relative to the window's parent</param>
/// <param name="y">The new Y co-ordinate relative to the window's parent</param>
/// <param name="width">The new width</param>
/// <param name="height">The new height</param>
[DllImport("libX11", EntryPoint = "XMoveResizeWindow")]
public extern static int XMoveResizeWindow(IntPtr display, IntPtr window, int x, int y, int width, int height);

/// <summary>Changes the location of the specified window without raising it</summary>
/// <param name="display">The display connection to the XServer</param>
/// <param name="window">The window to modify</param>
/// <param name="x">The new X co-ordinate relative to the window's parent</param>
/// <param name="y">The new Y co-ordinate relative to the window's parent</param>
[DllImport("libX11", EntryPoint = "XMoveWindow")]
public extern static int XMoveWindow(IntPtr display, IntPtr w, int x, int y);
public extern static int XMoveWindow(IntPtr display, IntPtr window, int x, int y);


/// <summary>Changes the size of the specified window without raising it</summary>
/// <param name="display">The display connection to the XServer</param>
/// <param name="window">The window to modify</param>
/// <param name="width">The new width</param>
/// <param name="height">The new height</param>
[DllImport("libX11", EntryPoint = "XResizeWindow")]
public extern static int XResizeWindow(IntPtr display, IntPtr window, int width, int height);

Expand Down Expand Up @@ -246,6 +281,12 @@ public static int XSendEvent(IntPtr display, IntPtr window, bool propagate, Even
[DllImport("libX11", EntryPoint = "XGetAtomName")]
public extern static IntPtr XGetAtomName(IntPtr display, IntPtr atom);

/// <summary>Replaces a window's WM_PROTOCOLS property with the list of specified atoms</summary>
/// <param name="display">The display connection to the XServer</param>
/// <param name="window">The window to modify</param>
/// <param name="protocols">The new WM_PROTOCOLS atom list</param>
/// <param name="count">The number of elements in the protocols array</param>
/// <returns></returns>
[DllImport("libX11", EntryPoint = "XSetWMProtocols")]
public extern static int XSetWMProtocols(IntPtr display, IntPtr window, IntPtr[] protocols, int count);

Expand Down Expand Up @@ -673,6 +714,12 @@ public static void SendNetWMMessage(X11WindowInfo window, IntPtr message_type, I
ref xev);
}

/// <summary>Sends an X11 client message to the specified window</summary>
/// <param name="window">The window recieving the event</param>
/// <param name="message_type">The X11 message type</param>
/// <param name="l0"></param>
/// <param name="l1"></param>
/// <param name="l2"></param>
public static void SendNetClientMessage(X11WindowInfo window, IntPtr message_type,
IntPtr l0, IntPtr l1, IntPtr l2)
{
Expand Down
49 changes: 17 additions & 32 deletions src/OpenTK/Platform/X11/X11GLNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ internal sealed class X11GLNative : NativeWindowBase
private static readonly IntPtr _atom_add = (IntPtr)1;
private static readonly IntPtr _atom_toggle = (IntPtr)2;

#pragma warning disable 649 // never assigned, compiler bug in Mono 3.4.0
private Rectangle bounds;
private Size client_size;
#pragma warning restore 649
private int border_left, border_right, border_top, border_bottom;
private Icon icon;
private bool has_focus;
Expand Down Expand Up @@ -148,7 +146,6 @@ internal sealed class X11GLNative : NativeWindowBase
private readonly IntPtr EmptyCursor;

#pragma warning disable 414 // Field assigned but never used, we do that on purpose
private readonly bool xi2_supported;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this field removed and not the others here? To me it would only make sense to keep the all or remove them all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually use the version number itself here:

if (xi2_version >= 210)

As the opcode is technically part of the version number string, keeping it seemed logical too. Left the pragma there to suppress the fact we don't use it.

Could probably re-hack slightly to use the underlying variable this refers to inside the X11 mouse / keyboard driver, but that's heading towards the re-write.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we could keep this field, it's unnecessary to remove it.
If we are really unlucky someone uses reflection to read the value of this variable.

private readonly int xi2_opcode;
private readonly int xi2_version;
#pragma warning restore 414
Expand Down Expand Up @@ -258,7 +255,7 @@ public X11GLNative(int x, int y, int width, int height, string title,
Functions.XSetWMNormalHints(window.Display, window.Handle, ref hints);

// Register for window destroy notification
Functions.XSetWMProtocols(window.Display, window.Handle, new IntPtr[] { _atom_wm_destroy }, 1);
Functions.XSetWMProtocols(window.Display, window.Handle, new[] { _atom_wm_destroy }, 1);
Comment on lines -261 to +258
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can keep the type of the array here.


// Set the window class hints
Functions.XSetClassHint(window.Display, window.Handle, ref class_hint);
Expand Down Expand Up @@ -295,7 +292,7 @@ public X11GLNative(int x, int y, int width, int height, string title,

// The XInput2 extension makes keyboard and mouse handling much easier.
// Check whether it is available.
xi2_supported = XI2MouseKeyboard.IsSupported(window.Display);
bool xi2_supported = XI2MouseKeyboard.IsSupported(window.Display);
if (xi2_supported)
{
xi2_opcode = XI2MouseKeyboard.XIOpCode;
Expand Down Expand Up @@ -348,16 +345,17 @@ public X11GLNative()
}
}

private void ReadProperty(IntPtr window, IntPtr property, IntPtr type, ref IntPtr data, ref IntPtr itemsCount)
private void ReadProperty(IntPtr targetWindow, IntPtr property, IntPtr type, ref IntPtr data, out int itemsCount)
{
int format;
IntPtr length = new IntPtr(int.MaxValue);
IntPtr actualType;
IntPtr bytesLeft;

Functions.XGetWindowProperty(this.window.Display, window, property, IntPtr.Zero,
IntPtr itemCount;
Functions.XGetWindowProperty(this.window.Display, targetWindow, property, IntPtr.Zero,
length, false, type,
out actualType, out format, out itemsCount, out bytesLeft, ref data);
out actualType, out format, out itemCount, out bytesLeft, ref data);
itemsCount = itemCount.ToInt32();
}

private string[] parseUriList(string rawString)
Expand Down Expand Up @@ -854,9 +852,7 @@ public override void ProcessEvents()
int formatCount;
if (useList)
{
IntPtr count = IntPtr.Zero;
ReadProperty(sourceHandler, _atom_xdnd_type_list, (IntPtr)AtomName.XA_ATOM, ref formats, ref count);
formatCount = count.ToInt32();
ReadProperty(sourceHandler, _atom_xdnd_type_list, (IntPtr)AtomName.XA_ATOM, ref formats, out formatCount);
}
else
{
Expand Down Expand Up @@ -946,10 +942,6 @@ public override void ProcessEvents()
}
}
}
else if (e.ClientMessageEvent.message_type == _atom_xdnd_leave)
{
break;
}
Comment on lines -1011 to -1014
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to go back and check, but IIRC it can never be true from here.

break;

case XEventName.DestroyNotify:
Expand Down Expand Up @@ -983,8 +975,7 @@ public override void ProcessEvents()
{
// Translate XKeyPress to characters and
// raise KeyPress events
int status = 0;
status = Functions.XLookupString(
int status = Functions.XLookupString(
ref e.KeyEvent, ascii, ascii.Length, null, IntPtr.Zero);
Encoding.Default.GetChars(ascii, 0, status, chars, 0);

Expand Down Expand Up @@ -1109,23 +1100,18 @@ public override void ProcessEvents()
break;

case XEventName.PropertyNotify:
if (e.PropertyEvent.atom == _atom_net_wm_state)
{
OnWindowStateChanged(EventArgs.Empty);
}

//if (e.PropertyEvent.atom == _atom_net_frame_extents)
//{
// RefreshWindowBorders();
//}
break;
if (e.PropertyEvent.atom == _atom_net_wm_state)
{
OnWindowStateChanged(EventArgs.Empty);
}
break;

case XEventName.SelectionNotify:
if (e.SelectionEvent.property == _atom_xdnd_primary)
{
IntPtr data = IntPtr.Zero;
IntPtr count = IntPtr.Zero;
ReadProperty(e.SelectionEvent.requestor, e.SelectionEvent.property, e.SelectionEvent.target, ref data, ref count);
int count;
ReadProperty(e.SelectionEvent.requestor, e.SelectionEvent.property, e.SelectionEvent.target, ref data, out count);

string rawString = Marshal.PtrToStringAnsi(data);
Functions.XFree(data);
Expand Down Expand Up @@ -1345,7 +1331,7 @@ public override OpenTK.WindowState WindowState
{
for (int i = 0; i < (long)nitems; i++)
{
atom = (IntPtr)Marshal.ReadIntPtr(prop, i * IntPtr.Size);
atom = Marshal.ReadIntPtr(prop, i * IntPtr.Size);

if (atom == _atom_net_wm_state_maximized_horizontal ||
atom == _atom_net_wm_state_maximized_vertical)
Expand Down Expand Up @@ -1699,7 +1685,6 @@ public IntPtr Handle
}

/// <summary>
/// TODO: Use atoms for this property.
/// Gets or sets the GameWindow title.
/// </summary>
public override string Title
Expand Down