OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-tps / scenes / test / probe_window.gd

55 lines · gd ref:
extends RefCounted
## Shared by the real-window GPU probes. ⚠ A HIDDEN WINDOW IS NOT DRAWN AND MEASURES
## 0.000 ms: under i3 a new window opens behind a fullscreen one (Guake) and Godot reports
## itself minimized. move_to_free_display() puts THIS window (matched by its X id — a
## title match could grab the player's own game), floating at a fixed size, on the
## visible workspace of the left-most display with no fullscreen window.
##   const ProbeWindow := preload("res://scenes/test/probe_window.gd")


## "" when i3 is not running or every display is covered.
static func free_workspace() -> String:
	var o: Array = []
	if OS.execute("i3-msg", ["-t", "get_workspaces"], o) != 0 or o.is_empty():
		return ""
	var wss: Variant = JSON.parse_string(String(o[0]))
	var t: Array = []
	OS.execute("i3-msg", ["-t", "get_tree"], t)
	var tree: Variant = JSON.parse_string(String(t[0])) if not t.is_empty() else null
	if not (wss is Array) or not (tree is Dictionary):
		return ""
	var covered := {}
	_fullscreen(tree, "", covered)
	var best := ""
	var best_x := INF
	for x: Dictionary in wss:
		if bool(x.get("visible")) and not covered.has(String(x.get("name"))):
			var rx := float((x.get("rect", {}) as Dictionary).get("x", 0))
			if rx < best_x:
				best_x = rx
				best = str(int(x.get("num")))
	return best


static func _fullscreen(n: Dictionary, ws: String, covered: Dictionary) -> void:
	if String(n.get("type")) == "workspace":
		ws = String(n.get("name"))
	elif int(n.get("fullscreen_mode", 0)) != 0 and n.get("window") != null and ws != "":
		covered[ws] = true      # workspace nodes themselves report mode 1 — skipped above
	for c: Variant in (n.get("nodes", []) as Array) + (n.get("floating_nodes", []) as Array):
		_fullscreen(c, ws, covered)


## Move + float + size this window onto workspace `ws`. Call repeatedly until drawn.
static func move_to(ws: String, size: Vector2i) -> void:
	if ws == "":
		return
	var crit := '[id="%d"]' % DisplayServer.window_get_native_handle(DisplayServer.WINDOW_HANDLE)
	OS.execute("i3-msg", ['%s move container to workspace number %s; %s floating enable, resize set %d %d'
		% [crit, ws, crit, size.x, size.y]], [])


## True once the window really draws (a hidden window has size 0 and is "minimized").
static func drawing(root: Window, min_w: int = 1000) -> bool:
	return root.size.x >= min_w and DisplayServer.window_get_mode() != DisplayServer.WINDOW_MODE_MINIMIZED