awesome/my.lua
changeset 12 ffc02bf394cb
parent 11 7226709dc4c4
child 13 e9e2f624cd99
equal deleted inserted replaced
11:7226709dc4c4 12:ffc02bf394cb
       
     1 local os = require("os")
       
     2 local io = require("io")
       
     3 local debug = require("debug")
       
     4 local awesome = require("awesome")
     1 local naughty = require("naughty")
     5 local naughty = require("naughty")
     2 local awful = require("awful")
     6 local awful = require("awful")
     3 local debug = require("debug")
       
     4 local vicious = require("vicious")
     7 local vicious = require("vicious")
     5 local wicked = require("wicked")
     8 local wicked = require("wicked")
     6 local beautiful = require("beautiful")
     9 local beautiful = require("beautiful")
     7 local timer = require("timer")
    10 local timer = require("timer")
     8 local wibox = require("wibox")
    11 local wibox = require("wibox")
     9 
    12 
    10 local ipairs = ipairs
    13 local ipairs = ipairs
    11 local pairs = pairs
    14 local pairs = pairs
    12 
    15 
    13 local capi = {
    16 local capi = {
    14    keygrabber = keygrabber,
    17    keygrabber = require("keygrabber"),
    15    client = client,
    18    client = require("client"),
    16    string = string,
    19    string = require("string"),
    17    widget = widget,
       
    18 }
    20 }
    19 
    21 
    20 module("my")
    22 module("my")
       
    23 
       
    24 --- Stolen from shifty: Shows a popup and logs to a file
       
    25 -- @param message The text message.
       
    26 -- @param log_level 1 = INFO, 2 = WARN, 3 = ERROR, if nothting is provided 1 is used.
       
    27 function log(message, log_level)
       
    28   if log_level == nil then
       
    29     log_level = 1
       
    30   end
       
    31 
       
    32   local log_table = {
       
    33     { level = "INFO", bg_colour = "#18F92C", fg_colour  = "#0E0E0E" },
       
    34     { level = "WARN", bg_colour = "#9E731F", fg_colour  = "#0E0E0E" },
       
    35     { level = "ERROR", bg_colour = "#FF0015", fg_colour  = "#000000" }
       
    36   }
       
    37   -- %c eg: Wed Jan 30 14:25:13 2013
       
    38   local time = os.date("%c")
       
    39   message = time .. " - " .. log_table[log_level].level .. " - " .. message ..  "\n" .. debug.traceback()
       
    40 
       
    41   local home = os.getenv("HOME")
       
    42   local log_file = io.open(home .. "/.awesome.log", "a+")
       
    43   log_file:write(message .."\n")
       
    44   log_file:close()
       
    45 
       
    46   naughty.notify({ preset = naughty.config.presets.critical, text = message, bg = log_table[log_level].bg_colour, fg = log_table[log_level].fg_colour})
       
    47 end
       
    48 
       
    49 local in_error = false
       
    50 function mk_notify_error_dialog(level)
       
    51    return function(msg)
       
    52       -- Make sure we don't go into an endless error loop
       
    53       if in_error then return end
       
    54       in_error = true
       
    55 
       
    56       log(msg, level)
       
    57       
       
    58       in_error = false
       
    59    end
       
    60 end
       
    61    
       
    62 notify_error = mk_notify_error_dialog(3)
    21 
    63 
    22 local function get_gradient_colors(c1, c2, c3)
    64 local function get_gradient_colors(c1, c2, c3)
    23    return { type = "linear", from = { 0, 0 }, to = { 0, 20 }, stops = { { 0, c1 }, { 0.5, c2 }, { 1, c3 } }}
    65    return { type = "linear", from = { 0, 0 }, to = { 0, 20 }, stops = { { 0, c1 }, { 0.5, c2 }, { 1, c3 } }}
    24 end
       
    25 
       
    26 local in_error = false
       
    27 function notify_error(err)
       
    28    -- Make sure we don't go into an endless error loop
       
    29    if in_error then return end
       
    30    in_error = true
       
    31 
       
    32    naughty.notify({ preset = naughty.config.presets.critical,
       
    33                     title = "Oops, an error happened!",
       
    34                     text = err })
       
    35    in_error = false
       
    36 end
    66 end
    37 
    67 
    38 function mkspawn(p)
    68 function mkspawn(p)
    39    return function () awful.util.spawn(p) end
    69    return function () awful.util.spawn(p) end
    40 end
    70 end
    53       c:kill()
    83       c:kill()
    54    end
    84    end
    55 end
    85 end
    56 
    86 
    57 function quit()
    87 function quit()
    58    kill_all(awful.rules.any)
    88    kill_all(function (c) return true end)
    59    awesome.quit()
    89    awesome.quit()
    60 end
    90 end
    61 
    91 
    62 function show_clients()
    92 function show_clients()
    63    if instance then
    93    if instance then
   117                    1)
   147                    1)
   118    return w
   148    return w
   119 end
   149 end
   120 
   150 
   121 local function widgets_mode()
   151 local function widgets_mode()
       
   152    local bg = wibox.widget.background()
   122    local w = wibox.widget.textbox()
   153    local w = wibox.widget.textbox()
       
   154    bg:set_widget(w)
   123    widgets.mode_widget = w
   155    widgets.mode_widget = w
   124    return w
   156    widgets.mode_widget_bg = bg
       
   157    return bg
   125 end
   158 end
   126 
   159 
   127 local function widgets_keyboard()
   160 local function widgets_keyboard()
   128    local w = wibox.widget.textbox()
   161    local w = wibox.widget.textbox()
   129    widgets.keyboard_widget = w
   162    widgets.keyboard_widget = w
   189 local function kt_handler(keytable, mod, key, event)
   222 local function kt_handler(keytable, mod, key, event)
   190    if event == "release" then 
   223    if event == "release" then 
   191       return true 
   224       return true 
   192    end
   225    end
   193    local theme = beautiful.get()
   226    local theme = beautiful.get()
   194    widgets.mode_widget.bg = theme.bg_focus
   227    widgets.mode_widget_bg:set_bg(theme.bg_focus)
   195    if key == "Escape" then   
   228    if key == "Escape" then   
   196       return false
   229       return false
   197    end
   230    end
   198    local skey = capi.string.lower(translate_key(mod, key))
   231    local skey = capi.string.lower(translate_key(mod, key))
   199    if skey == "control+h" or skey == "shift+?" then
   232    if skey == "control+h" or skey == "shift+?" then
   204       if skey == capi.string.lower(k.key) then
   237       if skey == capi.string.lower(k.key) then
   205          return k.func(c)
   238          return k.func(c)
   206       end
   239       end
   207    end
   240    end
   208    -- notify_error("Nothing for " .. skey)
   241    -- notify_error("Nothing for " .. skey)
   209    widgets.mode_widget.bg = theme.bg_urgent
   242    widgets.mode_widget_bg:set_bg(theme.bg_urgent)
   210    return true
   243    return true
   211 end
   244 end
   212 
   245 
   213 local function stop_kt()
   246 local function stop_kt()
   214    capi.keygrabber.stop()
   247    capi.keygrabber.stop()
   215    widgets.mode_widget.visible = false
   248    widgets.mode_widget:set_text("")
   216 end
   249 end
   217 
   250 
   218 function mkinteractive(f)
   251 function mkinteractive(f)
   219   return function (c)
   252   return function (c)
   220      stop_kt()
   253      stop_kt()
   223 end
   256 end
   224 
   257 
   225 
   258 
   226 -- local is_in_run_kt = false
   259 -- local is_in_run_kt = false
   227 function run_kt(c, keytable, title)
   260 function run_kt(c, keytable, title)
   228    widgets.mode_widget.text = "Mode: " .. title
   261    widgets.mode_widget:set_text("Mode: " .. title)
   229    widgets.mode_widget.visible = true
   262    -- widgets.mode_widget:show()
   230    widgets.mode_widget.bg = "LightBlue"
   263    widgets.mode_widget_bg:set_bg("LightBlue")
   231    widgets.mode_widget:buttons(
   264    widgets.mode_widget:buttons(
   232       awful.util.table.join(
   265       awful.util.table.join(
   233          awful.button({}, 1, function () show_kt(keytable, "Binding") end),
   266          awful.button({}, 1, function () show_kt(keytable, "Binding") end),
   234          awful.button({}, 3, stop_kt)))
   267          awful.button({}, 3, stop_kt)))
   235    capi.keygrabber.run(function (mod, key, event)
   268    capi.keygrabber.run(function (mod, key, event)
   312    sloppy_timer:connect_signal("timeout", sloppy_on_timeout)
   345    sloppy_timer:connect_signal("timeout", sloppy_on_timeout)
   313    sloppy_timer:start()
   346    sloppy_timer:start()
   314    capi.client.connect_signal("manage", function (c)
   347    capi.client.connect_signal("manage", function (c)
   315                              c:connect_signal("mouse::enter", sloppy_mouse_enter)
   348                              c:connect_signal("mouse::enter", sloppy_mouse_enter)
   316    end)
   349    end)
   317    capi.client.connect_signal("focus", function () sloppy_timer:stop() end)
   350    capi.client.connect_signal("focus", function ()
   318 end
   351                                  if sloppy_timer.started then
       
   352                                     sloppy_timer:stop()
       
   353                                  end
       
   354    end)
       
   355 end