Skip to content

Commit

Permalink
Fixed issues where Chromely does not work on some Linux distros - czt…
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkol authored and Stelzi79 committed Oct 27, 2020
1 parent 8298341 commit 63a6f50
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 134 deletions.
12 changes: 9 additions & 3 deletions src/Chromely.Core/Configuration/DefaultConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Chromely.Core.Network;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Chromely.Core.Configuration
Expand Down Expand Up @@ -85,7 +86,13 @@ public DefaultConfiguration()
});

ControllerAssemblies = new List<ControllerAssemblyInfo>();
ControllerAssemblies.RegisterServiceAssembly("Chromely.External.Controllers.dll");

var appDirectory = AppDomain.CurrentDomain.BaseDirectory;
var externaControllerFile = Path.Combine(appDirectory, "Chromely.External.Controllers.dll");
if (File.Exists(externaControllerFile))
{
ControllerAssemblies.RegisterServiceAssembly("Chromely.External.Controllers.dll");
}

CustomSettings = new Dictionary<string, string>()
{
Expand Down Expand Up @@ -124,8 +131,7 @@ public static IChromelyConfiguration CreateForPlatform(ChromelyPlatform platform
config.CommandLineOptions = new List<string>()
{
"no-zygote",
"disable-gpu",
"disable-software-rasterizer"
"disable-gpu"
};
break;

Expand Down
49 changes: 49 additions & 0 deletions src/Chromely/Native/LinuxGtk3/GListUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Runtime.InteropServices;

namespace Chromely.Native
{
public class GListUtil
{
private readonly IntPtr _list;
public GListUtil(IntPtr list)
{
_list = list;
}

public int Length
{
get
{
return g_list_length(_list);
}
}

public void Free()
{
if (_list != IntPtr.Zero)
g_list_free(_list);
}

public IntPtr GetItem(int nth)
{
if (_list != IntPtr.Zero)
return g_list_nth_data(_list, (uint)nth);

return IntPtr.Zero;
}

#region DLLIMPORTS GlibLib

[DllImport(Library.GlibLib, CallingConvention = CallingConvention.Cdecl)]
static extern int g_list_length(IntPtr l);

[DllImport(Library.GlibLib, CallingConvention = CallingConvention.Cdecl)]
static extern void g_list_free(IntPtr l);

[DllImport(Library.GlibLib, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_list_nth_data(IntPtr l, uint n);

#endregion
}
}
17 changes: 17 additions & 0 deletions src/Chromely/Native/LinuxGtk3/Library.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Chromely.Native
{
public static class Library
{
internal const string GtkLib = "libgtk-3.so.0";
internal const string GObjLib = "libgobject-2.0.so.0";
internal const string GdkLib = "libgdk-3.so.0";
internal const string GlibLib = "libglib-2.0.so.0";
internal const string GioLib = "libgio-2.0.so.0";
internal const string GdkPixBuf = "libgdk_pixbuf-2.0.so.0";
internal const string X11Lib = "libX11.so";
}
}
111 changes: 58 additions & 53 deletions src/Chromely/Native/LinuxGtk3/LinuxGtk3Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,40 @@ public class LinuxGtk3Host : IChromelyNativeHost

private IWindowOptions _options;
private IntPtr _handle;
private IntPtr _gdkHandle;
private IntPtr _xid;
private bool _isInitialized;
private bool _debugging;

private RealizeCallback _onRealizedDelegate;
private SizeAllocateCallback _onSizeAllocateDelegate;
private ResizeCallback _onResizeDelegate;
private DestroyCallback _onDestroyDelegate;

private IntPtr _onRealizedSignal;
private IntPtr _onSizeAllocateSignal;
private IntPtr _onResizeSignal;
private IntPtr _onDestroySignal;

private GClosureNotify _onFreeNotify;

private XHandleXError _onHandleErrorDelegate;
private XHandleXIOError _onHandleIOErrorDelegate;

public LinuxGtk3Host()
{
_isInitialized = false;
_handle = IntPtr.Zero;
_gdkHandle = IntPtr.Zero;
_xid = IntPtr.Zero;

_onRealizedDelegate = new RealizeCallback(OnRealized);
_onSizeAllocateDelegate = new SizeAllocateCallback(OnSizeAllocate);
_onResizeDelegate = new ResizeCallback(OnResize);
_onDestroyDelegate = new DestroyCallback(OnDestroy);

_onFreeNotify = new GClosureNotify(FreeData);

_onHandleErrorDelegate = new XHandleXError(HandleError);
_onHandleIOErrorDelegate = new XHandleXIOError(HandleIOError);
}

public void CreateWindow(IWindowOptions config, bool debugging)
Expand Down Expand Up @@ -71,10 +94,12 @@ public void CreateWindow(IWindowOptions config, bool debugging)
break;
}

ConnectRealizeSignal(OnRealized, FreeData);
ConnectSizeAllocateSignal(OnSizeAllocate, FreeData);
ConnectResizeSignal(OnResize, FreeData);
ConnectDestroySignal(OnDestroy, FreeData);
ConnectRealizeSignal(_onRealizedDelegate, _onFreeNotify);
ConnectSizeAllocateSignal(_onSizeAllocateDelegate, _onFreeNotify);
ConnectResizeSignal(_onResizeDelegate, _onFreeNotify);
ConnectDestroySignal(_onDestroyDelegate, _onFreeNotify);

SetDefaultWindowVisual(_handle);

ShowWindow();
}
Expand All @@ -83,11 +108,11 @@ public IntPtr GetNativeHandle()
{
try
{
_gdkHandle = gtk_widget_get_window(_handle);
Utils.AssertNotNull("GetNativeHandle:gtk_widget_get_window", _gdkHandle);
_xid = gdk_x11_window_get_xid(_gdkHandle);
Utils.AssertNotNull("GetNativeHandle:gdk_x11_window_get_xid", _xid);
return _xid;
IntPtr gdkHandle = gtk_widget_get_window(_handle);
Utils.AssertNotNull("GetNativeHandle:gtk_widget_get_window", gdkHandle);
IntPtr xid = gdk_x11_window_get_xid(gdkHandle);
Utils.AssertNotNull("GetNativeHandle:gdk_x11_window_get_xid", xid);
return xid;
}
catch (Exception exception)
{
Expand Down Expand Up @@ -198,6 +223,9 @@ public void MessageBox(string message, int type)
private delegate bool DestroyCallback(IntPtr window, IntPtr data);
private delegate void QuitCallback();

private delegate short HandleErrorCallback(IntPtr display, ref XErrorEvent errorEven);
private delegate short HandleIOErrorCallback(IntPtr d);

private void Init(int argc, string[] argv)
{
try
Expand All @@ -219,8 +247,8 @@ private void InstallX11ErrorHandlers()
// Copied from CEF upstream cefclient.
// Install xlib error handlers so that the application won't be terminated
// on non-fatal errors. Must be done after initializing GTK.
XSetErrorHandler(HandleError);
XSetIOErrorHandler(HandleIOError);
XSetErrorHandler(_onHandleErrorDelegate);
XSetIOErrorHandler(_onHandleIOErrorDelegate);
}
catch (Exception exception)
{
Expand All @@ -233,17 +261,12 @@ private short HandleError(IntPtr display, ref XErrorEvent errorEvent)
{
try
{
if (this._debugging)
if (this._debugging && errorEvent.request_code > 0 && errorEvent.error_code > 0)
{
var builder = new StringBuilder();
builder.AppendLine("X error received: ");
builder.AppendLine("type " + errorEvent.type);
builder.AppendLine("serial " + errorEvent.serial);
builder.AppendLine("error_code " + errorEvent.error_code);
builder.AppendLine("request_code " + errorEvent.request_code);
builder.AppendLine("minor_code " + errorEvent.minor_code);

Logger.Instance.Log.Warn(builder.ToString());
var errorMsgSb = new StringBuilder(160);
XGetErrorText(display, errorEvent.error_code, errorMsgSb, errorMsgSb.Capacity);
var requestType = GetRequestType(errorEvent.request_code);
Logger.Instance.Log.Warn($"Request Code: {errorEvent.request_code}\tRequest Type: {requestType}\tX11 Error: {errorMsgSb.ToString()}");
}

return 0;
Expand Down Expand Up @@ -280,22 +303,6 @@ private IntPtr CreateNewWindow(int windowType)
return IntPtr.Zero;
}

private IntPtr GetGdkHandle()
{
try
{
_gdkHandle = gtk_widget_get_window(_handle);
Utils.AssertNotNull("GetGdkHandle", _gdkHandle);
return _gdkHandle;
}
catch (Exception exception)
{
Logger.Instance.Log.Error("Error in LinuxGtk3Host::GetGdkHandle");
Logger.Instance.Log.Error(exception);
}

return IntPtr.Zero;
}
private void SetWindowTitle(string title)
{
try
Expand Down Expand Up @@ -438,49 +445,47 @@ private bool OnDestroy(IntPtr window, IntPtr data)

private void ConnectRealizeSignal(RealizeCallback callback, GClosureNotify destroyData)
{
_onRealizedSignal = Marshal.GetFunctionPointerForDelegate(callback);

RegisterHandler(
"realize",
Marshal.GetFunctionPointerForDelegate(callback),
_onRealizedSignal,
destroyData,
GConnectFlags.GConnectAfter,
IntPtr.Zero);
}

private void ConnectSizeAllocateSignal(SizeAllocateCallback callback, GClosureNotify destroyData)
{
_onSizeAllocateSignal = Marshal.GetFunctionPointerForDelegate(callback);

RegisterHandler(
"size-allocate",
Marshal.GetFunctionPointerForDelegate(callback),
_onSizeAllocateSignal,
destroyData,
GConnectFlags.GConnectAfter,
IntPtr.Zero);
}

private void ConnectResizeSignal(ResizeCallback callback, GClosureNotify destroyData)
{
_onResizeSignal = Marshal.GetFunctionPointerForDelegate(callback);

RegisterHandler(
"configure-event",
Marshal.GetFunctionPointerForDelegate(callback),
_onResizeSignal,
destroyData,
GConnectFlags.GConnectAfter,
IntPtr.Zero);
}

private void ConnectDestroySignal(DestroyCallback callback, GClosureNotify destroyData)
{
RegisterHandler(
"delete-event",
Marshal.GetFunctionPointerForDelegate(callback),
destroyData,
GConnectFlags.GConnectAfter,
IntPtr.Zero);
}
_onDestroySignal = Marshal.GetFunctionPointerForDelegate(callback);

private void ConnectQuitSignal(QuitCallback callback, GClosureNotify destroyData)
{
RegisterHandler(
"destroy",
Marshal.GetFunctionPointerForDelegate(callback),
"delete-event",
_onDestroySignal,
destroyData,
GConnectFlags.GConnectAfter,
IntPtr.Zero);
Expand Down
Loading

0 comments on commit 63a6f50

Please sign in to comment.