Ion: pull Firefox with Lua

I use the orphaned Ion as my window manager, and I love it. Here’s a Lua script I wrote for Ion that lets me “pull” a Firefox window to the current frame, parameterized on the Firefox profile. It finds the pid of the Firefox process by looking at the ~/.mozilla/firefox/[PROFILE]/lock file, then searches ion for an X window with that pid in its _NET_WM_PID X property and attaches it.

pid_atom = ioncore.x_intern_atom('_NET_WM_PID', true)
pull_result = nil

function pull_pid_fn(pid)
  return function(window)
    local window_pid = ioncore.x_get_window_property(window:xid(), pid_atom, 0, 0, true)
    if window_pid and window_pid[1] == pid then
      -- have to use this hack because WFrame.attach() can't be called in
      -- restricted mode. boo.
      pull_result = window
      return false
    else
      return true
    end
  end
end

function pull_firefox(to, profile)
  pull_result = nil
  local lock = io.popen("readlink -q ~/.mozilla/firefox/" .. profile .. "/lock"):lines()()
  if not lock then
    ioncore.warn(profile .. " firefox is not running")
    return
  end
  local pid = string.match(lock, ":%+(%d+)$")

  ioncore.clientwin_i(pull_pid_fn(tonumber(pid)))
  if pull_result then
    to:attach(pull_result, {switchto=true})
  else
    ioncore.warn("couldn't find " .. profile .. " firefox (pid " .. pid .. ")")
  end
end

6 thoughts on “Ion: pull Firefox with Lua

  1. Wow man I have been struggling to do something like this for a long time. Well a couple of months.

    The way I got the id was exactly the same as you! Great minds we both have! :)

    But I used lsof to get the pid.

    Right now I’m working on trying to focus the most recent window of the PID and then I came across your code. I searched on bing for “firefox _NET_WM_PID”, i wanted to search to see if firefox set the _NET_WM_PID

  2. Hey man do you have any ideas on how to test the “.parentlock” file instead of the “lock” symlink?

    New firefoxes use “.parentlock”, they still have “lock” but mac os doesnt have the “lock” so im trying to find a way to detect if firefox is running based on testing “.parentlock” but i cant figure out

    Thx man

  3. @Noiitidart interesting! I actually haven’t used this for a while, so you’re ahead of me. sorry I can’t help more. good luck though, and definitely post another comment if you get it working!

  4. Thanks so much man for a reply from such an old post! :)

    I think I got it! :) I’m using fcntl to check if the .parentlock file is locked. Then if fcntl fails, then I fall back and do readlink on the lock file. Then check if the pid in the lock file was last claimed after before the lock file was last modified. (if plug was pulled the lock file persists :( )

    Very similar to the checks they make when setting the lock here: http://mxr.mozilla.org/mozilla-release/source/profile/dirserviceprovider/src/nsProfileLock.cpp#289

    Right now I’m working on figuring out how to get information on a PID so i can figure out last time it was claimed.

Leave a Reply

Your email address will not be published.