Skip to content

Commit 248bcf6

Browse files
pinotreeslouken
authored andcommitted
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.
1 parent d83503f commit 248bcf6

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

src/core/linux/SDL_fcitx.c

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "SDL_fcitx.h"
2626
#include "../../video/SDL_sysvideo.h"
2727
#include "../../events/SDL_keyboard_c.h"
28+
#include "../../core/unix/SDL_appid.h"
2829
#include "SDL_dbus.h"
2930

3031
#ifdef SDL_VIDEO_DRIVER_X11
@@ -53,32 +54,14 @@ typedef struct FcitxClient
5354

5455
static FcitxClient fcitx_client;
5556

56-
static char *GetAppName(void)
57+
static const char *GetAppName(void)
5758
{
58-
#if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_FREEBSD)
59-
char *spot;
60-
char procfile[1024];
61-
char linkfile[1024];
62-
int linksize;
63-
64-
#ifdef SDL_PLATFORM_LINUX
65-
(void)SDL_snprintf(procfile, sizeof(procfile), "/proc/%d/exe", getpid());
66-
#elif defined(SDL_PLATFORM_FREEBSD)
67-
(void)SDL_snprintf(procfile, sizeof(procfile), "/proc/%d/file", getpid());
68-
#endif
69-
linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
70-
if (linksize > 0) {
71-
linkfile[linksize] = '\0';
72-
spot = SDL_strrchr(linkfile, '/');
73-
if (spot) {
74-
return SDL_strdup(spot + 1);
75-
} else {
76-
return SDL_strdup(linkfile);
77-
}
59+
const char *exe_name = SDL_GetExeName();
60+
if (exe_name) {
61+
return exe_name;
7862
}
79-
#endif // SDL_PLATFORM_LINUX || SDL_PLATFORM_FREEBSD
8063

81-
return SDL_strdup("SDL_App");
64+
return "SDL_App";
8265
}
8366

8467
static size_t Fcitx_GetPreeditString(SDL_DBusContext *dbus,
@@ -281,7 +264,7 @@ static bool FcitxCreateInputContext(SDL_DBusContext *dbus, const char *appname,
281264

282265
static bool FcitxClientCreateIC(FcitxClient *client)
283266
{
284-
char *appname = GetAppName();
267+
const char *appname = GetAppName();
285268
char *ic_path = NULL;
286269
SDL_DBusContext *dbus = client->dbus;
287270

@@ -290,8 +273,6 @@ static bool FcitxClientCreateIC(FcitxClient *client)
290273
ic_path = NULL; // just in case.
291274
}
292275

293-
SDL_free(appname);
294-
295276
if (ic_path) {
296277
SDL_free(client->ic_path);
297278
client->ic_path = SDL_strdup(ic_path);

0 commit comments

Comments
 (0)