
---- An object implementing global key functions ----
-------------------------------------------------------------------------------

GlobalCallbacks = {
	funcs = {},
	ctrlread = Controls.read,
	fakeread = NILFUNC
}

function GlobalCallbacks:register(f)
	table.insert(self.funcs, f)
end

function GlobalCallbacks:remove(f)
	for i, v in pairs(self.funcs) do
		if v == f then
			table.remove(self.funcs, i)
		end
	end
end

function GlobalCallbacks:clear()
	self.funcs = {}
end

function GlobalCallbacks:read()
	local ctrl = self.ctrlread()
	Controls.read = self.ctrlread
	for _, v in pairs(self.funcs) do
		v(ctrl)
	end
	Controls.read = self.fakeread
	return ctrl
end

function GlobalCallbacks:cleanup()
	Controls.read = self.ctrlread
end

GlobalCallbacks.fakeread = function() 
	return GlobalCallbacks:read() 
end

Controls.read = GlobalCallbacks.fakeread
