Skip to content

Instantly share code, notes, and snippets.

View engelmarkus's full-sized avatar

Markus Engel engelmarkus

View GitHub Profile
@engelmarkus
engelmarkus / test.py
Last active December 26, 2023 15:07
[POC] Auto-stopping Spotify while other programs play audio
#!/usr/bin/env python
# pip install dbus-python pulsectl
import signal
import sys
import pulsectl
import dbus
def sig_handler(signal, frame):
@engelmarkus
engelmarkus / wireguard.txt
Last active December 5, 2022 19:17
Setting up a VPN with WireGuard on a vServer
Setting up a VPN with WireGuard
Prerequisites
A linux server with
- ip forwarding enabled:
~> sysctl net/ipv4/ip_forward
net.ipv4.ip_forward = 1
- the "tun" kernel module loaded:
~> lsmod | grep tun
@engelmarkus
engelmarkus / tagged_ptr.cpp
Created November 27, 2017 01:17
A pointer class storing flags in unused address bits.
#include <cassert>
#include <cstdint>
#include <iostream>
#include <type_traits>
template <unsigned arg, unsigned result = 0>
constexpr unsigned log2 = log2<(arg >> 1), result + 1>;
template <unsigned result>
constexpr unsigned log2<1, result> = result;
@engelmarkus
engelmarkus / Makefile
Created November 27, 2017 01:09
Runtime code generation and execution in C++
all: program
program: program.cpp
g++ -std=c++14 -o program program.cpp -ldl -rdynamic
run: program query.cpp
./program "`< query.cpp`"
.PHONY: clean
clean:
@engelmarkus
engelmarkus / order_deps.rb
Last active December 21, 2016 18:58
Script for determining the correct order of static libraries for GNU ld
#!/usr/bin/env ruby
# When passing static libraries to GNU ld, one has to make sure the order is correct or undefined references will occur.
# This is due to the linker reading each static library just once.
# With "--start-group" and "--end-group" this behaviour can be overridden but using these options is discouraged.
# call the linker verbosely and print the correct library order to the shell
# ~> gcc prog.c -static -Wl,--verbose -Wl,-\( -llib1 -llib2 -lrest_of_the_libs -Wl,-\) | ./order_deps.rb
libs = Array.new
@engelmarkus
engelmarkus / variadic_bind.cpp
Created October 29, 2016 00:27
std::bind for variadic functions
/*
When using std::bind, all parameters have to be bound either to a value or to a placeholder.
This makes it impossible to use std::bind with variadic functions if you do not know the number
of parameters they take in advance.
This gist shows how to allow binding the first parameters of variadic functions.
The original idea can be found here: http://stackoverflow.com/questions/21192659/variadic-templates-and-stdbind#21193316
*/
#include <experimental/tuple>
#include <functional>
@engelmarkus
engelmarkus / producer_consumer.cpp
Created July 14, 2016 01:16
C++17 implementation of the producer-consumer problem.
#include <algorithm>
#include <experimental/any>
#include <array>
#include <condition_variable>
#include <functional>
#include <iostream>
#include <iterator>
#include <mutex>
#include <thread>
#include <vector>
@engelmarkus
engelmarkus / creator.cpp
Last active July 13, 2016 21:23
k-way merge sort - sorting data using a fixed amount of memory
#include <algorithm>
#include <array>
#include <fstream>
#include <numeric>
#include <random>
using namespace std;
// Step 1 - Create a file with 256 MiB of data
array<uint64_t, 256 * 1024 * 1024 / sizeof(uint64_t)> data;
@engelmarkus
engelmarkus / times.cs
Created June 30, 2016 12:21
C# - adding some rubyness: times extension method for ints
using System;
using System.Linq;
namespace Times {
static class Extensions {
public static void times(this int t, Action f) {
for (int i = 0; i < t; ++i) {
f();
}
}
@engelmarkus
engelmarkus / gtkmm-example.cpp
Created May 18, 2016 22:31
Creating a window with gtkmm 3 and a glade description.
// g++ -std=c++14 -o gtkmm-example gtkmm-example.cpp `pkg-config --cflags --libs gtkmm-3.0`
#include <memory>
#include <gtkmm.h>
class MainWindow : public Gtk::ApplicationWindow {
public:
MainWindow(BaseObjectType* obj, Glib::RefPtr<Gtk::Builder> const& builder)
: Gtk::ApplicationWindow(obj)
, builder{builder}