Files
wmaker/WINGs/wapplication.c
T

217 lines
4.8 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
#include <unistd.h>
2010-04-16 18:12:25 +02:00
#include <X11/Xlocale.h>
1998-09-29 22:36:29 +00:00
#include "WINGsP.h"
2001-07-23 20:31:32 +00:00
#include "wconfig.h"
#include "userdefaults.h"
2001-07-23 20:31:32 +00:00
1998-09-29 22:36:29 +00:00
struct W_Application WMApplication;
const char *_WINGS_progname = NULL;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
Bool W_ApplicationInitialized(void)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
return _WINGS_progname != NULL;
1998-09-29 22:36:29 +00:00
}
void WMInitializeApplication(const char *applicationName, int *argc, char **argv)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
int i;
2001-07-23 20:31:32 +00:00
2009-08-20 00:59:40 +02:00
assert(argc != NULL);
assert(argv != NULL);
assert(applicationName != NULL);
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
setlocale(LC_ALL, "");
2001-07-23 20:31:32 +00:00
#ifdef I18N
2009-08-20 00:59:40 +02:00
if (getenv("NLSPATH"))
bindtextdomain("WINGs", getenv("NLSPATH"));
else
bindtextdomain("WINGs", LOCALEDIR);
bind_textdomain_codeset("WINGs", "UTF-8");
2001-07-23 20:31:32 +00:00
#endif
2009-08-20 00:59:40 +02:00
_WINGS_progname = argv[0];
2009-08-20 00:59:40 +02:00
WMApplication.applicationName = wstrdup(applicationName);
WMApplication.argc = *argc;
1998-09-29 22:36:29 +00:00
2009-08-20 00:59:40 +02:00
WMApplication.argv = wmalloc((*argc + 1) * sizeof(char *));
for (i = 0; i < *argc; i++) {
WMApplication.argv[i] = wstrdup(argv[i]);
}
WMApplication.argv[i] = NULL;
2009-08-20 00:59:40 +02:00
/* initialize notification center */
W_InitNotificationCenter();
1998-09-29 22:36:29 +00:00
}
void WMReleaseApplication(void) {
int i;
/*
* We save the configuration on exit, this used to be handled
* through an 'atexit' registered function but if application
* properly calls WMReleaseApplication then the info to that
* will have been freed by us.
*/
w_save_defaults_changes();
W_ReleaseNotificationCenter();
if (WMApplication.applicationName) {
wfree(WMApplication.applicationName);
WMApplication.applicationName = NULL;
}
if (WMApplication.argv) {
for (i = 0; i < WMApplication.argc; i++)
wfree(WMApplication.argv[i]);
wfree(WMApplication.argv);
WMApplication.argv = NULL;
}
}
void WMSetResourcePath(const char *path)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
if (WMApplication.resourcePath)
wfree(WMApplication.resourcePath);
WMApplication.resourcePath = wstrdup(path);
1998-09-29 22:36:29 +00:00
}
char *WMGetApplicationName(void)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
return WMApplication.applicationName;
1998-09-29 22:36:29 +00:00
}
static char *checkFile(const char *path, const char *folder, const char *ext, const char *resource)
1998-09-29 22:36:29 +00:00
{
2009-08-20 00:59:40 +02:00
char *ret;
int extralen;
2010-09-28 22:25:44 +02:00
size_t slen;
2009-08-20 00:59:40 +02:00
if (!path || !resource)
return NULL;
2010-09-28 22:25:44 +02:00
extralen = (ext ? strlen(ext) + 1 : 0) + (folder ? strlen(folder) + 1 : 0) + 1;
slen = strlen(path) + strlen(resource) + 1 + extralen;
ret = wmalloc(slen);
2009-08-20 00:59:40 +02:00
2010-09-29 03:16:55 +02:00
if (wstrlcpy(ret, path, slen) >= slen)
2010-09-28 22:25:44 +02:00
goto error;
if (folder &&
(wstrlcat(ret, "/", slen) >= slen ||
wstrlcat(ret, folder, slen) >= slen))
goto error;
if (ext &&
(wstrlcat(ret, "/", slen) >= slen ||
wstrlcat(ret, ext, slen) >= slen))
goto error;
if (wstrlcat(ret, "/", slen) >= slen ||
wstrlcat(ret, resource, slen) >= slen)
goto error;
if (access(ret, F_OK) != 0)
goto error;
2009-08-20 00:59:40 +02:00
return ret;
2010-09-28 22:25:44 +02:00
error:
if (ret)
wfree(ret);
return NULL;
1998-09-29 22:36:29 +00:00
}
char *WMPathForResourceOfType(const char *resource, const char *ext)
1998-09-29 22:36:29 +00:00
{
const char *gslocapps, *gssysapps, *gsuserapps;
char *path, *appdir;
char buffer[PATH_MAX];
2010-09-23 14:46:09 +02:00
size_t slen;
path = appdir = NULL;
2009-08-20 00:59:40 +02:00
/*
* Paths are searched in this order:
* - resourcePath/ext
2010-09-23 14:46:09 +02:00
* - dirname(argv[0])/ext
* - WMAKER_USER_ROOT/Applications/ApplicationName.app/ext
* - GNUSTEP_USER_APPS/ApplicationName.app/ext
* - GNUSTEP_LOCAL_APPS/ApplicationName.app/ext
* - /usr/local/lib/GNUstep/Applications/ApplicationName.app/ext
* - GNUSTEP_SYSTEM_APPS/ApplicationName.app/ext
* - /usr/lib/GNUstep/Applications/ApplicationName.app/ext
2009-08-20 00:59:40 +02:00
*/
if (WMApplication.resourcePath) {
path = checkFile(WMApplication.resourcePath, NULL, ext, resource);
if (path)
2010-09-23 14:46:09 +02:00
goto out;
2009-08-20 00:59:40 +02:00
}
if (WMApplication.argv[0]) {
char *ptr_slash;
ptr_slash = strrchr(WMApplication.argv[0], '/');
if (ptr_slash != NULL) {
char tmp[ptr_slash - WMApplication.argv[0] + 1];
strncpy(tmp, WMApplication.argv[0], sizeof(tmp)-1);
tmp[sizeof(tmp) - 1] = '\0';
2009-08-20 00:59:40 +02:00
path = checkFile(tmp, NULL, ext, resource);
if (path)
goto out;
2009-08-20 00:59:40 +02:00
}
}
snprintf(buffer, sizeof(buffer), "Applications/%s.app", WMApplication.applicationName);
path = checkFile(GETENV("WMAKER_USER_ROOT"), buffer, ext, resource);
2010-09-23 14:46:09 +02:00
if (path)
goto out;
2009-08-20 00:59:40 +02:00
slen = strlen(WMApplication.applicationName) + sizeof("/.app");
appdir = wmalloc(slen);
if (snprintf(appdir, slen, "/%s.app", WMApplication.applicationName) >= slen)
2010-09-23 14:46:09 +02:00
goto out;
2009-08-20 00:59:40 +02:00
gsuserapps = GETENV("GNUSTEP_USER_APPS");
if (!gsuserapps) {
snprintf(buffer, sizeof(buffer), "%s/Applications", wusergnusteppath());
gsuserapps = buffer;
}
path = checkFile(gsuserapps, appdir, ext, resource);
2010-09-23 14:46:09 +02:00
if (path)
goto out;
2009-08-20 00:59:40 +02:00
gslocapps = GETENV("GNUSTEP_LOCAL_APPS");
if (!gslocapps)
gslocapps = "/usr/local/lib/GNUstep/Applications";
path = checkFile(gslocapps, appdir, ext, resource);
2010-09-23 14:46:09 +02:00
if (path)
goto out;
2009-08-20 00:59:40 +02:00
gssysapps = GETENV("GNUSTEP_SYSTEM_APPS");
if (!gssysapps)
gssysapps = "/usr/lib/GNUstep/Applications";
path = checkFile(gssysapps, appdir, ext, resource);
2010-09-23 14:46:09 +02:00
if (path)
goto out;
2009-08-20 00:59:40 +02:00
path = checkFile("/usr/GNUstep/System/Applications", appdir, ext, resource); /* falls through */
2010-09-23 14:46:09 +02:00
out:
if (appdir)
2009-08-20 00:59:40 +02:00
wfree(appdir);
2010-09-23 14:46:09 +02:00
return path;
1998-09-29 22:36:29 +00:00
}