Skip to content

Instantly share code, notes, and snippets.

@tkkcc
Last active January 2, 2025 11:16
Show Gist options
  • Save tkkcc/10f1a38ebc3a6b253ab4bba2f75e024e to your computer and use it in GitHub Desktop.
Save tkkcc/10f1a38ebc3a6b253ab4bba2f75e024e to your computer and use it in GitHub Desktop.
i3 single-window-tiling & auto start app & adaptive scroll speed
#!/usr/bin/env python
import i3ipc
import re
import mmap
r = re.compile(r"chrom|telegram|Master PDF Editor|typora|thorium", re.I)
fd = open("/tmp/libinput_discrete_deltay_multiplier", "r+b")
m = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_WRITE)
state = {"pre_workspace_name": ""}
def on_window_focus(i3, e):
# scroll
v = 1
c = e.container.window_class or e.container.app_id
if not c:
return
if r.search(c):
v = 2
m.seek(0)
m.write(str(v).encode())
# out = i3.command(f"input type:pointer scroll_factor {v}")[0].ipc_data
# print(c, v, out)
# __import__('pdb').set_trace()
return
# popup
ws = i3.get_tree().find_focused().workspace()
cur_workspace_name = ws.name
cur_window_title = e.container.window_title
if e.container.window_title == "popup":
for w in ws.descendants():
if w.window_title and w.window_title != "popup":
print("w.window_title", w.window_title)
if (
state["pre_workspace_name"] == cur_workspace_name
and state["pre_window_title"] != cur_window_title
):
i3.command("workspace back_and_forth")
else:
i3.command(f'[id="{w.window}"] focus')
break
state["pre_workspace_name"] = cur_workspace_name
state["pre_window_title"] = cur_window_title
def on_workspace_focus(i3, e):
if len(e.current.leaves()):
return
w = e.current.name
if w == "_":
i3.command("exec --no-startup-id $TERMINAL -T note -e fish -c 'vs 2'")
elif w == "d":
i3.command("exec --no-startup-id $TERMINAL -T file -e fish -c n")
elif w == "2":
i3.command("exec --no-startup-id $TERMINAL -T htop -e htop")
elif w == "w":
i3.command("exec --no-startup-id $BROWSER")
elif w == "4":
i3.command("exec --no-startup-id thunar")
# elif w in ("s", "e", "a", "r", "f"):
# i3.command("exec --no-startup-id $TERMINAL")
elif w == "z":
i3.command("exec --no-startup-id netease-cloud-music-unblock-enhanced")
# elif w == "1":
# i3.command("exec --no-startup-id nxsession sst")
def kill_previous_instances():
import os
current_pid = os.getpid()
command = f"ps -ef | grep 'python.*{__file__}'"
process_list = os.popen(command).read().splitlines()
for process in process_list:
pid = int(process.split()[1])
if pid != current_pid:
os.popen(f"kill {pid} 2>/dev/null")
def on_window_new(i3, e):
ws = i3.get_tree().find_focused().workspace()
leaves = ws.leaves()
if len(leaves) == 1:
leaves[0].command('floating disable')
def on_window_move(i3, e):
id = e.container.nodes[0].id
ws = i3.get_tree().workspaces()
for ws in ws:
leaves = ws.leaves()
if id in (x.id for x in leaves):
if len(leaves)==1:
leaves[0].command('floating disable')
break
kill_previous_instances()
i3 = i3ipc.Connection()
i3.on("window::focus", on_window_focus)
i3.on("window::new", on_window_new)
i3.on("window::move", on_window_move)
i3.on("workspace::focus", on_workspace_focus)
i3.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment