| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
local awful = require('awful') |
| 7 |
|
| 8 |
local naughty = require("naughty") |
| 9 |
local table = table |
| 10 |
local tostring = tostring |
| 11 |
local floor = require("math").floor |
| 12 |
local capi = { |
| 13 |
|
| 14 |
client = client, |
| 15 |
keygrabber = keygrabber, |
| 16 |
|
| 17 |
mouse = mouse, |
| 18 |
screen = screen, |
| 19 |
awesome = awesome, |
| 20 |
} |
| 21 |
local wibox = require("wibox") |
| 22 |
|
| 23 |
local xresources = require("beautiful").xresources |
| 24 |
local dpi = xresources and xresources.apply_dpi or function() end |
| 25 |
|
| 26 |
|
| 27 |
local escape_markup = function(s) |
| 28 |
local escape_pattern = "[<>&]" |
| 29 |
local escape_subs = { ['<'] = "<", ['>'] = ">", ['&'] = "&" } |
| 30 |
return s:gsub(escape_pattern, escape_subs) |
| 31 |
end |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
local cyclefocus |
| 36 |
cyclefocus = { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
show_clients = true, |
| 42 |
|
| 43 |
|
| 44 |
focus_clients = true, |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
raise_client = true, |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
move_mouse_pointer = true, |
| 54 |
|
| 55 |
|
| 56 |
display_next_count = 3, |
| 57 |
display_prev_count = 3, |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
default_preset = {}, |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
preset_for_offset = { |
| 69 |
|
| 70 |
default = function (preset, args) |
| 71 |
|
| 72 |
preset.font = 'sans 8' |
| 73 |
preset.icon_size = dpi(24) |
| 74 |
preset.text = escape_markup(cyclefocus.get_client_title(args.client, false)) |
| 75 |
end, |
| 76 |
|
| 77 |
|
| 78 |
["0"] = function (preset, args) |
| 79 |
preset.font = 'sans 14' |
| 80 |
preset.icon_size = dpi(36) |
| 81 |
preset.text = escape_markup(cyclefocus.get_client_title(args.client, true)) |
| 82 |
|
| 83 |
if screen.count() > 1 then |
| 84 |
preset.text = preset.text .. " [screen " .. tostring(args.client.screen.index) .. "]" |
| 85 |
end |
| 86 |
preset.text = preset.text .. " [#" .. args.idx .. "] " |
| 87 |
preset.text = '<b>' .. preset.text .. '</b>' |
| 88 |
end, |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
}, |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
cycle_filters = { |
| 102 |
function(c, source_c) return not c.minimized end, |
| 103 |
}, |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
icon_col_width = dpi(36), |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
filter_focus_history = awful.client.focus.filter, |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
display_notifications = true, |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
debug_level = 0, |
| 137 |
|
| 138 |
debug_use_naughty_notify = false, |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
local get_icon_wrapper_widget = function(w, preset) |
| 144 |
local icon_margin = preset.icon_margin or dpi(5) |
| 145 |
local icon_center_margin = icon_margin + math.max(0, (cyclefocus.icon_col_width - preset.icon_size)/2) |
| 146 |
local iconmarginbox = wibox.container.margin(w) |
| 147 |
iconmarginbox:set_left(icon_center_margin) |
| 148 |
iconmarginbox:set_right(icon_center_margin) |
| 149 |
iconmarginbox:set_top(icon_margin) |
| 150 |
iconmarginbox:set_bottom(icon_margin) |
| 151 |
return iconmarginbox |
| 152 |
end |
| 153 |
|
| 154 |
|
| 155 |
if awful.widget.clienticon then |
| 156 |
cyclefocus.get_client_icon_widget = function(c, preset) |
| 157 |
local w = awful.widget.clienticon(c) |
| 158 |
w:set_forced_width(preset.icon_size) |
| 159 |
w:set_forced_height(preset.icon_size) |
| 160 |
return get_icon_wrapper_widget(w, preset) |
| 161 |
end |
| 162 |
else |
| 163 |
local has_gears, gears = pcall(require, 'gears') |
| 164 |
local icon_loader |
| 165 |
if has_gears then |
| 166 |
|
| 167 |
icon_loader = gears.surface.load |
| 168 |
else |
| 169 |
icon_loader = function(icon) return icon end |
| 170 |
end |
| 171 |
|
| 172 |
cyclefocus.get_client_icon_widget = function(c, preset) |
| 173 |
if not c.icon then |
| 174 |
return |
| 175 |
end |
| 176 |
local icon = icon_loader(c.icon) |
| 177 |
local icon_size = preset.icon_size |
| 178 |
|
| 179 |
|
| 180 |
local cairo = require("lgi").cairo |
| 181 |
local scaled = cairo.ImageSurface(cairo.Format.ARGB32, icon_size, icon_size) |
| 182 |
local cr = cairo.Context(scaled) |
| 183 |
cr:scale(preset.icon_size / icon:get_height(), preset.icon_size / icon:get_width()) |
| 184 |
cr:set_source_surface(icon, 0, 0) |
| 185 |
cr:paint() |
| 186 |
icon = scaled |
| 187 |
|
| 188 |
local iconbox = wibox.widget.imagebox() |
| 189 |
iconbox:set_resize(false) |
| 190 |
iconbox:set_image(icon) |
| 191 |
return get_icon_wrapper_widget(iconbox, preset) |
| 192 |
end |
| 193 |
end |
| 194 |
|
| 195 |
|
| 196 |
cyclefocus.filters = { |
| 197 |
|
| 198 |
same_screen = function (c, source_c) |
| 199 |
return (c.screen or capi.mouse.screen) == source_c.screen |
| 200 |
end, |
| 201 |
|
| 202 |
same_class = function (c, source_c) |
| 203 |
return c.class == source_c.class |
| 204 |
end, |
| 205 |
|
| 206 |
|
| 207 |
marked = function (c, source_c) |
| 208 |
return awful.client.ismarked(c) |
| 209 |
end, |
| 210 |
|
| 211 |
common_tag = function (c, source_c) |
| 212 |
if c == source_c then |
| 213 |
return true |
| 214 |
end |
| 215 |
cyclefocus.debug("common_tag_filter\n" |
| 216 |
.. cyclefocus.get_object_name(c) .. " <=> " .. cyclefocus.get_object_name(source_c), 3) |
| 217 |
for _, t in pairs(c:tags()) do |
| 218 |
for _, t2 in pairs(source_c:tags()) do |
| 219 |
if t == t2 then |
| 220 |
cyclefocus.debug('common_tag_filter: client shares tag "' |
| 221 |
.. cyclefocus.get_object_name(t) |
| 222 |
.. '" with "' .. cyclefocus.get_object_name(c)..'"', 2) |
| 223 |
return true |
| 224 |
end |
| 225 |
end |
| 226 |
end |
| 227 |
return false |
| 228 |
end, |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
not_through_focus_signal = function (c, source_c) |
| 234 |
local attribs = cyclefocus.history.attribs(c) |
| 235 |
return not attribs.source or attribs.source ~= "focus" |
| 236 |
end, |
| 237 |
} |
| 238 |
|
| 239 |
local ignore_focus_signal = false |
| 240 |
local showing_client |
| 241 |
|
| 242 |
|
| 243 |
cyclefocus.get_shown_client = function() |
| 244 |
return showing_client |
| 245 |
end |
| 246 |
|
| 247 |
|
| 248 |
cyclefocus.debug = function(msg, level) |
| 249 |
level = level or 1 |
| 250 |
if not cyclefocus.debug_level or cyclefocus.debug_level < level then |
| 251 |
return |
| 252 |
end |
| 253 |
|
| 254 |
if cyclefocus.debug_use_naughty_notify then |
| 255 |
naughty.notify({ |
| 256 |
|
| 257 |
|
| 258 |
text = tostring(msg), |
| 259 |
timeout = 10, |
| 260 |
}) |
| 261 |
end |
| 262 |
print("cyclefocus: " .. msg) |
| 263 |
end |
| 264 |
|
| 265 |
local get_object_name = function (o) |
| 266 |
if not o then |
| 267 |
return '[no object]' |
| 268 |
elseif not o.valid then |
| 269 |
return '[invalid object]' |
| 270 |
elseif not o.name then |
| 271 |
return '[no object name]' |
| 272 |
else |
| 273 |
return o.name |
| 274 |
end |
| 275 |
end |
| 276 |
cyclefocus.get_object_name = get_object_name |
| 277 |
|
| 278 |
local utf8_truncate = function (s, length) |
| 279 |
if length == 0 then |
| 280 |
return s |
| 281 |
end |
| 282 |
local n = 0 |
| 283 |
for i = 1, s:len() do |
| 284 |
local b = s:byte(i) |
| 285 |
if b < 0x80 or b >= 0xc0 then |
| 286 |
n = n + 1 |
| 287 |
if n > length then |
| 288 |
return s:sub(1, i - 1) .. '…' |
| 289 |
end |
| 290 |
end |
| 291 |
end |
| 292 |
return s |
| 293 |
end |
| 294 |
|
| 295 |
cyclefocus.get_client_title = function (c, current) |
| 296 |
|
| 297 |
local title = cyclefocus.get_object_name(c) |
| 298 |
return utf8_truncate(title, 80) |
| 299 |
end |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
local history = { |
| 306 |
stack = {} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
function history.delete(c) |
| 312 |
local k = history._get_key(c) |
| 313 |
if k then |
| 314 |
table.remove(history.stack, k) |
| 315 |
end |
| 316 |
end |
| 317 |
|
| 318 |
function history._get_key(c) |
| 319 |
for k, v in ipairs(history.stack) do |
| 320 |
if v[1] == c then |
| 321 |
return k |
| 322 |
end |
| 323 |
end |
| 324 |
end |
| 325 |
|
| 326 |
function history.attribs(c) |
| 327 |
local k = history._get_key(c) |
| 328 |
if k then |
| 329 |
return history.stack[k][2] |
| 330 |
end |
| 331 |
end |
| 332 |
|
| 333 |
function history.clear() |
| 334 |
history.stack = {} |
| 335 |
end |
| 336 |
|
| 337 |
|
| 338 |
function history.add(c, filter, append, attribs) |
| 339 |
filter = filter or cyclefocus.filter_focus_history |
| 340 |
append = append or false |
| 341 |
attribs = attribs or {} |
| 342 |
|
| 343 |
|
| 344 |
cyclefocus.debug("history.add: " .. get_object_name(c), capi.awesome.startup and 4 or 2) |
| 345 |
|
| 346 |
if filter and type(filter) == "function" then |
| 347 |
if not filter(c) then |
| 348 |
cyclefocus.debug("Filtered! " .. get_object_name(c), 2) |
| 349 |
return true |
| 350 |
end |
| 351 |
end |
| 352 |
|
| 353 |
|
| 354 |
history.delete(c) |
| 355 |
|
| 356 |
if append then |
| 357 |
table.insert(history.stack, {c, attribs}) |
| 358 |
else |
| 359 |
table.insert(history.stack, 1, {c, attribs}) |
| 360 |
end |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
awful.client.focus.history.add(c) |
| 365 |
end |
| 366 |
|
| 367 |
function history.movetotop(c) |
| 368 |
local attribs = history.attribs(c) |
| 369 |
history.add(c, true, false, attribs) |
| 370 |
end |
| 371 |
|
| 372 |
function history.append(c, filter, attribs) |
| 373 |
return history.add(c, filter, true, attribs) |
| 374 |
end |
| 375 |
|
| 376 |
|
| 377 |
function history.persist() |
| 378 |
local ids = {} |
| 379 |
for _, v in ipairs(history.stack) do |
| 380 |
table.insert(ids, v[1].window) |
| 381 |
end |
| 382 |
local xprop = table.concat(ids, " ") |
| 383 |
capi.awesome.set_xproperty('awesome.cyclefocus.history', xprop) |
| 384 |
end |
| 385 |
|
| 386 |
|
| 387 |
function history.load() |
| 388 |
local xprop = capi.awesome.get_xproperty('awesome.cyclefocus.history') |
| 389 |
if not xprop or xprop == "" then |
| 390 |
return |
| 391 |
end |
| 392 |
|
| 393 |
local cls = capi.client.get() |
| 394 |
local ids = {} |
| 395 |
for id in string.gmatch(xprop, "%S+") do |
| 396 |
table.insert(ids, 1, id) |
| 397 |
end |
| 398 |
for _,window in ipairs(ids) do |
| 399 |
for _,c in pairs(cls) do |
| 400 |
if tonumber(window) == c.window then |
| 401 |
history.add(c, true, false, {source="load"}) |
| 402 |
break |
| 403 |
end |
| 404 |
end |
| 405 |
end |
| 406 |
end |
| 407 |
|
| 408 |
|
| 409 |
capi.awesome.register_xproperty('awesome.cyclefocus.history', 'string') |
| 410 |
capi.awesome.connect_signal("exit", function(restarting) |
| 411 |
ignore_focus_signal = true |
| 412 |
if restarting then |
| 413 |
history.persist() |
| 414 |
end |
| 415 |
end) |
| 416 |
|
| 417 |
|
| 418 |
cyclefocus.load_on_startup = function() |
| 419 |
capi.awesome.disconnect_signal("refresh", cyclefocus.load_on_startup) |
| 420 |
|
| 421 |
ignore_focus_signal = true |
| 422 |
history.load() |
| 423 |
if history.stack[1] then |
| 424 |
showing_client = history.stack[1][1] |
| 425 |
showing_client:jump_to() |
| 426 |
showing_client = nil |
| 427 |
end |
| 428 |
ignore_focus_signal = false |
| 429 |
end |
| 430 |
capi.awesome.connect_signal("refresh", cyclefocus.load_on_startup) |
| 431 |
|
| 432 |
|
| 433 |
cyclefocus.history = history |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
capi.client.connect_signal("focus", function (c) |
| 440 |
if ignore_focus_signal or capi.awesome.startup then |
| 441 |
cyclefocus.debug("Ignoring focus signal: " .. get_object_name(c), 4) |
| 442 |
return |
| 443 |
end |
| 444 |
history.add(c, nil, nil, {source="focus"}) |
| 445 |
end) |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
if awful.client.focus.history.disable_tracking then |
| 450 |
awful.client.focus.history.disable_tracking() |
| 451 |
else |
| 452 |
capi.client.disconnect_signal("focus", awful.client.focus.history.add) |
| 453 |
end |
| 454 |
|
| 455 |
capi.client.connect_signal("manage", function (c) |
| 456 |
if ignore_focus_signal then |
| 457 |
cyclefocus.debug("Ignoring focus signal (manage): " .. get_object_name(c), 2) |
| 458 |
return |
| 459 |
end |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
if capi.awesome.startup then |
| 464 |
history.append(c) |
| 465 |
else |
| 466 |
history.add(c, nil, false, {source="manage"}) |
| 467 |
end |
| 468 |
end) |
| 469 |
|
| 470 |
capi.client.connect_signal("unmanage", function (c) |
| 471 |
history.delete(c) |
| 472 |
end) |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
cyclefocus.raise_client_without_focus = function(c) |
| 483 |
|
| 484 |
local t = c:tags()[1] |
| 485 |
if t then |
| 486 |
t:view_only() |
| 487 |
end |
| 488 |
c:jump_to() |
| 489 |
end |
| 490 |
|
| 491 |
|
| 492 |
local restore_callback_show_client |
| 493 |
local show_client_restore_client_props = {} |
| 494 |
client.connect_signal("unmanage", function (c) |
| 495 |
if c == restore_callback_show_client then |
| 496 |
restore_callback_show_client = nil |
| 497 |
end |
| 498 |
if c == showing_client then |
| 499 |
showing_client = nil |
| 500 |
end |
| 501 |
|
| 502 |
if show_client_restore_client_props[c] then |
| 503 |
show_client_restore_client_props[c] = nil |
| 504 |
end |
| 505 |
end) |
| 506 |
|
| 507 |
|
| 508 |
local beautiful = require("beautiful") |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
cyclefocus.decorate_show_client = function(c) |
| 514 |
return { |
| 515 |
|
| 516 |
border_color = beautiful.border_focus, |
| 517 |
border_width = c.border_width or 1, |
| 518 |
|
| 519 |
|
| 520 |
} |
| 521 |
end |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
cyclefocus.decorate_show_client_others = function(c) |
| 526 |
return { |
| 527 |
|
| 528 |
|
| 529 |
} |
| 530 |
end |
| 531 |
|
| 532 |
local show_client_apply_props = {} |
| 533 |
|
| 534 |
local show_client_apply_props_others = {} |
| 535 |
local show_client_restore_client_props_others = {} |
| 536 |
|
| 537 |
local callback_show_client_lock |
| 538 |
local decorate_if_showing_client = function (c) |
| 539 |
if c == showing_client then |
| 540 |
cyclefocus.callback_show_client(c) |
| 541 |
end |
| 542 |
end |
| 543 |
|
| 544 |
local update_show_client_restore_client_props = {} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
cyclefocus.callback_show_client = function (c, restore) |
| 551 |
if callback_show_client_lock then return end |
| 552 |
callback_show_client_lock = true |
| 553 |
|
| 554 |
if restore then |
| 555 |
|
| 556 |
if show_client_restore_client_props[c] then |
| 557 |
|
| 558 |
for k,_ in pairs(show_client_restore_client_props[c]) do |
| 559 |
client.disconnect_signal("property::" .. k, decorate_if_showing_client) |
| 560 |
client.disconnect_signal("property::" .. k, update_show_client_restore_client_props[c][k]) |
| 561 |
end |
| 562 |
|
| 563 |
for k,v in pairs(show_client_restore_client_props[c]) do |
| 564 |
c[k] = v |
| 565 |
end |
| 566 |
|
| 567 |
|
| 568 |
for _c,props in pairs(show_client_restore_client_props_others[c]) do |
| 569 |
for k,v in pairs(props) do |
| 570 |
|
| 571 |
_c[k] = v |
| 572 |
end |
| 573 |
end |
| 574 |
|
| 575 |
show_client_apply_props[c] = nil |
| 576 |
show_client_restore_client_props[c] = nil |
| 577 |
show_client_restore_client_props_others[c] = nil |
| 578 |
end |
| 579 |
else |
| 580 |
|
| 581 |
local first_call = not show_client_restore_client_props[c] |
| 582 |
if first_call then |
| 583 |
show_client_restore_client_props[c] = {} |
| 584 |
show_client_apply_props[c] = {} |
| 585 |
|
| 586 |
|
| 587 |
show_client_apply_props[c] = cyclefocus.decorate_show_client(c) |
| 588 |
update_show_client_restore_client_props[c] = {} |
| 589 |
for k,_ in pairs(show_client_apply_props[c]) do |
| 590 |
show_client_restore_client_props[c][k] = c[k] |
| 591 |
end |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
show_client_apply_props_others[c] = cyclefocus.decorate_show_client_others(c) |
| 596 |
show_client_restore_client_props_others[c] = {} |
| 597 |
for s in capi.screen do |
| 598 |
for _,_c in pairs(awful.client.visible(s)) do |
| 599 |
if _c ~= c then |
| 600 |
show_client_restore_client_props_others[c][_c] = {} |
| 601 |
for k,_ in pairs(show_client_apply_props_others[c]) do |
| 602 |
show_client_restore_client_props_others[c][_c][k] = _c[k] |
| 603 |
end |
| 604 |
end |
| 605 |
end |
| 606 |
end |
| 607 |
end |
| 608 |
|
| 609 |
for k,v in pairs(show_client_apply_props[c]) do |
| 610 |
c[k] = v |
| 611 |
end |
| 612 |
|
| 613 |
for _c,_ in pairs(show_client_restore_client_props_others[c]) do |
| 614 |
for k,v in pairs(show_client_apply_props_others[c]) do |
| 615 |
_c[k] = v |
| 616 |
end |
| 617 |
end |
| 618 |
|
| 619 |
if first_call then |
| 620 |
for k,_ in pairs(show_client_apply_props[c]) do |
| 621 |
client.connect_signal("property::" .. k, decorate_if_showing_client) |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
update_show_client_restore_client_props[c][k] = function() |
| 626 |
if c.valid then |
| 627 |
show_client_restore_client_props[c][k] = c[k] |
| 628 |
end |
| 629 |
end |
| 630 |
client.connect_signal("property::" .. k, update_show_client_restore_client_props[c][k]) |
| 631 |
end |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
end |
| 637 |
end |
| 638 |
|
| 639 |
callback_show_client_lock = false |
| 640 |
end |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
local restore_ontop_c |
| 647 |
|
| 648 |
|
| 649 |
cyclefocus.show_client = function (c) |
| 650 |
showing_client = c |
| 651 |
|
| 652 |
if c then |
| 653 |
if restore_callback_show_client then |
| 654 |
cyclefocus.callback_show_client(restore_callback_show_client, true) |
| 655 |
end |
| 656 |
restore_callback_show_client = c |
| 657 |
|
| 658 |
|
| 659 |
if restore_ontop_c then |
| 660 |
restore_ontop_c() |
| 661 |
restore_ontop_c = nil |
| 662 |
end |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
if not c.ontop then |
| 668 |
if c.fullscreen then |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
local ontop_restore_clients = {} |
| 675 |
for _,_c in pairs(awful.client.visible(client.screen)) do |
| 676 |
if _c.ontop then |
| 677 |
table.insert(ontop_restore_clients, _c) |
| 678 |
_c.ontop = false |
| 679 |
end |
| 680 |
end |
| 681 |
if #ontop_restore_clients then |
| 682 |
function restore_ontop_c() |
| 683 |
for _,_c in pairs(ontop_restore_clients) do |
| 684 |
if _c.valid then |
| 685 |
_c.ontop = true |
| 686 |
end |
| 687 |
end |
| 688 |
end |
| 689 |
end |
| 690 |
else |
| 691 |
local ontop_orig_props = {c.ontop, c.below, c.above, c.fullscreen} |
| 692 |
function restore_ontop_c() |
| 693 |
if c.valid then |
| 694 |
c.ontop = ontop_orig_props[1] |
| 695 |
c.below = ontop_orig_props[2] |
| 696 |
c.above = ontop_orig_props[3] |
| 697 |
c.fullscreen = ontop_orig_props[4] |
| 698 |
end |
| 699 |
end |
| 700 |
c.ontop = true |
| 701 |
end |
| 702 |
end |
| 703 |
|
| 704 |
|
| 705 |
local sel_tags = c.screen.selected_tags |
| 706 |
local c_tag = c.first_tag or c:tags()[1] |
| 707 |
if c_tag and not awful.util.table.hasitem(sel_tags, c_tag) then |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
local restore_sticky = c.sticky |
| 715 |
c.sticky = true |
| 716 |
|
| 717 |
for _, t in pairs(c.screen.tags) do |
| 718 |
if t ~= c_tag then |
| 719 |
t.selected = false |
| 720 |
end |
| 721 |
end |
| 722 |
c_tag.selected = true |
| 723 |
|
| 724 |
|
| 725 |
c.sticky = restore_sticky |
| 726 |
end |
| 727 |
cyclefocus.callback_show_client(c, false) |
| 728 |
|
| 729 |
else |
| 730 |
if restore_ontop_c then |
| 731 |
restore_ontop_c() |
| 732 |
restore_ontop_c = nil |
| 733 |
end |
| 734 |
cyclefocus.callback_show_client(restore_callback_show_client, true) |
| 735 |
showing_client = nil |
| 736 |
end |
| 737 |
end |
| 738 |
|
| 739 |
|
| 740 |
local wbox |
| 741 |
local wbox_screen |
| 742 |
local layout |
| 743 |
|
| 744 |
|
| 745 |
cyclefocus.cycle = function(startdirection_or_args, args) |
| 746 |
if type(startdirection_or_args) == 'number' then |
| 747 |
awful.util.deprecate('startdirection is not used anymore: pass in args only', {raw=true}) |
| 748 |
else |
| 749 |
args = startdirection_or_args |
| 750 |
end |
| 751 |
args = awful.util.table.join(awful.util.table.clone(cyclefocus), args) |
| 752 |
|
| 753 |
local modifier = args.modifier or 'Alt_L' |
| 754 |
local keys = args.keys or {'Tab', 'ISO_Left_Tab'} |
| 755 |
local shift = args.shift or 'Shift' |
| 756 |
|
| 757 |
local cycle_filters = awful.util.table.join(args.cycle_filters or {}, |
| 758 |
cyclefocus.cycle_filters) |
| 759 |
|
| 760 |
|
| 761 |
local exit_key = args.exit_key |
| 762 |
if exit_key == nil then |
| 763 |
for _,key in pairs({'Escape', 'q'}) do |
| 764 |
if not awful.util.table.hasitem(keys, key) then |
| 765 |
exit_key = key |
| 766 |
break |
| 767 |
end |
| 768 |
end |
| 769 |
end |
| 770 |
|
| 771 |
|
| 772 |
local get_client_icon_widget = args.get_client_icon_widget |
| 773 |
|
| 774 |
local filter_result_cache = {} |
| 775 |
|
| 776 |
local show_clients = args.show_clients |
| 777 |
if show_clients and type(show_clients) ~= 'function' then |
| 778 |
show_clients = cyclefocus.show_client |
| 779 |
end |
| 780 |
|
| 781 |
local raise_client_fn = args.raise_client |
| 782 |
if raise_client_fn and type(raise_client_fn) ~= 'function' then |
| 783 |
raise_client_fn = cyclefocus.raise_client_without_focus |
| 784 |
end |
| 785 |
|
| 786 |
|
| 787 |
if args.cycle_filter then |
| 788 |
cycle_filters = awful.util.table.clone(cycle_filters) |
| 789 |
table.insert(cycle_filters, args.cycle_filter) |
| 790 |
end |
| 791 |
|
| 792 |
|
| 793 |
ignore_focus_signal = true |
| 794 |
|
| 795 |
|
| 796 |
local initiating_client = args.initiating_client or capi.client.focus |
| 797 |
|
| 798 |
|
| 799 |
local restore_tag_selected = {} |
| 800 |
for s in capi.screen do |
| 801 |
restore_tag_selected[s] = {} |
| 802 |
for _,t in pairs(s.tags) do |
| 803 |
restore_tag_selected[s][t] = t.selected |
| 804 |
end |
| 805 |
end |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
local get_next_client = function(direction, idx, stack, consider_cur_idx) |
| 815 |
local startidx = idx |
| 816 |
stack = stack or history.stack |
| 817 |
consider_cur_idx = consider_cur_idx or args.focus_clients |
| 818 |
|
| 819 |
local nextc |
| 820 |
|
| 821 |
cyclefocus.debug('get_next_client: #' .. idx .. ", dir=" .. direction |
| 822 |
.. ", start=" .. startidx .. ", consider_cur=" .. tostring(consider_cur_idx), 2) |
| 823 |
|
| 824 |
local n = #stack |
| 825 |
if consider_cur_idx then |
| 826 |
local c_top = stack[idx][1] |
| 827 |
if c_top ~= capi.client.focus then |
| 828 |
n = n+1 |
| 829 |
cyclefocus.debug("Considering nextc from top of stack: " .. tostring(c_top), 2) |
| 830 |
else |
| 831 |
consider_cur_idx = false |
| 832 |
end |
| 833 |
end |
| 834 |
for loop_stack_i = 1, n do |
| 835 |
if not consider_cur_idx or loop_stack_i ~= 1 then |
| 836 |
idx = idx + direction |
| 837 |
if idx < 1 then |
| 838 |
idx = #stack |
| 839 |
elseif idx > #stack then |
| 840 |
idx = 1 |
| 841 |
end |
| 842 |
end |
| 843 |
cyclefocus.debug('find loop: #' .. idx .. ", dir=" .. direction, 3) |
| 844 |
nextc = stack[idx][1] |
| 845 |
|
| 846 |
if nextc then |
| 847 |
|
| 848 |
if cycle_filters then |
| 849 |
|
| 850 |
|
| 851 |
local get_cached_filter_result = function(f, a, b) |
| 852 |
b = b or false |
| 853 |
if filter_result_cache[f] == nil then |
| 854 |
filter_result_cache[f] = { [a] = { [b] = { } } } |
| 855 |
return nil |
| 856 |
elseif filter_result_cache[f][a] == nil then |
| 857 |
filter_result_cache[f][a] = { [b] = { } } |
| 858 |
return nil |
| 859 |
elseif filter_result_cache[f][a][b] == nil then |
| 860 |
return nil |
| 861 |
end |
| 862 |
return filter_result_cache[f][a][b] |
| 863 |
end |
| 864 |
local set_cached_filter_result = function(f, a, b, value) |
| 865 |
b = b or false |
| 866 |
get_cached_filter_result(f, a, b) |
| 867 |
filter_result_cache[f][a][b] = value |
| 868 |
end |
| 869 |
|
| 870 |
|
| 871 |
local filter_result |
| 872 |
for _k, filter in pairs(cycle_filters) do |
| 873 |
cyclefocus.debug("Checking filter ".._k.."/"..#cycle_filters..": "..tostring(filter), 4) |
| 874 |
filter_result = get_cached_filter_result(filter, nextc, initiating_client) |
| 875 |
if filter_result ~= nil then |
| 876 |
if not filter_result then |
| 877 |
nextc = false |
| 878 |
break |
| 879 |
end |
| 880 |
else |
| 881 |
filter_result = filter(nextc, initiating_client) |
| 882 |
set_cached_filter_result(filter, nextc, initiating_client, filter_result) |
| 883 |
if not filter_result then |
| 884 |
cyclefocus.debug("Filtering/skipping client: " .. get_object_name(nextc), 3) |
| 885 |
nextc = false |
| 886 |
break |
| 887 |
end |
| 888 |
end |
| 889 |
end |
| 890 |
end |
| 891 |
if nextc then |
| 892 |
|
| 893 |
break |
| 894 |
end |
| 895 |
end |
| 896 |
end |
| 897 |
cyclefocus.debug("get_next_client returns: " .. get_object_name(nextc) .. ', idx=' .. idx, 1) |
| 898 |
return nextc, idx |
| 899 |
end |
| 900 |
|
| 901 |
local first_run = true |
| 902 |
local nextc |
| 903 |
local idx = 1 |
| 904 |
|
| 905 |
|
| 906 |
local initial_screen = awful.screen.focused and awful.screen.focused() or mouse.screen |
| 907 |
|
| 908 |
|
| 909 |
local restore_mouse_coords |
| 910 |
if show_clients and args.move_mouse_pointer then |
| 911 |
local s = capi.screen[capi.mouse.screen] |
| 912 |
local coords = capi.mouse.coords() |
| 913 |
restore_mouse_coords = {s = s, x = coords.x, y = coords.y} |
| 914 |
local pos = {x = s.geometry.x, y = s.geometry.y} |
| 915 |
|
| 916 |
capi.mouse.coords(pos, true) |
| 917 |
restore_mouse_coords.moved = pos |
| 918 |
end |
| 919 |
|
| 920 |
capi.keygrabber.run(function(mod, key, event) |
| 921 |
|
| 922 |
|
| 923 |
local exit_grabber = function(c) |
| 924 |
cyclefocus.debug("exit_grabber: " .. get_object_name(c), 2) |
| 925 |
if wbox then |
| 926 |
wbox.visible = false |
| 927 |
end |
| 928 |
capi.keygrabber.stop() |
| 929 |
|
| 930 |
|
| 931 |
if show_clients then |
| 932 |
show_clients() |
| 933 |
end |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
if restore_tag_selected then |
| 939 |
for s in capi.screen do |
| 940 |
if not c or s ~= c.screen then |
| 941 |
for _,t in pairs(s.tags) do |
| 942 |
t.selected = restore_tag_selected[s][t] |
| 943 |
end |
| 944 |
end |
| 945 |
end |
| 946 |
end |
| 947 |
|
| 948 |
|
| 949 |
if restore_mouse_coords then |
| 950 |
if restore_mouse_coords.s == capi.screen[capi.mouse.screen] then |
| 951 |
local coords = capi.mouse.coords() |
| 952 |
local moved_coords = restore_mouse_coords.moved |
| 953 |
if moved_coords.x == coords.x and moved_coords.y == coords.y then |
| 954 |
capi.mouse.coords({x = restore_mouse_coords.x, y = restore_mouse_coords.y}, true) |
| 955 |
end |
| 956 |
end |
| 957 |
end |
| 958 |
|
| 959 |
if c then |
| 960 |
showing_client = c |
| 961 |
raise_client_fn(c) |
| 962 |
if c ~= initiating_client then |
| 963 |
history.movetotop(c) |
| 964 |
end |
| 965 |
showing_client = nil |
| 966 |
end |
| 967 |
ignore_focus_signal = false |
| 968 |
|
| 969 |
return true |
| 970 |
end |
| 971 |
|
| 972 |
cyclefocus.debug("grabber: mod: " .. table.concat(mod, ',') |
| 973 |
.. ", key: " .. tostring(key) |
| 974 |
.. ", event: " .. tostring(event) |
| 975 |
.. ", modifier_key: " .. tostring(modifier), 3) |
| 976 |
|
| 977 |
if exit_key and key == exit_key then |
| 978 |
return exit_grabber(initiating_client) |
| 979 |
elseif #mod == 1 and mod[1] == 'Control' and key == 'c' then |
| 980 |
|
| 981 |
return exit_grabber(initiating_client) |
| 982 |
end |
| 983 |
|
| 984 |
|
| 985 |
local direction = awful.util.table.hasitem(mod, shift) and -1 or 1 |
| 986 |
|
| 987 |
if event == "release" and key == modifier then |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
if first_run then |
| 992 |
nextc, idx = get_next_client(direction, idx) |
| 993 |
end |
| 994 |
if show_clients then |
| 995 |
show_clients(nextc) |
| 996 |
end |
| 997 |
return exit_grabber(nextc) |
| 998 |
end |
| 999 |
|
| 1000 |
|
| 1001 |
if not first_run then |
| 1002 |
if not awful.util.table.hasitem(keys, key) then |
| 1003 |
cyclefocus.debug("Ignoring unexpected key: " .. tostring(key), 1) |
| 1004 |
return true |
| 1005 |
end |
| 1006 |
if event == "release" then |
| 1007 |
return true |
| 1008 |
end |
| 1009 |
end |
| 1010 |
first_run = false |
| 1011 |
|
| 1012 |
nextc, idx = get_next_client(direction, idx) |
| 1013 |
if not nextc then |
| 1014 |
return exit_grabber() |
| 1015 |
end |
| 1016 |
|
| 1017 |
|
| 1018 |
if show_clients then |
| 1019 |
show_clients(nextc) |
| 1020 |
end |
| 1021 |
|
| 1022 |
if args.focus_clients then |
| 1023 |
capi.client.focus = nextc |
| 1024 |
end |
| 1025 |
|
| 1026 |
if not args.display_notifications then |
| 1027 |
return true |
| 1028 |
end |
| 1029 |
|
| 1030 |
local container_margin_top_bottom = dpi(5) |
| 1031 |
local container_margin_left_right = dpi(5) |
| 1032 |
if not wbox then |
| 1033 |
wbox = wibox({ontop = true }) |
| 1034 |
wbox._for_screen = mouse.screen |
| 1035 |
wbox:set_fg(beautiful.fg_normal) |
| 1036 |
wbox:set_bg("#ffffff00") |
| 1037 |
|
| 1038 |
local container_inner = wibox.layout.align.vertical() |
| 1039 |
local container_layout = wibox.container.margin( |
| 1040 |
container_inner, |
| 1041 |
container_margin_left_right, container_margin_left_right, |
| 1042 |
container_margin_top_bottom, container_margin_top_bottom) |
| 1043 |
container_layout = wibox.container.background(container_layout) |
| 1044 |
container_layout:set_bg(beautiful.bg_normal..'cc') |
| 1045 |
|
| 1046 |
wbox:set_widget(container_layout) |
| 1047 |
|
| 1048 |
|
| 1049 |
layout = wibox.layout.fixed.vertical() |
| 1050 |
container_inner:set_middle(layout) |
| 1051 |
else |
| 1052 |
layout:reset() |
| 1053 |
end |
| 1054 |
|
| 1055 |
|
| 1056 |
if not wbox_screen or wbox_screen ~= initial_screen then |
| 1057 |
wbox_screen = initial_screen |
| 1058 |
local wa = screen[wbox_screen].workarea |
| 1059 |
local w = math.ceil(wa.width * 0.618) |
| 1060 |
wbox:geometry({ |
| 1061 |
|
| 1062 |
x = math.ceil(wa.x + wa.width - w), |
| 1063 |
width = w, |
| 1064 |
}) |
| 1065 |
end |
| 1066 |
local wbox_height = 0 |
| 1067 |
|
| 1068 |
|
| 1069 |
local display_entry_for_idx_offset = function(offset, c, _idx, displayed_list) |
| 1070 |
local preset = awful.util.table.clone(args.default_preset) |
| 1071 |
|
| 1072 |
|
| 1073 |
local args_for_cb = { |
| 1074 |
client=c, |
| 1075 |
offset=offset, |
| 1076 |
idx=_idx, |
| 1077 |
displayed_list=displayed_list } |
| 1078 |
local preset_for_offset = args.preset_for_offset |
| 1079 |
|
| 1080 |
if preset_for_offset.default then |
| 1081 |
preset_for_offset.default(preset, args_for_cb) |
| 1082 |
end |
| 1083 |
|
| 1084 |
local preset_cb = preset_for_offset[tostring(offset)] |
| 1085 |
if preset_cb then |
| 1086 |
preset_cb(preset, args_for_cb) |
| 1087 |
end |
| 1088 |
|
| 1089 |
local entry_layout = wibox.layout.fixed.horizontal() |
| 1090 |
|
| 1091 |
if preset.icon_size then |
| 1092 |
local icon_widget = get_client_icon_widget(c, preset) |
| 1093 |
if icon_widget then |
| 1094 |
entry_layout:add(icon_widget) |
| 1095 |
end |
| 1096 |
end |
| 1097 |
|
| 1098 |
local textbox = wibox.widget.textbox() |
| 1099 |
textbox:set_markup(preset.text) |
| 1100 |
textbox:set_font(preset.font) |
| 1101 |
textbox:set_wrap("word_char") |
| 1102 |
textbox:set_ellipsize("middle") |
| 1103 |
|
| 1104 |
local _, h = textbox:get_preferred_size(c.screen) |
| 1105 |
textbox:set_forced_height(h) |
| 1106 |
local textbox_margin = wibox.container.margin(textbox) |
| 1107 |
textbox_margin:set_margins(dpi(5)) |
| 1108 |
|
| 1109 |
entry_layout:add(textbox_margin) |
| 1110 |
entry_layout = wibox.container.margin( |
| 1111 |
entry_layout, dpi(5), dpi(5), dpi(2), dpi(2)) |
| 1112 |
local entry_with_bg = wibox.container.background(entry_layout) |
| 1113 |
if offset == 0 then |
| 1114 |
entry_with_bg:set_fg(beautiful.fg_focus) |
| 1115 |
entry_with_bg:set_bg(beautiful.bg_focus) |
| 1116 |
else |
| 1117 |
entry_with_bg:set_fg(beautiful.fg_normal) |
| 1118 |
|
| 1119 |
end |
| 1120 |
layout:add(entry_with_bg) |
| 1121 |
|
| 1122 |
|
| 1123 |
local context = {dpi=beautiful.xresources.get_dpi(initial_screen)} |
| 1124 |
_, h = entry_with_bg:fit(context, wbox.width, 2^20) |
| 1125 |
wbox_height = wbox_height + h |
| 1126 |
end |
| 1127 |
|
| 1128 |
|
| 1129 |
local prevnextlist = awful.util.table.clone(history.stack) |
| 1130 |
local _idx = idx |
| 1131 |
|
| 1132 |
local dlist = {} |
| 1133 |
|
| 1134 |
dlist[0] = _idx |
| 1135 |
prevnextlist[_idx][1] = false |
| 1136 |
|
| 1137 |
|
| 1138 |
for _,dir in ipairs({1, -1}) do |
| 1139 |
_idx = dlist[0] |
| 1140 |
local n = dir == 1 and args.display_next_count or args.display_prev_count |
| 1141 |
for i = 1, n do |
| 1142 |
local _i = i * dir |
| 1143 |
_, _idx = get_next_client(dir, _idx, prevnextlist, false) |
| 1144 |
if _ then |
| 1145 |
dlist[_i] = _idx |
| 1146 |
end |
| 1147 |
prevnextlist[_idx][1] = false |
| 1148 |
end |
| 1149 |
end |
| 1150 |
|
| 1151 |
|
| 1152 |
local offsets = {} |
| 1153 |
for n in pairs(dlist) do table.insert(offsets, n) end |
| 1154 |
table.sort(offsets) |
| 1155 |
|
| 1156 |
|
| 1157 |
for _,i in ipairs(offsets) do |
| 1158 |
_idx = dlist[i] |
| 1159 |
display_entry_for_idx_offset(i, history.stack[_idx][1], _idx, dlist) |
| 1160 |
end |
| 1161 |
local wa = screen[initial_screen].workarea |
| 1162 |
local h = wbox_height + container_margin_top_bottom*2 |
| 1163 |
wbox:geometry({ |
| 1164 |
height = h, |
| 1165 |
y = wa.y + floor(wa.height/2 - h/2), |
| 1166 |
}) |
| 1167 |
wbox.visible = true |
| 1168 |
return true |
| 1169 |
end) |
| 1170 |
end |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
function cyclefocus.key(mods, key, startdirection_or_args, args) |
| 1175 |
mods = mods or {modkey} or {"Mod4"} |
| 1176 |
key = key or "Tab" |
| 1177 |
if type(startdirection_or_args) == 'number' then |
| 1178 |
awful.util.deprecate('startdirection is not used anymore: pass in mods, key, args', {raw=true}) |
| 1179 |
else |
| 1180 |
args = startdirection_or_args |
| 1181 |
end |
| 1182 |
args = args and awful.util.table.clone(args) or {} |
| 1183 |
if not args.keys then |
| 1184 |
if key == "Tab" then |
| 1185 |
args.keys = {"Tab", "ISO_Left_Tab"} |
| 1186 |
else |
| 1187 |
args.keys = {key} |
| 1188 |
end |
| 1189 |
end |
| 1190 |
if not args.modifier then |
| 1191 |
|
| 1192 |
|
| 1193 |
local conversion = { |
| 1194 |
mod4 = "Super_L", |
| 1195 |
control = "Control_L", |
| 1196 |
shift = "Shift_L", |
| 1197 |
mod1 = "Alt_L", |
| 1198 |
|
| 1199 |
mod5 = "ISO_Level3_Shift", |
| 1200 |
} |
| 1201 |
args.modifier = conversion[mods[1]:lower()] |
| 1202 |
if not args.modifier then |
| 1203 |
args.modifier = mods[1] |
| 1204 |
end |
| 1205 |
end |
| 1206 |
|
| 1207 |
return awful.key(mods, key, function(c) |
| 1208 |
args.initiating_client = c |
| 1209 |
cyclefocus.cycle(args) |
| 1210 |
end) |
| 1211 |
end |
| 1212 |
|
| 1213 |
return cyclefocus |