dotfiles

Obligatory dotfiles repo
git clone git://git.danielmoch.com/dotfiles.git
Log | Files | Refs

commit 93a0918668904c87610b2e454b7c9173717c29bd
parent 4492dd0e567f11a75bc0d0993bb029a719ac837a
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Fri, 19 Jan 2018 19:32:25 -0500

Awesome: add xrandr module

Diffstat:
M.config/awesome/rc.lua | 4++++
A.config/awesome/xrandr.lua | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 139 insertions(+), 0 deletions(-)

diff --git a/.config/awesome/rc.lua b/.config/awesome/rc.lua @@ -13,6 +13,8 @@ local hotkeys_popup = require("awful.hotkeys_popup").widget -- Enable hotkeys help widget for VIM and other apps -- when client with a matching name is opened: require("awful.hotkeys_popup.keys") +-- Xrandr library +local xrandr = require("xrandr") -- {{{ Error handling -- Check if awesome encountered an error during startup and fell back to @@ -289,6 +291,8 @@ globalkeys = gears.table.join( {description = "focus the next screen", group = "screen"}), awful.key({ modkey, "Control" }, "k", function () awful.screen.focus_relative(-1) end, {description = "focus the previous screen", group = "screen"}), + awful.key({ modkey, "Control" }, "Return", function () xrandr.xrandr() end, + {description = "Cycle monitor layouts", group = "screen"}), awful.key({ modkey, }, "u", awful.client.urgent.jumpto, {description = "jump to urgent client", group = "client"}), awful.key({ modkey, }, "Tab", diff --git a/.config/awesome/xrandr.lua b/.config/awesome/xrandr.lua @@ -0,0 +1,135 @@ +--- Separating Multiple Monitor functions as a separeted module (taken from awesome wiki) + +local awful = require("awful") +local naughty = require("naughty") + +-- A path to a fancy icon +local icon_path = "" + +-- Get active outputs +local function outputs() + local outputs = {} + local xrandr = io.popen("xrandr -q --current") + + if xrandr then + for line in xrandr:lines() do + local output = line:match("^([%w-]+) connected ") + if output then + outputs[#outputs + 1] = output + end + end + xrandr:close() + end + + return outputs +end + +local function arrange(out) + -- We need to enumerate all permutations of horizontal outputs. + + local choices = {} + local previous = { {} } + for i = 1, #out do + -- Find all permutation of length `i`: we take the permutation + -- of length `i-1` and for each of them, we create new + -- permutations by adding each output at the end of it if it is + -- not already present. + local new = {} + for _, p in pairs(previous) do + for _, o in pairs(out) do + if not awful.util.table.hasitem(p, o) then + new[#new + 1] = awful.util.table.join(p, {o}) + end + end + end + choices = awful.util.table.join(choices, new) + previous = new + end + + return choices +end + +-- Build available choices +local function menu() + local menu = {} + local out = outputs() + local choices = arrange(out) + + for _, choice in pairs(choices) do + local cmd = "xrandr" + -- Enabled outputs + for i, o in pairs(choice) do + cmd = cmd .. " --output " .. o .. " --auto" + if i > 1 then + cmd = cmd .. " --right-of " .. choice[i-1] + end + end + -- Disabled outputs + for _, o in pairs(out) do + if not awful.util.table.hasitem(choice, o) then + cmd = cmd .. " --output " .. o .. " --off" + end + end + + local label = "" + if #choice == 1 then + label = 'Only <span weight="bold">' .. choice[1] .. '</span>' + else + for i, o in pairs(choice) do + if i > 1 then label = label .. " + " end + label = label .. '<span weight="bold">' .. o .. '</span>' + end + end + + menu[#menu + 1] = { label, cmd } + end + + return menu +end + +-- Display xrandr notifications from choices +local state = { cid = nil } + +local function naughty_destroy_callback(reason) + if reason == naughty.notificationClosedReason.expired or + reason == naughty.notificationClosedReason.dismissedByUser then + local action = state.index and state.menu[state.index - 1][2] + if action then + awful.util.spawn(action, false) + state.index = nil + end + end +end + +local function xrandr() + -- Build the list of choices + if not state.index then + state.menu = menu() + state.index = 1 + end + + -- Select one and display the appropriate notification + local label, action + local next = state.menu[state.index] + state.index = state.index + 1 + + if not next then + label = "Keep the current configuration" + state.index = nil + else + label, action = unpack(next) + end + state.cid = naughty.notify({ text = label, + icon = icon_path, + timeout = 4, + screen = mouse.screen, + replaces_id = state.cid, + destroy = naughty_destroy_callback}).id +end + +return { + outputs = outputs, + arrange = arrange, + menu = menu, + xrandr = xrandr +}