在 AwesomeWM 中可以运行间隔计时器吗?

我正在 Manjaro Linux(Arch)上使用 Awesome 4.3。我从 KDE 桌面转变为 XFCE 桌面。我编写了一款壁纸切换器,可以在四个不同的屏幕上随机更换壁纸。我在一般的 Lua 文件中为开发目的编写了一个随机排序程序。我将它添加到了 rc.lua 中,但似乎 Lua 计时器(luv)没有在 rc.lua 中运行。我可以分别检查所有方法以从我的随机排序程序中加载壁纸,当我直接调用一个文件集来对四个屏幕进行改变时,它们都可以完美地运行。但是一旦添加了间隔计时器,就没有任何反应,没有错误,什么都没有发生。

这是只加载一个屏幕的简化代码,我漏掉了什么吗?如果将 awesome 对象去掉并替换为打印语句,则在一般的 Lua 文件中它可以正常运行

谢谢,任何帮助都将不胜感激

local lfs = require('lfs') -- lua 文件系统
local uv = require('luv') -- lua 计时器
local dir = "/home/jerry/Documents/wallpaper"
local images = {} -- 图片集合
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("屏幕 " .. screenIndex)
        return gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
    end
end
点赞
用户436275
用户436275

你忘了调用uv.run()。这个调用运行了定时器。然而,它也不会运行其他任何东西,因此 AwesomeWM 将停止工作。

uv.run() 运行一个主循环。这个循环等待事件(比如:一个定时器到期),然后处理该事件。AwesomeWM 也使用一个主循环,它运行 GLib 的 GMainLoop

由于您不能同时运行两个主循环,因此您需要使用 GLib 和 AwesomeWM 的工具。

我在 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") -- 这是 AwesomeWM 的一部分
local timer1 = gears.start_new(1, function()
    print("我每秒运行一次")
    return true -- 表示定时器应继续运行
end)
timer1:emit_signal("timeout") -- 使回调函数立即运行

gears.timer 使用 GLib 运行定时器:https://github.com/awesomeWM/awesome/blob/87e7b84ff54c050f86541421ec0aa93e325dd49d/lib/gears/timer.lua#L94

2020-04-19 05:29:59
用户1792817
用户1792817

这是使用 @uli schlachter 的建议完成的实现。然而,我发现它似乎会定期消耗大量 RAM 和 CPU。如果有人知道原因,那就太好了,这比 bash 或其他实现要简单得多。

-- 壁纸更换程序
local lfs = require('lfs') -- lua 文件系统
local dir = "/home/jerry/Documents/wallpaper" -- 图片位置
local images = {} -- 图片集合
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 cb(screenIndex)
    return function()
        print(getImage())
        gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
        return true;
    end
end
-- 部分应用程序
-- 用于计时器的回调函数
-- 预应用屏幕索引以创建四个分别用于每个屏幕的 cb 函数
cb1 = cb(1)
cb2 = cb(2)
cb3 = cb(3)
cb4 = cb(4)
-- 使用唯一的时间间隔创建计时器
-- 以卖出随机化方法
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)
-- 触发计时器
timer1:emit_signal("timeout")
timer2:emit_signal("timeout")
timer3:emit_signal("timeout")
timer4:emit_signal("timeout")

带垃圾收集器调用的回调函数的最终版本

-- 用于计时器的回调函数
local function cb(screenIndex)
    return function()
        gears.wallpaper.maximized(getImage(), screen[screenIndex], true)
        collectgarbage("step", 4000)
        return true;
    end
end
2020-04-22 14:42:06
用户1248175
用户1248175

强大的gears库有一个timer功能:

local gears = require("gears")
local naughty = require("naughty")

gears.timer {
    timeout   = 10,
    call_now  = true,
    autostart = true,
    callback  = function()
        naughty.notify({ title = "This runs every 10 seconds" })
    end
}

https://awesomewm.org/doc/api/classes/gears.timer.html

2022-12-24 17:08:55