我可以在 AwesomeWM 中 运行 间隔计时器吗?
Can I run interval timers in AwesomeWM?
我在 Manjaro 上使用 Awesome 4.3 Linux (Arch) 我从 KDE 桌面换成了 XFCE 桌面。我写了一个壁纸切换器,可以在四个独立的屏幕上随机更换壁纸。出于开发目的,我在标准 Lua 文件中编写了随机例程。我将它添加到 rc.lua,但 Lua 计时器 (luv) 似乎不是 rc.lua 中的 运行。我分别检查了所有方法以从我的随机化例程中加载墙纸,当我直接为四个屏幕调用一组文件时,它们工作得很好。一旦我添加了间隔计时器,什么也没有发生,没有错误,什么也没有。
这是简化为仅加载一个屏幕的代码,我是否遗漏了什么,取出很棒的对象并替换为 print 语句,它在标准 Lua 文件
中完美运行
感谢任何帮助,不胜感激
local lfs = require('lfs') -- lua file system
local uv = require('luv') -- lua timer
local dir = "/home/jerry/Documents/wallpaper"
local images = {} -- images collection
local idx = 0
math.randomseed(os.time())
local function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
local function readImages(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
images[idx] = path .. '/' .. file
idx = idx + 1
end
end
end
readImages(dir);
imageCount = tablelength(images)
local function getImage()
number = math.random(1, imageCount)
return images[number]
end
local function setInterval(interval, callback)
local timer = uv.new_timer()
uv.timer_start(timer, 0, interval, callback)
return timer
end
local function cb()
local path = getImage()
gears.wallpaper.maximized(path, screen[1], true)
end
timer1 = setInterval(1000, cb)
local function cb(gears, getImage, screen, screenIndex)
return function()
print(gears)
print(getImage())
print(screen)
print("Screen " .. screenIndex)
return gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
end
end
您忘记打电话给 uv.run()
。那调用 运行s 计时器。
但是,它也不会 运行 其他任何东西,因此 AwesomeWM 将停止工作。
uv.run()
运行是一个主循环。此循环等待事件(例如:计时器到期)然后处理该事件。 AwesomeWM 还使用主循环。它 运行 是 GLib 的 GMainLoop
。
由于您不能同时运行 两个主循环,因此您将不得不使用 GLib 和 AwesomeWMs 工具。
我为 luv timer_start
找到了一些文档:https://github.com/luvit/luv/blob/master/docs.md#uvtimer_starttimer-timeout-repeat-callback
据此,您的 timer_start(timer, 0, interval, callback)
意味着计时器立即触发,然后每秒触发一次。这个的 AwesomeWM 版本是:
local gears = require("gears") -- This is part of AwesomeWM
local timer1 = gears.start_new(1, function()
print("I run once per second")
return true -- Signal that the timer should continue running
end)
timer1:emit_signal("timeout") -- Cause the callback function to run right now
gears.timer
使用 GLib 来 运行 计时器:https://github.com/awesomeWM/awesome/blob/87e7b84ff54c050f86541421ec0aa93e325dd49d/lib/gears/timer.lua#L94
这是使用@uli schlachter 的建议完成的实现,但我发现它似乎周期性地吸收大量 ram 和 CPU。如果有人知道它的原因会很棒,这比 bash 或其他实现要容易得多。
-- wallpaper changer routine
local lfs = require('lfs') -- lua file system
local dir = "/home/jerry/Documents/wallpaper" --images location
local images = {} -- images collection
local idx = 0 -- index for the image collection
math.randomseed(os.time())
-- function to generate the image count for the randomize function
local function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- function to read the images from the directory
local function readImages(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
images[idx] = path .. '/' .. file
idx = idx + 1
end
end
end
-- execute read the images
readImages(dir);
-- grab the image count
imageCount = tablelength(images)
-- Generate the random image path
local function getImage()
number = math.random(1, imageCount)
return images[number]
end
-- call back for timer
local function cb(screenIndex)
return function()
print(getImage())
gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
return true;
end
end
-- partial application of the
-- call back for the timer
-- pre-apply the screen index to create the
-- four separate cb's one for each screen
cb1 = cb(1)
cb2 = cb(2)
cb3 = cb(3)
cb4 = cb(4)
-- create the timers with unique time spans
-- to sell the ramdomized approach
local timer1 = gears.timer.start_new(14, cb1)
local timer2 = gears.timer.start_new(16, cb2)
local timer3 = gears.timer.start_new(18, cb3)
local timer4 = gears.timer.start_new(20, cb4)
-- trigger the timers
timer1:emit_signal("timeout")
timer2:emit_signal("timeout")
timer3:emit_signal("timeout")
timer4:emit_signal("timeout")
调用垃圾收集器的回调的最终版本
-- call back for timer
local function cb(screenIndex)
return function()
gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
collectgarbage("step", 4000)
return true;
end
end
我在 Manjaro 上使用 Awesome 4.3 Linux (Arch) 我从 KDE 桌面换成了 XFCE 桌面。我写了一个壁纸切换器,可以在四个独立的屏幕上随机更换壁纸。出于开发目的,我在标准 Lua 文件中编写了随机例程。我将它添加到 rc.lua,但 Lua 计时器 (luv) 似乎不是 rc.lua 中的 运行。我分别检查了所有方法以从我的随机化例程中加载墙纸,当我直接为四个屏幕调用一组文件时,它们工作得很好。一旦我添加了间隔计时器,什么也没有发生,没有错误,什么也没有。
这是简化为仅加载一个屏幕的代码,我是否遗漏了什么,取出很棒的对象并替换为 print 语句,它在标准 Lua 文件
中完美运行感谢任何帮助,不胜感激
local lfs = require('lfs') -- lua file system
local uv = require('luv') -- lua timer
local dir = "/home/jerry/Documents/wallpaper"
local images = {} -- images collection
local idx = 0
math.randomseed(os.time())
local function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
local function readImages(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
images[idx] = path .. '/' .. file
idx = idx + 1
end
end
end
readImages(dir);
imageCount = tablelength(images)
local function getImage()
number = math.random(1, imageCount)
return images[number]
end
local function setInterval(interval, callback)
local timer = uv.new_timer()
uv.timer_start(timer, 0, interval, callback)
return timer
end
local function cb()
local path = getImage()
gears.wallpaper.maximized(path, screen[1], true)
end
timer1 = setInterval(1000, cb)
local function cb(gears, getImage, screen, screenIndex)
return function()
print(gears)
print(getImage())
print(screen)
print("Screen " .. screenIndex)
return gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
end
end
您忘记打电话给 uv.run()
。那调用 运行s 计时器。
但是,它也不会 运行 其他任何东西,因此 AwesomeWM 将停止工作。
uv.run()
运行是一个主循环。此循环等待事件(例如:计时器到期)然后处理该事件。 AwesomeWM 还使用主循环。它 运行 是 GLib 的 GMainLoop
。
由于您不能同时运行 两个主循环,因此您将不得不使用 GLib 和 AwesomeWMs 工具。
我为 luv timer_start
找到了一些文档:https://github.com/luvit/luv/blob/master/docs.md#uvtimer_starttimer-timeout-repeat-callback
据此,您的 timer_start(timer, 0, interval, callback)
意味着计时器立即触发,然后每秒触发一次。这个的 AwesomeWM 版本是:
local gears = require("gears") -- This is part of AwesomeWM
local timer1 = gears.start_new(1, function()
print("I run once per second")
return true -- Signal that the timer should continue running
end)
timer1:emit_signal("timeout") -- Cause the callback function to run right now
gears.timer
使用 GLib 来 运行 计时器:https://github.com/awesomeWM/awesome/blob/87e7b84ff54c050f86541421ec0aa93e325dd49d/lib/gears/timer.lua#L94
这是使用@uli schlachter 的建议完成的实现,但我发现它似乎周期性地吸收大量 ram 和 CPU。如果有人知道它的原因会很棒,这比 bash 或其他实现要容易得多。
-- wallpaper changer routine
local lfs = require('lfs') -- lua file system
local dir = "/home/jerry/Documents/wallpaper" --images location
local images = {} -- images collection
local idx = 0 -- index for the image collection
math.randomseed(os.time())
-- function to generate the image count for the randomize function
local function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- function to read the images from the directory
local function readImages(path)
for file in lfs.dir(path) do
if file ~= "." and file ~= ".." then
images[idx] = path .. '/' .. file
idx = idx + 1
end
end
end
-- execute read the images
readImages(dir);
-- grab the image count
imageCount = tablelength(images)
-- Generate the random image path
local function getImage()
number = math.random(1, imageCount)
return images[number]
end
-- call back for timer
local function cb(screenIndex)
return function()
print(getImage())
gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
return true;
end
end
-- partial application of the
-- call back for the timer
-- pre-apply the screen index to create the
-- four separate cb's one for each screen
cb1 = cb(1)
cb2 = cb(2)
cb3 = cb(3)
cb4 = cb(4)
-- create the timers with unique time spans
-- to sell the ramdomized approach
local timer1 = gears.timer.start_new(14, cb1)
local timer2 = gears.timer.start_new(16, cb2)
local timer3 = gears.timer.start_new(18, cb3)
local timer4 = gears.timer.start_new(20, cb4)
-- trigger the timers
timer1:emit_signal("timeout")
timer2:emit_signal("timeout")
timer3:emit_signal("timeout")
timer4:emit_signal("timeout")
调用垃圾收集器的回调的最终版本
-- call back for timer
local function cb(screenIndex)
return function()
gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
collectgarbage("step", 4000)
return true;
end
end