Skip to content

Instantly share code, notes, and snippets.

@palopezv
Forked from neuro-sys/dwmconfig.h
Last active February 19, 2024 19:54
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} },
};
@lytvyn139
Copy link

Thank you ! {for those who doesn't get it, if you added lines 24-27, the hot keys are:

  • MOD+F9 mute
  • MOD+F11 down
  • MOD+F12 up
    worked fine with dwm-bar+pulse.sh
    }

@patrickmwila
Copy link

Thank You

@im-alexandre
Copy link

It looks great. Does it work with dose wheels in key keyboard?

@nong10
Copy link

nong10 commented Oct 4, 2020

It works thanks so much

@bdealbuquerque
Copy link

bdealbuquerque commented Dec 17, 2020

It works perfectly. Thank you very much !

@Hritik14
Copy link

Hritik14 commented Apr 1, 2021

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

pzaszp commented Apr 11, 2021

What are the keys to change vol?

@crackself
Copy link

Thanks, it work on my laptop.

@JavisDohnny
Copy link

KINO

@ProNebo
Copy link

ProNebo commented Oct 7, 2021

Thanks! It helped!

Copy link

ghost commented Dec 9, 2021

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

@ahmedsamyh
Copy link

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

eMarce1 commented Dec 31, 2021

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

@palopezv
Copy link
Author

palopezv commented Jan 1, 2022

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

@palopezv
Copy link
Author

palopezv commented Jan 1, 2022

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

hard-tek commented Jan 5, 2022

Worked like a charm!

Copy link

ghost commented Jan 28, 2022

Amazing! Thanks.

@Arqib
Copy link

Arqib commented Feb 28, 2022

This works! Thanks.

@krindra
Copy link

krindra commented Mar 9, 2022

PERFECT

@GabubuAvailable
Copy link

That helped me. Thank you so much :-D

@DaniOrt3ga
Copy link

DaniOrt3ga commented Jun 16, 2022

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

i used amixer long time ago but thanks :)

@pranshuthegamer
Copy link

how do i install the x11 dev thing on arch?

@moodfew
Copy link

moodfew commented Oct 3, 2022

GOD BLESS YOU DUDE!

@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.

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