Skip to content

Commit

Permalink
adhere to standardjs style 🖌
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Aug 12, 2020
1 parent 7d39a9c commit db6e995
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -21,7 +21,7 @@ here's an example of a `~/.wmrc`:
M-return = termite
M-A-e = rofi-emoji
M-A-p = rofi-pass
M-space = rofi -show drun -display-drun '' -font 'source code pro 20' -separator-style 'none'
M-space = rofi -show drun -display-drun '' -font 'source code pro 20' -separator-style 'none'
XF86AudioLowerVolume = ponymix decrease 10 -N
XF86AudioRaiseVolume = ponymix increase 10 -N

Expand Down
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -9,13 +9,16 @@ const configure = require('./src/configure')

const name = require('./lib/name')

const emitter = new events.EventEmitter
const emitter = new events.EventEmitter()

process.env.PATH = `${dirname(process.argv[1])}/bin:${process.env.PATH}`

// globals are: X, screen, root, keybindings, workspaces & currentWorkspace
function createClient() {
function createClient () {
x11.createClient((error, display) => {
if (error) {
console.error('error:', error)
}
global.screen = display.screen[0]
global.root = global.screen.root
global.X = display.client
Expand Down
1 change: 0 additions & 1 deletion lib/keys.js
Expand Up @@ -65,7 +65,6 @@ module.exports = {
wiggly: 49,
'~': 49,


// media
XF86MonBrightnessDown: 232,
XF86MonBrightnessUp: 233,
Expand Down
34 changes: 17 additions & 17 deletions package.json
@@ -1,19 +1,19 @@
{
"name": "wm",
"version": "1.0.0",
"description": "window manager",
"main": "index.js",
"author": "chee <chee@snake.dog>",
"license": "GPL-3.0",
"dependencies": {
"ewmh": "^0.3.0",
"rc": "^1.1.6",
"x11": "^2.2.0"
},
"scripts": {
"start": "node index.js"
},
"devDependencies": {
"standard": "^10.0.2"
}
"name": "wm",
"version": "1.0.0",
"description": "window manager",
"main": "index.js",
"author": "chee <chee@snake.dog>",
"license": "GPL-3.0",
"dependencies": {
"ewmh": "^0.3.0",
"rc": "^1.1.6",
"x11": "^2.2.0"
},
"scripts": {
"start": "node index.js"
},
"devDependencies": {
"standard": "^10.0.2"
}
}
63 changes: 36 additions & 27 deletions src/command.js
Expand Up @@ -6,82 +6,91 @@ const configure = require('./configure')
module.exports = {
wm: { reload: configure },
workspace: {
switch(id) {
switch (id) {
const target = global.workspaces[constrainNumber(id - 1, global.workspaces.length)]
if (global.currentWorkspace.id == target.id) return
if (global.currentWorkspace.id === target.id) return
getAllWindows().filter(window => !window.pinned).forEach(Window.hide)
global.currentWorkspace = target
Workspace.show(global.currentWorkspace)
}
},
window: {
destroy() {
destroy () {
global.X.DestroyWindow(global.currentWorkspace.currentWindow.id)
},
workspace(id) {
workspace (id) {
const window = global.currentWorkspace.currentWindow
const target = global.workspaces[constrainNumber(id - 1, global.workspaces.length)]
if (target.id == global.currentWorkspace.id) return
if (target.id === global.currentWorkspace.id) return
Workspace.removeWindow(global.currentWorkspace, window)
Workspace.addWindow(target, window)
Window.hide(window)
global.currentWorkspace.currentWindow = null
},
tile(direction) {
tile (direction) {
const id = global.currentWorkspace.currentWindow.id
const {pixel_width, pixel_height} = global.screen
const pixelWidth = global.screen.pixel_width
const pixelHeight = global.screen.pixel_height
let match = null
if (direction == 'left' || direction == 'right') {
global.X.ResizeWindow(id, pixel_width / 2, pixel_height)
direction == 'left'
if (direction === 'left' || direction === 'right') {
global.X.ResizeWindow(id, pixelWidth / 2, pixelHeight)
direction === 'left'
? global.X.MoveWindow(id, 0, 0)
: global.X.MoveWindow(id, pixel_width / 2, 0)
: global.X.MoveWindow(id, pixelWidth / 2, 0)
} else if ((match = direction.match(/^(top)-(\w+)/)) || (match = direction.match(/^(bottom)-(\w+)/))) {
const [, vertical, horizontal] = match
global.X.ResizeWindow(id, pixel_width / 2, pixel_height / 2)
vertical == 'top'
? horizontal == 'left' ? global.X.MoveWindow(id, 0, 0) : global.X.MoveWindow(id, pixel_width / 2, 0)
: horizontal == 'left' ? global.X.MoveWindow(id, 0, pixel_height / 2) : global.X.MoveWindow(id, pixel_width / 2, pixel_height / 2)
} else if (direction == 'full') {
global.X.ResizeWindow(id, pixel_width, pixel_height)
global.X.ResizeWindow(id, pixelWidth / 2, pixelHeight / 2)
vertical === 'top'
? horizontal === 'left' ? global.X.MoveWindow(id, 0, 0) : global.X.MoveWindow(id, pixelWidth / 2, 0)
: horizontal === 'left' ? global.X.MoveWindow(id, 0, pixelHeight / 2) : global.X.MoveWindow(id, pixelWidth / 2, pixelHeight / 2)
} else if (direction === 'full') {
global.X.ResizeWindow(id, pixelWidth, pixelHeight)
global.X.MoveWindow(id, 0, 0)
}
},
// todo: constrain these to a grid
// todo: dry these up
resize(arg) {
resize (arg) {
const id = global.currentWorkspace.currentWindow.id
const match = arg.match(/(x|y)(\+|-)(\d+)/)
if (!match) return
const [, axis, operation, percent] = match
const signedPercent = Number(`${operation}${percent}`)
const {pixel_width, pixel_height} = global.screen
const pixelWidth = global.screen.pixel_width
const pixelHeight = global.screen.pixelHeight
global.X.GetGeometry(id, (error, attributes) => {
if (error) {
console.error('resize get geometry error', error)
}
const {width, height} = attributes
global.X.ResizeWindow(global.currentWorkspace.currentWindow.id,
axis == 'x' ? width + (pixel_width / 100 * signedPercent) : width,
axis == 'y' ? height + (pixel_height / 100 * signedPercent) : height
axis === 'x' ? width + (pixelWidth / 100 * signedPercent) : width,
axis === 'y' ? height + (pixelHeight / 100 * signedPercent) : height
)
})
},
move(arg) {
move (arg) {
const id = global.currentWorkspace.currentWindow.id
const match = arg.match(/(x|y)(\+|-)(\d+)/)
if (!match) return
const [, axis, operation, percent] = match
const signedPercent = Number(`${operation}${percent}`)
const {pixel_width, pixel_height} = global.screen
const pixelWidth = global.screen.pixel_width
const pixelHeight = global.screen.pixelHeight
global.X.GetGeometry(id, (error, attributes) => {
if (error) {
console.error('move get geometry error', error)
}
global.X.MoveWindow(global.currentWorkspace.currentWindow.id,
axis == 'x' ? attributes.xPos + (pixel_width / 100 * signedPercent) : attributes.xPos,
axis == 'y' ? attributes.yPos + (pixel_height / 100 * signedPercent) : attributes.yPos
axis === 'x' ? attributes.xPos + (pixelWidth / 100 * signedPercent) : attributes.xPos,
axis === 'y' ? attributes.yPos + (pixelHeight / 100 * signedPercent) : attributes.yPos
)
})
},
'toggle-pinning'() {
'toggle-pinning' () {
const workspace = global.currentWorkspace
const window = workspace.currentWindow
if (window.id == global.root.id) return
if (window.id === global.root.id) return
global.workspaces.forEach(workspace => {
Workspace.removeWindow(workspace, window)
})
Expand Down
23 changes: 11 additions & 12 deletions src/configure.js
Expand Up @@ -6,7 +6,7 @@ const x11 = require('x11')
const ASYNC = 1
const NOPE = 0

function grabKeys(X, bindings) {
function grabKeys (X, bindings) {
bindings.forEach(binding => {
X.GrabKey(global.root, true,
binding.buttons,
Expand All @@ -18,7 +18,7 @@ function grabKeys(X, bindings) {
}

// todo: make this key configurable
function grabButtons(X) {
function grabButtons (X) {
X.GrabButton(
global.root, true, x11.eventMask.ButtonPress | x11.eventMask.ButtonRelease | x11.eventMask.PointerMotion,
ASYNC, ASYNC, NOPE, NOPE, 1, keys.buttons.M
Expand All @@ -29,8 +29,7 @@ function grabButtons(X) {
)
}


function makeWorkspaces(size) {
function makeWorkspaces (size) {
const workspaces = []
for (let id = 0; id < size; id++) {
global.workspaces && global.workspaces[id]
Expand All @@ -40,7 +39,7 @@ function makeWorkspaces(size) {
return workspaces
}

module.exports = function() {
module.exports = function () {
const configuration = require('../lib/configuration')

// todo: ungrab old keys
Expand All @@ -56,12 +55,12 @@ module.exports = function() {
)

global.X.ChangeWindowAttributes(global.root, {
eventMask: x11.eventMask.SubstructureNotify
| x11.eventMask.SubstructureRedirect
| x11.eventMask.ResizeRedirect
| x11.eventMask.Exposure
| x11.eventMask.MapRequest
| x11.eventMask.EnterWindow
| x11.eventMask.FocusChange
eventMask: x11.eventMask.SubstructureNotify |
x11.eventMask.SubstructureRedirect |
x11.eventMask.ResizeRedirect |
x11.eventMask.Exposure |
x11.eventMask.MapRequest |
x11.eventMask.EnterWindow |
x11.eventMask.FocusChange
}, console.error)
}
35 changes: 19 additions & 16 deletions src/event.js
Expand Up @@ -9,14 +9,14 @@ const {getWindow} = require('./util')
let start
let attributes
module.exports = {
KeyPress({buttons, keycode}) {
KeyPress ({buttons, keycode}) {
global.keybindings.forEach(binding => {
if (buttons == binding.buttons && keycode == binding.keycode) {
if (buttons === binding.buttons && keycode === binding.keycode) {
exec(binding.cmd, console.log)
}
})
},
ButtonPress(event) {
ButtonPress (event) {
const {child} = event
global.X.RaiseWindow(child)
global.currentWorkspace.currentWindow = getWindow(child)
Expand All @@ -25,29 +25,32 @@ module.exports = {
Workspace.addWindow(global.currentWorkspace, global.currentWorkspace.currentWindow)
}
global.X.GetGeometry(child, (error, attr) => {
if (error) {
console.error('get geometry error', error)
}
start = event
attributes = attr
})
},
MotionNotify({rootx, rooty}) {
MotionNotify ({rootx, rooty}) {
if (!start) return
const xdiff = rootx - start.rootx
const ydiff = rooty - start.rooty
start.keycode == 1 && start.buttons == keys.buttons.M && global.X.MoveWindow(
start.keycode === 1 && start.buttons === keys.buttons.M && global.X.MoveWindow(
start.child,
attributes.xPos + (start.keycode == 1 ? xdiff : 0),
attributes.yPos + (start.keycode == 1 ? ydiff : 0)
attributes.xPos + (start.keycode === 1 ? xdiff : 0),
attributes.yPos + (start.keycode === 1 ? ydiff : 0)
)
start.keycode == 3 && global.X.ResizeWindow(
start.keycode === 3 && global.X.ResizeWindow(
start.child,
Math.max(1, attributes.width + (start.keycode == 3 ? xdiff : 0)),
Math.max(1, attributes.height + (start.keycode == 3 ? ydiff : 0))
Math.max(1, attributes.width + (start.keycode === 3 ? xdiff : 0)),
Math.max(1, attributes.height + (start.keycode === 3 ? ydiff : 0))
)
},
ButtonRelease() {
ButtonRelease () {
attributes = start = null
},
MapRequest(event) {
MapRequest (event) {
global.X.GetWindowAttributes(event.wid, (error, attributes) => {
if (error) return console.error(error)
if (attributes[8]) {
Expand All @@ -61,18 +64,18 @@ module.exports = {
const window = Window.create(event.wid)
Workspace.addWindow(global.currentWorkspace, window)
},
FocusIn(event) {
FocusIn (event) {
global.currentWorkspace.currentWindow = getWindow(event.wid)
Window.focus(global.currentWorkspace.currentWindow)
},
EnterNotify(event) {
EnterNotify (event) {
const window = global.currentWorkspace.currentWindow = getWindow(event.wid)
Window.focus(window)
},
ConfigureRequest({wid, width, height}) {
ConfigureRequest ({wid, width, height}) {
global.X.ResizeWindow(wid, width, height)
},
DestroyNotify({wid}) {
DestroyNotify ({wid}) {
const window = getWindow(wid)
// todo: if windows kept track of their workspace, this would not be necessary
global.workspaces.forEach(workspace => Workspace.removeWindow(workspace, window))
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Expand Up @@ -39,7 +39,7 @@ const getAllWindows = () => (
)

const getWindow = id => (
getAllWindows().filter(window => window.id == id)[0] || Window.create(id)
getAllWindows().filter(window => window.id === id)[0] || Window.create(id)
)

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions src/workspace.js
Expand Up @@ -17,7 +17,7 @@ const addWindow = (workspace, window) => {
const removeWindow = (workspace, window) => {
if (!window) return
const {id} = window
workspace.windows = workspace.windows.filter(window => window.id != id)
workspace.windows = workspace.windows.filter(window => window.id !== id)
}

const show = workspace => {
Expand All @@ -33,7 +33,7 @@ const hide = workspace => {

const contains = (workspace, window) => {
const {id} = window
return workspace.windows.some(window => window.id == id)
return workspace.windows.some(window => window.id === id)
}

module.exports = {create, addWindow, removeWindow, show, hide, contains}

0 comments on commit db6e995

Please sign in to comment.