@@ -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]\n xserver-allow-tcp=true\n \n to /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]\n xserver-allow-tcp=true\n \n to /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