Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gillescastel committed Mar 8, 2019
0 parents commit 278de2d
Show file tree
Hide file tree
Showing 17 changed files with 718 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/*
data/**/*
20 changes: 20 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"


[packages]

evdev = "*"
xlib = "*"


[dev-packages]



[requires]

python_version = "3.6"
43 changes: 43 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added __init__.py
Empty file.
27 changes: 27 additions & 0 deletions clipboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import subprocess

def copy(string, target=None):
extra_args = []
if target != None:
extra_args += ['-target', target]

return subprocess.run(
['xclip', '-selection', 'c'] + extra_args,
universal_newlines=True,
input=string
)

def get(target=None):
extra_args = []
if target != None:
extra_args += ['-target', target]

result = subprocess.run(
['xclip', '-selection', 'c', '-o'] + extra_args,
stdout=subprocess.PIPE,
universal_newlines=True
)

# returncode = result.returncode
stdout = result.stdout.strip()
return stdout
61 changes: 61 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
KEYSYM_MAP = {
65307: "ESC",
32: "SPACE",
39: "'",
44: ",",
45: "-",
46: ".",
47: "/",
48: "0",
49: "1",
50: "2",
51: "3",
52: "4",
53: "5",
54: "6",
55: "7",
56: "8",
57: "9",
59: ";",
61: "=",
91: "[",
92: "\\",
93: "]",
96: "`",
97: "a",
98: "b",
99: "c",
100: "d",
101: "e",
102: "f",
103: "g",
104: "h",
105: "i",
106: "j",
107: "k",
108: "l",
109: "m",
110: "n",
111: "o",
112: "p",
113: "q",
114: "r",
115: "s",
116: "t",
117: "u",
118: "v",
119: "w",
120: "x",
121: "y",
122: "z",
}

TARGET = 'image/x-inkscape-svg'

NORMAL = ''
VIM = 'Vim'
STYLE = 'Style'
SAVE_STYLE = 'Save style'
OBJECT = 'Object'
SAVE_OBJECT = 'Save object'
DISABLED = 'Disabled'
Binary file added constants.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions disabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from Xlib import X
from constants import KEYSYM_MAP, NORMAL
from mode import mode
import normal

def disabled_mode(event, keysym, manager):
if keysym in KEYSYM_MAP:
character = KEYSYM_MAP.get(keysym, 0)

if event.type == X.KeyPress:
if character == '`':
mode(NORMAL)
manager.teardown()
manager.listen(normal.normal_mode)
elif event.type == X.KeyRelease:
pass
76 changes: 76 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from Xlib.display import Display
from Xlib import X
from Xlib.ext import record
from Xlib.protocol import rq

import time
from constants import KEYSYM_MAP, NORMAL

from mode import mode
from normal import normal_mode

class ShortcutManager():
def __init__(self):
self.disp2 = Display()

def is_inkscape(self):
window = self.disp2.get_input_focus().focus
cls = window.get_wm_class()
return cls and cls[0] == 'inkscape'

def meta_handler(self, handler):
def handle(reply):
print('yes')
data = reply.data
while len(data):
event, data = rq.EventField(None).parse_binary_value(
data, self.disp.display, None, None)

if not self.is_inkscape():
return

keysym = self.disp.keycode_to_keysym(event.detail, 0)
handler(event, keysym, self)

return handle

def listen(self, handler):
self.disp = Display()
root = self.disp.screen().root
self.ctx = self.disp.record_create_context(
# datum_flags
0,
# clients
[record.AllClients],
# rangers
[{
'core_requests': (0, 0),
'core_replies': (0, 0),
'ext_requests': (0, 0, 0, 0),
'ext_replies': (0, 0, 0, 0),
'delivered_events': (0, 0),
'device_events': (0x02, 0x05),
'errors': (0, 0),
'client_started': False,
'client_died': False,
}])

self.disp.record_enable_context(self.ctx, self.meta_handler(handler))
self.disp.record_free_context(self.ctx)

try:
while True:
time.sleep(1)
# Infinite wait, doesn't do anything as no events are grabbed
event = root.display.next_event()
except:
print('exception!')

def teardown(self):
self.disp.record_disable_context(self.ctx)

sm = ShortcutManager()
mode('INIT')
time.sleep(1)
mode(NORMAL)
sm.listen(normal_mode)
2 changes: 2 additions & 0 deletions mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def mode(name):
print(name)
Binary file added mode.pyc
Binary file not shown.
Loading

0 comments on commit 278de2d

Please sign in to comment.