altdesktop / i3ipc-python

🐍 An improved Python library to control i3wm and sway.
http://i3ipc-python.readthedocs.io
BSD 3-Clause "New" or "Revised" License
855 stars 110 forks source link

How do I access the current mode? #58

Closed RomanSC closed 7 years ago

RomanSC commented 7 years ago

Hey, thanks for this library. I'm pretty new to programming and CS, having just started writing python recently. So having this library is really awesome because I can write some of my first "real" pieces of software for a WM that I love. Anyway.

Is there a way to get the current mode? I tried dir(i3ipc.Connection()) and have managed to acces the status of workspaces through i3ipc.Connection().get_workspaces()

i3 config example:

mode "resize" {

With vim like bindings

bindsym h resize shrink width 4 px or 4 ppt
bindsym j  resize grow height 4 px or 4 ppt
bindsym k resize shrink height 4 px or 4 ppt
bindsym l resize grow width 4 px or 4 ppt
# With arrow keys
bindsym Left resize shrink width 4 px or 4 ppt
bindsym Down resize grow height 4 px or 4 ppt
bindsym Up resize shrink height 4 px or 4 ppt
bindsym Right resize grow width 4 px or 4 ppt
# Esc
bindsym Escape mode "default"
bindsym Return mode "default"

}

Will gladly send you my finished program for example code once it's done. Thanks again.

joepvd commented 7 years ago

The current mode is not exposed with the IPC, see https://i3wm.org/docs/ipc.html, and so is not available in this library. There is a binding modes reply, which lists the available modes.

An alternative would be to listen to changes in the binding mode. This way, you would be able to store the current binding in a variable based on the most recent change.

acrisci commented 7 years ago

That's kind of strange that you can't do that. Open an issue on the i3 project if you would like it added to the ipc.

RomanSC commented 7 years ago

Thanks for the help. I'll post an issue to i3 so that maybe ipc can get it. I'll use a workaround somehow.

Here's a link to that issue: https://github.com/i3/i3/issues/2711

RomanSC commented 7 years ago

Airblader says: "You can get the mode (i3bar needs it as well). You just can't query it directly, but there is an event when it changes for which you can listen."

xfce4-i3-workspaces must be using this somehow. I don't know because I can't write C yet. But if you search for mode in that project I think it might be relevent in some way. Line 425-440: https://github.com/denesb/xfce4-i3-workspaces-plugin/blob/master/panel-plugin/i3w-plugin.c

acrisci commented 7 years ago

Here are the docs for listening to events

RomanSC commented 7 years ago

@acrisci Thanks thats making more sense. I understand now I need to do something like:

import i3ipc
i3 = i3ipc.Connection() # starts the socket to listen on

Then create some sort of function to run for when the event happens.

def myfun():
    #do something
    print("Super fun")

Then do something like:

# listen for events
i3.on('workspace::focus', myfun())

Closing the issue because it's not an issue. I just need to figure out how to listen for an event for mode change and name somehow.

Thanks again.