-- luafiler (c) 2006 mallchin, http://www.mallchin.com/luafm/
-- You are free to distribute this file without modifications.

function readpad()

  -- initiate timer(s)
  padtimer = Timer.new()

  -- wait for input
  while true do

    -- read controls
    pad = Controls.read()

    -- filelist mode
    if padmode == "filelist" then

      -- press up
      if pad:up() and not keypress then

        -- select previous file
        mp_prevfile()

        screen:clear()
        render_filelist()
        render_statmsg("up", white)
        screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press down
      if pad:down() and not keypress then

        -- select next file
        mp_nextfile()

        screen:clear()
        render_filelist()
        render_statmsg("down", white)
        screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press triangle
      if pad:triangle() and not keypress then
        assert(loadstring(files[menu_pointer]["action_triangle"]))()
	pausepad(true)
      end

      -- press cross
      if pad:cross() and not keypress then
        assert(loadstring(files[menu_pointer]["action_cross"]))()
	pausepad(true)
      end

      -- press square
      if pad:square() and not keypress then
        assert(loadstring(files[menu_pointer]["action_square"]))()
	pausepad(true)
      end

    -- press select
    if pad:select() then
        screen:clear()
	display_help()
	screen.flip()
        screen.waitVblankStart()
	pausepad(true)
	padmode = "help"
    end

    -- press start
    if pad:start() then
        screen:clear()
	display_credits()
	screen.flip()
        screen.waitVblankStart()
	pausepad(true)
	padmode = "credits"
    end

    -- confirm mode
    elseif padmode == "confirm" then

      -- press left
      if pad:left() and not keypress then

        screen:clear()
        render_filelist()
        render_statmsg("left", white)

        confirm_option = true
	render_confirm(msgconfirm, msgcolour, confirm_option)

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press right
      if pad:right() and not keypress then

        screen:clear()
        render_filelist()
        render_statmsg("right", white)

        confirm_option = false
	render_confirm(msgconfirm, msgcolour, confirm_option)

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press cross
      if pad:cross() and not keypress then

	screen:clear()
        render_filelist()
	
	if confirm_option then
          render_statmsg("Confirmed yes", white)
	  assert(loadstring(confirm_action))()
        else
          render_statmsg("Confirmed no", white)
	end
	padmode = "filelist"

	screen:clear()
        render_filelist()

	screen.flip()
        screen.waitVblankStart()

	pausepad(true)

        -- reset skipping file operations
        skip_finish_file_operations = false

      end

      -- press circle
      if pad:circle() and not keypress then
        
	screen:clear()
        render_filelist()
        render_statmsg("Cancel", white)
	
	padmode = "filelist"

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

    elseif padmode == "file_operations" then

      -- press up
      if pad:up() and not keypress then

        screen:clear()
        render_filelist()
        render_statmsg("up", white)

        if selected_operation > 1 then
	  selected_operation = selected_operation - 1
	end
	render_file_operations(selected_operation)

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press down
      if pad:down() and not keypress then

        screen:clear()
        render_filelist()
        render_statmsg("down", white)

        if selected_operation < table.getn(file_operations) then
	  selected_operation = selected_operation + 1
	end
	render_file_operations(selected_operation)

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

      -- press cross
      if pad:cross() and not keypress then

	pausepad(true)

        for k,v in pairs(file_operations) do
          if k == selected_operation and string.len(v.name) > 1 then
            assert(loadstring(v.action))()
	  end
	end

        -- ugly hack to skip loading file list
        if not skip_finish_file_operations then

	  padmode = "filelist"

	  screen:clear()
          render_filelist()

          render_statmsg("Finished file operations", white)

	  screen.flip()
          screen.waitVblankStart()

	end

      end

      -- press circle
      if pad:circle() and not keypress then
        
	screen:clear()
        render_filelist()
        render_statmsg("Cancel", white)
	
	padmode = "filelist"

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

    elseif padmode == "help" then

      -- press circle
      if pad:circle() and not keypress then
        
	screen:clear()
        render_filelist()
        render_statmsg("Cancel", white)
	
	padmode = "filelist"

	screen.flip()
        screen.waitVblankStart()
	pausepad(true)

      end

    end

    -- filelist speed scrolling
    if (pad:up() or pad:down()) and keypress and padmode == "filelist" then
      if filelist_scroll > 0 and (padtimer:time() >= 175 or init_padpause) then
        if init_padpause then
          if padtimer:time() >= filelist_scroll then
  	    pausepad(false)
          end
	else
	  init_padpause = true
	end
      end
    end

    -- no keypress
    if not pad:up() and not pad:down() and not pad:left() and not pad:right() and not pad:triangle() and not pad:circle() and not pad:cross() and not pad:square() then

      -- reset speed scrolling
      init_padpause = false
      pausepad(false)

      -- refresh filelist
      if padmode == "filelist" then
        refresh_filelist()
      end

    end

    -- debug commands
    if debug then

      -- press l
      if pad:l() then
        break
      end

      -- press r
      if pad:r() then
        screenshot()
      end
    
    end

    -- quit
    if break_app then
        break
    end
  
  end

end

function pausepad(dopause)

  -- pause pad
  if dopause then

    keypress = true
    padtimer:start()

  else

    -- unpause pad
    keypress = false
    padtimer:reset()
    padtimer:stop()

  end

end
