Skip to content

Instantly share code, notes, and snippets.

View fphammerle's full-sized avatar

Fabian Peter Hammerle fphammerle

View GitHub Profile
@fphammerle
fphammerle / ranger-screen.py
Last active July 22, 2016 19:09
ranger plugin: update gnu screen's working directory whenever the current directory in ranger changes
import os
import ranger.api
import subprocess
# https://github.com/ranger/ranger/wiki/Signals-and-Hooks
def ranger_signal_bind(signal_name):
def decorate(handler):
old_hook_ready = ranger.api.hook_ready
def hook_ready(fm):
fm.notify('%s %r' % (signal_name, handler))
@fphammerle
fphammerle / gatttool-polar-h10.txt
Created April 8, 2017 10:01
bluez gatttool: receive heart rate notifications from polar h10 (bluetooth heart rate sensor)
$ sudo hcitool lescan
AA:BB:CC:DD:EE:FF Polar H10 ABCDEFGH
$ gatttool -t random --device=AA:BB:CC:DD:EE:FF --interactive
[AA:BB:CC:DD:EE:FF][LE]> connect
Attempting to connect to AA:BB:CC:DD:EE:FF
Connection successful
[AA:BB:CC:DD:EE:FF][LE]> primary
attr handle: 0x0001, end grp handle: 0x0009 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x000a, end grp handle: 0x000d uuid: 00001801-0000-1000-8000-00805f9b34fb
@fphammerle
fphammerle / sabre-dav-yii2-auth-backend.php
Last active July 21, 2017 09:31
use yii2's authentication capabilities to control access to SabreDAV's webdav server
<?php
use \Sabre\HTTP\RequestInterface;
use \Sabre\HTTP\ResponseInterface;
class SabreDavYii2AuthBackend implements \Sabre\DAV\Auth\Backend\BackendInterface
{
function check(RequestInterface $request, ResponseInterface $response)
{
if(\Yii::$app->user->isGuest) {
@fphammerle
fphammerle / xlib-non-blocking-event-loop.py
Last active January 14, 2018 13:13
Wrap python-xlib's Xlib.display.next_event() so it does not block the main loop
"""
python-xlib's Xlib.display.next_event() does not have a timeout when waiting for an event to be queued.
It may block the script's execution forever.
Workaround:
Call display.next_event() only if an event is available in the queue.
Use select.select() on the xserver's socket to wait for an event and specify a timeout.
based on: https://stackoverflow.com/a/8592969/5894777
@fphammerle
fphammerle / xlib-xrandr-ctypes-print-output-names.py
Created January 21, 2018 15:33
Display names of outputs available to the X server by accessing the xrandr extension via python's ctypes
#!/usr/bin/env python3
"""
Display names of outputs available to the X server similiar to 'xrandr' command.
Access xlib and xrandr extension via python's ctypes library.
tags: x11, xorg, x.org, xwindow-system, xlib, libx, xrandr, outputs, monitor, display
"""
import ctypes
// gcc -lgcrypt ...
#include <assert.h>
#include <gcrypt.h>
#include <stdio.h>
#include <stdlib.h>
const size_t KEYGRIP_LENGTH = 20;
unsigned char* read_binary(const char* path, size_t* bytes_read) {
@fphammerle
fphammerle / libpam-authenticate.c
Last active May 20, 2018 11:32
Authenticate user via libpam.
#include <assert.h>
#include <security/pam_appl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // getpass
// apt install libpam0g-dev
// compile with flag -lpam
#!/usr/bin/env python3
"""
> sstream = io.StringIO()
> subprocess.run(['hostname'], stdout=sstream, check=True)
io.UnsupportedOperation: fileno
"""
import os
import select
#!/usr/bin/env python3
import os
import select
DEFAULT_BUFFER_SIZE_BYTES = 8196
DEFAULT_READ_TIMEOUT_SECONDS = 1.0
def rselect(fd, timeout_seconds=None):
# https://forum.pine64.org/showthread.php?tid=1297&pid=15958#pid15958
$ fdtget --type x /boot/pine64/sun50i-a64-pine64-plus.dtb /soc@01c00000/eth@01c30000 reg
0 1c30000 0 10000 0 1c00000 0 30
# https://forum.pine64.org/showthread.php?tid=1297&pid=16138#pid16138
$ fdtget /boot/pine64/sun50i-a64-pine64-plus.dtb /aliases spi0
/soc@01c00000/spi@01c68000
$ fdtget /boot/pine64/sun50i-a64-pine64-plus.dtb /soc@01c00000/spi@01c68000 status
disabled
$ sudo fdtput --verbose --type s /boot/pine64/sun50i-a64-pine64-plus.dtb /soc@01c00000/spi@01c68000 status okay