mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-04 15:19:11 +00:00
Expose sc_get_env() to read environment variable
Contrary to getenv(), sc_get_env() returns an allocated string that is guaranteed to be encoded in UTF-8 on all platforms (it uses _wgetenv() internally on Windows and converts the strings). PR #5560 <https://github.com/Genymobile/scrcpy/pull/5560>
This commit is contained in:
parent
0fd7534bd5
commit
131372d2c4
5 changed files with 52 additions and 34 deletions
|
@ -46,6 +46,7 @@ src = [
|
||||||
'src/util/acksync.c',
|
'src/util/acksync.c',
|
||||||
'src/util/audiobuf.c',
|
'src/util/audiobuf.c',
|
||||||
'src/util/average.c',
|
'src/util/average.c',
|
||||||
|
'src/util/env.c',
|
||||||
'src/util/file.c',
|
'src/util/file.c',
|
||||||
'src/util/intmap.c',
|
'src/util/intmap.c',
|
||||||
'src/util/intr.c',
|
'src/util/intr.c',
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "compat.h"
|
#include "compat.h"
|
||||||
|
#include "util/env.h"
|
||||||
#include "util/file.h"
|
#include "util/file.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/str.h"
|
#include "util/str.h"
|
||||||
|
@ -19,35 +20,22 @@
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
get_icon_path(void) {
|
get_icon_path(void) {
|
||||||
#ifdef __WINDOWS__
|
char *icon_path = sc_get_env("SCRCPY_ICON_PATH");
|
||||||
const wchar_t *icon_path_env = _wgetenv(L"SCRCPY_ICON_PATH");
|
if (icon_path) {
|
||||||
#else
|
|
||||||
const char *icon_path_env = getenv("SCRCPY_ICON_PATH");
|
|
||||||
#endif
|
|
||||||
if (icon_path_env) {
|
|
||||||
// if the envvar is set, use it
|
// if the envvar is set, use it
|
||||||
#ifdef __WINDOWS__
|
|
||||||
char *icon_path = sc_str_from_wchars(icon_path_env);
|
|
||||||
#else
|
|
||||||
char *icon_path = strdup(icon_path_env);
|
|
||||||
#endif
|
|
||||||
if (!icon_path) {
|
|
||||||
LOG_OOM();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
LOGD("Using SCRCPY_ICON_PATH: %s", icon_path);
|
LOGD("Using SCRCPY_ICON_PATH: %s", icon_path);
|
||||||
return icon_path;
|
return icon_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef PORTABLE
|
#ifndef PORTABLE
|
||||||
LOGD("Using icon: " SCRCPY_DEFAULT_ICON_PATH);
|
LOGD("Using icon: " SCRCPY_DEFAULT_ICON_PATH);
|
||||||
char *icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH);
|
icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH);
|
||||||
if (!icon_path) {
|
if (!icon_path) {
|
||||||
LOG_OOM();
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
char *icon_path = sc_file_get_local_path(SCRCPY_PORTABLE_ICON_FILENAME);
|
icon_path = sc_file_get_local_path(SCRCPY_PORTABLE_ICON_FILENAME);
|
||||||
if (!icon_path) {
|
if (!icon_path) {
|
||||||
LOGE("Could not get icon path");
|
LOGE("Could not get icon path");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include "adb/adb.h"
|
#include "adb/adb.h"
|
||||||
#include "util/binary.h"
|
#include "util/binary.h"
|
||||||
|
#include "util/env.h"
|
||||||
#include "util/file.h"
|
#include "util/file.h"
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
#include "util/net_intr.h"
|
#include "util/net_intr.h"
|
||||||
|
@ -25,35 +26,22 @@
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
get_server_path(void) {
|
get_server_path(void) {
|
||||||
#ifdef __WINDOWS__
|
char *server_path = sc_get_env("SCRCPY_SERVER_PATH");
|
||||||
const wchar_t *server_path_env = _wgetenv(L"SCRCPY_SERVER_PATH");
|
if (server_path) {
|
||||||
#else
|
|
||||||
const char *server_path_env = getenv("SCRCPY_SERVER_PATH");
|
|
||||||
#endif
|
|
||||||
if (server_path_env) {
|
|
||||||
// if the envvar is set, use it
|
// if the envvar is set, use it
|
||||||
#ifdef __WINDOWS__
|
|
||||||
char *server_path = sc_str_from_wchars(server_path_env);
|
|
||||||
#else
|
|
||||||
char *server_path = strdup(server_path_env);
|
|
||||||
#endif
|
|
||||||
if (!server_path) {
|
|
||||||
LOG_OOM();
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
|
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
|
||||||
return server_path;
|
return server_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef PORTABLE
|
#ifndef PORTABLE
|
||||||
LOGD("Using server: " SC_SERVER_PATH_DEFAULT);
|
LOGD("Using server: " SC_SERVER_PATH_DEFAULT);
|
||||||
char *server_path = strdup(SC_SERVER_PATH_DEFAULT);
|
server_path = strdup(SC_SERVER_PATH_DEFAULT);
|
||||||
if (!server_path) {
|
if (!server_path) {
|
||||||
LOG_OOM();
|
LOG_OOM();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
char *server_path = sc_file_get_local_path(SC_SERVER_FILENAME);
|
server_path = sc_file_get_local_path(SC_SERVER_FILENAME);
|
||||||
if (!server_path) {
|
if (!server_path) {
|
||||||
LOGE("Could not get local file path, "
|
LOGE("Could not get local file path, "
|
||||||
"using " SC_SERVER_FILENAME " from current directory");
|
"using " SC_SERVER_FILENAME " from current directory");
|
||||||
|
|
29
app/src/util/env.c
Normal file
29
app/src/util/env.c
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
#include "env.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "util/str.h"
|
||||||
|
|
||||||
|
char *
|
||||||
|
sc_get_env(const char *varname) {
|
||||||
|
#ifdef _WIN32
|
||||||
|
wchar_t *w_varname = sc_str_to_wchars(varname);
|
||||||
|
if (!w_varname) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
const wchar_t *value = _wgetenv(w_varname);
|
||||||
|
free(w_varname);
|
||||||
|
if (!value) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sc_str_from_wchars(value);
|
||||||
|
#else
|
||||||
|
const char *value = getenv(varname);
|
||||||
|
if (!value) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return strdup(value);
|
||||||
|
#endif
|
||||||
|
}
|
12
app/src/util/env.h
Normal file
12
app/src/util/env.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef SC_ENV_H
|
||||||
|
#define SC_ENV_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
// Return the value of the environment variable (may be NULL).
|
||||||
|
//
|
||||||
|
// The returned value must be freed by the caller.
|
||||||
|
char *
|
||||||
|
sc_get_env(const char *varname);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Add table
Add a link
Reference in a new issue