Murga-Projects Forums

Full Version: basic wacom mapper (linux/osx-i-assume)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Long time since i've done any coding, but i needed a small and simple way to switch my wacom stylus back and forth between monitors. This script can probably use some tweaks and features, but it works for me for now.

It requires xsetwacom and xrandr, which I'm guessing are available in OSX...not likely Windows.

And I'm still using tooltips as a kludge for getting table labels in a my_function(self) situation. It seems to me that my_table[self:value()] should work, but it always seems to return my_table[1]
EDIT: After thinking more about it, i suppose it would be impossible to automatically determine a widget's index in a table, without doing a loop comparison, considering a widget could be part of any number of tables, or none. I think in the future i'll go with just using a simple "HDMI1" naming of the widget, and put any additional information in a separate widget...then i can get the proper monitor just with self:label()

anyway....play with it if you like...

Code:
#!/usr/bin/env murgaLua

-- mikshaw, dec 2013
-- simple GUI for picking a monitor on which to map a wacom stylus
-- connected monitors must already be mapped to the screen for this to work
-- requires xsetwacom and xrandr

-- Whatever device you want to map
wacom = "Wacom Intuos PT M Pen stylus"

monitor = {}
monitor[1] = "LVDS1"
monitor[2] = "VGA1"
monitor[3] = "HDMI1"
monitor[4] = "DP1"

-- Check for Wacom tablet
xsetwacom = io.popen("xsetwacom --list")
xsetwacom_string = xsetwacom:read("*a")
xsetwacom:close()
have_wacom    = string.find(xsetwacom_string, wacom)
if not have_wacom then fltk.fl_message("Wacom is not connected"); os.exit(1) end

function set_wacom(self)
-- I still don't know how to grab a table value from self, so i use a tooltip
os.execute("xsetwacom --set \""..wacom.."\" MapToOutput "..self:tooltip())
os.exit(0)
end

w = fltk:Fl_Double_Window(500, 300, "screen control")

space = 30
row = space
wacom_status    = fltk:Fl_Box(10, 10, 20, 20, wacom.." connected.")
wacom_status2   = fltk:Fl_Box(10, row, 20, 20, "Map to:")
wacom_status:align(8)
wacom_status2:align(8)

-- Check for monitors
xrandr = io.popen("xrandr -q")
xrandr_string = xrandr:read("*a")
xrandr:close()
m_label = {}
for i = 1, #monitor do
    row = row + space
    m_label[i] = fltk:Fl_Radio_Round_Button(10, row, w:w()-20, space, monitor[i])
    if string.find(xrandr_string, monitor[i].." connected") then
        m_label[i]:label(monitor[i].." (connected)")
        m_label[i]:callback(set_wacom)
        m_label[i]:tooltip(monitor[i])
    else
        m_label[i]:label(monitor[i].." (disconnected)")
        m_label[i]:deactivate()
    end
end

w:show()
Fl:run()

[/code]

did some more work on it....now it should automatically create a list of monitors (assuming monitor names are all one or more uppercase letters followed by zero or one digits), and if a monitor is connected but not active its button will be disabled (again, assuming xrandr keeps its format pretty much the same)

I think it's in a state now where i'll probably leave it at least for a while. I could have made the tables more tidy (put everything under one "monitor" table), but i'm not terribly comfortable with nested arrays yet.

Code:
#!/usr/bin/env murgaLua

-- mikshaw, dec 2013
-- simple GUI for picking a monitor on which to map a wacom stylus
-- requires xsetwacom and xrandr

-- Whatever device you want to map
wacom = "Wacom Intuos PT M Pen stylus"

-- Check for Wacom tablet
xsetwacom = io.popen("xsetwacom --list")
xsetwacom_string = xsetwacom:read("*a")
xsetwacom:close()
have_wacom    = string.find(xsetwacom_string, wacom)
if not have_wacom then fltk.fl_message("Wacom is not connected"); os.exit(1) end

function set_wacom(self)
os.execute("xsetwacom --set \""..wacom.."\" MapToOutput "..self:tooltip())
os.exit(0)
end

w = fltk:Fl_Double_Window(360, 180, "screen control")
pack = fltk:Fl_Pack(10, 10, w:w()-20, w:h()-20)
wacom_status = fltk:Fl_Box(0, 0, 20, 20, "Map "..wacom.." to:")

-- Check for monitors
xrandr = io.popen("xrandr -q")
xrandr_string = xrandr:read("*a")
xrandr:close()

monitor_line = {}; monitor = {}; m_connected = {}; m_avail = {}; m_label = {}
n = 0
for line in xrandr_string:gmatch("(%u+%d?%s.-)\n") do
    n = n + 1
    monitor_line[n] = line
    monitor[n] = line:gsub("%s.*", "") -- Grab the actual monitor name
    m_label[n] = fltk:Fl_Radio_Round_Button(0, 0, w:w()-20, 30, monitor[n].." (disconnected)")
    m_label[n]:deactivate()
    if string.find(monitor_line[n], " connected ") then
        m_label[n]:label(monitor[n].." (connected but not active)")
    end
    -- Monitor must be both connected and mapped to the screen
    if string.find(monitor_line[n], "%d+x%d+") then
        m_label[n]:callback(set_wacom)
        m_label[n]:label(monitor[n].." (connected)")
        m_label[n]:tooltip(monitor[n])
        m_label[n]:activate()
    end
end

fltk:Fl_End(pack)
w:show()
Fl:run()

You always have interesting projects. Good to see you around.
Happy New Year!

I've been learning a bit of C, C#, z80 assembler, along with a bit of Livecode (that went open source)
and Microsoft BASIC 1.0
plus the all new 2014 update: Boot Loader Basic

I'm all over the place after coming out of my hole.
Moved to Windows after my five year old iMac crashed about four years ago? Did you know Windows has a ton of games and software?
The development tools seem like thirty years of upgrades to programming basic on a command line, that's nice and homey.

The UI is so much like Mac now that it's not so bad to use, nonstop.
Wish I could say the same for my designs with FLTK.
I wonder what's changed with that in 7 years.
I just found SpEdito for my C coding, made with version 1 made with version 1.0.
Pretty sure we can convert that to all MurgaLua.
Soonish.

Gotta run. Stay in touch!
Reference URL's