Merge pull request #395 from daemitus/ime

This commit is contained in:
goaaats 2021-08-11 15:16:42 +02:00 committed by GitHub
commit 970cbc0c1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1139 additions and 3 deletions

View file

@ -11,6 +11,11 @@
<NoWarn>IDE0003</NoWarn>
</PropertyGroup>
<PropertyGroup Label="Documentation">
<DocumentationFile></DocumentationFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup Label="Build">
<OutputPath>$(AppData)\XIVLauncher\devPlugins\Dalamud.CorePlugin</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>

View file

@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using Dalamud.Interface.Windowing;
@ -11,6 +12,7 @@ namespace Dalamud.CorePlugin
/// </summary>
internal class PluginWindow : Window, IDisposable
{
[SuppressMessage("CodeQuality", "IDE0052:Remove unread private members", Justification = "This is a placeholder.")]
private readonly Dalamud dalamud;
/// <summary>

View file

@ -9,6 +9,7 @@ using Dalamud.Data;
using Dalamud.Game;
using Dalamud.Game.ClientState;
using Dalamud.Game.Command;
using Dalamud.Game.Gui.Internal;
using Dalamud.Game.Internal;
using Dalamud.Game.Network.Internal;
using Dalamud.Game.Text.SeStringHandling;
@ -100,6 +101,11 @@ namespace Dalamud
/// </summary>
internal InterfaceManager InterfaceManager { get; private set; }
/// <summary>
/// Gets the Input Method subsystem.
/// </summary>
internal DalamudIME IME { get; private set; }
/// <summary>
/// Gets ClientState subsystem.
/// </summary>
@ -293,6 +299,16 @@ namespace Dalamud
}
}
try
{
this.IME = new DalamudIME(this);
Log.Information("[T2] IME OK!");
}
catch (Exception e)
{
Log.Information(e, "Could not init IME.");
}
this.Data = new DataManager(this.StartInfo.Language, this.InterfaceManager);
try
{
@ -415,6 +431,11 @@ namespace Dalamud
{
this.hasDisposedPlugins = true;
// this must be done before unloading interface manager, in order to do rebuild
// the correct cascaded WndProc (IME -> RawDX11Scene -> Game). Otherwise the game
// will not receive any windows messages
this.IME?.Dispose();
// this must be done before unloading plugins, or it can cause a race condition
// due to rendering happening on another thread, where a plugin might receive
// a render call after it has been disposed, which can crash if it attempts to

View file

@ -51,8 +51,9 @@
</PropertyGroup>
<PropertyGroup Label="Warnings">
<NoWarn>IDE0003;IDE0044;IDE1006;CS1591;CS1701;CS1702</NoWarn>
<NoWarn>IDE0003;IDE0044;IDE1006;CA1822;CS1591;CS1701;CS1702</NoWarn>
<!-- IDE1006 - Naming violation -->
<!-- CA1822 - Can be marked as static -->
<!-- CS1591 - Missing XML comment for publicly visible type or member -->
<!-- CS1701 - Runtime policy may be needed -->
<!-- CS1702 - Runtime policy may be needed -->

View file

@ -8,6 +8,7 @@ using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Hooking;
using Dalamud.Interface;
using Dalamud.Utility;
using ImGuiNET;
using Serilog;
namespace Dalamud.Game.Gui
@ -32,6 +33,7 @@ namespace Dalamud.Game.Gui
private readonly Hook<HandleItemOutDelegate> handleItemOutHook;
private readonly Hook<HandleActionHoverDelegate> handleActionHoverHook;
private readonly Hook<HandleActionOutDelegate> handleActionOutHook;
private readonly Hook<HandleImmDelegate> handleImmHook;
private readonly Hook<ToggleUiHideDelegate> toggleUiHideHook;
private GetUIMapObjectDelegate getUIMapObject;
@ -57,6 +59,7 @@ namespace Dalamud.Game.Gui
Log.Verbose($"SetGlobalBgm address 0x{this.address.SetGlobalBgm.ToInt64():X}");
Log.Verbose($"HandleItemHover address 0x{this.address.HandleItemHover.ToInt64():X}");
Log.Verbose($"HandleItemOut address 0x{this.address.HandleItemOut.ToInt64():X}");
Log.Verbose($"HandleImm address 0x{this.address.HandleImm.ToInt64():X}");
Log.Verbose($"GetUIObject address 0x{this.address.GetUIObject.ToInt64():X}");
Log.Verbose($"GetAgentModule address 0x{this.address.GetAgentModule.ToInt64():X}");
@ -65,13 +68,15 @@ namespace Dalamud.Game.Gui
this.Toast = new ToastGui(scanner, dalamud);
this.setGlobalBgmHook = new Hook<SetGlobalBgmDelegate>(this.address.SetGlobalBgm, this.HandleSetGlobalBgmDetour);
this.handleItemHoverHook = new Hook<HandleItemHoverDelegate>(this.address.HandleItemHover, this.HandleItemHoverDetour);
this.handleItemHoverHook = new Hook<HandleItemHoverDelegate>(this.address.HandleItemHover, this.HandleItemHoverDetour);
this.handleItemOutHook = new Hook<HandleItemOutDelegate>(this.address.HandleItemOut, this.HandleItemOutDetour);
this.handleActionHoverHook = new Hook<HandleActionHoverDelegate>(this.address.HandleActionHover, this.HandleActionHoverDetour);
this.handleActionOutHook = new Hook<HandleActionOutDelegate>(this.address.HandleActionOut, this.HandleActionOutDetour);
this.handleImmHook = new Hook<HandleImmDelegate>(this.address.HandleImm, this.HandleImmDetour);
this.getUIObject = Marshal.GetDelegateForFunctionPointer<GetUIObjectDelegate>(this.address.GetUIObject);
this.getMatrixSingleton = Marshal.GetDelegateForFunctionPointer<GetMatrixSingletonDelegate>(this.address.GetMatrixSingleton);
@ -135,6 +140,9 @@ namespace Dalamud.Game.Gui
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate IntPtr HandleActionOutDelegate(IntPtr agentActionDetail, IntPtr a2, IntPtr a3, int a4);
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate char HandleImmDelegate(IntPtr framework, char a2, byte a3);
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
private delegate IntPtr ToggleUiHideDelegate(IntPtr thisPtr, byte unknownByte);
@ -494,6 +502,7 @@ namespace Dalamud.Game.Gui
this.setGlobalBgmHook.Enable();
this.handleItemHoverHook.Enable();
this.handleItemOutHook.Enable();
this.handleImmHook.Enable();
this.toggleUiHideHook.Enable();
this.handleActionHoverHook.Enable();
this.handleActionOutHook.Enable();
@ -510,6 +519,7 @@ namespace Dalamud.Game.Gui
this.setGlobalBgmHook.Dispose();
this.handleItemHoverHook.Dispose();
this.handleItemOutHook.Dispose();
this.handleImmHook.Dispose();
this.toggleUiHideHook.Dispose();
this.handleActionHoverHook.Dispose();
this.handleActionOutHook.Dispose();
@ -641,5 +651,13 @@ namespace Dalamud.Game.Gui
return this.toggleUiHideHook.Original(thisPtr, unknownByte);
}
private char HandleImmDetour(IntPtr framework, char a2, byte a3)
{
var result = this.handleImmHook.Original(framework, a2, a3);
return ImGui.GetIO().WantTextInput
? (char)0
: result;
}
}
}

View file

@ -52,6 +52,11 @@ namespace Dalamud.Game.Gui
/// </summary>
public IntPtr HandleActionOut { get; private set; }
/// <summary>
/// Gets the address of the native HandleImm method.
/// </summary>
public IntPtr HandleImm { get; private set; }
/// <summary>
/// Gets the address of the native GetUIObject method.
/// </summary>
@ -100,6 +105,7 @@ namespace Dalamud.Game.Gui
this.HandleItemOut = sig.ScanText("48 89 5C 24 ?? 57 48 83 EC 20 48 8B FA 48 8B D9 4D");
this.HandleActionHover = sig.ScanText("E8 ?? ?? ?? ?? E9 ?? ?? ?? ?? 83 F8 0F");
this.HandleActionOut = sig.ScanText("48 89 5C 24 ?? 57 48 83 EC 20 48 8B DA 48 8B F9 4D 85 C0 74 1F");
this.HandleImm = sig.ScanText("E8 ?? ?? ?? ?? 84 C0 75 10 48 83 FF 09");
this.GetUIObject = sig.ScanText("E8 ?? ?? ?? ?? 48 8B C8 48 8B 10 FF 52 40 80 88 ?? ?? ?? ?? 01 E9");
this.GetMatrixSingleton = sig.ScanText("E8 ?? ?? ?? ?? 48 8D 4C 24 ?? 48 89 4c 24 ?? 4C 8D 4D ?? 4C 8D 44 24 ??");
this.ScreenToWorld = sig.ScanText("48 83 EC 48 48 8B 05 ?? ?? ?? ?? 4D 8B D1");

View file

@ -0,0 +1,234 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Dalamud.Logging.Internal;
using ImGuiNET;
using static Dalamud.NativeFunctions;
namespace Dalamud.Game.Gui.Internal
{
/// <summary>
/// This class handles IME for non-English users.
/// </summary>
internal class DalamudIME : IDisposable
{
private static readonly ModuleLog Log = new("IME");
private readonly Dalamud dalamud;
private IntPtr interfaceHandle;
private IntPtr wndProcPtr;
private IntPtr oldWndProcPtr;
private WndProcDelegate wndProcDelegate;
/// <summary>
/// Initializes a new instance of the <see cref="DalamudIME"/> class.
/// </summary>
/// <param name="dalamud">The Dalamud instance.</param>
internal DalamudIME(Dalamud dalamud)
{
this.dalamud = dalamud;
}
private delegate long WndProcDelegate(IntPtr hWnd, uint msg, ulong wParam, long lParam);
/// <summary>
/// Gets a value indicating whether the module is enabled.
/// </summary>
internal bool IsEnabled { get; private set; }
/// <summary>
/// Gets the imm candidates.
/// </summary>
internal List<string> ImmCand { get; private set; } = new();
/// <summary>
/// Gets the imm component.
/// </summary>
internal string ImmComp { get; private set; } = string.Empty;
/// <inheritdoc/>
public void Dispose()
{
if (this.oldWndProcPtr != IntPtr.Zero)
{
SetWindowLongPtrW(this.interfaceHandle, WindowLongType.WndProc, this.oldWndProcPtr);
this.oldWndProcPtr = IntPtr.Zero;
}
}
/// <summary>
/// Enables the IME module.
/// </summary>
internal void Enable()
{
try
{
this.wndProcDelegate = this.WndProcDetour;
this.interfaceHandle = this.dalamud.InterfaceManager.WindowHandlePtr;
this.wndProcPtr = Marshal.GetFunctionPointerForDelegate(this.wndProcDelegate);
this.oldWndProcPtr = SetWindowLongPtrW(this.interfaceHandle, WindowLongType.WndProc, this.wndProcPtr);
this.IsEnabled = true;
Log.Information("Enabled!");
}
catch (Exception ex)
{
Log.Information(ex, "Enable failed");
}
}
private void ToggleWindow(bool visible)
{
if (visible)
this.dalamud.DalamudUi.OpenIMEWindow();
else
this.dalamud.DalamudUi.CloseIMEWindow();
}
private long WndProcDetour(IntPtr hWnd, uint msg, ulong wParam, long lParam)
{
try
{
if (hWnd == this.interfaceHandle && ImGui.GetCurrentContext() != IntPtr.Zero && ImGui.GetIO().WantTextInput)
{
var io = ImGui.GetIO();
var wmsg = (WindowsMessage)msg;
switch (wmsg)
{
case WindowsMessage.WM_IME_NOTIFY:
switch ((IMECommand)wParam)
{
case IMECommand.ChangeCandidate:
this.ToggleWindow(true);
if (hWnd == IntPtr.Zero)
return 0;
var hIMC = ImmGetContext(hWnd);
if (hIMC == IntPtr.Zero)
return 0;
var size = ImmGetCandidateListW(hIMC, 0, IntPtr.Zero, 0);
if (size > 0)
{
var candlistPtr = Marshal.AllocHGlobal((int)size);
size = ImmGetCandidateListW(hIMC, 0, candlistPtr, (uint)size);
var candlist = Marshal.PtrToStructure<CandidateList>(candlistPtr);
var pageSize = candlist.PageSize;
var candCount = candlist.Count;
if (pageSize > 0 && candCount > 1)
{
var dwOffsets = new int[candCount];
for (var i = 0; i < candCount; i++)
dwOffsets[i] = Marshal.ReadInt32(candlistPtr + ((i + 6) * sizeof(int)));
var pageStart = candlist.PageStart;
// var pageEnd = pageStart + pageSize;
var cand = new string[pageSize];
this.ImmCand.Clear();
for (var i = 0; i < pageSize; i++)
{
var offStart = dwOffsets[i + pageStart];
var offEnd = i + pageStart + 1 < candCount ? dwOffsets[i + pageStart + 1] : size;
var pStrStart = candlistPtr + (int)offStart;
var pStrEnd = candlistPtr + (int)offEnd;
var len = (int)(pStrEnd.ToInt64() - pStrStart.ToInt64());
if (len > 0)
{
var candBytes = new byte[len];
Marshal.Copy(pStrStart, candBytes, 0, len);
var candStr = Encoding.Unicode.GetString(candBytes);
cand[i] = candStr;
this.ImmCand.Add(candStr);
}
}
}
Marshal.FreeHGlobal(candlistPtr);
}
break;
case IMECommand.OpenCandidate:
this.ToggleWindow(true);
this.ImmCand.Clear();
break;
case IMECommand.CloseCandidate:
this.ToggleWindow(false);
this.ImmCand.Clear();
break;
default:
break;
}
break;
case WindowsMessage.WM_IME_COMPOSITION:
if ((lParam & (long)IMEComposition.ResultStr) > 0)
{
var hIMC = ImmGetContext(hWnd);
if (hIMC == IntPtr.Zero)
return 0;
var dwSize = ImmGetCompositionStringW(hIMC, IMEComposition.ResultStr, IntPtr.Zero, 0);
var unmanagedPointer = Marshal.AllocHGlobal((int)dwSize);
ImmGetCompositionStringW(hIMC, IMEComposition.ResultStr, unmanagedPointer, (uint)dwSize);
var bytes = new byte[dwSize];
Marshal.Copy(unmanagedPointer, bytes, 0, (int)dwSize);
Marshal.FreeHGlobal(unmanagedPointer);
var lpstr = Encoding.Unicode.GetString(bytes);
io.AddInputCharactersUTF8(lpstr);
this.ImmComp = string.Empty;
this.ImmCand.Clear();
this.ToggleWindow(false);
}
if (((long)(IMEComposition.CompStr | IMEComposition.CompAttr | IMEComposition.CompClause |
IMEComposition.CompReadAttr | IMEComposition.CompReadClause | IMEComposition.CompReadStr) & lParam) > 0)
{
var hIMC = ImmGetContext(hWnd);
if (hIMC == IntPtr.Zero)
return 0;
var dwSize = ImmGetCompositionStringW(hIMC, IMEComposition.CompStr, IntPtr.Zero, 0);
var unmanagedPointer = Marshal.AllocHGlobal((int)dwSize);
ImmGetCompositionStringW(hIMC, IMEComposition.CompStr, unmanagedPointer, (uint)dwSize);
var bytes = new byte[dwSize];
Marshal.Copy(unmanagedPointer, bytes, 0, (int)dwSize);
Marshal.FreeHGlobal(unmanagedPointer);
var lpstr = Encoding.Unicode.GetString(bytes);
this.ImmComp = lpstr;
if (lpstr == string.Empty)
this.ToggleWindow(false);
}
break;
default:
break;
}
}
}
catch (Exception ex)
{
Log.Error(ex, "Prevented a crash in an IME hook");
}
return CallWindowProcW(this.oldWndProcPtr, hWnd, msg, wParam, lParam);
}
}
}

View file

@ -84,6 +84,12 @@ namespace Dalamud.Interface.Internal
ShowInHelp = false,
});
this.dalamud.CommandManager.AddHandler("/xlime", new CommandInfo(this.OnDebugDrawIMEPanel)
{
HelpMessage = Loc.Localize("DalamudIMEPanelHelp", "Draw IME panel"),
ShowInHelp = false,
});
this.dalamud.CommandManager.AddHandler("/xllog", new CommandInfo(this.OnOpenLog)
{
HelpMessage = Loc.Localize("DalamudDevLogHelp", "Open dev log DEBUG"),
@ -245,6 +251,11 @@ namespace Dalamud.Interface.Internal
this.dalamud.DalamudUi.OpenDataWindow(arguments);
}
private void OnDebugDrawIMEPanel(string command, string arguments)
{
this.dalamud.DalamudUi.OpenIMEWindow();
}
private void OnOpenLog(string command, string arguments)
{
this.dalamud.DalamudUi.ToggleLogWindow();

View file

@ -32,6 +32,7 @@ namespace Dalamud.Interface.Internal
private readonly CreditsWindow creditsWindow;
private readonly DataWindow dataWindow;
private readonly GamepadModeNotifierWindow gamepadModeNotifierWindow;
private readonly IMEWindow imeWindow;
private readonly ConsoleWindow consoleWindow;
private readonly PluginStatWindow pluginStatWindow;
private readonly PluginInstallerWindow pluginWindow;
@ -64,6 +65,7 @@ namespace Dalamud.Interface.Internal
this.creditsWindow = new CreditsWindow(dalamud) { IsOpen = false };
this.dataWindow = new DataWindow(dalamud) { IsOpen = false };
this.gamepadModeNotifierWindow = new GamepadModeNotifierWindow();
this.imeWindow = new IMEWindow(dalamud);
this.consoleWindow = new ConsoleWindow(dalamud) { IsOpen = this.dalamud.Configuration.LogOpenAtStartup };
this.pluginStatWindow = new PluginStatWindow(dalamud) { IsOpen = false };
this.pluginWindow = new PluginInstallerWindow(dalamud) { IsOpen = false };
@ -77,6 +79,7 @@ namespace Dalamud.Interface.Internal
this.windowSystem.AddWindow(this.creditsWindow);
this.windowSystem.AddWindow(this.dataWindow);
this.windowSystem.AddWindow(this.gamepadModeNotifierWindow);
this.windowSystem.AddWindow(this.imeWindow);
this.windowSystem.AddWindow(this.consoleWindow);
this.windowSystem.AddWindow(this.pluginStatWindow);
this.windowSystem.AddWindow(this.pluginWindow);
@ -160,6 +163,11 @@ namespace Dalamud.Interface.Internal
/// </summary>
public void OpenGamepadModeNotifierWindow() => this.gamepadModeNotifierWindow.IsOpen = true;
/// <summary>
/// Opens the <see cref="IMEWindow"/>.
/// </summary>
public void OpenIMEWindow() => this.imeWindow.IsOpen = true;
/// <summary>
/// Opens the <see cref="ConsoleWindow"/>.
/// </summary>
@ -192,6 +200,15 @@ namespace Dalamud.Interface.Internal
#endregion
#region Close
/// <summary>
/// Closes the <see cref="IMEWindow"/>.
/// </summary>
public void CloseIMEWindow() => this.imeWindow.IsOpen = false;
#endregion
#region Toggle
/// <summary>
@ -237,6 +254,11 @@ namespace Dalamud.Interface.Internal
/// </summary>
public void ToggleGamepadModeNotifierWindow() => this.gamepadModeNotifierWindow.Toggle();
/// <summary>
/// Toggles the <see cref="IMEWindow"/>.
/// </summary>
public void ToggleIMEWindow() => this.imeWindow.Toggle();
/// <summary>
/// Toggles the <see cref="ConsoleWindow"/>.
/// </summary>

View file

@ -397,6 +397,8 @@ namespace Dalamud.Interface.Internal
ImGuiHelpers.MainViewport = ImGui.GetMainViewport();
Log.Information("[IM] Scene & ImGui setup OK!");
this.dalamud.IME.Enable();
}
// Process information needed by ImGuiHelpers each frame.

View file

@ -0,0 +1,46 @@
using System.Numerics;
using Dalamud.Game.Gui.Internal;
using Dalamud.Interface.Windowing;
using ImGuiNET;
namespace Dalamud.Interface.Internal.Windows
{
/// <summary>
/// A window for displaying IME details.
/// </summary>
internal class IMEWindow : Window
{
private readonly DalamudIME dalamudIME;
/// <summary>
/// Initializes a new instance of the <see cref="IMEWindow"/> class.
/// </summary>
/// <param name="dalamud">The Dalamud instance.</param>
public IMEWindow(Dalamud dalamud)
: base("Dalamud IME", ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoFocusOnAppearing)
{
this.dalamudIME = dalamud.IME;
this.Size = new Vector2(100, 200);
this.SizeCondition = ImGuiCond.FirstUseEver;
}
/// <inheritdoc/>
public override void Draw()
{
if (this.dalamudIME == null || !this.dalamudIME.IsEnabled)
{
ImGui.Text("IME unavailable.");
return;
}
ImGui.Text(this.dalamudIME.ImmComp);
ImGui.Separator();
for (var i = 0; i < this.dalamudIME.ImmCand.Count; i++)
{
ImGui.Text($"{i + 1}. {this.dalamudIME.ImmCand[i]}");
}
}
}
}

View file

@ -67,7 +67,7 @@ namespace Dalamud.Interface.Internal.Windows
private OperationStatus installStatus = OperationStatus.Idle;
private OperationStatus updateStatus = OperationStatus.Idle;
private List<int> openPluginCollapsibles = new List<int>();
private List<int> openPluginCollapsibles = new();
/// <summary>
/// Initializes a new instance of the <see cref="PluginInstallerWindow"/> class.

View file

@ -48,6 +48,92 @@ namespace Dalamud
TimerNoFG = 12,
}
/// <summary>
/// IDC_* from winuser.
/// </summary>
public enum CursorType
{
/// <summary>
/// Standard arrow and small hourglass.
/// </summary>
AppStarting = 32650,
/// <summary>
/// Standard arrow.
/// </summary>
Arrow = 32512,
/// <summary>
/// Crosshair.
/// </summary>
Cross = 32515,
/// <summary>
/// Hand.
/// </summary>
Hand = 32649,
/// <summary>
/// Arrow and question mark.
/// </summary>
Help = 32651,
/// <summary>
/// I-beam.
/// </summary>
IBeam = 32513,
/// <summary>
/// Obsolete for applications marked version 4.0 or later.
/// </summary>
Icon = 32641,
/// <summary>
/// Slashed circle.
/// </summary>
No = 32648,
/// <summary>
/// Obsolete for applications marked version 4.0 or later.Use IDC_SIZEALL.
/// </summary>
Size = 32640,
/// <summary>
/// Four-pointed arrow pointing north, south, east, and west.
/// </summary>
SizeAll = 32646,
/// <summary>
/// Double-pointed arrow pointing northeast and southwest.
/// </summary>
SizeNeSw = 32643,
/// <summary>
/// Double-pointed arrow pointing north and south.
/// </summary>
SizeNS = 32645,
/// <summary>
/// Double-pointed arrow pointing northwest and southeast.
/// </summary>
SizeNwSe = 32642,
/// <summary>
/// Double-pointed arrow pointing west and east.
/// </summary>
SizeWE = 32644,
/// <summary>
/// Vertical arrow.
/// </summary>
UpArrow = 32516,
/// <summary>
/// Hourglass.
/// </summary>
Wait = 32514,
}
/// <summary>
/// MB_* from winuser.
/// </summary>
@ -235,6 +321,329 @@ namespace Dalamud
ServiceNotification = 0x200000,
}
/// <summary>
/// GWL_* from winuser.
/// </summary>
public enum WindowLongType
{
/// <summary>
/// Sets a new extended window style.
/// </summary>
ExStyle = -20,
/// <summary>
/// Sets a new application instance handle.
/// </summary>
HInstance = -6,
/// <summary>
/// Sets a new identifier of the child window.The window cannot be a top-level window.
/// </summary>
Id = -12,
/// <summary>
/// Sets a new window style.
/// </summary>
Style = -16,
/// <summary>
/// Sets the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero.
/// </summary>
UserData = -21,
/// <summary>
/// Sets a new address for the window procedure.
/// </summary>
WndProc = -4,
// The following values are also available when the hWnd parameter identifies a dialog box.
// /// <summary>
// /// Sets the new pointer to the dialog box procedure.
// /// </summary>
// DWLP_DLGPROC = DWLP_MSGRESULT + sizeof(LRESULT),
/// <summary>
/// Sets the return value of a message processed in the dialog box procedure.
/// </summary>
MsgResult = 0,
// /// <summary>
// /// Sets new extra information that is private to the application, such as handles or pointers.
// /// </summary>
// DWLP_USER = DWLP_DLGPROC + sizeof(DLGPROC),
}
/// <summary>
/// WM_* from winuser.
/// These are spread throughout multiple files, find the documentation manually if you need it.
/// https://gist.github.com/amgine/2395987.
/// </summary>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:Enumeration items should be documented", Justification = "No documentation available.")]
public enum WindowsMessage
{
WM_NULL = 0x0000,
WM_CREATE = 0x0001,
WM_DESTROY = 0x0002,
WM_MOVE = 0x0003,
WM_SIZE = 0x0005,
WM_ACTIVATE = 0x0006,
WM_SETFOCUS = 0x0007,
WM_KILLFOCUS = 0x0008,
WM_ENABLE = 0x000A,
WM_SETREDRAW = 0x000B,
WM_SETTEXT = 0x000C,
WM_GETTEXT = 0x000D,
WM_GETTEXTLENGTH = 0x000E,
WM_PAINT = 0x000F,
WM_CLOSE = 0x0010,
WM_QUERYENDSESSION = 0x0011,
WM_QUERYOPEN = 0x0013,
WM_ENDSESSION = 0x0016,
WM_QUIT = 0x0012,
WM_ERASEBKGND = 0x0014,
WM_SYSCOLORCHANGE = 0x0015,
WM_SHOWWINDOW = 0x0018,
WM_WININICHANGE = 0x001A,
WM_SETTINGCHANGE = WM_WININICHANGE,
WM_DEVMODECHANGE = 0x001B,
WM_ACTIVATEAPP = 0x001C,
WM_FONTCHANGE = 0x001D,
WM_TIMECHANGE = 0x001E,
WM_CANCELMODE = 0x001F,
WM_SETCURSOR = 0x0020,
WM_MOUSEACTIVATE = 0x0021,
WM_CHILDACTIVATE = 0x0022,
WM_QUEUESYNC = 0x0023,
WM_GETMINMAXINFO = 0x0024,
WM_PAINTICON = 0x0026,
WM_ICONERASEBKGND = 0x0027,
WM_NEXTDLGCTL = 0x0028,
WM_SPOOLERSTATUS = 0x002A,
WM_DRAWITEM = 0x002B,
WM_MEASUREITEM = 0x002C,
WM_DELETEITEM = 0x002D,
WM_VKEYTOITEM = 0x002E,
WM_CHARTOITEM = 0x002F,
WM_SETFONT = 0x0030,
WM_GETFONT = 0x0031,
WM_SETHOTKEY = 0x0032,
WM_GETHOTKEY = 0x0033,
WM_QUERYDRAGICON = 0x0037,
WM_COMPAREITEM = 0x0039,
WM_GETOBJECT = 0x003D,
WM_COMPACTING = 0x0041,
WM_COMMNOTIFY = 0x0044,
WM_WINDOWPOSCHANGING = 0x0046,
WM_WINDOWPOSCHANGED = 0x0047,
WM_POWER = 0x0048,
WM_COPYDATA = 0x004A,
WM_CANCELJOURNAL = 0x004B,
WM_NOTIFY = 0x004E,
WM_INPUTLANGCHANGEREQUEST = 0x0050,
WM_INPUTLANGCHANGE = 0x0051,
WM_TCARD = 0x0052,
WM_HELP = 0x0053,
WM_USERCHANGED = 0x0054,
WM_NOTIFYFORMAT = 0x0055,
WM_CONTEXTMENU = 0x007B,
WM_STYLECHANGING = 0x007C,
WM_STYLECHANGED = 0x007D,
WM_DISPLAYCHANGE = 0x007E,
WM_GETICON = 0x007F,
WM_SETICON = 0x0080,
WM_NCCREATE = 0x0081,
WM_NCDESTROY = 0x0082,
WM_NCCALCSIZE = 0x0083,
WM_NCHITTEST = 0x0084,
WM_NCPAINT = 0x0085,
WM_NCACTIVATE = 0x0086,
WM_GETDLGCODE = 0x0087,
WM_SYNCPAINT = 0x0088,
WM_NCMOUSEMOVE = 0x00A0,
WM_NCLBUTTONDOWN = 0x00A1,
WM_NCLBUTTONUP = 0x00A2,
WM_NCLBUTTONDBLCLK = 0x00A3,
WM_NCRBUTTONDOWN = 0x00A4,
WM_NCRBUTTONUP = 0x00A5,
WM_NCRBUTTONDBLCLK = 0x00A6,
WM_NCMBUTTONDOWN = 0x00A7,
WM_NCMBUTTONUP = 0x00A8,
WM_NCMBUTTONDBLCLK = 0x00A9,
WM_NCXBUTTONDOWN = 0x00AB,
WM_NCXBUTTONUP = 0x00AC,
WM_NCXBUTTONDBLCLK = 0x00AD,
WM_INPUT_DEVICE_CHANGE = 0x00FE,
WM_INPUT = 0x00FF,
WM_KEYFIRST = WM_KEYDOWN,
WM_KEYDOWN = 0x0100,
WM_KEYUP = 0x0101,
WM_CHAR = 0x0102,
WM_DEADCHAR = 0x0103,
WM_SYSKEYDOWN = 0x0104,
WM_SYSKEYUP = 0x0105,
WM_SYSCHAR = 0x0106,
WM_SYSDEADCHAR = 0x0107,
WM_UNICHAR = 0x0109,
WM_KEYLAST = WM_UNICHAR,
WM_IME_STARTCOMPOSITION = 0x010D,
WM_IME_ENDCOMPOSITION = 0x010E,
WM_IME_COMPOSITION = 0x010F,
WM_IME_KEYLAST = WM_IME_COMPOSITION,
WM_INITDIALOG = 0x0110,
WM_COMMAND = 0x0111,
WM_SYSCOMMAND = 0x0112,
WM_TIMER = 0x0113,
WM_HSCROLL = 0x0114,
WM_VSCROLL = 0x0115,
WM_INITMENU = 0x0116,
WM_INITMENUPOPUP = 0x0117,
WM_MENUSELECT = 0x011F,
WM_MENUCHAR = 0x0120,
WM_ENTERIDLE = 0x0121,
WM_MENURBUTTONUP = 0x0122,
WM_MENUDRAG = 0x0123,
WM_MENUGETOBJECT = 0x0124,
WM_UNINITMENUPOPUP = 0x0125,
WM_MENUCOMMAND = 0x0126,
WM_CHANGEUISTATE = 0x0127,
WM_UPDATEUISTATE = 0x0128,
WM_QUERYUISTATE = 0x0129,
WM_CTLCOLORMSGBOX = 0x0132,
WM_CTLCOLOREDIT = 0x0133,
WM_CTLCOLORLISTBOX = 0x0134,
WM_CTLCOLORBTN = 0x0135,
WM_CTLCOLORDLG = 0x0136,
WM_CTLCOLORSCROLLBAR = 0x0137,
WM_CTLCOLORSTATIC = 0x0138,
MN_GETHMENU = 0x01E1,
WM_MOUSEFIRST = WM_MOUSEMOVE,
WM_MOUSEMOVE = 0x0200,
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_LBUTTONDBLCLK = 0x0203,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_RBUTTONDBLCLK = 0x0206,
WM_MBUTTONDOWN = 0x0207,
WM_MBUTTONUP = 0x0208,
WM_MBUTTONDBLCLK = 0x0209,
WM_MOUSEWHEEL = 0x020A,
WM_XBUTTONDOWN = 0x020B,
WM_XBUTTONUP = 0x020C,
WM_XBUTTONDBLCLK = 0x020D,
WM_MOUSEHWHEEL = 0x020E,
WM_PARENTNOTIFY = 0x0210,
WM_ENTERMENULOOP = 0x0211,
WM_EXITMENULOOP = 0x0212,
WM_NEXTMENU = 0x0213,
WM_SIZING = 0x0214,
WM_CAPTURECHANGED = 0x0215,
WM_MOVING = 0x0216,
WM_POWERBROADCAST = 0x0218,
WM_DEVICECHANGE = 0x0219,
WM_MDICREATE = 0x0220,
WM_MDIDESTROY = 0x0221,
WM_MDIACTIVATE = 0x0222,
WM_MDIRESTORE = 0x0223,
WM_MDINEXT = 0x0224,
WM_MDIMAXIMIZE = 0x0225,
WM_MDITILE = 0x0226,
WM_MDICASCADE = 0x0227,
WM_MDIICONARRANGE = 0x0228,
WM_MDIGETACTIVE = 0x0229,
WM_MDISETMENU = 0x0230,
WM_ENTERSIZEMOVE = 0x0231,
WM_EXITSIZEMOVE = 0x0232,
WM_DROPFILES = 0x0233,
WM_MDIREFRESHMENU = 0x0234,
WM_IME_SETCONTEXT = 0x0281,
WM_IME_NOTIFY = 0x0282,
WM_IME_CONTROL = 0x0283,
WM_IME_COMPOSITIONFULL = 0x0284,
WM_IME_SELECT = 0x0285,
WM_IME_CHAR = 0x0286,
WM_IME_REQUEST = 0x0288,
WM_IME_KEYDOWN = 0x0290,
WM_IME_KEYUP = 0x0291,
WM_MOUSEHOVER = 0x02A1,
WM_MOUSELEAVE = 0x02A3,
WM_NCMOUSEHOVER = 0x02A0,
WM_NCMOUSELEAVE = 0x02A2,
WM_WTSSESSION_CHANGE = 0x02B1,
WM_TABLET_FIRST = 0x02c0,
WM_TABLET_LAST = 0x02df,
WM_CUT = 0x0300,
WM_COPY = 0x0301,
WM_PASTE = 0x0302,
WM_CLEAR = 0x0303,
WM_UNDO = 0x0304,
WM_RENDERFORMAT = 0x0305,
WM_RENDERALLFORMATS = 0x0306,
WM_DESTROYCLIPBOARD = 0x0307,
WM_DRAWCLIPBOARD = 0x0308,
WM_PAINTCLIPBOARD = 0x0309,
WM_VSCROLLCLIPBOARD = 0x030A,
WM_SIZECLIPBOARD = 0x030B,
WM_ASKCBFORMATNAME = 0x030C,
WM_CHANGECBCHAIN = 0x030D,
WM_HSCROLLCLIPBOARD = 0x030E,
WM_QUERYNEWPALETTE = 0x030F,
WM_PALETTEISCHANGING = 0x0310,
WM_PALETTECHANGED = 0x0311,
WM_HOTKEY = 0x0312,
WM_PRINT = 0x0317,
WM_PRINTCLIENT = 0x0318,
WM_APPCOMMAND = 0x0319,
WM_THEMECHANGED = 0x031A,
WM_CLIPBOARDUPDATE = 0x031D,
WM_DWMCOMPOSITIONCHANGED = 0x031E,
WM_DWMNCRENDERINGCHANGED = 0x031F,
WM_DWMCOLORIZATIONCOLORCHANGED = 0x0320,
WM_DWMWINDOWMAXIMIZEDCHANGE = 0x0321,
WM_GETTITLEBARINFOEX = 0x033F,
WM_HANDHELDFIRST = 0x0358,
WM_HANDHELDLAST = 0x035F,
WM_AFXFIRST = 0x0360,
WM_AFXLAST = 0x037F,
WM_PENWINFIRST = 0x0380,
WM_PENWINLAST = 0x038F,
WM_APP = 0x8000,
WM_USER = 0x0400,
WM_REFLECT = WM_USER + 0x1C00,
}
/// <summary>
/// Returns true if the current application has focus, false otherwise.
/// </summary>
@ -254,6 +663,38 @@ namespace Dalamud
return activeProcId == Environment.ProcessId;
}
/// <summary>
/// Passes message information to the specified window procedure.
/// </summary>
/// <param name="lpPrevWndFunc">
/// The previous window procedure. If this value is obtained by calling the GetWindowLong function with the nIndex parameter set to
/// GWL_WNDPROC or DWL_DLGPROC, it is actually either the address of a window or dialog box procedure, or a special internal value
/// meaningful only to CallWindowProc.
/// </param>
/// <param name="hWnd">
/// A handle to the window procedure to receive the message.
/// </param>
/// <param name="msg">
/// The message.
/// </param>
/// <param name="wParam">
/// Additional message-specific information. The contents of this parameter depend on the value of the Msg parameter.
/// </param>
/// <param name="lParam">
/// More additional message-specific information. The contents of this parameter depend on the value of the Msg parameter.
/// </param>
/// <returns>
/// Use the CallWindowProc function for window subclassing. Usually, all windows with the same class share one window procedure. A
/// subclass is a window or set of windows with the same class whose messages are intercepted and processed by another window procedure
/// (or procedures) before being passed to the window procedure of the class.
/// The SetWindowLong function creates the subclass by changing the window procedure associated with a particular window, causing the
/// system to call the new window procedure instead of the previous one.An application must pass any messages not processed by the new
/// window procedure to the previous window procedure by calling CallWindowProc.This allows the application to create a chain of window
/// procedures.
/// </returns>
[DllImport("user32.dll")]
public static extern long CallWindowProcW(IntPtr lpPrevWndFunc, IntPtr hWnd, uint msg, ulong wParam, long lParam);
/// <summary>
/// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-flashwindowex.
/// Flashes the specified window. It does not change the active state of the window.
@ -326,6 +767,31 @@ namespace Dalamud
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int MessageBoxW(IntPtr hWnd, string text, string caption, MessageBoxType type);
/// <summary>
/// Changes an attribute of the specified window. The function also sets a value at the specified offset in the extra window memory.
/// </summary>
/// <param name="hWnd">
/// A handle to the window and, indirectly, the class to which the window belongs. The SetWindowLongPtr function fails if the
/// process that owns the window specified by the hWnd parameter is at a higher process privilege in the UIPI hierarchy than the
/// process the calling thread resides in.
/// </param>
/// <param name="nIndex">
/// The zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window
/// memory, minus the size of a LONG_PTR. To set any other value, specify one of the values.
/// </param>
/// <param name="dwNewLong">
/// The replacement value.
/// </param>
/// <returns>
/// If the function succeeds, the return value is the previous value of the specified offset. If the function fails, the return
/// value is zero.To get extended error information, call GetLastError. If the previous value is zero and the function succeeds,
/// the return value is zero, but the function does not clear the last error information. To determine success or failure, clear
/// the last error information by calling SetLastError with 0, then call SetWindowLongPtr.Function failure will be indicated by
/// a return value of zero and a GetLastError result that is nonzero.
/// </returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowLongPtrW(IntPtr hWnd, WindowLongType nIndex, IntPtr dwNewLong);
/// <summary>
/// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-flashwinfo.
/// Contains the flash status for a window and the number of times the system should flash the window.
@ -361,6 +827,308 @@ namespace Dalamud
}
}
/// <summary>
/// Native imm32 functions.
/// </summary>
internal static partial class NativeFunctions
{
/// <summary>
/// GCS_* from imm32.
/// These values are used with ImmGetCompositionString and WM_IME_COMPOSITION.
/// </summary>
[Flags]
public enum IMEComposition
{
/// <summary>
/// Retrieve or update the attribute of the composition string.
/// </summary>
CompAttr = 0x0010,
/// <summary>
/// Retrieve or update clause information of the composition string.
/// </summary>
CompClause = 0x0020,
/// <summary>
/// Retrieve or update the attributes of the reading string of the current composition.
/// </summary>
CompReadAttr = 0x0002,
/// <summary>
/// Retrieve or update the clause information of the reading string of the composition string.
/// </summary>
CompReadClause = 0x0004,
/// <summary>
/// Retrieve or update the reading string of the current composition.
/// </summary>
CompReadStr = 0x0001,
/// <summary>
/// Retrieve or update the current composition string.
/// </summary>
CompStr = 0x0008,
/// <summary>
/// Retrieve or update the cursor position in composition string.
/// </summary>
CursorPos = 0x0080,
/// <summary>
/// Retrieve or update the starting position of any changes in composition string.
/// </summary>
DeltaStart = 0x0100,
/// <summary>
/// Retrieve or update clause information of the result string.
/// </summary>
ResultClause = 0x1000,
/// <summary>
/// Retrieve or update clause information of the reading string.
/// </summary>
ResultReadClause = 0x0400,
/// <summary>
/// Retrieve or update the reading string.
/// </summary>
ResultReadStr = 0x0200,
/// <summary>
/// Retrieve or update the string of the composition result.
/// </summary>
ResultStr = 0x0800,
}
/// <summary>
/// IMN_* from imm32.
/// Input Method Manager Commands, this enum is not exhaustive.
/// </summary>
public enum IMECommand
{
/// <summary>
/// Notifies the application when an IME is about to change the content of the candidate window.
/// </summary>
ChangeCandidate = 0x0003,
/// <summary>
/// Notifies an application when an IME is about to close the candidates window.
/// </summary>
CloseCandidate = 0x0004,
/// <summary>
/// Notifies an application when an IME is about to open the candidate window.
/// </summary>
OpenCandidate = 0x0005,
/// <summary>
/// Notifies an application when the conversion mode of the input context is updated.
/// </summary>
SetConversionMode = 0x0006,
}
/// <summary>
/// Returns the input context associated with the specified window.
/// </summary>
/// <param name="hWnd">Unnamed parameter 1.</param>
/// <returns>
/// Returns the handle to the input context.
/// </returns>
[DllImport("imm32.dll")]
public static extern IntPtr ImmGetContext(IntPtr hWnd);
/// <summary>
/// Retrieves information about the composition string.
/// </summary>
/// <param name="hImc">
/// Unnamed parameter 1.
/// </param>
/// <param name="arg2">
/// Unnamed parameter 2.
/// </param>
/// <param name="lpBuf">
/// Pointer to a buffer in which the function retrieves the composition string information.
/// </param>
/// <param name="dwBufLen">
/// Size, in bytes, of the output buffer, even if the output is a Unicode string. The application sets this parameter to 0
/// if the function is to return the size of the required output buffer.
/// </param>
/// <returns>
/// Returns the number of bytes copied to the output buffer. If dwBufLen is set to 0, the function returns the buffer size,
/// in bytes, required to receive all requested information, excluding the terminating null character. The return value is
/// always the size, in bytes, even if the requested data is a Unicode string.
/// This function returns one of the following negative error codes if it does not succeed:
/// - IMM_ERROR_NODATA.Composition data is not ready in the input context.
/// - IMM_ERROR_GENERAL.General error detected by IME.
/// </returns>
[DllImport("imm32.dll")]
public static extern long ImmGetCompositionStringW(IntPtr hImc, IMEComposition arg2, IntPtr lpBuf, uint dwBufLen);
/// <summary>
/// Retrieves a candidate list.
/// </summary>
/// <param name="hImc">
/// Unnamed parameter 1.
/// </param>
/// <param name="deIndex">
/// Zero-based index of the candidate list.
/// </param>
/// <param name="lpCandList">
/// Pointer to a CANDIDATELIST structure in which the function retrieves the candidate list.
/// </param>
/// <param name="dwBufLen">
/// Size, in bytes, of the buffer to receive the candidate list. The application can specify 0 for this parameter if the
/// function is to return the required size of the output buffer only.
/// </param>
/// <returns>
/// Returns the number of bytes copied to the candidate list buffer if successful. If the application has supplied 0 for
/// the dwBufLen parameter, the function returns the size required for the candidate list buffer. The function returns 0
/// if it does not succeed.
/// </returns>
[DllImport("imm32.dll")]
public static extern long ImmGetCandidateListW(IntPtr hImc, uint deIndex, IntPtr lpCandList, uint dwBufLen);
/// <summary>
/// Sets the position of the composition window.
/// </summary>
/// <param name="hImc">
/// Unnamed parameter 1.
/// </param>
/// <param name="frm">
/// Pointer to a COMPOSITIONFORM structure that contains the new position and other related information about
/// the composition window.
/// </param>
/// <returns>
/// Returns a nonzero value if successful, or 0 otherwise.
/// </returns>
[DllImport("imm32.dll", CharSet = CharSet.Auto)]
public static extern bool ImmSetCompositionWindow(IntPtr hImc, ref CompositionForm frm);
/// <summary>
/// Releases the input context and unlocks the memory associated in the input context. An application must call this
/// function for each call to the ImmGetContext function.
/// </summary>
/// <param name="hwnd">
/// Unnamed parameter 1.
/// </param>
/// <param name="hImc">
/// Unnamed parameter 2.
/// </param>
/// <returns>
/// Returns a nonzero value if successful, or 0 otherwise.
/// </returns>
[DllImport("imm32.dll", CharSet = CharSet.Auto)]
public static extern bool ImmReleaseContext(IntPtr hwnd, IntPtr hImc);
/// <summary>
/// Contains information about a candidate list.
/// </summary>
public struct CandidateList
{
/// <summary>
/// Size, in bytes, of the structure, the offset array, and all candidate strings.
/// </summary>
public int Size;
/// <summary>
/// Candidate style values. This member can have one or more of the IME_CAND_* values.
/// </summary>
public int Style;
/// <summary>
/// Number of candidate strings.
/// </summary>
public int Count;
/// <summary>
/// Index of the selected candidate string.
/// </summary>
public int Selection;
/// <summary>
/// Index of the first candidate string in the candidate window. This varies as the user presses the PAGE UP and PAGE DOWN keys.
/// </summary>
public int PageStart;
/// <summary>
/// Number of candidate strings to be shown in one page in the candidate window. The user can move to the next page by pressing IME-defined keys, such as the PAGE UP or PAGE DOWN key. If this number is 0, an application can define a proper value by itself.
/// </summary>
public int PageSize;
// /// <summary>
// /// Offset to the start of the first candidate string, relative to the start of this structure. The offsets
// /// for subsequent strings immediately follow this member, forming an array of 32-bit offsets.
// /// </summary>
// public IntPtr Offset; // manually handle
}
/// <summary>
/// Contains style and position information for a composition window.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct CompositionForm
{
/// <summary>
/// Position style. This member can be one of the CFS_* values.
/// </summary>
public int Style;
/// <summary>
/// A POINT structure containing the coordinates of the upper left corner of the composition window.
/// </summary>
public Point CurrentPos;
/// <summary>
/// A RECT structure containing the coordinates of the upper left and lower right corners of the composition window.
/// </summary>
public Rect Area;
}
/// <summary>
/// Contains coordinates for a point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
/// <summary>
/// The X position.
/// </summary>
public int X;
/// <summary>
/// The Y position.
/// </summary>
public int Y;
}
/// <summary>
/// Contains dimensions for a rectangle.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
/// <summary>
/// The left position.
/// </summary>
public int Left;
/// <summary>
/// The top position.
/// </summary>
public int Top;
/// <summary>
/// The right position.
/// </summary>
public int Right;
/// <summary>
/// The bottom position.
/// </summary>
public int Bottom;
}
}
/// <summary>
/// Native kernel32 functions.
/// </summary>