1

So I've been trying to configure my Awesome WM config (rc.lua) to detect if my IBM model M13 is connected to my laptop upon login/reset. This is to change what the modkey should be since the M13 doesn't have a super key.

The following code makes sense to me and changes modkey within the function being made for the awful.spawn.easy_async function, but after finishing the modkey changes back to Mod4.

modkey = "Mod4"

awful.spawn.easy_async(
   "xinput list",
   function(stdout, stderr, reason, code)
      local msg = "Regular keyboard Modkey = Super"

      -- Debug notification that shows that the modkey is 
      -- at its default for the superkey Mod4
      naughty.notify({
         text = modkey,
         timeout =7
      })

      if code ~= 0 then
         msg = "Missing xinput to see devices\nModkey = Super"
      elseif stdout:match("CHESEN") == "CHESEN" then 
         -- CHESEN is my PS/2 to USB adapter
         msg = "IBM M13 detected\nModkey = Alt"
         modkey = "Mod1"  -- Sets new modkey to Alt
      end

      -- Notification message
      naughty.notify({
         text = msg,
         timeout =7
      })
   end
)
-- Debug notification to verify key but key goes back to Mod4
naughty.notify({
   text = modkey,
   timeout =7
})

The output can be seen here. It doesn't print the notifications in order but the prints of Mod 4 are both of the debug prints.

Notification Output

I don't use Lua much aside from changing my configs from time to time so I'm having difficulty understanding how my global variable modkey can be changed with out it resetting. Other methods I tried was to have the function defined as a function I called setModKey to be passed as a parameter to easy_async and I tried setting modkey using _G to set it as _G.modkey, but I end up getting the same result.

Am I missing something fundamental to Lua or is this affected by how Awesome WM utilizes Lua? Any help will be very appreciated.

  • Why is this in `awful.spawn.easy_async`? When you want this to happen on every reset, just put the contents of the function after `modkey = "Mod4"`. – Henri Menke Jan 27 '18 at 08:11

1 Answers1

0

Use io.popen instead of awful.spawn.easy_async. Yes, normally using io.popen is really not recommended, but here the following happens:

  • Awesome starts
  • You call easy_async to capture the output of xinput list
  • Since it is async, your config continues to be loaded, so e.g. all your keybindings are set
  • easy_async does its job and you set modkey to something else.

This means that any keybinding which will be defined from now on use the new modkey, but all already-existing keybindings are not modified by this. So, basically nothing happens.

And for your debugging calls to naughty.notify: The one after the function is triggered first and only then, later, the inner one triggers. So it does not go back, but instead you first show the old value and only later the new one.

Uli Schlachter
  • 9,337
  • 1
  • 23
  • 39
  • Thanks you so much. I should of thought to check Lua's reference material for this, but I was primarily just checking Awesome's API documentation. I appreciate the help :) – Christopher Ranc Jan 27 '18 at 20:37