How to elegantly create a Windows shortcut that starts a WSL program under X
Asked Answered
M

4

16

I'm struggling a little to launch xfce4-terminal from my WSL installation under VcXsrv from a button on the Windows taskbar. I don't want any DOS box/console/terminal other than the xfce4-terminal when I'm done. I have it working, but man is it ugly. Does anybody have a cleaner way of doing this?

Here is what I got working: I created a windows shortcut on the Desktop with this target (all in one line, broken with newlines for readability here):

C:\Windows\System32\wscript.exe
    \\wsl$\Ubuntu-20.04\home\peter\bin\windows\startTerminal.vbs

startTerminal.vbs was inspired by from 10 Ways To Run Batch Files Silently And Hide The Console Window • Raymond.CC (one of the few solutions that didn't require installing a separate program for this!) and it contains:

CreateObject("Wscript.Shell").Run "C:\Windows\System32\wsl.exe -u peter --exec /home/peter/bin/windows/startTerminal.sh",0,True

and startTerminal.sh contains:

export DISPLAY=localhost:0.0
xfce4-terminal --command=/bin/zsh

Setting DISPLAY is apparently required, even though I set the DISPLAY environment in ~/.zshrc. wsl.exe apparently doesn't do that unless you run the login shell.

Once all of this is working, I can drag my shortcut to the taskbar and click on it there.

I count 3, three files cooperating to achieve this simple goal? Can I limit that to one or two without installing an external program?

Mimetic answered 18/10, 2020 at 21:13 Comment(0)
M
3

I removed the need for a startTerminal.sh file by:

Editing startTerminal.vbs to contain:

CreateObject("Wscript.Shell").Run "C:\Windows\System32\wsl.exe -u peter bash -l -c xfce4-terminal",0,True

And then I created a ~/.bash_profile to set the $DISPLAY variable, which probably should've been done anyway:

$ cat .bash_profile
if [ -z "$DISPLAY" ]; then 
    export DISPLAY=:0
fi

So now there is the shortcut and a startTerminal.vbs that starts it up. It still isn't very elegant...

Mimetic answered 19/10, 2020 at 13:7 Comment(3)
Apparently, export DISPLAY=localhost:0.0 used to work. But on a current Windows 10 installation, I've had to change that to export DISPLAY=:0 to get it to work. I have no idea why...Amiraamis
AFAICT, DISPLAY=localhost:0.0 connects to the X server over TCP/IP, whereas DISPLAY=:0 connects to the X server using a socket. They both should work, but...Histochemistry
Thanks, @BryanLarsen: That totally makes sense. I'm now using wsld to be more resilient to Windows networking changes so now I am using a (v)socket (whatever that is).Amiraamis
L
14

Yes, I think you can do what I do in one shortcut file. I'll share what works for me then you'll have to fiddle for your program.

Try putting all your commands in the Target section of the Shortcut link file.

Step 1. Right click on desktop then New then Shortcut then name it (LaunchXFCE4)

Step 2. Right click LaunchXFCE4 select Properties

Step 3. Enter all your launch sequences in the Target section

Step 4. Right click LaunchXFCE4 select Pin to Taskbar

I'm launching Emacs 27.1 on WSL 2 with Ubuntu 20.10 on Windows 10 using this code which perhaps you can modify.

C:\Windows\System32\wsl.exe --distribution Ubuntu20 bash -c "export DISPLAY=$(ip route | awk '{print $3; exit}'):0 && export LIBGL_ALWAYS_INDIRECT=1 && export XCURSOR_SIZE=16 && setsid emacs"

The first export display is similar to your localhost:0.0 so replace it or try mine. Maybe your xfce4 replaces my emacs. Read this post about double ampersands. So possibly this

C:\Windows\System32\wsl.exe --distribution Ubuntu20 bash -c "export DISPLAY=localhost:0.0 && export LIBGL_ALWAYS_INDIRECT=1 && export XCURSOR_SIZE=16 && setsid xfce4-terminal --command=/bin/zsh"

Note, my VcXsrv starts when windows starts by putting a shortcut to config.xlaunch in my C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.

By the way, Microsoft will provide a GUI for WSL in a few months, according to their blog

GUI app support in WSL is becoming a reality! We are getting closer to an initial preview and happy to announce a preview release for Windows Insiders within the next couple of months.

Leighleigha answered 16/11, 2020 at 19:12 Comment(8)
Thanks, @saj for your suggestion. When I try it I get the xfce-terminal, but also the "dos box" as shown here: imgur.com/30Nmi90. I need that dos box to not appear. Also, we all know what "within the next couple of months" means... A year? :-)Amiraamis
You're welcome Peter. I don't have any extra windows/dos boxes when I use this. So you'll have to fiddle with the parameters in that code or in the VcXsrv launch config.Leighleigha
It just occurred to me that perhaps you need to set the Run field to Minimized in your shortcut that launches config.xlaunch, which should be in your user > Start Menu > Programs > Startup folder.Leighleigha
Then the dos box will be minimized but still in the task bar. I need it not to be there a all.Amiraamis
Thank you! I used this to start alacritty with tabbed: C:\Windows\System32\wsl.exe --distribution Ubuntu bash -c "export DISPLAY=localhost:0.0 && export LIBGL_ALWAYS_INDIRECT=1 && setsid tabbed -g 2300x1800 -c alacritty --working-directory ~ --embed" The only catch is that only Cygwin X server works (VcXsrv doesn't) and trying to make the geometry bigger causes it to close itself for some reason.Zenaidazenana
@PeterV.Mørch were you able to get rid of the DOS box eventually?Mystical
@Bobbbay: Did you read my original post and/or my answer? I just didn't succeed to get it working with this answer.Amiraamis
@PeterV.Mørch right, so the answer to my question is no, you weren't able to get rid of the DOS box with this solution. Thank you.Mystical
M
3

I removed the need for a startTerminal.sh file by:

Editing startTerminal.vbs to contain:

CreateObject("Wscript.Shell").Run "C:\Windows\System32\wsl.exe -u peter bash -l -c xfce4-terminal",0,True

And then I created a ~/.bash_profile to set the $DISPLAY variable, which probably should've been done anyway:

$ cat .bash_profile
if [ -z "$DISPLAY" ]; then 
    export DISPLAY=:0
fi

So now there is the shortcut and a startTerminal.vbs that starts it up. It still isn't very elegant...

Mimetic answered 19/10, 2020 at 13:7 Comment(3)
Apparently, export DISPLAY=localhost:0.0 used to work. But on a current Windows 10 installation, I've had to change that to export DISPLAY=:0 to get it to work. I have no idea why...Amiraamis
AFAICT, DISPLAY=localhost:0.0 connects to the X server over TCP/IP, whereas DISPLAY=:0 connects to the X server using a socket. They both should work, but...Histochemistry
Thanks, @BryanLarsen: That totally makes sense. I'm now using wsld to be more resilient to Windows networking changes so now I am using a (v)socket (whatever that is).Amiraamis
D
2

After experimenting with @Saj 's answer which did not work for me I resulted in the below:

C:\Windows\System32\wsl.exe --distribution Ubuntu-20.04 bash -c "export DISPLAY=$((route.exe print | Select-String 0.0.0.0 | select -first 1) -Split "\s+" | select -Index 4):0.0 && export LIBGL_ALWAYS_INDIRECT=1 && setsid emacs"

Note that my problem was that it could not use bash commands inside the parenthesis (grep, awk)

Environment: WSL2 on Windows 10

Disfavor answered 5/2, 2022 at 17:29 Comment(1)
This works pretty well. Just a note about the LIBGL_ALWAYS_INDIRECT=1 - some applications don't need that set, for instance, opening alacritty only works without that option.Hunger
T
2

One of the few good additions to Windows was WSL-2, it's much more capable now. It's part of late Win 10 and Win 11. Here is how to elegently create a terminal window, replace the terminal with any X app you want.

  1. Linux: "apt-get install xfce4-terminal" (or equivalent)
  2. Windows: Create a new Shortcut (right click in any windows folder) 2.1) Shortcut to -> C:\Windows\System32\wslg.exe -u root -- /usr/bin/xfce4-terminal If you have multiple distributions use it like: "-d Debian"

That's it. You can give it a better icon and name it. It will launch the application as if it was a windows application.

Tie answered 7/11, 2022 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.