diff --git a/common/include/common/proctitle.h b/common/include/common/proctitle.h new file mode 100644 index 00000000..8fe0b9d0 --- /dev/null +++ b/common/include/common/proctitle.h @@ -0,0 +1,26 @@ +/** + * Looking Glass + * Copyright © 2017-2026 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program 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 General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#pragma once +#include + +void lgInitProcessTitle(int argc, char **pargv[]); +bool lgCanSetProcessTitle(void); +void lgSetProcessTitle(const char * title); diff --git a/common/src/platform/linux/CMakeLists.txt b/common/src/platform/linux/CMakeLists.txt index e723ea58..82b91adf 100644 --- a/common/src/platform/linux/CMakeLists.txt +++ b/common/src/platform/linux/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(lg_common_platform_code STATIC paths.c open.c cpuinfo.c + proctitle.c ) if(ENABLE_BACKTRACE) diff --git a/common/src/platform/linux/proctitle.c b/common/src/platform/linux/proctitle.c new file mode 100644 index 00000000..73773f86 --- /dev/null +++ b/common/src/platform/linux/proctitle.c @@ -0,0 +1,132 @@ +/** + * Looking Glass + * Copyright © 2017-2026 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program 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 General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "common/debug.h" +#include "common/proctitle.h" + +#include +#include + +static bool initialized; +static char * buffer; +static size_t totalSize = 0; + +static char ** duplicateArray(char ** array, int count) +{ + size_t size = (count + 1) * sizeof(char *); + char ** new = malloc(size); + if (!new) + return false; + + memset(new, 0, size); + for (int i = 0; i < count; ++i) + { + new[i] = strdup(array[i]); + if (!new[i]) + goto fail; + } + + new[count] = NULL; + return new; + +fail: + for (int i = 0; i < count; ++i) + if (new[i]) + free(new[i]); + + return NULL; +} + +static int extendContiguous(char ** end, char ** array) +{ + int i; + for (i = 0; array[i]; ++i) + if (array[i] == *end) + *end += strlen(array[i]) + 1; + + return i; +} + +void lgInitProcessTitle(int argc, char ***pargv) +{ + if (initialized) + return; + + char **argv = *pargv; + // it is technically possible to have no arguments, but then no titles for you + if (!argv[0]) + { + initialized = true; + return; + } + + char * start = argv[0]; + char * end = start; + extendContiguous(&end, argv); + + char ** newArgv = duplicateArray(argv, argc); + if (!newArgv) + { + initialized = true; + return; + } + *pargv = argv; + + char * envEnd = end; + int vars = extendContiguous(&envEnd, environ); + + if (envEnd != end) + { + char ** newEnv = duplicateArray(environ, vars); + if (newEnv) + { + environ = newEnv; + end = envEnd; + } + } + + initialized = true; + buffer = start; + totalSize = end - start; +} + +bool lgCanSetProcessTitle(void) +{ + return initialized && totalSize > 0; +} + +void lgSetProcessTitle(const char * title) +{ + if (!initialized) + { + DEBUG_ERROR("Attempt to use lgSetProcessTitle without lgInitProcessTitle"); + return; + } + + if (!totalSize) + return; + + size_t effective = strlen(title) + 1; + if (effective > totalSize) + effective = totalSize; + + memcpy(buffer, title, effective - 1); + buffer[effective - 1] = '\0'; +} diff --git a/common/src/platform/windows/CMakeLists.txt b/common/src/platform/windows/CMakeLists.txt index 5602a072..0f25eb81 100644 --- a/common/src/platform/windows/CMakeLists.txt +++ b/common/src/platform/windows/CMakeLists.txt @@ -40,6 +40,7 @@ add_library(lg_common_platform_code STATIC time.c cpuinfo.c display.c + proctitle.c ) target_link_libraries(lg_common_platform_code diff --git a/common/src/platform/windows/proctitle.c b/common/src/platform/windows/proctitle.c new file mode 100644 index 00000000..b0e8e8e1 --- /dev/null +++ b/common/src/platform/windows/proctitle.c @@ -0,0 +1,36 @@ +/** + * Looking Glass + * Copyright © 2017-2026 The Looking Glass Authors + * https://looking-glass.io + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program 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 General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., 59 + * Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "common/proctitle.h" + +void lgInitProcessTitle(int argc, char **pargv[]) +{ + // not implemented on Windows +} + +bool lgCanSetProcessTitle(void) +{ + return false; +} + +void lgSetProcessTitle(const char * title) +{ + // not implemented on Windows +}