Skip to content

Instantly share code, notes, and snippets.

@palopezv
Forked from neuro-sys/dwmconfig.h
Last active June 25, 2026 21:25
Show Gist options
  • Select an option

  • Save palopezv/efd34059af6126ad970940bcc6a90f2e to your computer and use it in GitHub Desktop.

Select an option

Save palopezv/efd34059af6126ad970940bcc6a90f2e to your computer and use it in GitHub Desktop.
dwm volume control with hardware multimedia keys (pipewire, pulseaudio, amixer and light as an extra)
/**
* dwmconfig.h
* Hardware multimedia keys
*/
/* Somewhere at the beginning of config.h include: */
/*
You obviously need the X11 development packages installed, X11proto in particular, but
here is the location of the keysyms header upstream copy if you can't bother
using the contents of your own hard drive. ;-P
https://cgit.freedesktop.org/xorg/proto/x11proto/tree/XF86keysym.h
*/
#include <X11/XF86keysym.h>
/* If you use pipewire add somewhere in your constants definition section. Use "wpctl status" to
find out the real sink ID, 0 is a placeholder here. */
static const char *upvol[] = { "/usr/bin/wpctl", "set-volume", "0", "5%+", NULL };
static const char *downvol[] = { "/usr/bin/wpctl", "set-volume", "0", "5%-", NULL };
static const char *mutevol[] = { "/usr/bin/wpctl", "set-mute", "0", "toggle", NULL };
/* czardien made it work with @DEFAULT_AUDIO_SINK@ */
static const char *upvol[] = { "/usr/bin/wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "5%+", NULL };
static const char *downvol[] = { "/usr/bin/wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", "5%-", NULL };
static const char *mutevol[] = { "/usr/bin/wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", "toggle", NULL };
/* If you use pulsaudio add somewhere in your constants definition section instead. */
static const char *upvol[] = { "/usr/bin/pactl", "set-sink-volume", "0", "+5%", NULL };
static const char *downvol[] = { "/usr/bin/pactl", "set-sink-volume", "0", "-5%", NULL };
static const char *mutevol[] = { "/usr/bin/pactl", "set-sink-mute", "0", "toggle", NULL };
/* AidenThing suggests using this general solution for dynamically changing outputs. */
static const char *upvol[] = { "/usr/bin/pactl", "set-sink-volume", "@DEFAULT_SINK@", "+5%", NULL };
static const char *downvol[] = { "/usr/bin/pactl", "set-sink-volume", "@DEFAULT_SINK@", "-5%", NULL };
static const char *mutevol[] = { "/usr/bin/pactl", "set-sink-mute", "@DEFAULT_SINK@", "toggle", NULL };
/* If you use amixer, use this instead. Thanks go to DaniOrt3ga. */
static const char *upvol[] = { "/usr/bin/amixer", "set", "Master", "5%+", NULL };
static const char *downvol[] = { "/usr/bin/amixer", "set", "Master", "5%-", NULL };
static const char *mutevol[] = { "/usr/bin/amixerl", "set", "Master", "toggle", NULL };
/* To use light add this to the constant definition section. Thanks Hritik14. */
static const char *light_up[] = { "/usr/bin/light", "-A", "5", NULL };
static const char *light_down[] = { "/usr/bin/light", "-U", "5", NULL };
/* Add to keys[] array. With 0 as modifier, you are able to use the keys directly. */
static Key keys[] = {
{ 0, XF86XK_AudioLowerVolume, spawn, {.v = downvol } },
{ 0, XF86XK_AudioMute, spawn, {.v = mutevol } },
{ 0, XF86XK_AudioRaiseVolume, spawn, {.v = upvol } },
};
/* If you have a small laptop keyboard or don't want to spring your fingers too far away. */
static Key keys[] = {
{ MODKEY, XK_F11, spawn, {.v = downvol } },
{ MODKEY, XK_F9, spawn, {.v = mutevol } },
{ MODKEY, XK_F12, spawn, {.v = upvol } },
};
/* To use light add this to the keys[] array. Thanks Hritik14. */
static Key keys[] = {
{ 0, XF86XK_MonBrightnessUp, spawn, {.v = light_up} },
{ 0, XF86XK_MonBrightnessDown, spawn, {.v = light_down} },
};
@nong10

nong10 commented Oct 4, 2020

Copy link
Copy Markdown

It works thanks so much

@bdealbuquerque

bdealbuquerque commented Dec 17, 2020

Copy link
Copy Markdown

It works perfectly. Thank you very much !

@Hritik14

Hritik14 commented Apr 1, 2021

Copy link
Copy Markdown

@manukll

static const char
	*light_up[] = {"/usr/bin/light", "-A", "5", NULL},
	*light_down[] = {"/usr/bin/light", "-U", "5", NULL};

static Key keys[] = {
	{ 0     ,			XF86XK_MonBrightnessUp  ,			spawn ,			{.v = light_up}}     ,
	{ 0     ,			XF86XK_MonBrightnessDown,			spawn ,			{.v = light_down}}   ,
};

You'll need to install light package, of course.

@pzaszp

pzaszp commented Apr 11, 2021

Copy link
Copy Markdown

What are the keys to change vol?

@crackself

Copy link
Copy Markdown

Thanks, it work on my laptop.

@JavisDohnny

Copy link
Copy Markdown

KINO

@ProNebo

ProNebo commented Oct 7, 2021

Copy link
Copy Markdown

Thanks! It helped!

ghost commented Dec 9, 2021

Copy link
Copy Markdown

Thanks bro, it helpful.
I want ask you if can I use it because I am not finding the licencing?

@momoyon

momoyon commented Dec 24, 2021

Copy link
Copy Markdown

thanks it helped a lot!
i have a problem... how can i change the mute and volume of Headphone and Master simultaneously? because when i mute when my Headphone is connected it doesn't mute it...

@eMarce1

eMarce1 commented Dec 31, 2021

Copy link
Copy Markdown

So, this question might be stupid, but where is the constants definition section? I can't seem to find it.

@palopezv

palopezv commented Jan 1, 2022

Copy link
Copy Markdown
Author

@oyMarcel Look for the section where constants are defined. The C syntax is static const char *pointer.

@palopezv

palopezv commented Jan 1, 2022

Copy link
Copy Markdown
Author

@ahmedsamyh You'll need to write a small script or program in the language of your choice that executes what you need and map it to a key combination in your keyboard, I advice you write another to undo it as well ;-).

@hard-tek

hard-tek commented Jan 5, 2022

Copy link
Copy Markdown

Worked like a charm!

ghost commented Jan 28, 2022

Copy link
Copy Markdown

Amazing! Thanks.

@Arqib

Arqib commented Feb 28, 2022

Copy link
Copy Markdown

This works! Thanks.

@krindra

krindra commented Mar 9, 2022

Copy link
Copy Markdown

PERFECT

@GabubuAvailable

Copy link
Copy Markdown

That helped me. Thank you so much :-D

@DaniOrt3ga

DaniOrt3ga commented Jun 16, 2022

Copy link
Copy Markdown

In case you use amixer:

static const char *upvol[] = { "/usr/bin/amixer", "set", "Master", "5%+", NULL };
static const char *downvol[] = { "/usr/bin/amixer", "set", "Master", "5%-", NULL };
static const char *mutevol[] = { "/usr/bin/amixer", "set", "Master", "toggle", NULL };

Check the +/- position

@GabubuAvailable

Copy link
Copy Markdown

i used amixer long time ago but thanks :)

@pranshuthegamer

Copy link
Copy Markdown

how do i install the x11 dev thing on arch?

@moodfew

moodfew commented Oct 3, 2022

Copy link
Copy Markdown

GOD BLESS YOU DUDE!

@AidenThing

Copy link
Copy Markdown

If you're using pulse and your default is not always the same, use:

static const char *upvol[] = { "/usr/bin/pactl", "set-sink-volume", "@DEFAULT_SINK@", "+5%", NULL };
static const char *downvol[] = { "/usr/bin/pactl", "set-sink-volume", "@DEFAULT_SINK@", "-5%", NULL };
static const char *mutevol[] = { "/usr/bin/pactl", "set-sink-mute", "@DEFAULT_SINK@", "toggle", NULL };

@czardien

czardien commented Nov 7, 2023

Copy link
Copy Markdown

With pipewire I ended up using @DEFAULT_AUDIO_SINK@ so I didn't have to fetch the Audio sink ID with wpctl status; seems to be working:

static const char *upvol[]      = { "/usr/bin/wpctl",   "set-volume", "@DEFAULT_AUDIO_SINK@",      "5%+",      NULL };
static const char *downvol[]    = { "/usr/bin/wpctl",   "set-volume", "@DEFAULT_AUDIO_SINK@",      "5%-",      NULL };
static const char *mutevol[]    = { "/usr/bin/wpctl",   "set-mute",   "@DEFAULT_AUDIO_SINK@",      "toggle",   NULL };

Edit: and of course thanks a million for the gist

@palopezv

palopezv commented Nov 7, 2023

Copy link
Copy Markdown
Author

@czardien thank you for the contribution and you're very welcome. 😁

@palopezv

palopezv commented Nov 7, 2023

Copy link
Copy Markdown
Author

@AidenThing thank you for your contribution and my apologies for not seeing it before.

@mwyvr

mwyvr commented Jan 29, 2024

Copy link
Copy Markdown

For wpctl it's wise to set a maximum limit for set-volume; above 100% distortion will creep in, and just for safety too. Add "-l 1.0" to the wpctl command to increase volume to avoid that.

@alissonmarcs

Copy link
Copy Markdown

Awesome! Thank you so much.

@raresgoidescu

raresgoidescu commented Sep 12, 2024

Copy link
Copy Markdown

In case someone wonders how to Play/Pause/Skip/GoBack on your fav media player:

static const char *playpause[]  = { "/usr/bin/playerctl", "play-pause", NULL };
static const char *play_next[]  = { "/usr/bin/playerctl", "next", NULL };
static const char *play_prev[]  = { "/usr/bin/playerctl", "previous", NULL};
static Key keys[] = {
	{ 0,					   XF86XK_AudioPlay, spawn, {.v = playpause } },
	{ 0,					   XF86XK_AudioPrev, spawn, {.v = play_prev } },
	{ 0,					   XF86XK_AudioNext, spawn, {.v = play_next } },
};

Also install playerctl.

@g114-39

g114-39 commented Jun 25, 2026

Copy link
Copy Markdown

For the Back, Forward, and Reload buttons on your keyboard add this to your config.h

/* commands */
static const char *browserback[]    = { "xdotool", "key", "Alt+Left", NULL };
static const char *browserforward[] = { "xdotool", "key", "Alt+Right", NULL };
static const char *browserreload[]  = { "xdotool", "key", "Ctrl+r", NULL };
static Key keys[] = {
    { 0, XF86XK_MonBrightnessUp,   spawn, {.v = brightup } },
    { 0, XF86XK_MonBrightnessDown, spawn, {.v = brightdown } },
    { 0, XF86XK_Back,              spawn, {.v = browserback } },
    { 0, XF86XK_Forward,           spawn, {.v = browserforward } },
    { 0, XF86XK_Refresh,           spawn, {.v = browserreload } },
};

If you need a dedicated notification for volume/brightness increase/decrease, you can use dunst and libnotify to show that.

Create shell scripts with the name dwm-volume.sh and dwm-brightness.sh and put them somewhere. You could also incorporate what the scripts do in C inside the config.h if you wish.

In dwm-volume.sh put this:

#!/bin/bash

# 1. Change the actual PulseAudio system volume stream
if [ "$1" == "mute" ]; then

    /* 
     For amixer, adjust command 
     */

    pactl set-sink-mute @DEFAULT_SINK@ toggle
else
    pactl set-sink-volume @DEFAULT_SINK@ "$1"
fi

# 2. Extract the exact current volume percentage and mute state
VOLUME=$(pactl get-sink-volume @DEFAULT_SINK@ | awk '{print $5}' | head -n 1 | sed 's/[^0-9%]//g')
MUTE=$(pactl get-sink-mute @DEFAULT_SINK@ | awk '{print $2}')

# 3. Trigger the Dunst notification with an explicit 1-second timeout (1000 milliseconds)
if [ "$MUTE" == "yes" ]; then
    notify-send -t 1000 -h string:x-canonical-private-synchronous:volume "Muted" -i notification-audio-volume-muted
else
    notify-send -t 1000 -h string:x-canonical-private-synchronous:volume "Volume: $VOLUME" -h int:value:"${VOLUME%\%}" -i notification-audio-volume-high
fi

and in dwm-brightness.sh, this:

#!/bin/bash

# You can use any command, but ```brightnessctl``` is used as an _example_ here

# 1. Change the hardware screen brightness
brightnessctl set "$1"

# 2. Extract the current brightness percentage math
CURRENT=$(brightnessctl get)
MAX=$(brightnessctl max)
PERCENT=$(( 100 * CURRENT / MAX ))

# 3. Trigger a Dunst notification bar that times out in 1 second
notify-send -t 1000 -h string:x-canonical-private-synchronous:brightness "Brightness: ${PERCENT}%" -h int:value:"$PERCENT" -i notification-display-brightness

And adjust config.h accordingly like this:

/* commands */
static const char *volup[]   = { "/location/of/script/dwm-volume.sh", "+5%",  NULL };
static const char *voldown[] = { "/location/of/script/dwm-volume.sh", "-5%",  NULL };
static const char *volmute[] = { "/home/location/of/script/dwm-volume.sh", "mute", NULL };

static const char *brightup[]   = { "/location/of/dwm-brightness.sh", "+10%", NULL };
static const char *brightdown[] = { "/location/of/dwm-brightness.sh", "10%-", NULL };
static Key keys[] = {
    { 0, XF86XK_MonBrightnessUp,   spawn, {.v = brightup } },
    { 0, XF86XK_MonBrightnessDown, spawn, {.v = brightdown } },
    { 0, XF86XK_Back,              spawn, {.v = browserback } },
    { 0, XF86XK_Forward,           spawn, {.v = browserforward } },
    { 0, XF86XK_Refresh,           spawn, {.v = browserreload } },

}

Adjust -t 1000 To the time in milliseconds you want the notification to last.

Thats it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment