#INCLUDE "occSDL.module"
#INCLUDE "course.module"

PROC do.stick.handler (VAL INT num, CT.SDLJS! stick, SHARED CHAN BYTE scr!)
  CT.SDLJSEVENT! ev:
  CT.SDLJSEVENT! ev2:
  CT.SDLJSCALIBRATE! cal:
  SEQ
    stick[in] ! enable
    stick[out] ? CASE event.chan; ev

    CLAIM scr!
      out.string ("joy2: got event channel..*n", 0, scr!)

    occ.SDL.joy.calibrator (ev, ev2, cal)

    PAR
      --{{{  calibrate joystick
      occ.SDL.joy.docalibrate (cal!, scr!)
      --}}}
      --{{{  loop processing events
      INITIAL BOOL run IS TRUE:
      WHILE run
        ev2[out] ? CASE
          INT da, db:
          MOBILE []INT16 axes:
          MOBILE []BYTE btns:
          change; da; db; axes; btns
            SEQ
              -- change!
              CLAIM scr!
                SEQ
                  out.string ("joy2.", 0, scr!)
                  out.int (num, 0, scr!)
                  out.string (" change: ", 0, scr!)
                  out.hex (da, 3, scr!)
                  scr ! ','
                  out.hex (db, 3, scr!)
                  out.string (": (", 0, scr!)
                  SEQ v = 0 FOR SIZE axes
                    INT aval:
                    SEQ
                      aval := INT axes[v]

                      out.int (aval, 0, scr!)
                      IF
                        v < ((SIZE axes) - 1)
                          scr ! ','
                        TRUE
                          SKIP
                  out.string (") btns (", 0, scr!)
                  SEQ v = 0 FOR SIZE btns
                    INT bval:
                    SEQ
                      IF
                        btns[v] = 0
                          bval := 0
                        TRUE
                          bval := 1
                      out.int (bval, 0, scr!)
                      IF
                        v < ((SIZE btns) - 1)
                          scr ! ','
                        TRUE
                          SKIP
                  out.string (")*n", 0, scr!)
          quit
            run := FALSE
      --}}}
:

PROC main (SHARED CHAN BYTE scr!)
  CT.SDLJOY! js:
  INT ns:
  SEQ
    CLAIM scr!
      out.string ("joy2: joystick test program*n", 0, scr!)

    occ.SDL.joy.start (js)

    js[in] ! num.sticks
    js[out] ? CASE num.sticks; ns

    CLAIM scr!
      SEQ
        out.string ("joy2: ", 0, scr!)
        out.int (ns, 0, scr!)
        out.string (" joysticks.*n", 0, scr!)

    IF
      ns > 0
        INT stk:
        [SDLJS.ITEMS]INT data:
        SEQ
          -- query
          js[in] ! query.stick; 0
          js[out] ? CASE query; stk; data

          CLAIM scr!
            SEQ
              out.string ("joy2: ", 0, scr!)
              out.int (stk, 0, scr!)
              out.string (": ", 0, scr!)
              out.int (data[0], 0, scr!)
              out.string (" axes, ", 0, scr!)
              out.int (data[1], 0, scr!)
              out.string (" buttons*n", 0, scr!)

          -- connect to first joystick
          js[in] ! connect; 0
          js[out] ? CASE
            SHARED CT.SDLJS! stick:
            joystick; stick
              SEQ
                -- got connection to joystick, run handler
                CLAIM stick!
                  do.stick.handler (0, stick!, scr!)
            error
              CLAIM scr!
                out.string ("joy2: failed to connect to joystick.*n", 0, scr!)
      TRUE
        SKIP
:

