SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#301 2012-07-22 22:25:07

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

ive been fiddling with my clickfunction
you dont need a second xwininfo command to get the window id

i did it like this

if click_start==1 then
click_lines={}
f = io.popen("xwininfo -name '"..conky_name.."'")
    for line in f:lines() do
    table.insert(click_lines,line)
    end
f:close()
    s,f,abstlx=string.find(click_lines[4],"X%p%s*([%p%d]*)")
    s,f,abstly=string.find(click_lines[5],"Y%p%s*([%p%d]*)")
    s,f,winid=string.find(click_lines[2],"Window id:%s*(%d%a[%d%a]*)")
click_start=nil
end

i try and do everything i can using native lue commands rather than "outside" programs like grep or tail
which is why i went for this method

Offline

Help fund CrunchBang, donate to the project!

#302 2012-07-22 22:34:43

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

mrpeachy wrote:

ive been fiddling with my clickfunction
you dont need a second xwininfo command to get the window id

i did it like this

if click_start==1 then
click_lines={}
f = io.popen("xwininfo -name '"..conky_name.."'")
    for line in f:lines() do
    table.insert(click_lines,line)
    end
f:close()
    s,f,abstlx=string.find(click_lines[4],"X%p%s*([%p%d]*)")
    s,f,abstly=string.find(click_lines[5],"Y%p%s*([%p%d]*)")
    s,f,winid=string.find(click_lines[2],"Window id:%s*(%d%a[%d%a]*)")
click_start=nil
end

i try and do everything i can using native lue commands rather than "outside" programs like grep or tail
which is why i went for this method

"xwininfo -name 'conky'" gets you a different window id than the one returned by "getmouselocation" from xdotool.
The command you are using will also give you window id numbers in hexadecimal and not decimal.

I made a post about that a few days ago that you might not have read.

arclance wrote:

It looks like this is not possible because "behave %@ mouse-click getmouselocation" returns a different number from the commands that do not use the mouse.

Using that I get

x:387 y:711 screen:0 window:13660277

But "xdotool search --name 'system_Conky'" returns "29360129".

"xwininfo -name 'system_Conky'" agrees with this.
It gives "0x1c00001" in hex which is "29360129" in dec.

"xwininfo -int -tree -name 'system_Conky'" returns this which has the window id that matches the xdotool output, the parent id.

xwininfo: Window id: 31457281 "system_Conky"

  Root window id: 634 (the root window) (has no name)
  Parent window id: 14309537 (has no name)
     0 children.

I just used grep to make it easier to write this line for a quick test.

s,f,xdotool_window_number = string.find(winData,"(%d+)%s")

You can get rid of it by changing the string.find() to work on the full output.

You can get all the information in one command like this.

io.popen("xwininfo -all -int -tree -name '"..conky_name.."'")

whose output looks like this.

xwininfo: Window id: 31457281 "system_Conky"

  Root window id: 634 (the root window) (has no name)
  Parent window id: 14309537 (has no name)
     0 children.

  Absolute upper-left X:  -1
  Absolute upper-left Y:  -2
  Relative upper-left X:  0
  Relative upper-left Y:  0
  Width: 622
  Height: 1102
  Depth: 24
  Visual: 0x21
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x20 (installed)
  Bit Gravity State: ForgetGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +-1+-2  -2979+-2  -2979--20  +-1--20
  -geometry 622x1102+-1+-2

  Bit gravity: ForgetGravity
  Window gravity: NorthWestGravity
  Backing-store hint: NotUseful
  Backing-planes to be preserved: 0xffffffff
  Backing pixel: 0
  Save-unders: No

  Someone wants these events:
      KeyPress
      ButtonPress
      ButtonRelease
      PointerMotion
      Exposure
      StructureNotify
      SubstructureNotify
      FocusChange
      PropertyChange
  Do not propagate these events:
      ButtonPress
      ButtonRelease
      ButtonMotion
  Override redirection?: No

  Window manager hints:
      Client accepts input or input focus: No
      Initial state is Normal State
      Displayed on all desktops
      Window type:
          Normal
      Window state:
          Sticky
          Below
          Skip Taskbar
          Skip Pager
      Process id: (unknown) on host TheForge
      Frame extents: 0, 0, 0, 0

  No normal window size hints defined
  No zoom window size hints defined

  No window shape defined
  No border shape defined

Last edited by arclance (2012-07-22 22:40:14)

Offline

#303 2012-07-22 22:45:51

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

it works for me

xwininfo -name 'conky_name' 

returns a window id eg

0x2a00002

then using

tonumber(0x2a00002)

returns

44040194

which is the same number i get from xdotool

x:864 y:180 screen:0 window:44040194

Last edited by mrpeachy (2012-07-22 22:46:37)

Offline

#304 2012-07-22 23:00:16

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

mrpeachy wrote:

it works for me

Well it does not work for me.

31457281 =/= 14309537

It might be due to us using different window mangers.

I guess we need more test subjects to see which output is more common and if the window manager affects it.

Could you run

xwininfo -all -int -tree -name 'conky_name'

and post the output so I can see what it looks like on your computer?

Offline

#305 2012-07-22 23:15:39

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

mcdowall@mcdowall-desktop:~/Desktop$ xwininfo -all -int -tree -name 'conky1'
xwininfo: Window id: 41943042 "conky1"

  Root window id: 349 (the root window) (has no name)
  Parent window id: 349 (the root window) (has no name)
     0 children.

  Absolute upper-left X:  825
  Absolute upper-left Y:  0
  Relative upper-left X:  825
  Relative upper-left Y:  0
  Width: 610
  Height: 410
  Depth: 32
  Visual: 0x23
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x2800001 (not installed)
  Bit Gravity State: ForgetGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +825+0  -5+0  -5-490  +825-490
  -geometry 610x410-5+0

  Bit gravity: ForgetGravity
  Window gravity: NorthWestGravity
  Backing-store hint: NotUseful
  Backing-planes to be preserved: 0xffffffff
  Backing pixel: 0
  Save-unders: No

  Someone wants these events:
      ButtonPress
      ButtonRelease
      EnterWindow
      LeaveWindow
      Exposure
      StructureNotify
      FocusChange
      PropertyChange
      ColormapChange
  Do not propagate these events:
  Override redirection?: No

  Window manager hints:
      Client accepts input or input focus: No
      Initial state is Normal State
      Displayed on all desktops
      Window type:
          Normal
      Window state:
          Skip Pager
          Skip Taskbar
          Below
          Sticky
      Process id: (unknown) on host mcdowall-desktop

  No normal window size hints defined
  No zoom window size hints defined

  No window shape defined
  No border shape defined

using gnome... edit - no im not im using cinnamon!

Last edited by mrpeachy (2012-07-23 01:33:06)

Offline

#306 2012-07-22 23:25:16

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

That's what I thought the "  Parent window id: " line is different in mine.

xwininfo -all -int -tree -name 'system_Conky'
xwininfo: Window id: 31457281 "system_Conky"

  Root window id: 634 (the root window) (has no name)
  Parent window id: 14309537 (has no name)
     0 children.

  Absolute upper-left X:  -1
  Absolute upper-left Y:  -2
  Relative upper-left X:  0
  Relative upper-left Y:  0
  Width: 622
  Height: 1102
  Depth: 24
  Visual: 0x21
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x20 (installed)
  Bit Gravity State: ForgetGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +-1+-2  -2979+-2  -2979--20  +-1--20
  -geometry 622x1102+-1+-2

  Bit gravity: ForgetGravity
  Window gravity: NorthWestGravity
  Backing-store hint: NotUseful
  Backing-planes to be preserved: 0xffffffff
  Backing pixel: 0
  Save-unders: No

  Someone wants these events:
      KeyPress
      ButtonPress
      ButtonRelease
      PointerMotion
      Exposure
      StructureNotify
      SubstructureNotify
      FocusChange
      PropertyChange
  Do not propagate these events:
      ButtonPress
      ButtonRelease
      ButtonMotion
  Override redirection?: No

  Window manager hints:
      Client accepts input or input focus: No
      Initial state is Normal State
      Displayed on all desktops
      Window type:
          Normal
      Window state:
          Sticky
          Below
          Skip Taskbar
          Skip Pager
      Process id: (unknown) on host TheForge
      Frame extents: 0, 0, 0, 0

  No normal window size hints defined
  No zoom window size hints defined

  No window shape defined
  No border shape defined

I am using fluxbox.

Maybe we should ask everyone in The New Monster Conky Thread to post the results for their conkys and what window manager they use to see what kind of output they get.

You could add a "get_window_id_from" variable at the top to choose between the two sources.

Offline

#307 2012-07-22 23:34:18

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

stoopid window managers

so you get a different number from

xwininfo -int -tree -name 'system_Conky'

and

xwininfo -name 'system_Conky' 

?

while i get the same from both
seems like

xwininfo -int -tree -name 'system_Conky'

is going to be the more universal

Last edited by mrpeachy (2012-07-22 23:34:40)

Offline

#308 2012-07-22 23:37:22

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

The problem is that for you this command

xdotool search --sync --classname 'conky' behave %@ mouse-click getmouselocation 2> /dev/null >> /tmp/xdo

returns the the click location with the window id number from this line

xwininfo: Window id: 31457281 "system_Conky"

but for me it returns the window id number from this line.

  Parent window id: 14309537 (has no name)

Here is a way to get the data from one io.popen() call.

    if main_start == 1 then--run at script start 
        --# get xdotool_window_number here
        local f = io.popen("xwininfo -all -int -tree -name '"..conkyWindowTitle.."'")
        local winData = f:read("*a")
        f:close()
        f = nil
        if winID_source = 0 then
            s,f,xdotool_window_number = string.find(winData,"Parent%swindow%sid.%s(%d+)%s")
        elseif winID_source = 1 then
            s,f,xdotool_window_number = string.find(winData,"xwininfo.%swindow%sid.%s(%d+)%s")
        else
            print("error: invalid winID_source")
        end --# if winID_source = 0 then
        s,f,abstlx = string.find(winData,"Absolute%supper.left%sX%p%s*(%p*%d*)")
        s,f,abstly = string.find(winData,"Absolute%supper.left%sY%p%s*(%p*%d*)")
        main_start = nil
    end--if main_start=1 ######################################

winID_source controls which place to get the window id from.
Set it to 1 to make it work on your computer, 0 works on my computer.

Last edited by arclance (2012-07-22 23:50:57)

Offline

#309 2012-07-23 00:08:23

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

Here is my example script with the adjustable window id source worked into it.
I also cleaned up some old lines leftover from a simplification I made before.

require 'cairo'

if tonumber(conky_parse('${updates}')) < 2 then --# don't clear buttons table if reloading lua script
    main_start = 1-- this starts the clickfunction
    buttons = {}--this table initially holds the values from the buttons
    conkyWindowTitle = "button_example" --# the title of the conky window (own_window_title) to make it easier to configure
    tempFileName = "xdo" --# the name of the temporary file to store mouse click locations in (makes configuring for use in more than one conky easier)
    winID_source = 0 --# settings for different window managers (maybe) 0 works for me in fluxbox, 1 works for mrpeachy in gnome
    abstlx = nil
    abstly = nil
    xdotool_window_number = nil
end --# if tonumber(conky_parse('${updates}')) < 2 then

function conky_main()
    if conky_window == nil then return end
    local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
    local cr = cairo_create(cs)
    local localx,localy,localnowx,localnowy = clickfunction()--this line activates the clickfunction and sets the click coordinates
    --#################################
    --###### Setup Buttons Here #######
    --#################################
    cairo_destroy(cr)
    cairo_surface_destroy(cs)
    cr = nil
    cs = nil
    local gcollect = collectgarbage ("collect") --# memory police
end --# function conky_main()

function clickfunction()
    --start click logging and calculations ##########################################
    local s = nil
    if main_start == 1 then--run at script start 
        --# get xdotool_window_number here
        local f = io.popen("xwininfo -all -int -tree -name '"..conkyWindowTitle.."'")
        local winData = f:read("*a")
        f:close()
        f = nil
        if winID_source = 0 then
            s,f,xdotool_window_number = string.find(winData,"Parent%swindow%sid.%s(%d+)%s")
        elseif winID_source = 1 then
            s,f,xdotool_window_number = string.find(winData,"xwininfo.%swindow%sid.%s(%d+)%s")
        else
            print("error: invalid winID_source")
        end --# if winID_source = 0 then
        s,f,abstlx = string.find(winData,"Absolute%supper.left%sX%p%s*(%p*%d*)")
        s,f,abstly = string.find(winData,"Absolute%supper.left%sY%p%s*(%p*%d*)")
        main_start = nil
    end--if main_start == 1 ######################################
    --click calculations #################################
    local f = io.popen("tail --lines=1 /tmp/"..tempFileName)
    local click = f:read("*a")
    f:close()
    f = nil
    local reset_xdotool_log = false
    local mousex = nil
    local mousey = nil
    if ((click == nil) or (click == "")) then
        print("error: could not read xdotool logfile")
        mousex = "0"
        mousey = "0"
    elseif (click:gsub("\n","") == "Command failed.") then
        print("error: xdotool error")
        mousex = "0"
        mousey = "0"
    else
        if string.find(click, xdotool_window_number) ~= nil then
            print("debug: click was in this window")
            s,f,mousex = string.find(click,"x%p(%d*)")
            s,f,mousey = string.find(click,"y%p(%d*)")
            reset_xdotool_log = true
        else
            --print("debug: click was not in this window")
            mousex = "0"
            mousey = "0"
        end --# if string.find(click, xdotool_window_number) ~= nil then
    end --# if ((click == nil) or (click == "")) then
    local localx = tonumber(mousex)-abstlx
    local localy = tonumber(mousey)-abstly
    --get now location
    local f = io.popen("xdotool getmouselocation 2> /dev/null")
    local mousenow = f:read("*a")
    f:close()
    f = nil
    local mousenowx = nil
    local mousenowy = nil
    if ((mousenow == nil) or (mousenow == "")) then
        print("error: could not get mouse location")
        mousenowx = "0"
        mousenowy = "0"
    else
        s,f,mousenowx = string.find(mousenow,"x%p(%d*)%s")
        s,f,mousenowy = string.find(mousenow,"y%p(%d*)%s")
    end --# if ((mousenow == nil) or (mousenow == "")) then
    local localnowx = tonumber(mousenowx)-abstlx
    local localnowy = tonumber(mousenowy)-abstly
    button_run_last(reset_xdotool_log)
    --END CLICK CALCULATIONS #################################
    return localx,localy,localnowx,localnowy
end --# function clickfunction()

function button_run_last(reset_xdotool_log)
    if reset_xdotool_log == true then
        local f = io.open("/tmp/"..tempFileName,"w")
        f:write("x:0 y:0 screen:0 window:0\n")
        f:close()
        f = nil
    end --# if reset_xdotool_log == true then
end --# function conky_run_last()

You might think about including this started with "lua_startup_hook" to reset xdotool every time you have to kill and restart a conky.

function conky_start_xdotool()
    os.execute("pkill -f \"xdotool search --sync --classname conky behave %@ mouse-click getmouselocation\"")
    os.execute("xdotool search --sync --classname 'conky' behave %@ mouse-click getmouselocation 2> /dev/null >> /tmp/xdo &")
end --# function conky_start_xdotool()

That way you won't have to manually kill and restart xdotool to make the conky window visible to xdotool again.

Last edited by arclance (2012-07-23 00:20:10)

Offline

#310 2012-07-23 01:08:05

falldown
#! Samurai
Registered: 2011-09-01
Posts: 1,726

Re: interactive conky

Not sure if this is of any help, but...

falldown@vaio:~$ xwininfo -all -int -tree -name 'conky'

xwininfo: Window id: 48234497 "conky"

  Root window id: 787 (the root window) (has no name)
  Parent window id: 12583215 (has no name)
     0 children.

  Absolute upper-left X:  1182
  Absolute upper-left Y:  5
  Relative upper-left X:  0
  Relative upper-left Y:  0
  Width: 253
  Height: 560
  Depth: 24
  Visual: 0x21
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x20 (installed)
  Bit Gravity State: ForgetGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +1182+5  -5+5  -5-335  +1182-335
  -geometry 253x560-5+5

  Bit gravity: ForgetGravity
  Window gravity: NorthWestGravity
  Backing-store hint: NotUseful
  Backing-planes to be preserved: 0xffffffff
  Backing pixel: 0
  Save-unders: No

  Someone wants these events:
      ButtonPress
      ButtonRelease
      Exposure
      StructureNotify
      SubstructureNotify
      PropertyChange
      ColormapChange
  Do not propagate these events:
      ButtonPress
      ButtonRelease
      ButtonMotion
  Override redirection?: No

  Window manager hints:
      Client accepts input or input focus: No
      Initial state is Normal State
      Displayed on all desktops
      Window type:
          Desktop
      Window state:
          Skip Taskbar
          Skip Pager
          Below
      Process id: (unknown) on host vaio
      Frame extents: 0, 0, 0, 0

  No normal window size hints defined
  No zoom window size hints defined

  No window shape defined
  No border shape defined

Using openbox.

Offline

#311 2012-07-23 01:13:13

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

falldown wrote:

Not sure if this is of any help, but...

falldown@vaio:~$ xwininfo -all -int -tree -name 'conky'

xwininfo: Window id: 48234497 "conky"

  Root window id: 787 (the root window) (has no name)
  Parent window id: 12583215 (has no name)

Using openbox.

Looks like yours is working like mine.

To confirm that could you run

xdotool search --sync --classname 'conky' behave %@ mouse-click getmouselocation 2> /dev/null

then click on one of your conkys and post the output here for comparison?

If the window manager is what causes the difference it would make sense that openbox and fluxbox do the same thing since they are both based on blackbox.

Offline

#312 2012-07-23 01:16:58

falldown
#! Samurai
Registered: 2011-09-01
Posts: 1,726

Re: interactive conky

Here you go..

falldown@vaio:~$ xdotool search --sync --classname 'conky' behave %@ mouse-click getmouselocation 2> /dev/null
x:1280 y:51 screen:0 window:12585826

Offline

#313 2012-07-23 01:26:14

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

instead of asking the user to pick a setting ive gotten both xwininfo numbers, window id and parent window id and compared them both to the xdotool number
like this

--blank file ####################################################################
if tonumber(xwin_winid)==tonumber(xdowinid) or tonumber(xwin_parentwinid)==tonumber(xdowinid) then
local f = io.open(click_data_file,"w")
f:write("")
f:close() 
end
--get now location #############################################################

that should cover both window manager types

Last edited by mrpeachy (2012-07-23 01:27:12)

Offline

#314 2012-07-23 01:26:20

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

falldown wrote:

Here you go..

falldown@vaio:~$ xdotool search --sync --classname 'conky' behave %@ mouse-click getmouselocation 2> /dev/null
x:1280 y:51 screen:0 window:12585826

Do you have more than one conky running?

I should have specified that you click on the same conky that generated the xwininfo output but it looks like you searched for a conky with the default "own_window_title" of "conky".

If you are running more than one conky like that there is no way to know which conky generated the xwininfo output.

Offline

#315 2012-07-23 01:29:28

falldown
#! Samurai
Registered: 2011-09-01
Posts: 1,726

Re: interactive conky

Yep running 2 conkys with window titles.. sorry.

Offline

#316 2012-07-23 01:31:09

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

^ It's okay I was about to change my post to say that but you answered to fast for me to do it.

If you kill all but one of the conkys and repeat both tests again you should get good data.

@ mrpeachy
Looks good, that is a more user friendly solution.

Last edited by arclance (2012-07-23 01:39:04)

Offline

#317 2012-07-23 19:41:28

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

also i though to get around the problem of having too many calls to read the same xdo log file... like this

--get now location and window id #############################################################
local f = io.popen("xdotool getmouselocation")
mousenow=string.lower(f:read())
f:close()
local s,f,xwinid=string.find(mousenow,"window%p(%d*)")
--read file if windowid matches
if tonumber(parentid)==tonumber(xwinid) or tonumber(windowid)==tonumber(xwinid) then
local f=io.open(click_data_file)
click=f:read()
f:close()
--blank file
local f = io.open(click_data_file,"w")
f:write("")
f:close() 
end--window id matching

so only if the mouse is in the corresponding conky window will the function read the file


here is my entire click function

function conky_conkyclick()
if conky_window == nil then return end
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
cr = cairo_create(cs)
--start click logging and calculations ##########################################
click_table={}
--###############################################################################
if tonumber(conky_parse("${updates}"))==2 then
local f=io.popen("xwininfo -all -int -tree -name '"..conky_name.."'")
windata = string.lower(f:read("*a"))
f:close()
local s,f,x=string.find(windata,"absolute%supper%pleft%sx%p%s*(%p*%d*)")
local s,f,y=string.find(windata,"absolute%supper%pleft%sy%p%s*(%p*%d*)")
local s,f,p=string.find(windata,"parent%swindow%sid%p%s(%d*)%s")
local s,f,w=string.find(windata,"xwininfo%p%swindow%sid%p%s(%d*)%s")
xwin={x,y,p,w}--dont blank
f=nil
windata=nil
end--if updates==2 ###########################################################

--click calculations ############################################################
--get now location and window id #############################################################
local f = io.popen("xdotool getmouselocation")
mousenow=string.lower(f:read())
f:close()
local s,f,mousenowx=string.find(mousenow,"x%p(%d*)%s")
local s,f,mousenowy=string.find(mousenow,"y%p(%d*)%s")
local s,f,xwinid=string.find(mousenow,"window%p(%d*)")
local localnowx=tonumber(mousenowx)-xwin[1]
local localnowy=tonumber(mousenowy)-xwin[2]
--read file if windowid matches
if tonumber(xwin[3])==tonumber(xwinid) or tonumber(xwin[4])==tonumber(xwinid) then
local f=io.open(click_data_file)
click=f:read()
f:close()
--blank file
local f = io.open(click_data_file,"w")
f:write("")
f:close() 
end--window id matching
--get click location
if click==nil then click="x:0 y:0 window:0 " end
local s,f,mousex=string.find(click,"x%p(%d*)%s")
local s,f,mousey=string.find(click,"y%p(%d*)%s")
local localx=tonumber(mousex)-xwin[1]
local localy=tonumber(mousey)-xwin[2]
--END CLICK CALCULATIONS #################################
click_table[1]=localx
click_table[2]=localy
click_table[3]=localnowx
click_table[4]=localnowy
mousenow=nil
click=nil
f=nil
cairo_destroy(cr)
cairo_surface_destroy(cs)
cr=nil
end-- end main function

it has the cr/cs setup stuff because its being called in the conkyrc code

im also finishing up "the ultimatest button generating script"
but i think its going to need me to write a tutorial before anyone will be able to use it big_smile

Last edited by mrpeachy (2012-07-23 19:49:41)

Offline

#318 2012-07-23 19:55:38

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

mrpeachy wrote:

also i though to get around the problem of having too many calls to read the same xdo log file... like this

Interesting idea, only read the click log file if the mouse is in the conky window.
That should make it work even better.

I have been running two conkys for a day using

xdotool search --sync --classname 'conky' behave %@ mouse-click getmouselocation 2> /dev/null >> /tmp/xdo &

and have not had any problems with the buttons but I will add this since it should reduce cpu use.
Probably not a detectable amount in my conkys though.

The only problem I have with that script is that you did not include a couple of checks I added that prevent crashes.

Since the crash conditions usually only last for one or two updates they keep you from having to restart the conky due to the buttons crashing the whole lua script (since the calculations need to run first) if you include them.

Last edited by arclance (2012-07-23 19:56:21)

Offline

#319 2012-07-23 20:23:25

arclance
#! Die Hard
Registered: 2012-03-29
Posts: 902

Re: interactive conky

arclance wrote:

The only problem I have with that script is that you did not include a couple of checks I added that prevent crashes.

Since the crash conditions usually only last for one or two updates they keep you from having to restart the conky due to the buttons crashing the whole lua script (since the calculations need to run first) if you include them.

Here is my example function with the crash checks labeled and explained.

if tonumber(conky_parse('${updates}')) < 2 then --# don't reset these global variables when changes are made to the lua script
    --#### Setup Buttons ####
    conkyWindowTitle = "system_Conky" --# the title of the conky window (own_window_title) to make it easier to configure
    tempFileName = "xdo" --# the name of the temporary file to store mouse click locations in (makes configuring for use in more than one conky easier)
    buttons = {} --# this table initially holds the values from the buttons
    --###### End Setup Buttons ######
end --# if tonumber(conky_parse('${updates}')) < 2 then

function conky_main()
    if conky_window == nil then return end
    local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
    local cr = cairo_create(cs)
    local localx,localy,localnowx,localnowy = clickfunction()--this line activates the clickfunction and sets the click coordinates
    --#################################
    --###### Setup Buttons Here #######
    --#################################
    cairo_destroy(cr)
    cairo_surface_destroy(cs)
    cr = nil
    cs = nil
    local gcollect = collectgarbage ("collect")
end

function clickfunction()
    --################################################################################################
    if tonumber(conky_parse('${updates}')) < 2 then
        local s = nil
        local f = io.popen("xwininfo -all -int -tree -name '"..conkyWindowTitle.."'")
        local winData = f:read("*a")
        f:close()
        f = nil
        s,f,xdotool_window_number = string.find(winData,"Parent%swindow%sid.%s(%d+)%s")
        s,f,abstlx = string.find(winData,"Absolute%supper.left%sX%p%s*(%p*%d*)")
        s,f,abstly = string.find(winData,"Absolute%supper.left%sY%p%s*(%p*%d*)")
        print("abstlx: "..tostring(abstlx).." abstly: "..tostring(abstly).." winNum: "..tostring(xdotool_window_number))
    end --# if tonumber(conky_parse('${updates}')) == 1 then
    --#################################################################################################
    --# start click logging and calculations ##########################################
    local s = nil
    --## Get Current Mouse Location
    local f = io.popen("xdotool getmouselocation 2> /dev/null")
    local mousenow = f:read("*a")
    f:close()
    f = nil
    local mousenowx = nil
    local mousenowy = nil
    local mousex = nil
    local mousey = nil
    if ((mousenow == nil) or (mousenow == "")) then --# check for failure of xdotool getmouselocation command (crashes script but usually only lasts one or two updates)
        print("error: could not get mouse location")
        mousenowx = "0"
        mousenowy = "0"
        mousex = "0"
        mousey = "0"
    elseif string.find(mousenow, xdotool_window_number) ~= nil then --# check if mouse is in this conky window
        print("debug: mouse is in this window")
        s,f,mousenowx = string.find(mousenow,"x%p(%d*)%s")
        s,f,mousenowy = string.find(mousenow,"y%p(%d*)%s")
        --#### Get Click Location
        f = io.popen("tail --lines=1 /tmp/"..tempFileName)
        local click = f:read("*a")
        f:close()
        f = nil
        if ((click == nil) or (click == "")) then --# xdotool log file does not exist or is empty (prevents crash before first click or if you specify the wrong file)
            print("error: could not read xdotool logfile")
            mousex = "0"
            mousey = "0"
        elseif (click:gsub("\n","") == "Command failed.") then --# this is does not crash xdotool (probably a communication error between xdotool and some part of the system)
            print("error: xdotool error")
            mousex = "0"
            mousey = "0"
        else --# no problems reading the xdotool log file
            if string.find(click, xdotool_window_number) ~= nil then --# last click was in this window
                print("debug: click was in this window")
                s,f,mousex = string.find(click,"x%p(%d*)")
                s,f,mousey = string.find(click,"y%p(%d*)")
                --### Reset xdotool log file
                f = io.open("/tmp/"..tempFileName,"w")
                f:write("x:0 y:0 screen:0 window:0\n")
                f:close()
                f = nil
            else --# last click was not in this window
                --print("debug: click not in this window")
                mousex = "0"
                mousey = "0"
            end --# if string.find(click, xdotool_window_number) ~= nil then
        end --# if ((click == nil) or (click == "")) then
    else --# mouse not in this window
        mousenowx = "0"
        mousenowy = "0"
        mousex = "0"
        mousey = "0"
    end --# if string.find(mousenow, xdotool_window_number) then
    local localx = tonumber(mousex) - abstlx
    local localy = tonumber(mousey) - abstly
    local localnowx = tonumber(mousenowx) - abstlx
    local localnowy = tonumber(mousenowy) - abstly
    --END CLICK CALCULATIONS #################################
    return localx,localy,localnowx,localnowy
end --# function clickfunction()

function conky_start_xdotool()
    os.execute("pkill -f \"xdotool search --sync --classname conky behave %@ mouse-click getmouselocation\"")
    os.execute("xdotool search --sync --classname 'conky' behave %@ mouse-click getmouselocation 2> /dev/null >> /tmp/xdo &")
end --# function conky_start_xdotool()

Edit: Updated with working version.

Last edited by arclance (2012-07-23 21:16:21)

Offline

#320 2012-07-25 18:28:30

falldown
#! Samurai
Registered: 2011-09-01
Posts: 1,726

Re: interactive conky

Peachy can I call the

conky_gradbar

&

conky_luacal

Within the weather_interactive.lua?
Similar to the way you call the

_G.weather_script = function()

??

I have a really cool template/design in my head for an interactive_allcombined.

Offline

#321 2012-07-25 19:10:36

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

@falldown
im not sure if that would cause a problem
they are set up to be called from the conkyrc so they all have function names beginning with conky_
and they all have the conky setup/exit lines

if conky_window == nil then return end
local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
local cr = cairo_create(cs)

other stuff

cairo_destroy(cr)
cairo_surface_destroy (cs)
cr=nil

however, it would be simple enough to copy the functions into the weather template file, probably put them after the template itself
then remove those conky related lines

it may not even matter at all that those lines are there
you could just copy the functions over and set them up in the template without any modification

--template file

conky_luacal({settings})

Offline

#322 2012-07-27 02:28:07

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

BUTTON SCRIPT HOW TO

PART 1 - this post!

PART 2 - http://crunchbanglinux.org/forums/post/245760/#p245760

PART 3 - http://crunchbanglinux.org/forums/post/245761/#p245761

PART 4 - http://crunchbanglinux.org/forums/post/245767/#p245767

ALSO - make sure you read the first post of this thread about getting xdotool

here are the files
https://dl.dropbox.com/u/19008369/inter … nky.tar.gz


UPDATE made some improvements to the script to eliminate errors and included a first draft restart script
UPDATE 7/30 - added image settings, see end of post
UPDATE 8/5 - added ability to send window name and click file path to the lua script from the rc.  this eliminates any need to edit the lua script
UPDATE 8/6 - removed the shell scripts from the archive.  Using the "lua_startup_hook" and "lua_shutdown_hook" conky setings to control xdotool instead
UPDATE 8/20 - added cs/cr setup code to conky_get_mouse function so that buttons set within lua will work!
UPDATE 8/20 - adjusted code in conky_start_xdo function, so that in conkyrc you send the function the click log file location direcrtly
UPDATE 9/13 - fixed error with startup function

UPDATE: startup function should be this

function conky_start_xdo(rc_info)--######################################################################################################################
click_data_file=rc_info or click_data_file
os.execute("pkill -f 'xdotool search --sync --classname conky behave %@ mouse-click getmouselocation'")
os.execute("xdotool search --sync --classname 'conky' behave %@ mouse-click getmouselocation 2> /dev/null >> "..click_data_file.." &")
rc_info=nil
end--of function ##################################################################################################################################

IN THE ARCHIVE you will find the following files:
interactive_conky is the conkyrc file
button_script.lua is the lua button script

also in the archive is lua_function.lua which are explained in the included conkyrc

button_script.lua
you no longer need to edit this script as you can send the required information to the script from the conkyrc

however, you can enter the information directly into the lua if you like

conky_name="interactiveconkyrc"
click_data_file = "/tmp/xdo"

conky_name must be the same as:

own_window_title interactiveconkyrc

in interactive_conky

if using with another conky, that conky needs to have a UNIQUE title also

conky_data_file is the location of the file that contains the xdotool click logging data

USING THE BUTTON SCRIPT PART1

SETTINGS
here i will list, with a short description all the available options

MINIMUM REQUIRED SETTINGS
btype=1, --type, 1=on/off, 2=mouse over, 3=execute, 4=cycle
bname="test1", --set button name unique for each button
blx_off=200, --xpos when off
bly_off=200, --ypos when off
bw_off=100, --height when off
bh_off=100, --width when off

OPTIONAL SETTINGS FOR TYPES 1,2 AND 3 BUTTONS
st_type=0, --sets button to start 1=on or 0=off
--click area setup when button is on
blx_on=200, --xpos when on -- default is same as blx_off
bly_on=200, --ypos when on -- default is same as bly_off
bw_on=200, --height when on -- default is same as bw_off
bh_on=100, --height when on -- default is same as bh_off
draw=1, --1 for draw, 0 for no draw -- default is 0
--draw type settings
dtype_off=3, --draw type when off,1=fill, 2=outline,3=outline and fill -- default is 1
dtype_on=3, --draw type when on,1=fill, 2=outline,3=outline and fill --default is 1
--fill settings when button is off
dfxadj_off=0, --draw x position adjust for fill when off, rel to blx -- default = 0
dfyadj_off=0, --draw y position adjust for fill  when off, rel to bly -- default = 0
dfw_off=100, --draw width fill if off -- default is same as bw_off
dfh_off=100, --draw height fill if off -- default is same as bh_off
dfcr_off=20, --draw fill corner radius when off -- default is 0
dfcol_off=0xffffff, --draw fill color when off --default is 0xffffff white
dfal_off=1, --draw fill alpha when off -- default is 1, full opaque
--fill settings when button is on
dfxadj_on=0, --draw x position adjust for fill when on, rel to blx -- default = 0
dfyadj_on=0, --draw y positionadjust  for fill when on, rel to bly -- default = 0
dfw_on=200, --draw width fill if on -- default is same as bw_on
dfh_on=100, --draw height fill if on -- default is same as bh_on
dfcr_on=30, --draw fill corner radius when on -- default is 0
dfcol_on=0xff0000, --draw fill color when on --default is 0xff0000 red
dfal_on=1, --draw fill alpha when on -- default is 1, full opaque
--outline settings when button is off
doxadj_off=-1, --draw x position adjust for outline when off, rel to blx -- default = 0
doyadj_off=1, --draw y position adjust for outline when off, rel to bly -- default = 0
dow_off=101, --draw width outline if off -- default is same as bw_off
doh_off=101, --draw height outline if off -- default is same as bw_off
docr_off=0, --draw outline corner radius when off -- default is 0
docol_off=0x000000, --draw outline color when off --default is 0xffffff white
doal_off=1, --draw outline alpha when off -- default is 1, full opaque
dolw_off=2, --draw outline line width off -- default is 1
--outline on
doxadj_on=0, --draw x position for outline adjust when on
doyadj_on=0, --draw y position for outline adjust when on
dow_on=200, --draw width outline if on -- default is same as bw_on
doh_on=100, --draw height outline if on -- default is same as bh_on
docr_on=30, --draw outline corner radius when on -- default is 0
docol_on=0x0000ff, --draw outline color when on --default is 0xff0000 red
doal_on=1, --draw outline alpha when on -- default is 1, full opaque
dolw_on=2, --draw outline line width on -- default is 1
--text
text=1, -- 1 if want print text, 0 if no text -- default is 0
--text setup if on, set multiple lines with attributes using tables
txt_off={"button off"}, --text when button off -- default is "text"
font_off={"Mono"}, -- font when off -- default is "mono"
fs_off={"12"}, -- font size when off -- default is 12
fxadj_off={15}, -- font x position adjust when off -- default is 0
fyadj_off={-15}, -- font y position adjust when off -- default is 0
fcol_off={0x000000}, -- font color off -- default is 0xffffff
fal_off={1}, --font alpha off -- default is 1
--text setup when on
txt_on={"button on"}, --text when button on -- default is same as txt_off
font_on={"Sans"}, -- font when on -- default is "mono"
fs_on={"18"}, -- font size when on -- default is 12
fxadj_on={15}, -- font x position adjust when on -- default is 0
fyadj_on={-15}, -- font y position adjust when on -- default is 0
fcol_on={0x00ffff}, -- font color on -- default is 0xffffff
fal_on={1}, --font alpha on -- default is 1

SETTINGS FOR TYPE 4 BUTTON, CYCLE
cynum=4, -- this is a required setting for type 4 button, the number tells the script how many numbers to cycle through before repeating
--position settings
blx_cyc={200}, --ypos when off in table format,  --each entry representing one cycle
bly_cyc={200}, --y pos when off                  --if entry is not made for a cycle
bw_cyc={100}, --height when off                  --last value in the table will be used
bh_cyc={100}, --width when off             --ie if want all blx positions to be 100, just enter {100}
--draw settings for a cycle type button
dtype_cyc={3}, --draw type in table format
--fill cycle
dfxadj_cyc={0}, --draw x position adjust for fill
dfyadj_cyc={0}, --draw y position adjust for fill
dfw_cyc={100}, --draw width fill
dfh_cyc={100}, --draw height fill
dfcr_cyc={20}, --draw fill corner radius
dfcol_cyc={0xffffff}, --draw fill color
dfal_cyc=1, --draw fill alpha
--line cycle
doxadj_cyc={0}, --draw x position for outline adjust
doyadj_cyc={0}, --draw y position for outline adjust
dow_cyc={200}, --draw width outline
doh_cyc={100}, --draw height outline
docr_cyc={30}, --draw outline corner radius
docol_cyc={0x0000ff}, --draw outline color
doal_cyc={1}, --draw outline alpha
dolw_cyc={2}, --draw outline line
--text for cycle type button --requires tables of tables for multiple buttons with multiple lines
--each sub table is lines of text for each button
txt_cyc={{"button on"},{}}, --text when button on -- default is same as txt_off
font_cyc={{"Sans"},{}}, -- font when on -- default is "mono"
fs_cyc={{"18"},{}}, -- font size when on -- default is 12
fxadj_cyc={{15},{}}, -- font x position adjust when on -- default is 0
fyadj_cyc={{-15},{}}, -- font y position adjust when on -- default is 0
fcol_cyc={{0x00ffff},{}}, -- font color on -- default is 0xffffff
fal_cyc={{1},{}}, --font alpha on -- default is 1

SETTINGS TO CONTROL OTHER BUTTONS
on_on_on={}, --list other buttons that this button should turn on
on_on_off={},
off_on_on={},
off_on_off={},

SETTING TO RETURN MOUSE COORDINATES ALONG WITH BUTTON STATUS
return_co=1, -- 1 for return coordiantes, 0 for not


EDIT - 07/30
addition of image settings

image=1 --1 for images 0 no images, default is 0
im_file_off=path --set path to off image (btype 1,2,3)
im_file_on=path --set path to on image (btype 1,2,3)
im_file_cyc={path1,path2} --set images for cycle type button in sub table (btype=4)


... later parts of the how to will go into more detail about use

Last edited by mrpeachy (2012-09-13 21:51:44)

Offline

#323 2012-07-27 02:29:49

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

PART 2 - BASIC OPERATION

this script can be operated in a number of ways
- all buttons set up in the lua script
- all buttons set up in the conkyrc code
- a mixture of lua buttons and conkyrc buttons

THE CONKYRC

the lua setup section of interactive_conky (above TEXT) looks like this:

############################################
## Set the path to button_script.lua here ##
############################################
lua_load /home/mcdowall/Dropbox/Public/interactive_conky/button_script.lua
##################################
## activate mouse click logging ##
##################################
lua_draw_hook_pre get_mouse {"interactiveconkyrc","/tmp/xdo"} 
## after function call, table containing window title and path to click log file
## before running script create a blank log file first.  In the example above "/tmp/xdo" credate a blank file called xdo in your /tmp directory 
###################################################
## restart click logging on conkyrc save/startup ##
###################################################
lua_startup_hook start_xdo /tmp/xdo
## send click file location following function call if not set up in lua script
###################################################
## kill xdotool process on conkyrc save/shutdown ##
###################################################
lua_shutdown_hook stop_xdo
#######################################################
## Set the path to lua_functions.lua here (optional) ##
#######################################################
lua_load /home/mcdowall/Dropbox/Public/interactive_conky/lua_functions.lua


lua_load /home/mcdowall/Dropbox/Public/interactive_conky/button_script.lua

this part loads the lua button script

lua_draw_hook_pre get_mouse {"interactiveconkyrc","/tmp/xdo"}
this part activates the function within the lua script that gets the current and on-click mouse location
NOTE - this function must be activated no matter where the buttons themselves will be set up
you don't have to activate the function like this:

lua_draw_hook_pre get_mouse

the code below would work equally well (but not both)

TEXT
${lua get_mouse}

UPDATE
you can now send the information about window title and click log path to this function in the lua script like this:

lua_draw_hook_pre get_mouse {"interactiveconkyrc","/tmp/xdo"} 

ADDITIONAL
if you set buttons up in the lua script, you don't need to have a separate draw_hook line as the lua buttons will be activated via the get_mouse function

lua_startup_hook start_xdo
this line activates the xdotool click logging process on startup of the conkyrc
also restarts the xdotool process when you save the rc
UPDATE - due to a problem with xdotool not starting correctly it is now a good idea to send the click log file location to this function like so

lua_startup_hook start_xdo /tmp/xdo


lua_shutdown_hook stop_xdo

this line kills the xdotool process on shutdown and also on conky save to prevent multiple xdotool processes from running
IMPORTANT - if you are using more than one conky and you shutdown only one of the conkys, the xdotool process will be killed and the buttons in other conkys will not work
saving one of the remaining conkys will restart the xdotool process

SETTING UP BUTTONS IN THE LUA SCRIPT

opening up the lua script, at the top it looks like this (more or less)

--[[buttons by mrpeachy - thanks to barrybarrykelly for the xdotool method, gmonti for finding the deb files for the xdotool and arclance for xdotool error catching
see here for instructions - http://crunchbanglinux.org/forums/post/245759/#p245759
]]

conky_name="interactiveconkyrc"
click_data_file ="/home/mcdowall/Dropbox/Public/interactive_conky/xdo"

--setup buttons here
function button_settings()

end--function button_settings

BUTTONS will be set up between:

function button_settings()

and

end--function button_settings

as will all the lua code that the buttons will control

NOTE - the buttons set up here will be activated by this line in the conkyrc

lua_draw_hook_pre get_mouse {"interactiveconkyrc","/tmp/xdo"} 

in the interactive_conky rc i have ${cpu cpu0} set up below TEXT so that you get something up on the screen when you launch conky
If you delete ${cpu cpu0} then make sure that there is at least 1 blank line under TEXT

OPEN A TERMINAL and type

conky -c /path-to-where-you-have-put/interactive_conky

you will see your current average cpu % usage in the top left corner of the conky window, but there are no buttons set up yet in this example

in the terminal you should see something like:

mcdowall@mcdowall-desktop:~$ conky -c /home/mcdowall/Dropbox/Public/interactive_conky/interactive_conky
Conky: desktop window (e00021) is subwindow of root window (15d)
Conky: window type - normal
Conky: drawing to created window (0x2800002)
Conky: drawing to double buffer

which means that things are working so far!

after 5 conky cycles following startup or after saving you will see a line like this appear in the terminal

own_window_title=interactiveconkyrc
1175    5    349    37748738    /tmp/xdo

this is the info from xwininfo
WHEN you see this line that means that the buttons have become responsive (you will get different info based on your conky and settings)

in this case:
1175 =number of pixels from left edge of screen to left edge of conky window
5 =number of pixels from top edge of screen to top edge of conky window
349 =window id of parent
37748738 =window id of conky window
/tmp/xdo =xdotool click log location

NOTE - above i showed that in the lua script i had these lines

conky_name="interactiveconkyrc"
click_data_file ="/home/mcdowall/Dropbox/Public/interactive_conky/xdo"

BUT in the rc i have this:

lua_draw_hook_pre get_mouse {"interactiveconkyrc","/tmp/xdo"} 

above there 2 things can be sent
interactiveconky is the conky window name
/tmp/xdo is the click log file location

both these settings, when sent from the rc, OVERRIDE the settings at the top of the lua script

Also you can send the click log file location to the start_xdo function

lua_startup_hook start_xdo /tmp/xdo

NOTE - no spaces are allowed in the path or filename to use the above method
NOTE this is something that I will update

AND when you click inside the conky window you will see the information from xdotool in the terminal like this:

x:1276 y:176 screen:0 window:46137346

if you see this then everything is working as it should be cool

SET UP A BASIC BUTTON
these are the settings required for an operational button:

MINIMUM REQUIRED SETTINGS
btype=1, --type, 1=on/off, 2=mouse over, 3=execute, 4=cycle
cynum=4, -- cycle number,required for type 4 button
bname="test1", --set button name unique for each button
blx_off=200, --xpos when off
bly_off=200, --ypos when off
bw_off=100, --height when off
bh_off=100, --width when off

so below "function button_settings()" we shall start putting together the code

GENERAL FORM OF THE BUTTON CODE
and basic code to work with the output of the button

button1=do_button({settings,settings,settings})
if button1==0 then
--do stuff when button is off
elseif button1==1 then
--do stuff if button is on
end--if button1==

REMEMBER
in lua anything that comes after -- is a comment and will not be read by the script
button1 is a string, and could be named anything you like providing that it doesn't start with a number or contain spaces

do_button is the name of the function in the lua script that will be doing the calculations/button drawing/text
we are sending the do_button function a list of settings, whjich is everything between the curved brackets ()
the information we are sending do_button is in the form of a table, which is everything in between the curly brackets {}

button1=do_button({btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1})

NOTE - the draw setting is not required to get an operational button, but without it you would not see anything on your screen, so im putting it in here
ALSO - cynum is ONLY required for a cycle type button which will be covered later
save the lua script and you should see this text in the terminal:

Conky: Lua script '/path/to/button_script.lua' reloaded

and you should see a white box appear in the top left hand corner of the conky window

the bottom left corner of the white box is 5 pixels to the right and 50 pixels down from the top of the conky window, as set in the code above
the box is 70 pixels wide and 30 pixels high
it is filled in white because the default draw type in the script is "fill" and the default fill color (for an off button) is fully opaque white

clicking on the white box should turn it red, and it should stay red
this means that everything is working normally
the default fill color (for an on button) is fully opaque red

click the red box and it will go back to white

this is an on off button

in the lua script put these lines below the button setup:

button1=do_button({btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1})
if button1==0 then
print ("button1 is off")
elseif button1==1 then
print ("button1 is on")
end

watch the terminal as you click the button.

NOTE - i'm not going into any detail about what to do with the output of your buttons, just explaining HOW to get the output big_smile

set up as many buttons as you want
make sure that each one has a UNIQUE name
and unless you want a click to activate more than one button, that they do not overlap

I'll go into making some more complex buttons later

FUN ACTIVITY
change the "btype" setting to 2 (mouse over) and then 3 (execute)
to see how those button types work

btype 4, cycle is a different kettle of fish, and ill explain that one later

Last edited by mrpeachy (2012-08-20 22:09:54)

Offline

#324 2012-07-27 02:31:15

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

PART 3 - A MORE COMPLEX EXAMPLE

the first page of this HOW TO goes through the list of button options.
most of which should be self explanatory
for example, draw type.
the default is fill, which is option 1 for dtype
but if you want to set an outline instead of fill put "dtype_off=2" into the settings as so

 
button1=do_button({btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1,dtype_off=2})

now instead of a filled in rectangle when off, you get a white outlined box (as white is the default when off) which changes to a red outlined rectangle when clicked

if you want to change the color of the button you use either "dfcol_off" and "dfcol_on" to change the fill color or "docol_off" and "docol_on" to change the outline color

this code:

 
button1=do_button({btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1,dtype_off=2,docol_off=0x00ff00,dtype_on=1,dfcol_on=0x0000ff})

would produce a button that when off, would have a green outline which when clicked turns to a blue fill.

you can also change:
- the position of the button from off to on
- the size of the button
- the shape of the button
           - you can give it rounded corners or even make it a circle as long as you remember that the actual clickable area will always be rectangular
- the draw type and colors
and you can set up text for each button that can change from on to off

SETTING TEXT FOR A BUTTON

setting text for a button is a little different from the other settings
for type 1 2 and 3 buttons, text setup is entered into sub-tables (within the main settings table)
this is so that multiple lines of text can be set and correspond to different  text settings

these are the settings involved:

--text
text=1, -- 1 if want print text, 0 if no text -- default is 0
--text setup if on, set multiple lines with attributes using tables
txt_off={"button off"}, --text when button off -- default is "text"
font_off={"Mono"}, -- font when off -- default is "mono"
fs_off={"12"}, -- font size when off -- default is 12
fxadj_off={15}, -- font x position adjust when off -- default is 0
fyadj_off={-15}, -- font y position adjust when off -- default is 0
fcol_off={0x000000}, -- font color off -- default is 0xffffff
fal_off={1}, --font alpha off -- default is 1
--text setup when on
txt_on={"button on"}, --text when button on -- default is same as txt_off
font_on={"Sans"}, -- font when on -- default is "mono"
fs_on={"18"}, -- font size when on -- default is 12
fxadj_on={15}, -- font x position adjust when on -- default is 0
fyadj_on={-15}, -- font y position adjust when on -- default is 0
fcol_on={0x00ffff}, -- font color on -- default is 0xffffff
fal_on={1}, --font alpha on -- default is 1

going back to our basic button:

button1=do_button({btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1})

we want to add text so that when the button is OFF we get:

BUTTON
OFF

are present and we when the button is ON we get:

BUTTON
ON

so we need to set "text=1" to turn text on

then we set up the off text... we want 2 lines of text which will be entered like this:

txt_off={"BUTTON","OFF"}

we can set different fonts, font sizes, colors and alphas (transparency values) for each entry
for example, if we wanted "BUTTON" to be in "mono" and "OFF" to be in "sans" we would set up fonts like this:

font_off={"Mono","Sans"}

NOTE font names must be in quotes.

if we wanted both words to be in the same font we only need to specify the font once like this:

font_off={"Sans"}

or if font_off is not set, mono is the default font

the method for setting font sizes is the same using the fs_off option

then we need to specify the positions of the words with

fxadj_off={}
fyadj_off={}

these are adjustments and are applied relative to blx_off and bly_off respectively
default positions apply the text at the coordinates set by blx_off and bly_off

BUT we want "BUTTON" at the top of the box and a little in from the left edge with "ON" aligned the same but below
our button is 30 pixels high, so something like this would do it:

fxadj_off={5}--positive x numbers move to the right, same for both buttons so only specify once
fyadj_off={-17,-2}--negative y numbers move up

default text color is full opaque white unless we specify text color and alpha.
NOTE - although i might go ahead and make the default text color black as the default off but fill is white! --DONE

THEN we can do the same for the button on text, to get a button setup that looks like this:

button1=do_button({btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1,text=1,fcol_off={0x000000},txt_off={"BUTTON","OFF"},font_off={"Sans"},fs_off={12},fxadj_off={5},fyadj_off={-17,-2},txt_on={"BUTTON","ON"}})

NOTE:
I didn't specify anything for the "on text" other than txt_on.  The "on text", unless setup differently, will be the same as the setting given for the "off text".  So, for example, I set fcol_off to black so fcol_on will also be set to black.

--#####################################################

SETTING UP BUTTONS IN THE CONKYRC

so far the buttons have been set up in the lua script but it is a simple matter to set up buttons directly in the conkyrc and use those buttons to affect the conky code.

FIRST
if we want to run buttons through the conkyrc code rather than the lua script we can edit "interactive_conky" like so

other settings
own_window_title interactiveconkyrc
other settings

lua settings
lua settings

TEXT
${lua rc_button {settings,settings}}

UPDATE - the lua settings part of the conkyrc does not need to change from using lua setup buttons to using rc setup buttons and vice versa

NOTE: the function used to set buttons in the conkyrc is called conky_rc_button
the reason why we only need to write "rc_button" is that conky assumes that any lua function being called by the program will start with conky_
we begin the button with ${lua which is a conky object that runs a lua function.
 
WATCH OUT for the correct number of curly brackets, as conky objects are encolsed by curly brackets but also our settings are enclosed by curly brackets, as are the text sub-tables!

It is as easy as copying the setting from the lua button we set up above and pasting them into the conkyrc object:

TEXT
${lua rc_button {btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1,text=1,fcol_off={0x000000},txt_off={"BUTTON","OFF"},font_off={"Sans"},fs_off={12},fxadj_off={5},fyadj_off={-17,-2},txt_on={"BUTTON","ON"}}}

because of the length of the button setup the conky setting of "template" can be useful here... like this instead:

template1 ${lua rc_button {btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1,text=1,fcol_off={0x000000},txt_off={"BUTTON","OFF"},font_off={"Sans"},fs_off={12},fxadj_off={5},fyadj_off={-17,-2},txt_on={"BUTTON","ON"}}}
TEXT
${template1}

when you come to use the button output with if_match, for example, this will make the conky code easier to work with
you can have a max of 10 templates, 0 to 9

ANY button that can be set up in the lua script can be set up in the conkyrc

BUT you will see something different on screen than you did through the lua script.
you will see the output of the button, either 0 or 1 displayed in the top left corner of conky along with the button itself

NOTE I have noticed that the buttons generated through the conkyrc are a little less responsive, ie slower to change when clicked than when set up through the lua script

USING THE OUTPUT TO EFFECT THE CONKY CODE

we can use the ${if_match} object to evaluate the output of the button like this:

template1 ${lua rc_button {btype=1,bname="conkybutton1",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1,text=1,fcol_off={0x000000},txt_off={"BUTTON","OFF"},font_off={"Sans"},fs_off={12},fxadj_off={5},fyadj_off={-17,-2},txt_on={"BUTTON","ON"}}}
TEXT
${if_match ${template1}==0}the button is off${else}the button is on${endif}

you will get some additional errors in the terminal until the button appears

Conky: failed to parse compare string '==0'
Conky: compare failed for expression '==0'

to be fixed!

UPDATE
controlling the xdotool process with startup and shutdown lua hooks means that there is no longer any need to kill conky and xdotool and restart when you save the conky
the xdotool process is killed and then restarted when the conky is saved automatically so buttons will continue to work

WITH this method you can have function buttons affecting your conky without having to know anything about writing lua code big_smile

Last edited by mrpeachy (2012-08-07 03:40:51)

Offline

Be excellent to each other!

#325 2012-07-27 04:28:14

mrpeachy
20% cooler
From: The Everfree Forest
Registered: 2009-11-08
Posts: 3,460

Re: interactive conky

PART 4.  CYCLE TYPE BUTTONS AND ON/OFF SETTINGS

with btype 1 (on/off), 2 (mouse over) and 3 (execute) the script returns a 0 when the button is OFF and 1 when the button is ON

with btype 4, a cycle button, you set how long the cycle loop is by the variable cynum and the function returns numbers from 1 through cynum in consecutive order.

so you set cynum=3
when the button starts, it returns "1"
on the first click it returns "2"
next click returns "3"
and the next click the output resets to "1"

so then you can have some code like this:

if button==1 then
--do button 1 stuff\
elseif button==2 then
--do button 2 stuff
elseif button==3 then
--do button 3 stuff
end

because cynum can be set to any number, the way that cycle buttons are set up is a little different to the other types.
each individual setting is entered into a sub table within the main settings table.

these are the options involved:

--position settings
blx_cyc={200}, --ypos when off in table format,  --each entry representing one cycle
bly_cyc={200}, --y pos when off                  --if entry is not made for a cycle
bw_cyc={100}, --height when off                  --last value in the table will be used
bh_cyc={100}, --width when off             --ie if want all blx positions to be 100, just enter {100}
--draw settings for a cycle type button
dtype_cyc={3}, --draw type in table format
--fill cycle
dfxadj_cyc={0}, --draw x position adjust for fill
dfyadj_cyc={0}, --draw y position adjust for fill
dfw_cyc={100}, --draw width fill
dfh_cyc={100}, --draw height fill
dfcr_cyc={20}, --draw fill corner radius
dfcol_cyc={0xffffff}, --draw fill color
dfal_cyc=1, --draw fill alpha
--line cycle
doxadj_cyc={0}, --draw x position for outline adjust
doyadj_cyc={0}, --draw y position for outline adjust
dow_cyc={200}, --draw width outline
doh_cyc={100}, --draw height outline
docr_cyc={30}, --draw outline corner radius
docol_cyc={0x0000ff}, --draw outline color
doal_cyc={1}, --draw outline alpha
dolw_cyc={2}, --draw outline line
--text for cycle type button --requires tables of tables for multiple buttons with multiple lines
--each sub table is lines of text for each button
txt_cyc={{"button on"},{}}, --text when button on -- default is same as txt_off
font_cyc={{"Sans"},{}}, -- font when on -- default is "mono"
fs_cyc={{"18"},{}}, -- font size when on -- default is 12
fxadj_cyc={{15},{}}, -- font x position adjust when on -- default is 0
fyadj_cyc={{-15},{}}, -- font y position adjust when on -- default is 0
fcol_cyc={{0x00ffff},{}}, -- font color on -- default is 0xffffff
fal_cyc={{1},{}}, --font alpha on -- default is 1

SO instead of giving one setting for off and one setting for on
you enter the appropriate number of settings into the sub table

in my 3 cycle button, keeping with the default fill draw type, if i wanted the button to be:
red when 1
green when 2
blue when 3

I would use this setup for colors:

dfcol_cyc={0xff0000,0x00ff00,0x0000ff}

the first entry applies to the button when it returns 1 and so on.

IF i wanted all the buttons to be red, then i only need to set red once like this

dfcol_cyc={0xff0000}

IF i did this:

dfcol_cyc={0xff0000,0x00ff00}

then on 1 the button would be red, on 2 it would be green and on 3 it would be green

the same principal applies to any of the other settings
BUT even if you only want to give one setting to apply to all button values, you still need to enter it into a sub table

ADDING TEXT ON A CYCLE BUTTON
adding text adds a little more complexity
Say that on a return value of 1 we want the button to say:

CYCLE
ONE

on a return value of 2 we want the button to say:

CYCLE
TWO

and on a return value of 3 to say:

CYCLE
THREE

we would do this for text:

txt_cyc={{"CYCLE","ONE"},{"CYCLE","TWO"},{"CYCLE","THREE"}}, fxadj_cyc={{5}},fyadj_cyc={{-17,-2}},

NOTE txt_cyc must be a sub table that itself contains tables
each table within the sub table MUST be separated from other tables by a comma
each entry in each tables must be separated by a comma also (and text must be in quotes)

so within the txt_cyc sub table, we have 3 individual tables, each one carrying the text for the button for each return value

ALSO i have set fxadj_cyc={{5}}
this means that EVERY piece of text will be offset from the left side of the button by 5 pixels
because i want all the text at 5 from the left, I only need to set one number BUT that number MUST be in a table within the sub table

if i wanted the 3 text outputs to be at different offsets, 5 pixels when button value is 1, 6 pixels when 2 and 7 pixels when 3 i would do this:

fxadj_cyc={{5},{6},{7}}

if i wanted to keep these offsets for the "CYCLE" part of the text but then have the "ONE", "TWO" and "THREE" an additional pixel to the right i would do this:

fxadj_cyc={{5,6},{6,7},{7,8}}

I set fyadj_cyc={{-17,-2}}
the first piece if text for the button at each return value will be 17 pixels up from the bottom of the button:
this will apply to the "CYCLE" text and place it at the top of the button

the second piece of text for the button at each return value will be 2 pixels up from the bottom of the button:
this will apply to the text "ONE", "TWO" and "THREE" and place this text below "CYCLE"

this button would look like this

button2=do_button({btype=4,bname=button2,cynum=3,blx_cyc={5},bly_cyc={50},bw_cyc={70},bh_cyc={30},draw=1,text=1,dfcol_cyc={0xff0000,0x00ff00,0x0000ff},txt_cyc={{"CYCLE","ONE"},{"CYCLE","TWO"},{"CYCLE","THREE"}}, fxadj_cyc={{5}},fyadj_cyc={{-17,-2}}})

blx_cyc
bly_cyc
bw_cyc
bh_cyc

are all required also, as is bname and btype and in the case of btype=4, cynum is required
if we want the button to be drawn we need to give draw=1 and if we want text we need to give text=1

--############################
BUTTON ON/OFF CONTROL

here is an example
Screenshot_at_2012_07_30_13_08_58.jpg

there are 2 buttons: "CPU INFO" and "MEM INFO"
and in the pic you can see that only memperc info is being displayed
clicking on the "CPU INFO" button then goes to this:
Screenshot_at_2012_07_30_13_11_50.jpg

here is the code for the above:

function button_settings()
cpubutton=do_button({btype=1,bname="cpubutton",blx_off=5,bly_off=50,bw_off=70,bh_off=30,draw=1,text=1,dfcol_on=0xffffff,txt_off={"CPU","INFO"},fcol_off={0x000000},fxadj_off={5},fyadj_off={-17,-2},off_on_on={"membutton"}})
membutton=do_button({btype=1,bname="membutton",blx_off=80,bly_off=50,bw_off=70,bh_off=30,draw=1,text=1,dfcol_on=0xffffff,txt_off={"MEM","INFO"},fcol_off={0x000000},fxadj_off={5},fyadj_off={-17,-2},off_on_on={"cpubutton"}})
cairo_set_source_rgba (cr,1,1,1,1)
cairo_select_font_face (cr, "mono", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size (cr, 30)
cairo_move_to (cr,5,80)
if cpubutton==1 then
cairo_show_text (cr,"CPU: "..cpunum.."%")
cairo_stroke (cr)
elseif membutton==1 then
cairo_show_text (cr,"MEM: "..memnum.."%")
cairo_stroke (cr)
end
end--function button_settings

to get only one or the other info type to display in the same space I am using the "off_on_on" option
SO when the cpu button (named "cpubutton") is on (return value of 1) you see cpu info
then when you click on the mem button (named "membutton"), this setting:

off_on_on={"cpubutton"}

turns off the cpu button and at the same time the click turns on the mem button
this setting in the cpu button setup:

off_on_on={"membutton"}

turns off the mem button when the cpu button is turned on

these are the options for button control:

on_on_on={},
on_on_off={},
off_on_on={},
off_on_off={},

button names are entered in a sub table to allow multiple buttons to be affected
button names must also be in quotes

Last edited by mrpeachy (2012-07-30 18:19:49)

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo