Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for playerctlrc file #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions playerctl/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ playerctl_sources = [
'playerctl-player.c',
'playerctl-common.c',
'playerctl-player-manager.c',
'playerctl-rc.c',
playerctl_generated,
]

Expand Down
14 changes: 12 additions & 2 deletions playerctl/playerctl-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "playerctl-common.h"
#include "playerctl-formatter.h"
#include "playerctl-player-private.h"
#include "playerctl-rc.h"

#define LENGTH(array) (sizeof array / sizeof array[0])

Expand Down Expand Up @@ -815,7 +816,7 @@ static const GOptionEntry entries[] = {
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &command_arg, NULL, "COMMAND"},
{NULL}};

static gboolean parse_setup_options(int argc, char *argv[], GError **error) {
static gboolean parse_setup_options(int argc, char *argv[], char** rc_options, GError **error) {
static const gchar *description =
"Available Commands:"
"\n play Command the player to play"
Expand Down Expand Up @@ -851,6 +852,13 @@ static gboolean parse_setup_options(int argc, char *argv[], GError **error) {
g_option_context_set_description(context, description);
g_option_context_set_summary(context, summary);

success = g_option_context_parse_strv(context, &rc_options, error);

if (!success) {
g_option_context_free(context);
return FALSE;
}

success = g_option_context_parse(context, &argc, &argv, error);

if (!success) {
Expand Down Expand Up @@ -1166,7 +1174,9 @@ int main(int argc, char *argv[]) {
// seems to be required to print unicode (see #8)
setlocale(LC_CTYPE, "");

if (!parse_setup_options(argc, argv, &error)) {
char **rc_options = playerctl_rc_read_options();

if (!parse_setup_options(argc, argv, rc_options, &error)) {
g_printerr("%s\n", error->message);
g_clear_error(&error);
exit(0);
Expand Down
116 changes: 116 additions & 0 deletions playerctl/playerctl-rc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* This file is part of playerctl.
*
* playerctl is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* playerctl is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with playerctl If not, see <http://www.gnu.org/licenses/>.
*
* Copyright © 2014, Tony Crisci and contributors.
*/

#include "playerctl/playerctl-rc.h"

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char **create_empty_options() {
char **options = (char**)malloc(sizeof(char*) * 2);
assert(options != NULL);

// Prepend empty to match argv size
options[0] = strdup("");
assert(options[0] != NULL);

// Null terminated array
options[1] = NULL;

return options;
}

static void trim(char **str) {
// Left trim
for (; **str == '\r' || **str == '\n' || **str == ' '; (*str)++);

// Right trim
for (char *end = *str + strlen(*str) - 1; end >= *str; end--) {
if (*end == '\r' || *end == '\n' || *end == ' ')
*end = '\0';
else
break;
}
}

static char **read_rc_file(const char* path) {
char **options = NULL;
int options_idx = 0;

FILE *rc_file;
if ((rc_file = fopen(path, "r")) != NULL) {
static int options_cap = 64;
options = (char**)malloc(sizeof(char*) * options_cap);
assert(options != NULL);

// Prepend empty to match argv size
options[options_idx] = strdup("");
assert(options[options_idx] != NULL);
options_idx++;

static int buf_size = 1024;
char line[buf_size];
while (fgets(line, buf_size, rc_file) != NULL && options_idx < options_cap - 1) {
char* context = NULL;
char* token = strtok_r(line, " ", &context);
while (token != NULL) {
trim(&token);
if (strlen(token) > 0) {
options[options_idx] = strdup(token);
assert(options[options_idx] != NULL);

if (++options_idx == options_cap - 1)
break;
}
token = strtok_r(NULL, " ", &context);
}
}

// Null terminated array
options[options_idx] = NULL;
}

return options;
}

char **playerctl_rc_read_options() {
char **options = NULL;
char *homedir = getenv("HOME");

if (homedir != NULL) {
static char rc_file_name[] = "/.playerctlrc";

char path[strlen(homedir) + strlen(rc_file_name) + 1];
sprintf(path, "%s%s", homedir, rc_file_name);

options = read_rc_file(path);
}

if (options == NULL) {
options = read_rc_file("/etc/playerctlrc");
}

if (options == NULL) {
options = create_empty_options();
}

return options;
}
25 changes: 25 additions & 0 deletions playerctl/playerctl-rc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This file is part of playerctl.
*
* playerctl is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* playerctl is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with playerctl If not, see <http://www.gnu.org/licenses/>.
*
* Copyright © 2022, Tony Crisci and contributors
*/

#ifndef __PLAYERCTL_RC_H__
#define __PLAYERCTL_RC_H__

char **playerctl_rc_read_options();

#endif /* __PLAYERCTL_RC_H__ */