Opentk/Source/OpenTK/Platform/Linux/LinuxNativeWindow.cs

548 lines
16 KiB
C#
Raw Normal View History

2014-06-23 20:22:04 +00:00
#region License
//
// LinuxNativeWindow.cs
//
// Author:
// Stefanos A. <stapostol@gmail.com>
//
// Copyright (c) 2006-2014 Stefanos Apostolopoulos
//
// 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;
2014-06-25 07:01:35 +00:00
using System.Diagnostics;
2014-06-24 17:27:38 +00:00
using System.Drawing;
2014-06-25 07:01:35 +00:00
using System.Runtime.InteropServices;
2014-06-24 17:27:38 +00:00
using OpenTK.Graphics;
using OpenTK.Input;
2014-06-24 17:27:38 +00:00
using OpenTK.Platform.Egl;
2014-06-23 20:22:04 +00:00
namespace OpenTK.Platform.Linux
{
2014-06-24 17:27:38 +00:00
using Egl = OpenTK.Platform.Egl.Egl;
2014-06-23 20:22:04 +00:00
class LinuxNativeWindow : NativeWindowBase
{
2014-06-25 07:01:35 +00:00
LinuxWindowInfo window;
2014-06-24 17:27:38 +00:00
string title;
Icon icon;
Rectangle bounds;
Size client_size;
bool exists;
bool is_focused;
2014-07-17 07:25:16 +00:00
bool is_cursor_visible = true;
KeyboardState previous_keyboard;
MouseState previous_mouse;
2014-06-24 17:27:38 +00:00
2014-07-17 07:25:16 +00:00
MouseCursor cursor_current;
BufferObject cursor_custom;
BufferObject cursor_default;
2014-07-17 07:45:13 +00:00
BufferObject cursor_empty;
2014-07-17 07:25:16 +00:00
public LinuxNativeWindow(IntPtr display, IntPtr gbm, int fd,
2014-06-25 07:01:35 +00:00
int x, int y, int width, int height, string title,
GraphicsMode mode, GameWindowFlags options,
2014-06-24 17:27:38 +00:00
DisplayDevice display_device)
{
2014-06-25 07:01:35 +00:00
Debug.Print("[KMS] Creating window on display {0:x}", display);
2014-06-24 17:27:38 +00:00
Title = title;
display_device = display_device ?? DisplayDevice.Default;
if (display_device == null)
{
throw new NotSupportedException("[KMS] Driver does not currently support headless systems");
}
2014-06-24 17:27:38 +00:00
2014-07-17 07:25:16 +00:00
window = new LinuxWindowInfo(display, fd, gbm, display_device.Id as LinuxDisplay);
// Note: we only support fullscreen windows on KMS.
// We implicitly override the requested width and height
// by the width and height of the DisplayDevice, if any.
width = display_device.Width;
height = display_device.Height;
bounds = new Rectangle(0, 0, width, height);
client_size = bounds.Size;
2014-06-25 07:01:35 +00:00
if (!mode.Index.HasValue)
{
mode = new EglGraphicsMode().SelectGraphicsMode(window, mode, 0);
}
Debug.Print("[KMS] Selected EGL mode {0}", mode);
SurfaceFormat format = GetSurfaceFormat(display, mode);
SurfaceFlags usage = SurfaceFlags.Rendering | SurfaceFlags.Scanout;
if (!Gbm.IsFormatSupported(gbm, format, usage))
2014-06-25 07:01:35 +00:00
{
Debug.Print("[KMS] Failed to find suitable surface format, using XRGB8888");
format = SurfaceFormat.XRGB8888;
}
Debug.Print("[KMS] Creating GBM surface on {0:x} with {1}x{2} {3} [{4}]",
gbm, width, height, format, usage);
IntPtr gbm_surface = Gbm.CreateSurface(gbm,
width, height, format, usage);
if (gbm_surface == IntPtr.Zero)
{
throw new NotSupportedException("[KMS] Failed to create GBM surface for rendering");
}
2014-06-24 17:27:38 +00:00
window.Handle = gbm_surface;
2014-06-25 07:01:35 +00:00
Debug.Print("[KMS] Created GBM surface {0:x}", window.Handle);
window.CreateWindowSurface(mode.Index.Value);
Debug.Print("[KMS] Created EGL surface {0:x}", window.Surface);
2014-07-17 07:25:16 +00:00
cursor_default = CreateCursor(gbm, Cursors.Default);
2014-07-17 07:45:13 +00:00
cursor_empty = CreateCursor(gbm, Cursors.Empty);
2014-07-17 07:25:16 +00:00
Cursor = MouseCursor.Default;
2014-06-24 17:27:38 +00:00
exists = true;
}
2014-07-17 07:25:16 +00:00
#region Private Members
static BufferObject CreateCursor(IntPtr gbm, MouseCursor cursor)
{
if (cursor.Width > 64 || cursor.Height > 64)
{
Debug.Print("[KMS] Cursor size {0}x{1} unsupported. Maximum is 64x64.",
cursor.Width, cursor.Height);
return default(BufferObject);
}
int width = 64;
int height = 64;
SurfaceFormat format = SurfaceFormat.ARGB8888;
SurfaceFlags usage = SurfaceFlags.Cursor64x64 | SurfaceFlags.Write;
2014-07-17 07:45:13 +00:00
Debug.Print("[KMS] Gbm.CreateBuffer({0:X}, {1}, {2}, {3}, {4}).",
gbm, width, height, format, usage);
2014-07-17 07:25:16 +00:00
BufferObject bo = Gbm.CreateBuffer(
gbm, width, height, format, usage);
if (bo == BufferObject.Zero)
{
2014-07-17 07:45:13 +00:00
Debug.Print("[KMS] Failed to create buffer.");
2014-07-17 07:25:16 +00:00
return bo;
}
// Copy cursor.Data into a new buffer of the correct size
byte[] cursor_data = new byte[width * height * 4];
for (int y = 0; y < cursor.Height; y++)
{
int dst_offset = y * width * 4;
int src_offset = y * cursor.Width * 4;
int src_length = cursor.Width * 4;
Array.Copy(
cursor.Data, src_offset,
cursor_data, dst_offset,
src_length);
}
bo.Write(cursor_data);
return bo;
}
void SetCursor(MouseCursor cursor)
{
BufferObject bo = default(BufferObject);
if (cursor == MouseCursor.Default)
{
bo = cursor_default;
}
else if (cursor == MouseCursor.Empty)
{
2014-07-17 07:45:13 +00:00
bo = cursor_empty;
2014-07-17 07:25:16 +00:00
}
else
{
2014-07-17 07:45:13 +00:00
if (cursor_custom != BufferObject.Zero)
cursor_custom.Dispose();
2014-07-17 07:25:16 +00:00
cursor_custom = CreateCursor(window.BufferManager, cursor);
2014-07-17 07:45:13 +00:00
bo = cursor_custom;
}
// If we failed to create a proper cursor, try falling back
// to the empty cursor. We do not want to crash here!
if (bo == BufferObject.Zero)
{
bo = cursor_empty;
2014-07-17 07:25:16 +00:00
}
2014-07-17 07:45:13 +00:00
if (bo != BufferObject.Zero)
{
Drm.SetCursor(window.FD, window.DisplayDevice.Id,
bo.Handle, bo.Width, bo.Height, cursor.X, cursor.Y);
}
2014-07-17 07:25:16 +00:00
}
static SurfaceFormat GetSurfaceFormat(IntPtr display, GraphicsMode mode)
2014-06-25 07:01:35 +00:00
{
// Use EGL 1.4 EGL_NATIVE_VISUAL_ID to retrieve
// the corresponding surface format. If that fails
// fall back to a manual algorithm.
int format;
Egl.GetConfigAttrib(display, mode.Index.Value,
Egl.NATIVE_VISUAL_ID, out format);
if ((SurfaceFormat)format != 0)
return (SurfaceFormat)format;
Debug.Print("[KMS] Failed to retrieve EGL visual from GBM surface. Error: {0}",
Egl.GetError());
Debug.Print("[KMS] Falling back to hardcoded formats.");
2014-06-25 07:01:35 +00:00
int r = mode.ColorFormat.Red;
int g = mode.ColorFormat.Green;
int b = mode.ColorFormat.Blue;
int a = mode.ColorFormat.Alpha;
if (mode.ColorFormat.IsIndexed)
return SurfaceFormat.C8;
if (r == 3 && g == 3 && b == 2 && a == 0)
return SurfaceFormat.RGB332;
if (r == 5 && g == 6 && b == 5 && a == 0)
return SurfaceFormat.RGB565;
if (r == 5 && g == 6 && b == 5 && a == 0)
return SurfaceFormat.RGB565;
if (r == 8 && g == 8 && b == 8 && a == 0)
return SurfaceFormat.RGB888;
if (r == 5 && g == 5 && b == 5 && a == 1)
return SurfaceFormat.RGBA5551;
if (r == 10 && g == 10 && b == 10 && a == 2)
return SurfaceFormat.RGBA1010102;
if (r == 4 && g == 4 && b == 4 && a == 4)
return SurfaceFormat.RGBA4444;
if (r == 8 && g == 8 && b == 8 && a == 8)
return SurfaceFormat.RGBA8888;
return SurfaceFormat.RGBA8888;
}
KeyboardState ProcessKeyboard(KeyboardState keyboard)
{
for (Key i = 0; i < Key.LastKey; i++)
{
if (keyboard[i])
{
OnKeyDown(i, previous_keyboard[i]);
// Todo: implement libxkb-common binding for text input
}
if (!keyboard[i] && previous_keyboard[i])
{
OnKeyUp(i);
}
}
return keyboard;
}
MouseState ProcessMouse(MouseState mouse)
{
2014-07-17 09:20:01 +00:00
// Handle mouse buttons
for (MouseButton i = 0; i < MouseButton.LastButton; i++)
{
if (mouse[i] && !previous_mouse[i])
{
OnMouseDown(i);
}
if (!mouse[i] && previous_mouse[i])
{
OnMouseUp(i);
}
}
2014-07-17 09:20:01 +00:00
// Handle mouse movement
{
2014-07-17 09:20:01 +00:00
int x = mouse.X;
int y = mouse.Y;
// Make sure the mouse cannot leave the GameWindow when captured
if (!CursorVisible)
{
x = MathHelper.Clamp(mouse.X, Bounds.Left, Bounds.Right - 1);
y = MathHelper.Clamp(mouse.Y, Bounds.Top, Bounds.Bottom - 1);
2014-07-17 09:20:01 +00:00
if (x != mouse.X || y != mouse.Y)
{
Mouse.SetPosition(x, y);
}
}
if (x != previous_mouse.X || y != previous_mouse.Y)
2014-07-17 09:20:01 +00:00
{
OnMouseMove(x, y);
}
}
2014-07-17 09:20:01 +00:00
// Handle mouse scroll
if (mouse.Scroll != previous_mouse.Scroll)
{
float dx = mouse.Scroll.X - previous_mouse.Scroll.X;
float dy = mouse.Scroll.Y - previous_mouse.Scroll.Y;
OnMouseWheel(dx, dy);
}
2014-07-17 09:20:01 +00:00
// Handle mouse focus
// Note: focus follows mouse. Literally.
bool cursor_in = Bounds.Contains(new Point(mouse.X, mouse.Y));
if (!cursor_in && Focused)
{
OnMouseLeave(EventArgs.Empty);
SetFocus(false);
}
else if (cursor_in && !Focused)
{
OnMouseEnter(EventArgs.Empty);
SetFocus(true);
}
return mouse;
}
void SetFocus(bool focus)
{
if (is_focused != focus)
{
is_focused = focus;
OnFocusedChanged(EventArgs.Empty);
}
}
2014-07-17 07:25:16 +00:00
#endregion
2014-06-23 20:22:04 +00:00
#region INativeWindow Members
public override void ProcessEvents()
{
// Note: there is no event-based keyboard/mouse input available.
// We will fake that by polling OpenTK.Input.
previous_keyboard = ProcessKeyboard(Keyboard.GetState());
previous_mouse = ProcessMouse(Mouse.GetCursorState());
base.ProcessEvents();
}
2014-06-23 20:22:04 +00:00
public override void Close()
{
2014-06-24 17:27:38 +00:00
exists = false;
2014-06-23 20:22:04 +00:00
}
2014-06-24 17:27:38 +00:00
public override Point PointToClient(Point point)
2014-06-23 20:22:04 +00:00
{
var origin = Point.Empty;
var display = DisplayDevice.Default;
if (display != null)
{
origin = display.Bounds.Location;
}
var client = Location;
return new Point(point.X + client.X - origin.X, point.Y + client.Y - origin.Y);
2014-06-23 20:22:04 +00:00
}
2014-06-24 17:27:38 +00:00
public override Point PointToScreen(Point point)
2014-06-23 20:22:04 +00:00
{
var origin = Point.Empty;
var display = DisplayDevice.Default;
if (display != null)
{
origin = display.Bounds.Location;
}
var client = Location;
return new Point(point.X + origin.X - client.X, point.Y + origin.Y - client.Y);
2014-06-23 20:22:04 +00:00
}
protected override void Dispose(bool disposing)
{
2014-06-25 07:01:35 +00:00
if (disposing)
{
Debug.Print("[KMS] Destroying window {0}.", window.Handle);
2014-06-25 07:01:35 +00:00
window.Dispose();
Gbm.DestroySurface(window.Handle);
}
else
{
Debug.Print("[KMS] {0} leaked. Did you forget to call Dispose()?", GetType().FullName);
}
2014-06-23 20:22:04 +00:00
}
2014-06-24 17:27:38 +00:00
public override Icon Icon
2014-06-23 20:22:04 +00:00
{
get
{
2014-06-24 17:27:38 +00:00
return icon;
2014-06-23 20:22:04 +00:00
}
set
{
2014-06-24 17:27:38 +00:00
if (icon != value)
{
icon = value;
OnIconChanged(EventArgs.Empty);
}
2014-06-23 20:22:04 +00:00
}
}
public override string Title
{
get
{
2014-06-24 17:27:38 +00:00
return title;
2014-06-23 20:22:04 +00:00
}
set
{
2014-06-24 17:27:38 +00:00
if (title != value)
{
title = value;
OnTitleChanged(EventArgs.Empty);
}
2014-06-23 20:22:04 +00:00
}
}
public override bool Focused
{
get
{
return is_focused;
2014-06-23 20:22:04 +00:00
}
}
public override bool Visible
{
get
{
2014-06-24 17:27:38 +00:00
return true;
2014-06-23 20:22:04 +00:00
}
set
{
}
}
public override bool Exists
{
get
{
2014-06-24 17:27:38 +00:00
return exists;
2014-06-23 20:22:04 +00:00
}
}
public override IWindowInfo WindowInfo
{
get
{
2014-06-25 07:01:35 +00:00
return window;
2014-06-23 20:22:04 +00:00
}
}
public override WindowState WindowState
{
get
{
2014-06-24 17:27:38 +00:00
return WindowState.Fullscreen;
2014-06-23 20:22:04 +00:00
}
set
{
}
}
public override WindowBorder WindowBorder
{
get
{
2014-06-24 17:27:38 +00:00
return WindowBorder.Hidden;
2014-06-23 20:22:04 +00:00
}
set
{
}
}
2014-06-24 17:27:38 +00:00
public override Rectangle Bounds
2014-06-23 20:22:04 +00:00
{
get
{
2014-06-24 17:27:38 +00:00
return bounds;
2014-06-23 20:22:04 +00:00
}
set
{
}
}
2014-06-24 17:27:38 +00:00
public override Size ClientSize
2014-06-23 20:22:04 +00:00
{
get
{
2014-06-24 17:27:38 +00:00
return client_size;
2014-06-23 20:22:04 +00:00
}
set
{
}
}
public override bool CursorVisible
{
get
{
2014-07-17 07:25:16 +00:00
return is_cursor_visible;
2014-06-23 20:22:04 +00:00
}
set
{
2014-07-17 07:45:13 +00:00
if (value && !is_cursor_visible)
{
SetCursor(cursor_current);
}
else if (!value && is_cursor_visible)
2014-07-17 07:25:16 +00:00
{
2014-07-17 07:45:13 +00:00
SetCursor(MouseCursor.Empty);
2014-07-17 07:25:16 +00:00
}
2014-07-17 07:45:13 +00:00
is_cursor_visible = value;
2014-06-23 20:22:04 +00:00
}
}
public override MouseCursor Cursor
{
get
{
2014-07-17 07:25:16 +00:00
return cursor_current;
2014-06-23 20:22:04 +00:00
}
set
{
2014-07-17 07:25:16 +00:00
if (cursor_current != value)
{
if (cursor_custom != BufferObject.Zero)
{
cursor_custom.Dispose();
}
2014-07-17 07:45:13 +00:00
if (CursorVisible)
{
SetCursor(value);
}
2014-07-17 07:25:16 +00:00
cursor_current = value;
}
2014-06-23 20:22:04 +00:00
}
}
#endregion
}
}