Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 8e6c99a

Browse files
committed
supercharge the display recognition routine
1 parent 710aa0f commit 8e6c99a

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

handlers.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -409,32 +409,50 @@ func checkSketchForMissingDisplayEnvVariable(errorString string, filepath string
409409
if strings.Contains(errorString, "Can't open display") || strings.Contains(errorString, "cannot open display") {
410410

411411
if os.Getenv("DISPLAY") == "NULL" {
412+
os.Setenv("DISPLAY", ":0")
412413
return
413414
}
414415

415-
setupDisplay()
416+
err := setupDisplay(true)
417+
if err != nil {
418+
setupDisplay(false)
419+
}
416420
spawnProcess(filepath, sketch, status)
417421
sketch.Status = "RUNNING"
418422
}
419423
}
420424

421-
func setupDisplay() {
425+
func setupDisplay(usermode bool) error {
422426
// Blindly set DISPLAY env variable to default
423-
os.Setenv("DISPLAY", ":0")
424-
// Unlock xorg session for localhost connections
425-
// TODO: find a way to automatically remove -nolisten tcp
426-
cmd := exec.Command("xhost", "+localhost")
427-
cmd.SysProcAttr = &syscall.SysProcAttr{}
428-
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: 1000, Gid: 1000}
429-
_, err := cmd.CombinedOutput()
430-
// Also try xrandr
431-
cmd = exec.Command("xrandr")
432-
_, err_xrandr := cmd.CombinedOutput()
433-
if err != nil || err_xrandr != nil {
434-
fmt.Println("Xorg server unavailable, make sure you have a display attached and a user logged in")
435-
fmt.Println("If it's already ok, try setting up Xorg to accept incoming connection (-listen tcp)")
436-
fmt.Println("On Ubuntu, add \n\n[SeatDefaults]\nxserver-allow-tcp=true\n\nto /etc/lightdm/lightdm.conf")
437-
os.Setenv("DISPLAY", "NULL")
427+
i := 0
428+
for {
429+
os.Setenv("DISPLAY", ":"+strconv.Itoa(i))
430+
fmt.Println("Exporting DISPLAY as " + ":" + strconv.Itoa(i))
431+
// Unlock xorg session for localhost connections
432+
// TODO: find a way to automatically remove -nolisten tcp
433+
cmd := exec.Command("xhost", "+localhost")
434+
if usermode {
435+
cmd.SysProcAttr = &syscall.SysProcAttr{}
436+
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: 1000, Gid: 1000}
437+
}
438+
out, err := cmd.CombinedOutput()
439+
fmt.Println(string(out))
440+
// Also try xrandr
441+
cmd = exec.Command("xrandr")
442+
out, err_xrandr := cmd.CombinedOutput()
443+
fmt.Println(string(out))
444+
if err != nil || err_xrandr != nil {
445+
if i > 2 {
446+
fmt.Println("Xorg server unavailable, make sure you have a display attached and a user logged in")
447+
fmt.Println("If it's already ok, try setting up Xorg to accept incoming connection (-listen tcp)")
448+
fmt.Println("On Ubuntu, add \n\n[SeatDefaults]\nxserver-allow-tcp=true\n\nto /etc/lightdm/lightdm.conf")
449+
os.Setenv("DISPLAY", "NULL")
450+
return errors.New("Unable to open display")
451+
}
452+
} else {
453+
return nil
454+
}
455+
i++
438456
}
439457
}
440458

0 commit comments

Comments
 (0)