diff --git a/src/posix_dbus.c b/src/posix_dbus.c index 83bdf993..87355b3c 100644 --- a/src/posix_dbus.c +++ b/src/posix_dbus.c @@ -125,37 +125,13 @@ void _glfwInitDBusPOSIX(void) _glfw.dbus.connection = NULL; } } + + _glfwCacheDesktopFilePathPOSIX(); } -void _glfwTerminateDBusPOSIX(void) +void _glfwCacheDesktopFilePathPOSIX(void) { - if (_glfw.dbus.connection) - { - dbus_connection_unref(_glfw.dbus.connection); - _glfw.dbus.connection = NULL; - } - - if (_glfw.dbus.handle) - { - _glfwPlatformFreeModule(_glfw.dbus.handle); - _glfw.dbus.handle = NULL; - } -} - -void _glfwUpdateTaskbarProgressDBusPOSIX(dbus_bool_t progressVisible, double progressValue) -{ - if(!_glfw.dbus.handle || !_glfw.dbus.connection) - { - _glfwInputError(GLFW_FEATURE_UNAVAILABLE, "POSIX: No DBus connection open to set taskbar progress"); - return; - } - - //Signal signature: - //signal com.canonical.Unity.LauncherEntry.Update (in s app_uri, in a{sv} properties) - - struct DBusMessageIter args; - memset(&args, 0, sizeof(args)); - + //Cache path of .desktop file //Get name of the running executable char exeName[PATH_MAX]; memset(exeName, 0, sizeof(char) * PATH_MAX); @@ -175,12 +151,50 @@ void _glfwUpdateTaskbarProgressDBusPOSIX(dbus_bool_t progressVisible, double pro //Create our final desktop file uri unsigned int desktopFileLength = strlen("application://") + exeNameLength + strlen(".desktop") + 1; - char desktopFile[desktopFileLength]; - memset(desktopFile, 0, sizeof(char) * desktopFileLength); - strcpy(desktopFile, "application://"); - memcpy(desktopFile + strlen("application://"), lastFound + 1, exeNameLength); - strcpy(desktopFile + strlen("application://") + (exeNameLength), ".desktop"); - desktopFile[desktopFileLength - 1] = '\0'; + _glfw.dbus.desktopFilePath = _glfw_calloc(desktopFileLength, sizeof(char)); + if(!_glfw.dbus.desktopFilePath) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, "Failed to allocate memory for .desktop file path"); + return; + } + else + { + memset(_glfw.dbus.desktopFilePath, 0, sizeof(char) * desktopFileLength); + strcpy(_glfw.dbus.desktopFilePath, "application://"); + memcpy(_glfw.dbus.desktopFilePath + strlen("application://"), lastFound + 1, exeNameLength); + strcpy(_glfw.dbus.desktopFilePath + strlen("application://") + (exeNameLength), ".desktop"); + _glfw.dbus.desktopFilePath[desktopFileLength - 1] = '\0'; + } +} + +void _glfwTerminateDBusPOSIX(void) +{ + if(_glfw.dbus.desktopFilePath) + _glfw_free(_glfw.dbus.desktopFilePath); + + if (_glfw.dbus.connection) + { + dbus_connection_unref(_glfw.dbus.connection); + _glfw.dbus.connection = NULL; + } + + if (_glfw.dbus.handle) + { + _glfwPlatformFreeModule(_glfw.dbus.handle); + _glfw.dbus.handle = NULL; + } +} + +void _glfwUpdateTaskbarProgressDBusPOSIX(dbus_bool_t progressVisible, double progressValue) +{ + if(!_glfw.dbus.handle || !_glfw.dbus.connection || !_glfw.dbus.desktopFilePath) + return; + + //Signal signature: + //signal com.canonical.Unity.LauncherEntry.Update (in s app_uri, in a{sv} properties) + + struct DBusMessageIter args; + memset(&args, 0, sizeof(args)); DBusMessage* msg = dbus_message_new_signal("/org/glfw", "com.canonical.Unity.LauncherEntry", "Update"); if(!msg) @@ -192,8 +206,7 @@ void _glfwUpdateTaskbarProgressDBusPOSIX(dbus_bool_t progressVisible, double pro dbus_message_iter_init_append(msg, &args); //Setup app_uri parameter - const char* desktopFileStr = desktopFile; - dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &desktopFileStr); + dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &_glfw.dbus.desktopFilePath); //Set properties parameter struct DBusMessageIter sub1, sub2, sub3; @@ -236,7 +249,7 @@ void _glfwUpdateTaskbarProgressDBusPOSIX(dbus_bool_t progressVisible, double pro void _glfwUpdateBadgeDBusPOSIX(dbus_bool_t badgeVisible, int badgeCount) { - if(!_glfw.dbus.handle || !_glfw.dbus.connection) + if(!_glfw.dbus.handle || !_glfw.dbus.connection || !_glfw.dbus.desktopFilePath) return; long long badgeCountLL = badgeCount; @@ -247,32 +260,6 @@ void _glfwUpdateBadgeDBusPOSIX(dbus_bool_t badgeVisible, int badgeCount) struct DBusMessageIter args; memset(&args, 0, sizeof(args)); - //Get name of the running executable - char exeName[PATH_MAX]; - memset(exeName, 0, sizeof(char) * PATH_MAX); - if(readlink("/proc/self/exe", exeName, PATH_MAX) == -1) - { - _glfwInputError(GLFW_PLATFORM_ERROR, "Failed to get name of the running executable"); - return; - } - char* exeNameEnd = strchr(exeName, '\0'); - char* lastFound = strrchr(exeName, '/'); - if(!lastFound || !exeNameEnd) - { - _glfwInputError(GLFW_PLATFORM_ERROR, "Failed to get name of the running executable"); - return; - } - unsigned int exeNameLength = (exeNameEnd - lastFound) - 1; - - //Create our final desktop file uri - unsigned int desktopFileLength = strlen("application://") + exeNameLength + strlen(".desktop") + 1; - char desktopFile[desktopFileLength]; - memset(desktopFile, 0, sizeof(char) * desktopFileLength); - strcpy(desktopFile, "application://"); - memcpy(desktopFile + strlen("application://"), lastFound + 1, exeNameLength); - strcpy(desktopFile + strlen("application://") + (exeNameLength), ".desktop"); - desktopFile[desktopFileLength - 1] = '\0'; - DBusMessage* msg = dbus_message_new_signal("/org/glfw", "com.canonical.Unity.LauncherEntry", "Update"); if(!msg) { @@ -283,8 +270,7 @@ void _glfwUpdateBadgeDBusPOSIX(dbus_bool_t badgeVisible, int badgeCount) dbus_message_iter_init_append(msg, &args); //Setup app_uri parameter - const char* desktopFileStr = desktopFile; - dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &desktopFileStr); + dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &_glfw.dbus.desktopFilePath); //Set properties parameter struct DBusMessageIter sub1, sub2, sub3; diff --git a/src/posix_dbus.h b/src/posix_dbus.h index ad86995b..305a9cba 100644 --- a/src/posix_dbus.h +++ b/src/posix_dbus.h @@ -125,9 +125,12 @@ typedef struct _GLFWDBusPOSIX DBusConnection* connection; struct DBusError error; + + char* desktopFilePath; } _GLFWDBusPOSIX; void _glfwInitDBusPOSIX(void); +void _glfwCacheDesktopFilePathPOSIX(void); void _glfwTerminateDBusPOSIX(void); void _glfwUpdateTaskbarProgressDBusPOSIX(dbus_bool_t progressVisible, double progressValue); void _glfwUpdateBadgeDBusPOSIX(dbus_bool_t badgeVisible, int badgeCount);