├── makefile ├── main.c └── README.md /makefile: -------------------------------------------------------------------------------- 1 | media-control-sway: main.c 2 | gcc main.c -o media-control-sway 3 | 4 | install: 5 | mv media-control-sway /usr/bin/media-control-sway 6 | 7 | uninstall: 8 | rm /usr/bin/media-control-sway 9 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char *argv[]){ 6 | char buff[255]; 7 | char prev_buff[255]; 8 | 9 | FILE * raw_players = popen("playerctl metadata -a", "r"); 10 | 11 | int comp; 12 | char *token; 13 | int player_arr_size = 0; 14 | static int player_arr_index = 0; 15 | char prev_token[255]; 16 | char stable_token[255]; 17 | char **players = malloc(sizeof(char*) * player_arr_size); 18 | 19 | while(comp != 0){ 20 | fgets(buff, 255, raw_players); 21 | comp = strcmp(prev_buff, buff); 22 | strcpy(prev_buff, buff); 23 | strcpy(prev_token, stable_token); 24 | token = strtok(buff, " "); 25 | strcpy(stable_token, token); 26 | 27 | if(strcmp(prev_token, stable_token) != 0){ 28 | player_arr_size++; 29 | players = realloc(players, sizeof(char*) * player_arr_size); 30 | players[player_arr_index] = malloc(sizeof(char*) * 255); 31 | strcpy(players[player_arr_index], token); 32 | player_arr_index++; 33 | } 34 | } 35 | fclose(raw_players); 36 | 37 | FILE * focused_window = popen("swaymsg -t get_tree | jq '.. | select(.type?) | select(.focused==true).app_id' | cut -b 2- | rev | cut -b 2- | rev", "r"); 38 | fgets(buff, 255, focused_window); 39 | 40 | buff[strlen(buff) - 1] = '\0'; 41 | char selected[255]; 42 | strcpy(selected, "cmus"); 43 | 44 | if(strcmp(buff, "mpv") == 0){ 45 | strcpy(selected, buff); 46 | } 47 | else if(strcmp(buff, "firefox") == 0){ 48 | for(int i = 0; i < player_arr_size; i++){ 49 | if(strcmp(players[i], "firefox") == 0){ 50 | strcpy(selected, buff); 51 | } 52 | } 53 | } 54 | else if(strcmp(buff, "spotify") == 0){ 55 | strcpy(selected, buff); 56 | } 57 | 58 | char command[255]; 59 | 60 | strcpy(command, "playerctl "); 61 | strcat(command, argv[1]); 62 | strcat(command, " -p "); 63 | 64 | system(strcat(command, selected)); 65 | 66 | for(int i = 0; i < player_arr_size; i++){ 67 | free(&players[i]); 68 | } 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # media-control-sway 2 | This program really only works with sway 3 | 4 | What `media-control-sway` does is it polls `playerctl` and finds the current "players". It then checks what the focused window is and if the focused window is a "player", it will call `playerctl` on that window. If the focused window is not listed as a "player", it will call `playerctl` on `cmus` by default. 5 | 6 | This was just a script I wrote for myself. It has been tested with: 7 | - `mpv` 8 | - `cmus` 9 | - `firefox` 10 | 11 | It'll probably work with `spotify`, but I don't use it and haven't tested it. 12 | 13 | This project was thrown together quickly, so I expect that there's a lot of area to improve. If you have any suggestions/changed or have packaged this for a distro, just let me know. I'd love to help! 14 | 15 | ## Installing 16 | ``` 17 | git clone https://github.com/JeromeSiljanUTA/media-control-sway 18 | cd media-control-sway 19 | make && sudo make install 20 | ``` 21 | 22 | ### Add these lines to your sway config: 23 | ``` 24 | bindsym --locked XF86AudioPlay exec media-control-sway play-pause 25 | bindsym --locked XF86AudioNext exec media-control-sway next 26 | bindsym --locked XF86AudioPrev exec media-control-sway previous 27 | ``` 28 | 29 | ### Dependencies: 30 | - `sway` 31 | - `jq` 32 | - `playerctl` 33 | - `make` 34 | 35 | ## Uninstalling 36 | `sudo make uninstall` 37 | 38 | ## Usage 39 | `media-control-sway` is really just a glorified shell script wrapper for `playerctl` 40 | 41 | It passes whatever argument you give it to `playerctl`, but specifies the player. 42 | 43 | ### Supported Arguments (taken from [Playerctl's README](https://github.com/altdesktop/playerctl)) 44 | |Command|Description| 45 | |-|-| 46 | | **`play`** | Command the player to play. | 47 | | **`pause`** | Command the player to pause | 48 | | **`play-pause`** | Command the player to toggle between play/pause. | 49 | | **`stop`** | Command the player to stop. | 50 | | **`next`** | Command the player to skip to the next track. | 51 | | **`previous`** | Command the player to skip to the previous track. | 52 | | **`position [OFFSET][+/-]`** | Command the player to go to the position or seek forward or backward OFFSET in seconds. | 53 | | **`volume [LEVEL][+/-]`** | Print or set the volume to LEVEL from 0.0 to 1.0. | 54 | | **`status`** | Get the play status of the player. Either "Playing", "Paused", or "Stopped". | 55 | | **`metadata [KEY...]`** | Print the metadata for the current track. If KEY is passed, print only those values from the metadata. | 56 | | **`open [URI]`** | Command for the player to open a given URI. Can be either a file path or a remote URL. | 57 | | **`loop [STATUS]`** | Print or set the loop status. Either "None", "Track", or "Playlist". | 58 | | **`shuffle [STATUS]`** | Print or set the shuffle status. Either "On", "Off". | 59 | 60 | ## To Do 61 | - [ ] Package media-control-sway for AUR 62 | - [ ] Make e-build for media-control-sway 63 | - [ ] Use `wlr-foreign-toplevel` protocol 64 | - [ ] Add config file functionality that allows changes to default player 65 | 66 | --------------------------------------------------------------------------------