scippie
scippie

Reputation: 2075

i3 wm: start my application on a specific screen and give it focus & move the mouse there

I have searched several forums and I always find partial answers, but none that seem to solve my issue.

I am developing my own game on a desktop PC (BTW. I use Arch ;-)) with two screens and the i3 window manager. I develop on the right (square 1:1) screen and I would like to open my game on the left (16:9) screen after compilation. I can simulate this by always moving my mouse to the left screen during compilation so that the game opens there, but then I need to have an empty workspace open to move to.

I can find solutions to be put in the i3-config file where I can specifically open a window with a specific class name on a specific workspace (which is not necessarily a specific monitor) and I can certainly not give it focus after that.

I would love to be able to do it just with command line commands: I develop in vim and have a hotkey that starts my make&run script. The script will run the application when there are no compiler errors. It would be great if that command could be extended with "and also put it on the left monitor in an empty workspace and give it focus, also by moving the mouse there".

A solid i3-config solution would also be ok, maybe even better because I don't need the functionality on my laptop.

I wouldn't mind reserving a specific workspace for my game if needed.

Upvotes: 1

Views: 7798

Answers (1)

prosoitos
prosoitos

Reputation: 7407

I can find solutions to be put in the i3-config file where I can specifically open a window with a specific class name on a specific workspace (which is not necessarily a specific monitor) and I can certainly not (sic?) give it focus after that.

(note: is the "not" a typo here? I am a little confused).

As far as a solution with a config file is concerned, you are already half way there. The only thing you need to do is to have that workspace on the monitor of your choice.

Here is a full solution (with config file), including the steps you already solved:

1. Dedicate a workspace for your game

Since you can create as many workspaces as you want, there is no downside to this.

assign [class="MyGame"] game

2. Assign this workspace to your left screen

First, you need to know the name of the screen with xrandr (xorg-xrandr package on Arch).

Here is an example output in my settings:

$ xrandr

Screen 0: minimum 8 x 8, current 2560 x 2520, maximum 32767 x 32767
eDP1 connected primary 1920x1080+0+1440 (normal left inverted right x axis y axis) 310mm x 170mm
   1920x1080     60.00*+  59.93    48.00  
   1680x1050     59.95    59.88  
   1400x1050     59.98  
   1600x900      60.00    59.95    59.82  
   1280x1024     60.02  
   1400x900      59.96    59.88  
   1280x960      60.00  
   1368x768      60.00    59.88    59.85  
   1280x800      59.81    59.91  
   1280x720      59.86    60.00    59.74  
   1024x768      60.00  
   1024x576      60.00    59.90    59.82  
   960x540       60.00    59.63    59.82  
   800x600       60.32    56.25  
   864x486       60.00    59.92    59.57  
   640x480       59.94  
   720x405       59.51    60.00    58.99  
   640x360       59.84    59.32    60.00  
DP1 disconnected (normal left inverted right x axis y axis)
DP2 connected 2560x1440+0+0 (normal left inverted right x axis y axis) 550mm x 310mm
   2560x1440     59.95*+
   2048x1080     60.00    24.00  
   1920x1080     60.00    50.00    59.94  
   1920x1080i    60.00    50.00    59.94  
   1600x1200     60.00  
   1280x1024     75.02    60.02  
   1152x864      75.00  
   1280x720      60.00    50.00    59.94  
   1024x768      75.03    60.00  
   800x600       75.00    60.32  
   720x576       50.00  
   720x480       60.00    59.94  
   640x480       75.00    60.00    59.94  
   720x400       70.08  
HDMI1 disconnected (normal left inverted right x axis y axis)
HDMI2 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

In my case, eDP1 is my primary (laptop) monitor and DP2 is my external monitor.

If I wanted the workspace game on my external monitor, I would use:

workspace game output DP2

3. Create a keybinding that will run your "make&run script" and move the focus and the mouse to your game workspace

bindsym <my-bind> exec --no-startup-id <make&run script>; workspace game

Instead of using your vim hotkey, you are using an i3 keybinding to both run the script (which will open the application and the application will open in the correct monitor thanks to the settings above) and move the focus to that monitor:

It is easy to have i3 execute commands while it is very hard to have a command move the mouse and focus around (since this is the task of a wm or desktop environment).


I can think of a downside of this method:

When you hit a compiler error, your focus and mouse will still jump to your left monitor. You might find this annoying if it happens often.


The way I go about jumping from workspace to workspace is that I have a list of named workspaces and easy mnemonic keybindings to get to them.

So I would handle this a little differently than have one keybinding (or command as you had asked) doing the whole thing:

  • I would have a workspace with a bunch of applications I always want on my left monitor
assign [class="MyGame"] workspaceleft
assign [class="App1"] workspaceleft
assign [class="App2"] workspaceleft
assign [class="App3"] workspaceleft

workspace workspaceleft output <left-monitor-name-given-by-xrandr>
workspace workspacevim output <right-monitor-name-given-by-xrandr>
  • and I would have an easy keybinding to go to that workspace (and others to go to my other workspaces. So I would also have keybindings to go back to the or , depending on whether you'd rather have one application per workspace or only one workspace per monitor, etc.)
bindsym <bind1> workspaceleft 
bindsym <bind2> workspacevim

So I would work in vim and use the vim hotkey. If compilation works, the game would land in the proper monitor thanks to our early settings. I would then press <bind1> to get to it (and <bind2> to come back to the right-hand side monitor when I want to).

I think that this may be more convenient than the do-the-whole-thing keybinding.

Plus, being able to jump to any application by a key press and have all my applications land by default where I want to and the way I want to is one of the things I really love about i3. So setting such things might make your workflow a lot nicer beside this particular application.


Alternatively, you can have a keybinding to toggle between your monitors:

bindsym <bind> focus output left

(Note that here "left" is not a workspace name but the direction. This will toggle between monitors set next to each other. Replace "left" with "up" if they are on top of each other).

Upvotes: 5

Related Questions