1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
#!/usr/bin/env python3
#######################################
# Qtile keybindings image generator #
#######################################
import getopt
import os
import sys
import cairocffi as cairo
from cairocffi import ImageSurface
this_dir = os.path.dirname(__file__)
base_dir = os.path.abspath(os.path.join(this_dir, ".."))
sys.path.insert(0, base_dir)
BUTTON_NAME_Y = 65
BUTTON_NAME_X = 10
COMMAND_Y = 20
COMMAND_X = 10
LEGEND = ["modifiers", "layout", "group", "window", "other"]
CUSTOM_KEYS = {
"Backspace": 2,
"Tab": 1.5,
"\\": 1.5,
"Return": 2.45,
"shift": 2,
"space": 5,
}
class Button:
def __init__(self, key, x, y, width, height):
self.key = key
self.x = x
self.y = y
self.width = width
self.height = height
class Pos:
WIDTH = 78
HEIGHT = 70
GAP = 5
def __init__(self, x, y):
self.x = x
self.row_x = x
self.y = y
self.custom_width = {}
for i, val in CUSTOM_KEYS.items():
self.custom_width[i] = val * self.WIDTH
def get_pos(self, name):
if name in self.custom_width:
width = self.custom_width[name]
else:
width = self.WIDTH
info = Button(name, self.x, self.y, width, self.HEIGHT)
self.x = self.x + self.GAP + width
return info
def skip_x(self, times=1):
self.x = self.x + self.GAP + times * self.WIDTH
def next_row(self):
self.x = self.row_x
self.y = self.y + self.GAP + self.HEIGHT
class KeyboardPNGFactory:
def __init__(self, modifiers, keys):
self.keys = keys
self.modifiers = modifiers.split("-")
self.key_pos = self.calculate_pos(20, 140)
def rgb_red(self, context):
context.set_source_rgb(0.8431372549, 0.3725490196, 0.3725490196)
def rgb_green(self, context):
context.set_source_rgb(0.6862745098, 0.6862745098, 0)
def rgb_yellow(self, context):
context.set_source_rgb(1, 0.6862745098, 0)
def rgb_cyan(self, context):
context.set_source_rgb(0.5137254902, 0.6784313725, 0.6784313725)
def rgb_violet(self, context):
context.set_source_rgb(0.831372549, 0.5215686275, 0.6784313725)
def calculate_pos(self, x, y):
pos = Pos(x, y)
key_pos = {}
for c in "`1234567890-=":
key_pos[c] = pos.get_pos(c)
key_pos["Backspace"] = pos.get_pos("Backspace")
pos.next_row()
key_pos["Tab"] = pos.get_pos("Tab")
for c in "qwertyuiop[]\\":
key_pos[c] = pos.get_pos(c)
pos.next_row()
pos.skip_x(1.6)
for c in "asdfghjkl;'":
key_pos[c] = pos.get_pos(c)
key_pos["Return"] = pos.get_pos("Return")
pos.next_row()
key_pos["shift"] = pos.get_pos("shift")
for c in "zxcvbnm":
key_pos[c] = pos.get_pos(c)
key_pos["period"] = pos.get_pos("period")
key_pos["comma"] = pos.get_pos("comma")
key_pos["/"] = pos.get_pos("/")
pos.next_row()
key_pos["control"] = pos.get_pos("control")
pos.skip_x()
key_pos["mod4"] = pos.get_pos("mod4")
key_pos["mod1"] = pos.get_pos("mod1")
key_pos["space"] = pos.get_pos("space")
key_pos["Print"] = pos.get_pos("Print")
pos.skip_x(3)
key_pos["Up"] = pos.get_pos("Up")
pos.next_row()
pos.skip_x(12.33)
key_pos["Left"] = pos.get_pos("Left")
key_pos["Down"] = pos.get_pos("Down")
key_pos["Right"] = pos.get_pos("Right")
pos.next_row()
for legend in LEGEND:
key_pos[legend] = pos.get_pos(legend)
pos.skip_x(5)
key_pos["Button1"] = pos.get_pos("Button1")
key_pos["Button2"] = pos.get_pos("Button2")
key_pos["Button3"] = pos.get_pos("Button3")
pos.next_row()
key_pos["FN_KEYS"] = pos.get_pos("FN_KEYS")
return key_pos
def add_logo(self, context):
logo_img = os.path.abspath(os.path.join(base_dir, "libqtile", "resources", "logo.png"))
context.save()
context.scale(0.5)
logo = ImageSurface.create_from_png(logo_img)
context.set_source_surface(logo, 20, 50)
context.paint()
context.restore()
def render(self, filename):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1280, 800)
context = cairo.Context(surface)
with context:
context.set_source_rgb(1, 1, 1)
context.paint()
self.add_logo(context)
context.move_to(210, 80)
context.set_font_size(28)
context.show_text("Keybindings for Qtile")
context.move_to(210, 100)
context.set_font_size(18)
if len([i for i in self.modifiers if i]):
context.show_text("Modifiers: " + ", ".join(self.modifiers))
else:
context.show_text("No modifiers used.")
for i in self.key_pos.values():
if i.key in ["FN_KEYS"]:
continue
self.draw_button(context, i.key, i.x, i.y, i.width, i.height)
# draw functional
fn = [i for i in keys.values() if i.key[:4] == "XF86"]
if len(fn):
fn_pos = self.key_pos["FN_KEYS"]
x = fn_pos.x
for i in fn:
self.draw_button(context, i.key, x, fn_pos.y, fn_pos.width, fn_pos.height)
x += Pos.GAP + Pos.WIDTH
# draw mouse base
context.rectangle(830, 660, 244, 90)
context.set_source_rgb(0, 0, 0)
context.stroke()
context.set_font_size(28)
context.move_to(900, 720)
context.show_text("MOUSE")
surface.write_to_png(filename)
def draw_button(self, context, key, x, y, width, height):
fn = False
if key[:4] == "XF86":
fn = True
if key in LEGEND:
if key == "modifiers":
self.rgb_red(context)
elif key == "group":
self.rgb_green(context)
elif key == "layout":
self.rgb_cyan(context)
elif key == "window":
self.rgb_yellow(context)
else:
self.rgb_violet(context)
context.rectangle(x, y, width, height)
context.fill()
if key in self.modifiers:
context.rectangle(x, y, width, height)
self.rgb_red(context)
context.fill()
if key in self.keys:
k = self.keys[key]
context.rectangle(x, y, width, height)
self.set_key_color(context, k)
context.fill()
self.show_multiline(context, x + COMMAND_X, y + COMMAND_Y, k)
context.rectangle(x, y, width, height)
context.set_source_rgb(0, 0, 0)
context.stroke()
if fn:
key = key[4:]
context.set_font_size(10)
else:
context.set_font_size(14)
context.move_to(x + BUTTON_NAME_X, y + BUTTON_NAME_Y)
context.show_text(self.translate(key))
def show_multiline(self, context, x, y, key):
"""Cairo doesn't support multiline. Added with word wrapping."""
c_width = 14
if key.key in CUSTOM_KEYS:
c_width *= CUSTOM_KEYS[key.key]
context.set_font_size(10)
context.set_source_rgb(0, 0, 0)
context.move_to(x, y)
words = key.command.split(" ")
words.reverse()
printable = last_word = words.pop()
while len(words):
last_word = words.pop()
if len(printable + " " + last_word) < c_width:
printable += " " + last_word
continue
context.show_text(printable)
y += 10
context.move_to(x, y)
printable = last_word
if last_word is not None:
context.show_text(printable)
def set_key_color(self, context, key):
if key.scope == "group":
self.rgb_green(context)
elif key.scope == "layout":
self.rgb_cyan(context)
elif key.scope == "window":
self.rgb_yellow(context)
else:
self.rgb_violet(context)
def translate(self, text):
dictionary = {
"period": ",",
"comma": ".",
"Left": "←",
"Down": "↓",
"Right": "→",
"Up": "↑",
"AudioRaiseVolume": "Volume up",
"AudioLowerVolume": "Volume down",
"AudioMute": "Audio mute",
"AudioMicMute": "Mic mute",
"MonBrightnessUp": "Brightness up",
"MonBrightnessDown": "Brightness down",
}
if text not in dictionary:
return text
return dictionary[text]
class KInfo:
NAME_MAP = {
"togroup": "to group",
"toscreen": "to screen",
}
KEY_MAP = {
"grave": "`",
"semicolon": ";",
"slash": "/",
"backslash": "\\",
"comma": ",",
"period": ".",
"bracketleft": "[",
"bracketright": "]",
"quote": "'",
"minus": "-",
"equals": "=",
}
def __init__(self, key):
if key.key in self.KEY_MAP:
self.key = self.KEY_MAP[key.key]
else:
self.key = key.key
self.command = self.get_command(key)
self.scope = self.get_scope(key)
def get_command(self, key):
if hasattr(key, "desc") and key.desc:
return key.desc
cmd = key.commands[0]
command = cmd.name
if command in self.NAME_MAP:
command = self.NAME_MAP[command]
command = command.replace("_", " ")
if len(cmd.args):
if isinstance(cmd.args[0], str):
command += " " + cmd.args[0]
return command
def get_scope(self, key):
selectors = key.commands[0].selectors
if len(selectors):
return selectors[0][0]
class MInfo(KInfo):
def __init__(self, mouse):
self.key = mouse.button
self.command = self.get_command(mouse)
self.scope = self.get_scope(mouse)
def get_kb_map(config_path=None):
from libqtile.confreader import Config
c = Config(config_path)
if config_path:
c.load()
kb_map = {}
for key in c.keys:
mod = "-".join(key.modifiers)
if mod not in kb_map:
kb_map[mod] = {}
info = KInfo(key)
kb_map[mod][info.key] = info
for mouse in c.mouse:
mod = "-".join(mouse.modifiers)
if mod not in kb_map:
kb_map[mod] = {}
info = MInfo(mouse)
kb_map[mod][info.key] = info
return kb_map
help_doc = """
usage: gen-keybinding-img [-h] [-c CONFIGFILE] [-o OUTPUT_DIR]
Qtile keybindings image generator
optional arguments:
-h, --help show this help message and exit
-c CONFIGFILE, --config CONFIGFILE
use specified configuration file. If no presented
default will be used
-o OUTPUT_DIR, --output-dir OUTPUT_DIR
set directory to export all images to
"""
if __name__ == "__main__":
config_path = None
output_dir = ""
try:
opts, args = getopt.getopt(sys.argv[1:], "hc:o:", ["help=", "config=", "output-dir="])
except getopt.GetoptError:
print(help_doc)
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print(help_doc)
sys.exit()
elif opt in ("-c", "--config"):
config_path = arg
elif opt in ("-o", "--output-dir"):
output_dir = arg
kb_map = get_kb_map(config_path)
for modifier, keys in kb_map.items():
if not modifier:
filename = "no_modifier.png"
else:
filename = f"{modifier}.png"
output_file = os.path.abspath(os.path.join(output_dir, filename))
f = KeyboardPNGFactory(modifier, keys)
f.render(output_file)
|