ime: fcitx: use SDL_GetExeName() in GetAppName()

Use the existing SDL_GetExeName(), available for all the UNIX
platforms, in the internal GetAppName(); this has few advantanges:
- SDL_GetExeName() (and SDL_GetAppID() that builds on top of it) are
  used in various places already; since it caches the executable name,
  this may remove one extra read of the application name
- SDL_GetExeName() has a non-dummy implementation in more OSes than
  GetAppName(), thus providing a small improvement for this IME

As drive-by change: since SDL_GetExeName() provides a constant string,
there is no more need to allocate a new string in GetAppName(), which
is used as constant string anyway. Hence, return a constant string in
GetAppName() too.
This commit is contained in:
Pino Toscano
2025-08-07 07:40:29 +02:00
committed by Sam Lantinga
parent d83503f80e
commit 248bcf6b29

View File

@@ -25,6 +25,7 @@
#include "SDL_fcitx.h" #include "SDL_fcitx.h"
#include "../../video/SDL_sysvideo.h" #include "../../video/SDL_sysvideo.h"
#include "../../events/SDL_keyboard_c.h" #include "../../events/SDL_keyboard_c.h"
#include "../../core/unix/SDL_appid.h"
#include "SDL_dbus.h" #include "SDL_dbus.h"
#ifdef SDL_VIDEO_DRIVER_X11 #ifdef SDL_VIDEO_DRIVER_X11
@@ -53,32 +54,14 @@ typedef struct FcitxClient
static FcitxClient fcitx_client; static FcitxClient fcitx_client;
static char *GetAppName(void) static const char *GetAppName(void)
{ {
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_FREEBSD) const char *exe_name = SDL_GetExeName();
char *spot; if (exe_name) {
char procfile[1024]; return exe_name;
char linkfile[1024];
int linksize;
#ifdef SDL_PLATFORM_LINUX
(void)SDL_snprintf(procfile, sizeof(procfile), "/proc/%d/exe", getpid());
#elif defined(SDL_PLATFORM_FREEBSD)
(void)SDL_snprintf(procfile, sizeof(procfile), "/proc/%d/file", getpid());
#endif
linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
if (linksize > 0) {
linkfile[linksize] = '\0';
spot = SDL_strrchr(linkfile, '/');
if (spot) {
return SDL_strdup(spot + 1);
} else {
return SDL_strdup(linkfile);
}
} }
#endif // SDL_PLATFORM_LINUX || SDL_PLATFORM_FREEBSD
return SDL_strdup("SDL_App"); return "SDL_App";
} }
static size_t Fcitx_GetPreeditString(SDL_DBusContext *dbus, static size_t Fcitx_GetPreeditString(SDL_DBusContext *dbus,
@@ -281,7 +264,7 @@ static bool FcitxCreateInputContext(SDL_DBusContext *dbus, const char *appname,
static bool FcitxClientCreateIC(FcitxClient *client) static bool FcitxClientCreateIC(FcitxClient *client)
{ {
char *appname = GetAppName(); const char *appname = GetAppName();
char *ic_path = NULL; char *ic_path = NULL;
SDL_DBusContext *dbus = client->dbus; SDL_DBusContext *dbus = client->dbus;
@@ -290,8 +273,6 @@ static bool FcitxClientCreateIC(FcitxClient *client)
ic_path = NULL; // just in case. ic_path = NULL; // just in case.
} }
SDL_free(appname);
if (ic_path) { if (ic_path) {
SDL_free(client->ic_path); SDL_free(client->ic_path);
client->ic_path = SDL_strdup(ic_path); client->ic_path = SDL_strdup(ic_path);