Eine Zeile oben in der Ecke der Leistenseite (380x52): Statuspunkt, ECHTGELD, Satz, darunter was laeuft / die Meldung / die letzte Entscheidung, Zug-Uhr, Hauptknopf, Not-Aus, Ausklappen, Beenden. Doppelklick, der Pfeil oder Strg+Alt+C klappt wieder aus. cockpit.json meldet form "pille" (Kacheln wie am Mac). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
180 lines
7.9 KiB
C#
180 lines
7.9 KiB
C#
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using Microsoft.UI.Dispatching;
|
|
|
|
namespace Pokerfreak.Cockpit.Kern;
|
|
|
|
/// <summary>
|
|
/// Wo das Cockpit steht - fuer das Kacheln und den Tisch-Desktop (wie cockpit/src/ort.rs).
|
|
/// 1. Rahmen melden: jede Sekunde den echten Rahmen (physische Pixel, ohne DWM-Rand) als
|
|
/// `cockpit.json` in den Treffpunkt schreiben, wenn er sich geaendert hat - die Tische werden
|
|
/// daneben gekachelt (sicht/tische.py `rahmen_fuer`). Versteckt oder minimiert: Datei weg, die Tische
|
|
/// duerfen die ganze Flaeche nehmen. Beim Ende wird sie geloescht.
|
|
/// Wie am Mac (Rahmen.datei) mit form "leiste", Seite, Mindestbreite und Zeit - jede Sekunde neu,
|
|
/// denn hand/act.py nimmt den Rahmen fuers Ausweichen nur, solange er hoechstens 10 s alt ist.
|
|
/// 2. Tisch-Desktop: liegen die Tische auf ihrem eigenen virtuellen Desktop (COINPOKER_DESKTOP=1),
|
|
/// zieht das Cockpit ueber den Fensterdienst (`desktop`) mit hinueber und dockt nur dort neben den
|
|
/// Tischen an (Owner 15.11.: "nur auf dem virtuellen Desktop docken, falls aktiviert"). Ohne
|
|
/// Tisch-Desktop bleibt es, wo es ist.
|
|
/// </summary>
|
|
public sealed class Ort : IDisposable
|
|
{
|
|
private const int AnheftenVersuche = 5;
|
|
|
|
private readonly IntPtr _hwnd;
|
|
private readonly string? _datei;
|
|
private readonly string? _fensterdienst;
|
|
private readonly DispatcherQueueTimer _timer;
|
|
private readonly DispatcherQueue _ui;
|
|
private (int, int, int, int)? _letzter;
|
|
private bool _wegGemeldet, _angeheftet, _heftet;
|
|
private int _versuche;
|
|
|
|
/// <summary>Wie client/umgebung.py `treffpunkt`: SCRATCH, sonst betrieb/scratch, ohne betrieb/ scratch/.</summary>
|
|
public static string Treffpunkt(string wurzel, string? scratch)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(scratch)) return scratch.Trim();
|
|
return Directory.Exists(Path.Combine(wurzel, "betrieb"))
|
|
? Path.Combine(wurzel, "betrieb", "scratch")
|
|
: Path.Combine(wurzel, "scratch");
|
|
}
|
|
|
|
/// <summary>Die Leiste fuers Kacheln (sicht/tische.py `_leiste_lage`) und fuers Ausweichen (hand/act.py).</summary>
|
|
public static string CockpitJson(int x, int y, int w, int h, string seite, int mindestbreite, double zeit, string form = "leiste") =>
|
|
$"{{\"rect\": [{x}, {y}, {w}, {h}], \"form\": \"{form}\", \"seite\": \"{seite}\", \"mindestbreite\": {mindestbreite}, "
|
|
+ $"\"zeit\": {zeit.ToString("0.###", System.Globalization.CultureInfo.InvariantCulture)}, \"quelle\": \"windows-cockpit\"}}";
|
|
|
|
/// <summary>Der Fensterdienst, release vor debug (hand/winfenster.py `_bin`).</summary>
|
|
public static string? Fensterdienst(string wurzel)
|
|
{
|
|
var basis = Path.Combine(wurzel, "werkzeuge", "fensterdienst", "target");
|
|
return new[] { "release", "debug" }.Select(a => Path.Combine(basis, a, "fensterdienst.exe")).FirstOrDefault(File.Exists);
|
|
}
|
|
|
|
/// <summary>Wo Laeufer, Server und Cockpit sich Dateien hinlegen - null ohne Repo.</summary>
|
|
public string? Treffpunkt() => _treffpunkt;
|
|
private readonly string? _treffpunkt;
|
|
|
|
/// <summary>Seite, Mindestbreite (physisch) und Form ("leiste"/"pille") fuer cockpit.json.</summary>
|
|
public Func<(string Seite, int Mindestbreite, string Form)> Lage { get; set; } = () => ("rechts", 0, "leiste");
|
|
|
|
/// <summary>Faellt, sobald das Cockpit auf dem Tisch-Desktop liegt (dann neu andocken).</summary>
|
|
public event Action? AufTischDesktop;
|
|
|
|
public Ort(IntPtr hwnd, DispatcherQueue ui)
|
|
{
|
|
_hwnd = hwnd;
|
|
_ui = ui;
|
|
var wurzel = Wurzel.Finden();
|
|
_treffpunkt = wurzel == null ? null : Treffpunkt(wurzel, Environment.GetEnvironmentVariable("SCRATCH"));
|
|
_datei = _treffpunkt == null ? null : Path.Combine(_treffpunkt, "cockpit.json");
|
|
var isoliert = Dienst.Desktop == "1";
|
|
_fensterdienst = wurzel == null ? null : Fensterdienst(wurzel);
|
|
if (isoliert && _fensterdienst == null)
|
|
Debug.WriteLine("Fensterdienst nicht gebaut (werkzeuge/fensterdienst): Cockpit bleibt auf diesem Desktop");
|
|
_angeheftet = !isoliert || _fensterdienst == null;
|
|
_timer = ui.CreateTimer();
|
|
_timer.Interval = TimeSpan.FromSeconds(1);
|
|
_timer.IsRepeating = true;
|
|
_timer.Tick += (_, _) => Takt();
|
|
_timer.Start();
|
|
Takt();
|
|
}
|
|
|
|
/// <summary>Liegt das Fenster auf dem Tisch-Desktop (oder braucht es das nicht)?</summary>
|
|
public bool Angeheftet => _angeheftet;
|
|
|
|
private void Takt()
|
|
{
|
|
if (_datei != null)
|
|
{
|
|
var sichtbar = IsWindowVisible(_hwnd) && !IsIconic(_hwnd);
|
|
if (sichtbar && Rahmen() is var (x, y, w, h))
|
|
{
|
|
var (seite, mindest, form) = Lage();
|
|
Schreiben(_datei, CockpitJson(x, y, w, h, seite, mindest, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0, form));
|
|
_letzter = (x, y, w, h);
|
|
_wegGemeldet = false;
|
|
}
|
|
else if (!_wegGemeldet)
|
|
{
|
|
try { File.Delete(_datei); } catch { }
|
|
_letzter = null;
|
|
_wegGemeldet = true;
|
|
}
|
|
}
|
|
if (!_angeheftet && !_heftet && _versuche++ < AnheftenVersuche && _fensterdienst != null)
|
|
{
|
|
_heftet = true;
|
|
var exe = _fensterdienst;
|
|
var h = _hwnd.ToInt64();
|
|
Task.Run(() => Anheften(exe, h));
|
|
}
|
|
}
|
|
|
|
/// <summary>Einmal `fensterdienst desktop {"id":hwnd}` - das Cockpit auf den Tisch-Desktop, im
|
|
/// Hintergrund (COM, ~100 ms). Legt den Tisch-Desktop an, falls es ihn noch nicht gibt.</summary>
|
|
private void Anheften(string exe, long h)
|
|
{
|
|
try
|
|
{
|
|
var info = new ProcessStartInfo(exe)
|
|
{
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
};
|
|
info.ArgumentList.Add("desktop");
|
|
info.ArgumentList.Add($"{{\"befehl\":\"desktop\",\"id\":{h}}}");
|
|
using var p = Process.Start(info)!;
|
|
var text = p.StandardOutput.ReadToEnd();
|
|
p.WaitForExit(5000);
|
|
if (text.Contains("\"ok\":true") || text.Contains("\"ok\": true"))
|
|
{
|
|
_angeheftet = true;
|
|
_ui.TryEnqueue(() => AufTischDesktop?.Invoke());
|
|
}
|
|
else Debug.WriteLine("Cockpit nicht auf dem Tisch-Desktop: " + text.Trim());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.WriteLine($"Fensterdienst nicht gestartet ({exe}): {e.Message}");
|
|
}
|
|
finally { _heftet = false; }
|
|
}
|
|
|
|
private (int, int, int, int)? Rahmen()
|
|
{
|
|
var ok = DwmGetWindowAttribute(_hwnd, 9 /* DWMWA_EXTENDED_FRAME_BOUNDS */, out RECT r, Marshal.SizeOf<RECT>()) == 0
|
|
|| GetWindowRect(_hwnd, out r);
|
|
return ok && r.Rechts > r.Links && r.Unten > r.Oben ? (r.Links, r.Oben, r.Rechts - r.Links, r.Unten - r.Oben) : null;
|
|
}
|
|
|
|
private static void Schreiben(string datei, string inhalt)
|
|
{
|
|
try
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(datei)!);
|
|
var tmp = Path.ChangeExtension(datei, ".json.tmp");
|
|
File.WriteAllText(tmp, inhalt);
|
|
File.Move(tmp, datei, true);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_timer.Stop();
|
|
if (_datei != null) try { File.Delete(_datei); } catch { }
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
private struct RECT { public int Links, Oben, Rechts, Unten; }
|
|
|
|
[DllImport("dwmapi.dll")] private static extern int DwmGetWindowAttribute(IntPtr h, int attr, out RECT r, int groesse);
|
|
[DllImport("user32.dll")] private static extern bool GetWindowRect(IntPtr h, out RECT r);
|
|
[DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr h);
|
|
[DllImport("user32.dll")] private static extern bool IsIconic(IntPtr h);
|
|
}
|