-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindx.py
More file actions
180 lines (139 loc) · 5.39 KB
/
windx.py
File metadata and controls
180 lines (139 loc) · 5.39 KB
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
#!/usr/bin/env/ python2
"""
windx by Omar Sandoval.
References:
http://incise.org/tinywm.html
http://plwm.sourceforge.net/
http://qtile.org/
This program is an extremely simple window manager for X.
Emphasis on EXTREMELY. It does not handle any errors and
assumes the user is sending correct events.
So far, it has trouble resizing windows and bringing windows
to the front.
It requires an installation of urxvt. If you'd prefer not to install it
then change "/usr/bin/urxvt" to /usr/bin/xterm"
Keybindings for windx are as follows:
Shift-Enter opens a urxvt.
RightClick-Drag will move a window.
"""
import os
import sys
import Xlib.rdb
import Xlib.X
import Xlib.XK
# Command to be called when Shift-Enter is read
urxvtCommand = ["/usr/bin/urxvt"]
class WindowManager(object):
def __init__(self, display):
self.display = display
self.dragWindow = None
self.dragOffset = (0, 0)
os.environ["DISPLAY"] = display.get_display_name()
self.enterCodes = set(code for code, index in self.display.keysym_to_keycodes(Xlib.XK.XK_Return))
self.screens = []
for screenID in xrange(0, display.screen_count()):
if self.screenEvent(screenID):
self.screens.append(screenID)
self.requestedEvent = {
Xlib.X.MapRequest: self.mapRequest,
Xlib.X.ConfigureRequest: self.configureRequest,
Xlib.X.MotionNotify: self.mouseMotion,
Xlib.X.ButtonPress: self.mousePress,
Xlib.X.ButtonRelease: self.mouseRelease,
Xlib.X.KeyPress: self.keyPress,
}
# Function to be called from main loop
def events(self):
event = self.display.next_event()
if event.type in self.requestedEvent:
handle = self.requestedEvent[event.type]
handle(event)
# Where to map event
def mapRequest(self, event):
event.window.map()
self.grabWindowEvents(event.window)
# Set options for configuring a window
def configureRequest(self, event):
window = event.window
args = { "borderWidth": 3 }
if event.value_mask & Xlib.X.CWX:
args["x"] = event.x
if event.value_mask & Xlib.X.CWY:
args["y"] = event.y
if event.value_mask & Xlib.X.CWWidth:
args["width"] = event.width
if event.value_mask & Xlib.X.CWHeight:
args["height"] = event.height
if event.value_mask & Xlib.X.CWSibling:
args["sibling"] = event.above
if event.value_mask & Xlib.X.CWStackMode:
args["stackMode"] = event.stack_mode
window.configure(**args)
# Redirect screen events
def screenEvent(self, screenID):
root_window = self.display.screen(screenID).root
mask = Xlib.X.SubstructureRedirectMask
root_window.change_attributes(event_mask=mask)
self.display.sync()
for code in self.enterCodes:
# Grab Shift-Enter
root_window.grab_key(code,
Xlib.X.ShiftMask & ~Xlib.X.AnyModifier,
1, Xlib.X.GrabModeAsync, Xlib.X.GrabModeAsync)
# Find all existing windows.
for window in root_window.query_tree().children:
self.grabWindowEvents(window)
return True
# Recognize and handle RightClick and drag events.
def grabWindowEvents(self, window):
window.grab_button(3, 0, True,
Xlib.X.ButtonMotionMask | Xlib.X.ButtonReleaseMask | Xlib.X.ButtonPressMask,
Xlib.X.GrabModeAsync,
Xlib.X.GrabModeAsync,
Xlib.X.NONE,
Xlib.X.NONE,
None)
# RightClick and drag to move the window about your X session
def mouseMotion(self, event):
#Right click & drag to move window.
if event.state & Xlib.X.Button3MotionMask:
if self.dragWindow is None:
# Start RightClick Drag
self.dragWindow = event.window
geom = self.dragWindow.get_geometry()
self.dragOffset = geom.x - event.root_x, geom.y - event.root_y
else:
# Continue RightClick Drag
x, y = self.dragOffset
self.dragWindow.configure(x=x + event.root_x, y=y + event.root_y)
# What to do when Shift-Enter is pressed
def mousePress(self, event):
if event.detail == 3:
# Right-click: raise window
event.window.configure(stackMode=Xlib.X.Above)
# Do nothing when the mouse is released, or let
# go of window that is being dragged.
def mouseRelease(self, event):
self.dragWindow = None
# Open an instance of urxvt for Shift+Enter
def keyPress(self, event):
if event.state & Xlib.X.ShiftMask and event.detail in self.enterCodes:
# Shift-Enter: start urxvt
self.systemCommand(urxvtCommand)
# System command to be called
def systemCommand(self, command):
# OS stuff for the command to run.
if os.fork() != 0:
return
os.setsid()
os.umask(0)
os.execve(command[0], command, os.environ)
sys.exit(1)
# Main loop for events
def main():
display, appname, resource_database, args = Xlib.rdb.get_display_opts(Xlib.rdb.stdopts)
wm = WindowManager(display)
while True:
wm.events()
if __name__ == "__main__":
sys.exit(main())