Skip to content

Instantly share code, notes, and snippets.

@palopezv
Forked from neuro-sys/dwmconfig.h
Last active November 20, 2024 02:06
Show Gist options
  • Save palopezv/efd34059af6126ad970940bcc6a90f2e to your computer and use it in GitHub Desktop.
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} },
};
@AidenThing
Copy link

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
Copy link

czardien commented Nov 7, 2023

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
Copy link
Author

palopezv commented Nov 7, 2023

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

@palopezv
Copy link
Author

palopezv commented Nov 7, 2023

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

@mwyvr
Copy link

mwyvr commented Jan 29, 2024

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

Awesome! Thank you so much.

@raresgoidescu
Copy link

raresgoidescu commented Sep 12, 2024

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.

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