pharo-graphics / Bloc

Low-level UI infrastructure & framework for Pharo
MIT License
80 stars 39 forks source link

Space isn't shown in a Pharo in headless mode #567

Open tinchodias opened 1 month ago

tinchodias commented 1 month ago

Steps to reproduce in MacOS terminal:

  1. Execute curl https://get.pharo.org/110+vmLatest | bash
  2. Install Bloc
  3. Execute ./pharo Pharo.image eval --no-quit "BlSpace new useSDL2Host; addEventHandlerOn: BlSpaceShownEvent do: [ Stdio stdout << 'OPENED'; lf ]; show"

After a second, OPENED is printed on the terminal, but the window doesn't appear.

tinchodias commented 1 month ago

Going more low-level, below Bloc, there is this example in OSWindow-SDL2 that works but only partially: ./pharo Pharo.image eval --no-quit "SDL2Example new simpleDrawWindow". A window is shown, but the example closes it after a delay, and that doesn't happen. I will check on Windows.

tinchodias commented 1 month ago

I debugged more low-level on this issue (plain SDL2 bindings). I share with you what I did and found:

Execute in a terminal ./pharo Pharo.image st example.st --quit after saving next contents in "example.st":

| window renderer eventHandlerBlock renderBlock background event shouldQuit |

"-- initialize SDL2 --"

SDL2 initEverything.
Stdio stdout print: SDL2 version; lf.

"-- create and show window --"

window := SDL2
            createWindow: 'Press any key to change color; Click on close window button to quit.'
            x: 100 y: 100
            width: 800 height: 400
            flags: 0.
renderer := window createDefaultRenderer.
window show.

"-- prepare to loop --"

shouldQuit := false.
background := Color blue.

eventHandlerBlock := [ :anEvent |
    Stdio stdout << anEvent asString; lf.
    anEvent class = SDL_QuitEvent
        ifTrue: [ shouldQuit := true ].
    anEvent class = SDL_KeyDownEvent
        ifTrue: [ background := Color random ] ].
renderBlock := [
    renderer
        drawColorR: background red * 255
            g: background green * 255
            b: background blue * 255
            a: 255;
        clear;
        present ].

"-- loop until quit --"

event := SDL_Event new.
[ shouldQuit ] whileFalse: [
    [ (SDL2 pollEvent: event) > 0 ] whileTrue: [
        eventHandlerBlock value: event mapped ].
        renderBlock value.
        10 milliSeconds wait ].

renderer destroy.
window destroy

That is a minimal demo to open a SDL2 window. I discovered that the window doesn't actually open without some events being polled (via SDL2 pollEvent: event). I mean, it's not enough to initialize SDL2, create a window and sending show; it is also mandatory to poll at least some events to have the window actually shown on the screen. You can check this by removing the code below "-- loop until quit --"... the window won't open (in Mac OS).

Via trial-and-error, this was the minimal polls to be done to make the window show (replacing the loop):

2 timesRepeat: [
   [ (SDL2 pollEvent: SDL_Event new) > 0 ] whileTrue.
   0.1 seconds wait ].
3 seconds wait.

The window will be shown for 3 seconds and then quit.

tinchodias commented 1 month ago

Now, coming back to Bloc and OSWindow (higher-level), I've found that Pharo opened in headless mode doesn't initialize the event loop. The first line of this script starts the event loop and fixes or work arounds the issue:

OSWindowDriver current startUp: true.

BlSpace new
    useSDL2Host;
    extent: 800@400;
    title: 'Press any key to change color; Click on close window button to quit.';
    addEventHandlerOn: BlSpaceShownEvent do: [ :evt |
        evt space root
            background: Color green;
            addEventHandlerOn: BlKeyDownEvent do: [
                evt space root background: Color random ] ];
    show

Put this script in "example.st" and execute in terminal: ./pharo Pharo.image st example.st and it works.

I'm on Pharo 12 and MacOs, I didn't try in other combinations yet.

I'll ask @tesonep when he's back from vacations about this.

tinchodias commented 1 month ago

Hey... I was always on Mac. Now on a Windows 10, I can't reproduce the original issue!! both in Pharo 11 and 12.

Strange, this is the OS where @ELePors reported the issue!

ELePors commented 3 weeks ago

But in your command line the headless parameter is missing ... ?

start 110-x64\Pharo.exe --headless "pharo.image" st "moncode.st"
tinchodias commented 3 weeks ago

Hello @ELePors I used pharo, that is a script that uses --headless but shorter. Did you try adding OSWindowDriver current startUp: true. at beginning of your .st?