-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Am 28.06.2010 14:22, Uli Schlachter wrote:
> Hi,
> 
> the attached patches stop obvious' keymap_switch from breaking my config (It
> kind-of leaked FDs until the next garbage collect which made awesome run into
> the FD limit). Also it makes sure the timer in keymap_switch is only started
> when it's needed.
> 
> In other news, I don't use this code, so no testing done (Except that I don't
> see those leaks anymore...).

...

- -- 
- - Buck, when, exactly, did you lose your mind?
- - Three months ago. I woke up one morning married to a pineapple.
  An ugly pineapple... But I loved her!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iQEcBAEBCAAGBQJMKJXZAAoJECLkKOvLj8sGql0H/1RlPjMZWGcuD02oqnvWywfU
wscRbX/IUS1vsS7sDBkzUYHmeZ+guPGInu/n1eN3AFkB1juHj3Wxy81q84TbJRJd
nhZpgwEwZuKvFDRhytEbMXqAjyoEAZuM4JwNfDqpTnr/gbW0fwDrczu8TND/ZLwW
nhjvxGa1eGY3wIEmwG3bJaeChxHgreHtiRy43DKY1mQvGw6T0yBCdisOn896Qhgm
3SY1rfOeqgCFNrpMTHoGdIPVCVKIFpGXrTX40UfXzjAkzlmUXEWgZsSyelHtS9LR
WKQJApNvsT72LclLlTzIC2KWsMhNzFMoiwpugjjgv+j6Z9LHQ2++9VWgCDsdr1k=
=Su25
-----END PGP SIGNATURE-----
>From e138fc02ce0ca7f2e229aa8f02241cc2a2e7b93d Mon Sep 17 00:00:00 2001
From: Uli Schlachter <[email protected]>
Date: Mon, 28 Jun 2010 13:18:07 +0200
Subject: [PATCH 1/3] keymap_switch: Use spawn() instead of popen()

popen() returns a file handle which can be used to interact with the started
process. This file handle is only closed when it's explicitly closed (not
happening here) or when it is garbage collected.

Turns out that the lua garbage collector kicks in seldom enough for my awesome
instance to run into the fd limit (1024 open FDs is the maximum by default).

Fix this by using awful.util.spawn() instead of io.popen(). This will make the
output of setxkbmap visible as awesome's output, but besides that it should work
as good as the old code.

Signed-off-by: Uli Schlachter <[email protected]>
---
 keymap_switch/init.lua |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/keymap_switch/init.lua b/keymap_switch/init.lua
index f344f02..5df10c1 100644
--- a/keymap_switch/init.lua
+++ b/keymap_switch/init.lua
@@ -87,7 +87,7 @@ local function delayed_update_once(start)
 end
 
 local function switch_keymap(layout_string)
-    io.popen("setxkbmap \"" .. layout_string .. "\"")
+    awful.util.spawn("setxkbmap \"" .. layout_string .. "\"")
     delayed_update_once(true)
 end
 
-- 
1.7.1

>From 1a977b5fd47de7085aac0ae13ad34e5a63524124 Mon Sep 17 00:00:00 2001
From: Uli Schlachter <[email protected]>
Date: Mon, 28 Jun 2010 13:21:29 +0200
Subject: [PATCH 2/3] keymap_switch: Close fd when it's no longer needed

By default, a file handle is only closed when its garbage collected. This
happens seldom enough for my awesome instance to run out of file handles (1024
fds is the default limit).

Fix this by explicitly closing the handle when we don't need it any more.

Signed-off-by: Uli Schlachter <[email protected]>
---
 keymap_switch/init.lua |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/keymap_switch/init.lua b/keymap_switch/init.lua
index 5df10c1..7d0b3b2 100644
--- a/keymap_switch/init.lua
+++ b/keymap_switch/init.lua
@@ -64,6 +64,7 @@ local function get_current_keymap()
         if line:match("xkb_symbols") then
             local keymap = line:match("\+.*\+")
 
+            fd:close()
             if not keymap then
                 return "unknown layout"
             else
@@ -72,6 +73,7 @@ local function get_current_keymap()
         end
     end
 
+    fd:close()
     return "unknown layout"
 end
 
-- 
1.7.1

>From e77acde6fa2f0f9528c8badfab32965215df98a9 Mon Sep 17 00:00:00 2001
From: Uli Schlachter <[email protected]>
Date: Mon, 28 Jun 2010 14:19:31 +0200
Subject: [PATCH 3/3] keymap_switch: Only do the setup when needed

Signed-off-by: Uli Schlachter <[email protected]>
---
 keymap_switch/init.lua |   17 +++++++++++++----
 1 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/keymap_switch/init.lua b/keymap_switch/init.lua
index 7d0b3b2..0834a57 100644
--- a/keymap_switch/init.lua
+++ b/keymap_switch/init.lua
@@ -38,7 +38,20 @@ end
 
 module("obvious.keymap_switch")
 
+setup_done = false
+local function init_once()
+    if setup_done then
+        return
+    end
+    lib.hooks.timer.register(5, 60, update, "Update for the keymap widget")
+    lib.hooks.timer.start(update)
+    delayed_update_once(true)
+    setup_done = true
+end
+
 local function init(widget)
+    init_once()
+
     -- Use the default widget if not specified
     if widget then
         settings.widget = widget
@@ -111,9 +124,5 @@ function update()
     settings.widget.text = get_current_keymap()
 end
 
-lib.hooks.timer.register(5, 60, update, "Update for the keymap widget")
-lib.hooks.timer.start(update)
-delayed_update_once(true)
-
 setmetatable(_M, { __call = function() return init(settings.widget) end }) -- TODO let the user specify widget here
 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:encoding=utf-8:textwidth=80
-- 
1.7.1

Attachment: 0001-keymap_switch-Use-spawn-instead-of-popen.patch.sig
Description: Binary data

Attachment: 0002-keymap_switch-Close-fd-when-it-s-no-longer-needed.patch.sig
Description: Binary data

Attachment: 0003-keymap_switch-Only-do-the-setup-when-needed.patch.sig
Description: Binary data

Reply via email to