---@meta ev

--------------------------------------------------------------------------------
-- lua-ev/libev event loop module
--------------------------------------------------------------------------------

---@alias ev_callback fun(loop:ev_loop, watcher:ev_watcher, revents:integer)
---@alias ev_timer_callback fun(loop:ev_loop, timer:ev_timer, revents:integer)
---@alias ev_io_callback fun(loop:ev_loop, io:ev_io, revents:integer)
---@alias ev_signal_callback fun(loop:ev_loop, signal:ev_signal, revents:integer)
---@alias ev_idle_callback fun(loop:ev_loop, idle:ev_idle, revents:integer)
---@alias ev_async_callback fun(loop:ev_loop, async:ev_async, revents:integer)
---@alias ev_child_callback fun(loop:ev_loop, child:ev_child, revents:integer)
---@alias ev_stat_callback fun(loop:ev_loop, stat:ev_stat, revents:integer)

---@class ev_loop*
local loop = {}

--- Determine whether it is the default event loop.
---@return boolean ok Whether to loop by default
function loop:is_default() end

--- Returns the number of event loop iterations.
---@return integer count Number of iterations
function loop:count() end

--- Returns the number of event loop iterations.
---@return integer count Number of iterations
function loop:iteration() end

--- Returns the event loop recursion depth.
---@return integer depth Recursion depth
function loop:depth() end

--- Returns the current loop timestamp.
---@return number seconds epoch seconds
function loop:now() end

--- Updates and returns the current loop timestamp.
---@return number seconds epoch seconds
function loop:update_now() end

--- Runs the event loop until there are no active watchers or until interrupted by `unloop`.
function loop:loop() end

--- Interrupt the event loop.
function loop:unloop() end

--- Returns the currently used libev backend id.
---@return integer backend_id backend id
function loop:backend() end

--- Reinitialize the loop after fork.
function loop:fork() end

---@class ev_watcher*
---@field [string] any watcher shadow table Field
local watcher = {}

--- Whether the watcher has been started.
---@return boolean active Is it active?
function watcher:is_active() end

--- Whether the watcher has pending events.
---@return boolean pending Whether pending
function watcher:is_pending() end

--- Clear pending events and return revents.
---@param loop ev_loop event loop
---@return integer revents event bit
function watcher:clear_pending(loop) end

--- Gets or sets the watcher callback.
---@param new_callback? ev_callback new callback
---@return ev_callback old_callback original callback
function watcher:callback(new_callback) end

--- Get or set watcher priority.
---@param new_priority? integer New priority, usually between `ev.MINPRI` and `ev.MAXPRI`
---@return integer old_priority Original priority
function watcher:priority(new_priority) end

---@class ev_timer: ev_watcher
local timer = {}

--- Start timer watcher.
---@param loop ev_loop event loop
---@param is_daemon? boolean Whether to prevent loop from exiting
function timer:start(loop, is_daemon) end

--- Stop timer watcher.
---@param loop ev_loop event loop
function timer:stop(loop) end

--- Reset timer, next trigger interval is optional.
---@param loop ev_loop event loop
---@param repeat_seconds? number New repeat seconds
function timer:again(loop, repeat_seconds) end

--- Clear pending events and return revents.
---@param loop ev_loop event loop
---@return integer revents event bit
function timer:clear_pending(loop) end

---@class ev_io: ev_watcher
local io = {}

--- Start IO watcher.
---@param loop ev_loop event loop
---@param is_daemon? boolean Whether to prevent loop from exiting
function io:start(loop, is_daemon) end

--- Stop IO watcher.
---@param loop ev_loop event loop
function io:stop(loop) end

--- Returns the file descriptor bound when the watcher was created.
---@return integer fd file descriptor
function io:getfd() end

---@class ev_signal: ev_watcher
local signal = {}

--- Start signal watcher.
---@param loop ev_loop event loop
---@param is_daemon? boolean Whether to prevent loop from exiting
function signal:start(loop, is_daemon) end

--- Stop signal watcher.
---@param loop ev_loop event loop
function signal:stop(loop) end

---@class ev_idle: ev_watcher
local idle = {}

--- Start idle watcher.
---@param loop ev_loop event loop
---@param is_daemon? boolean Whether to prevent loop from exiting
function idle:start(loop, is_daemon) end

--- Stop idle watcher.
---@param loop ev_loop event loop
function idle:stop(loop) end

---@class ev_async: ev_watcher
local async = {}

--- Start async watcher.
---@param loop ev_loop event loop
---@param is_daemon? boolean Whether to prevent loop from exiting
function async:start(loop, is_daemon) end

--- Stop async watcher.
---@param loop ev_loop event loop
function async:stop(loop) end

--- Send an async event to the loop.
---@param loop ev_loop event loop
function async:send(loop) end

---@class ev_child_status*
---@field status integer original wait status
---@field exited boolean Whether to exit normally
---@field stopped boolean Whether to stop
---@field signaled boolean Whether terminated by signal
---@field exit_status? integer exit code
---@field stop_signal? integer stop signal
---@field term_signal? integer Termination signal

---@class ev_child: ev_watcher
local child = {}

--- Start child watcher.
---@param loop ev_loop event loop
---@param is_daemon? boolean Whether to prevent loop from exiting
function child:start(loop, is_daemon) end

--- Stop child watcher.
---@param loop ev_loop event loop
function child:stop(loop) end

--- Returns the monitored pid, 0 means any pid.
---@return integer pid pid
function child:getpid() end

--- Returns the pid that triggered the state change.
---@return integer rpid pid
function child:getrpid() end

--- Returns child process status information.
---@return ev_child_status status status table
function child:getstatus() end

---@class ev_stat_attr*
---@field dev integer
---@field ino integer
---@field mode integer
---@field nlink integer
---@field uid integer
---@field gid integer
---@field rdev integer
---@field size integer
---@field atime integer
---@field mtime integer
---@field ctime integer

---@class ev_stat_data*
---@field path string Listening path
---@field interval number Check interval
---@field attr ev_stat_attr Current stat information
---@field prev ev_stat_attr Last stat information

---@class ev_stat: ev_watcher
local stat = {}

--- Start stat watcher.
---@param loop ev_loop event loop
---@param is_daemon? boolean Whether to prevent loop from exiting
function stat:start(loop, is_daemon) end

--- Stop stat watcher.
---@param loop ev_loop event loop
function stat:stop(loop) end

--- Return listening path and stat data.
---@return ev_stat_data data stat data
function stat:getdata() end

---@class ev_Loop*
---@field default ev_loop Default event loop
local Loop = {}

--- Create a new non-default event loop.
---@param flags? integer libev backend/flag bit, default EVFLAG_AUTO
---@return ev_loop loop event loop
function Loop.new(flags) end

Loop.default = loop

---@class ev_Timer*
local Timer = {}

--- Create timer watcher.
---@param callback ev_timer_callback callback function
---@param after_seconds number Seconds to first trigger
---@param repeat_seconds? number Number of seconds to retrigger; omitted or 0 means trigger only once
---@return ev_timer timer timer watcher
function Timer.new(callback, after_seconds, repeat_seconds) end

---@class ev_IO*
local IO = {}

--- Create IO watcher.
---@param callback ev_io_callback callback function
---@param fd integer file descriptor
---@param revents integer `ev.READ`、`ev.WRITE` or a combination of both
---@return ev_io io IO watcher
function IO.new(callback, fd, revents) end

---@class ev_Signal*
local Signal = {}

--- Create signal watcher.
---@param callback ev_signal_callback callback function
---@param signal_number integer signal number
---@return ev_signal signal signal watcher
function Signal.new(callback, signal_number) end

---@class ev_Idle*
local Idle = {}

--- Create idle watcher.
---@param callback ev_idle_callback callback function
---@return ev_idle idle idle watcher
function Idle.new(callback) end

---@class ev_Async*
local Async = {}

--- Create async watcher.
---@param callback ev_async_callback callback function
---@return ev_async async async watcher
function Async.new(callback) end

---@class ev_Child*
local Child = {}

--- Create child watcher.
---@param callback ev_child_callback callback function
---@param pid integer Listening pid, 0 means any pid
---@param trace boolean Whether to monitor stop/continue status
---@return ev_child child child watcher
function Child.new(callback, pid, trace) end

---@class ev_Stat*
local Stat = {}

--- Create stat watcher.
---@param callback ev_stat_callback callback function
---@param path string Listening path
---@param interval? number Check interval, determined by libev by default
---@return ev_stat stat stat watcher
function Stat.new(callback, path, interval) end

---@class ev*
---@field Loop ev_Loop
---@field Timer ev_Timer
---@field IO ev_IO
---@field Signal ev_Signal
---@field Idle ev_Idle
---@field Async ev_Async
---@field Child ev_Child
---@field Stat ev_Stat
---@field CHILD integer child event bit
---@field IDLE integer idle event bit
---@field ASYNC integer async event bit
---@field MINPRI integer lowest watcher priority
---@field MAXPRI integer Highest watcher priority
---@field READ integer IO read event bit
---@field SIGNAL integer signal event bit
---@field STAT integer stat event bit
---@field TIMEOUT integer timer timeout event bit
---@field WRITE integer IO write event bit
---@field SIGABRT integer
---@field SIGALRM integer
---@field SIGBUS integer
---@field SIGCHLD integer
---@field SIGCONT integer
---@field SIGFPE integer
---@field SIGHUP integer
---@field SIGINT integer
---@field SIGIO integer
---@field SIGIOT integer
---@field SIGKILL integer
---@field SIGPIPE integer
---@field SIGPOLL? integer
---@field SIGPROF integer
---@field SIGPWR? integer
---@field SIGQUIT integer
---@field SIGSEGV integer
---@field SIGSTKFLT? integer
---@field SIGSYS integer
---@field SIGTERM integer
---@field SIGTRAP integer
---@field SIGTSTP integer
---@field SIGTTIN integer
---@field SIGTTOU integer
---@field SIGURG integer
---@field SIGUSR1 integer
---@field SIGUSR2 integer
---@field SIGVTALRM integer
---@field SIGWINCH integer
---@field SIGXCPU integer
---@field SIGXFSZ integer
---@type ev*
local M = {}

M.Loop = Loop
M.Timer = Timer
M.IO = IO
M.Signal = Signal
M.Idle = Idle
M.Async = Async
M.Child = Child
M.Stat = Stat

M.CHILD = 0x00000800
M.IDLE = 0x00002000
M.ASYNC = 0x00080000
M.MINPRI = -2
M.MAXPRI = 2
M.READ = 0x01
M.SIGNAL = 0x00000400
M.STAT = 0x00001000
M.TIMEOUT = 0x00000100
M.WRITE = 0x02

M.SIGABRT = 6
M.SIGALRM = 14
M.SIGBUS = 10
M.SIGCHLD = 20
M.SIGCONT = 19
M.SIGFPE = 8
M.SIGHUP = 1
M.SIGINT = 2
M.SIGIO = 23
M.SIGIOT = 6
M.SIGKILL = 9
M.SIGPIPE = 13
M.SIGPOLL = 7
M.SIGPROF = 27
M.SIGQUIT = 3
M.SIGSEGV = 11
M.SIGSYS = 12
M.SIGTERM = 15
M.SIGTRAP = 5
M.SIGTSTP = 18
M.SIGTTIN = 21
M.SIGTTOU = 22
M.SIGURG = 16
M.SIGUSR1 = 30
M.SIGUSR2 = 31
M.SIGVTALRM = 26
M.SIGWINCH = 28
M.SIGXCPU = 24
M.SIGXFSZ = 25

--- Return the runtime libev version.
---@return integer major major version
---@return integer minor minor version
function M.version() end

--- Returns the current number of registered objects, mainly used for testing leaks.
---@return integer count Number of objects
function M.object_count() end

return M
