Skip to content

dwm msg Subscribe

Mihir Lad edited this page Jul 31, 2020 · 1 revision

The subscribe command for dwm-msg is useful for listening for particular types of changes that may occur. For example, you can subscribe to an event that is triggered by changing tags, and dwm-msg will output a JSON message describing the event every time a tag change occurs. A full list of events that can be subscribed to can be found here.

Example Usage

#!/usr/bin/bash

dwm-msg --ignore-reply subscribe tag_change_event |
  jq '.tag_change_event.new_state.selected'

This example uses dwm-msg subscribe to subscribe to the tag_change_event. The --ignore-reply option disables the initial {"result": "success"} reply message indicating a successful subscription which we don't care about.

The output of dwm-msg is piped into jq which retrieves the value of the selected tagset using the .tag_change_event.new_state.selected filter. The final output printed to the terminal after changing tags 3 times is:

4
5
3

This shows how dwm-msg and jq can be used together to retrieve information from dwm as changes occur which is far more efficient than querying dwm every x milliseconds and then manually checking for changes. More complicated scripts can be made by expanding on the example above.

One way to execute code on events is by piping the above into a while read bash loop.

#!/usr/bin/bash

dwm-msg --ignore-reply subscribe tag_change_event |
  jq --unbuffered '.tag_change_event.new_state.selected' |
  while IFS=$'\n' read -r selected_tags; do
    # Function to update tags on status bar
    update_tags "$selected_tags"
  done

This can be extended to perform other logic when event messages are received.