You are not logged in.

#1 2013-02-07 17:19:26

pdizzle
Member
Registered: 2013-02-07
Posts: 15

[SOLVED] Awesome 3.5 cpu widget attempt

I'm just trying to adapt the cpu widget found here: http://awesome.naquadah.org/wiki/CPU_Usage
to be usable on awesome 3.5

here is the original code from the link above:

   jiffies = {}
   function activecpu()
       local s = ""
       for line in io.lines("/proc/stat") do
           local cpu, newjiffies = string.match(line, "(cpu%d*)\ +(%d+)")
           if cpu and newjiffies then
               if not jiffies[cpu] then
                   jiffies[cpu] = newjiffies
               end
               --The string.format prevents your task list from jumping around 
               --when CPU usage goes above/below 10%
               s = s .. " " .. cpu .. ": " .. string.format("%02d", newjiffies-jiffies[cpu]) .. "% "
               jiffies[cpu] = newjiffies
           end
       end
       return s
   end

followed by this in rc.lua to call it every second:

awful.hooks.timer.register(1, function() cpuinfo.text = activecpu() end)

what follows is my current setup as i attempted to adapt it

awesome 3.5 didn't like the escape sequence so i had to change that to %s for the space. i adapted it a bit so it takes up less screen space and just displays as ##.##.##.##.##... etc. i have little/no computer programming experience aside from tooling around so please excuse my ignorance but heres what i came up with:

-- CPU widget from http://awesome.naquadah.org/wiki/CPU_Usage
-- attempted adaptation to awesome 3.5


-- {{ define wibox
cpuwidget = wibox.widget.textbox()
cpuwidget:set_align("right")
-- }}

-- {{ function
jiffies = {}

function activecpu()
        local cpus = ""
        for line in io.lines("/proc/stat") do
                local cpu, newjiffies = string.match(line, "(cpu%d*)%s+(%d+)")
                if cpu and newjiffies then
                        if not jiffies[cpu] then
                                jiffies[cpu] = newjiffies
                        end
                        --The string.format prevents your task list from jumping around 
                        --when CPU usage goes above/below 10%
                        cpus = cpus .. "." .. string.format("%02d", newjiffies-jiffies[cpu])
                        jiffies[cpu] = newjiffies
                end
        end
        cpuwidget:set_markup(cpus)
        --return s
end

-- }}

and in rc.lua to call it, based on the timer configuration in a battery widget on the awesomewiki (http://awesome.naquadah.org/wiki/Gigamo_Battery_Widget):

cpu_timer = timer({timeout = 1})
cpu_timer:connect_signal("cputimeout", function() activecpu() end)
cpu_timer:start()

with the current configuration, there are no naughty errors returned, but the widget doesn't display at all. If there are more useful log files somewhere, could you point me to them? if i tried calling it with the awful.hooks.timer setup, my X crashed immediately. I can't seem to be able to set the text = activecpu function, which was set to return the final string (?). I instead attempted to set the value of the text within the widget every time the function is called using set_markup, again based on the battery widget. thanks in advance for your help and patience.

Last edited by pdizzle (2013-02-08 01:45:28)

Offline

#2 2013-02-07 17:21:53

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 20,065
Website

Re: [SOLVED] Awesome 3.5 cpu widget attempt

Moving to Programming and Scripting...


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#3 2013-02-08 01:43:11

pdizzle
Member
Registered: 2013-02-07
Posts: 15

Re: [SOLVED] Awesome 3.5 cpu widget attempt

solved: turns out the major error was in my adding the widget, and seemingly the original syntax no longer worked as well. here is the finished result, in hopes that it helps someone:

-- CPU widget from http://awesome.naquadah.org/wiki/CPU_Usage
-- adapted to awesome 3.5 by pdizzle

-- {{ define wibox
cpuwidget = wibox.widget.textbox()
cpuwidget:set_align("right")
-- }}

-- {{ function
jiffies = {}

function activecpu()
        cpusage = ""
        for line in io.lines("/proc/stat") do
                local cpu, newjiffies = string.match(line, "(cpu%d*)%s+(%d+)")
                if cpu and newjiffies then
                        if not jiffies[cpu] then
                                jiffies[cpu] = newjiffies
                        end
                        --The string.format prevents your task list from jumping around 
                        --when CPU usage goes above/below 10%
                        cpusage = cpusage .. "." .. string.format("%02d", newjiffies-jiffies[cpu])
                        jiffies[cpu] = newjiffies
                end
        end
        cpuwidget:set_markup(cpusage)
end

-- }}

in rc.lua at the end to call a timer:

cpu_timer = timer({timeout = 1})
cpu_timer:connect_signal("timeout", function() activecpu() end)
cpu_timer:start()

where i had made the major error is there are 2 similar sections of what i believe was the default rc.lua, the first looks something like (do add systray widgets here):

 -- Create the wibox
    mywibox[s] = awful.wibox({ position = "top", screen = s })

    -- spacer widget
    spacerwidget = wibox.widget.textbox("|")
    spacerwidget:set_align("right")

    -- Widgets that are aligned to the left
    local left_layout = wibox.layout.fixed.horizontal()
    left_layout:add(mytaglist[s])
    left_layout:add(mypromptbox[s])

    -- Widgets that are aligned to the right
    local right_layout = wibox.layout.fixed.horizontal()
    if s == 1 then right_layout:add(wibox.widget.systray()) end
    right_layout:add(cpuwidget) --                        <==                      this is where you add it
    right_layout:add(mytextclock)
    right_layout:add(mylayoutbox[s])

    -- Now bring it all together (with the tasklist in the middle)
    local layout = wibox.layout.align.horizontal()
    layout:set_left(left_layout)
    layout:set_middle(mytasklist[s])
    layout:set_right(right_layout)

    mywibox[s]:set_widget(layout)

the second one is another table named right_layout that i was adding the widget to, and it seems to reference the buttons on titlebars of windows, if they are enabled. woops. no widgets should go there obviously:

    if titlebars_enabled and (c.type == "normal" or c.type == "dialog") then
        -- Widgets that are aligned to the left
        local left_layout = wibox.layout.fixed.horizontal()
        left_layout:add(awful.titlebar.widget.iconwidget(c))

        -- Widgets that are aligned to the right
        local right_layout = wibox.layout.fixed.horizontal()
        right_layout:add(awful.titlebar.widget.floatingbutton(c))
        right_layout:add(awful.titlebar.widget.maximizedbutton(c))
        right_layout:add(awful.titlebar.widget.stickybutton(c))
        right_layout:add(awful.titlebar.widget.ontopbutton(c))
        right_layout:add(awful.titlebar.widget.closebutton(c))
..
..

after the fact, i see that the rc.lua part was pretty obvious but still worth including if it helps others

Last edited by pdizzle (2013-02-08 01:51:23)

Offline

Board footer

Powered by FluxBB