Script for a dropdown terminal in i3wm

Coming from a DE (xfce) to i3wm made me miss a proper dropdown terminal. I was used to guake but it didn’t behave correctly in i3 with multi-monitors. As such, I needed a proper dropdown replacement. I made a script to make use of the scratchpad feature of i3 to have any terminal behave as guake should in my monitor-setup. The script uses the i3ipc library to control i3wm.

#!/usr/bin/env python3
from json import loads
from os import popen
import i3ipc

i3 = i3ipc.Connection()

focused = i3.get_tree().find_focused()

def ipc_query(req="command"):
	ans = popen("i3-msg -t " + req).readlines()[0]
	return loads(ans)

terminal = "tilix"

active_display = None
for w in ipc_query(req="get_workspaces"):
	if w['focused']:
		active_display = w['output']
		if active_display=="HDMI1":
			i3.command("[instance={0}], move scratchpad, scratchpad show, move position 0 19, resize set 1920 1061".format(terminal))
		elif active_display=="eDP1":
			i3.command("[instance={0}], move scratchpad, scratchpad show, move position 0 22, resize set 1920 1058".format(terminal))
		elif active_display=="VGA1":
			i3.command("[instance={0}], move scratchpad, scratchpad show, move position 1920 19, resize set 1366 749".format(terminal))

if focused.window_class.lower()==terminal.lower():
	i3.command("scratchpad show")

Performance wise, it is not as fast as I wanted (there’s a slight delay) but it’s good enough for now.