From db6e99579134834f15e504008e1bc1d9143de123 Mon Sep 17 00:00:00 2001 From: chee Date: Tue, 16 May 2017 22:02:45 +0100 Subject: [PATCH] =?UTF-8?q?adhere=20to=20standardjs=20style=20=F0=9F=96=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- index.js | 7 ++++-- lib/keys.js | 1 - package.json | 34 +++++++++++++------------- src/command.js | 63 +++++++++++++++++++++++++++--------------------- src/configure.js | 23 +++++++++--------- src/event.js | 35 +++++++++++++++------------ src/util.js | 2 +- src/workspace.js | 4 +-- 9 files changed, 92 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index 3bbf587..3fec532 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index b18876a..0c25d3d 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/lib/keys.js b/lib/keys.js index 7aee3f1..4e0a677 100644 --- a/lib/keys.js +++ b/lib/keys.js @@ -65,7 +65,6 @@ module.exports = { wiggly: 49, '~': 49, - // media XF86MonBrightnessDown: 232, XF86MonBrightnessUp: 233, diff --git a/package.json b/package.json index 2e469b5..f5f46e9 100644 --- a/package.json +++ b/package.json @@ -1,19 +1,19 @@ { - "name": "wm", - "version": "1.0.0", - "description": "window manager", - "main": "index.js", - "author": "chee ", - "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 ", + "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" + } } diff --git a/src/command.js b/src/command.js index 782a896..cf68799 100644 --- a/src/command.js +++ b/src/command.js @@ -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) }) diff --git a/src/configure.js b/src/configure.js index 34babba..7177d99 100644 --- a/src/configure.js +++ b/src/configure.js @@ -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, @@ -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 @@ -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] @@ -40,7 +39,7 @@ function makeWorkspaces(size) { return workspaces } -module.exports = function() { +module.exports = function () { const configuration = require('../lib/configuration') // todo: ungrab old keys @@ -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) } diff --git a/src/event.js b/src/event.js index 95bba9e..5969e1c 100644 --- a/src/event.js +++ b/src/event.js @@ -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) @@ -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]) { @@ -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)) diff --git a/src/util.js b/src/util.js index 9459e09..a14085c 100644 --- a/src/util.js +++ b/src/util.js @@ -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 = { diff --git a/src/workspace.js b/src/workspace.js index 307ec84..ae83b27 100644 --- a/src/workspace.js +++ b/src/workspace.js @@ -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 => { @@ -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}