src/core/linux/SDL_dbus.c
changeset 12456 f8041c025fd3
parent 12437 ab67e41946fe
child 12503 806492103856
equal deleted inserted replaced
12455:bc838d7a29dc 12456:f8041c025fd3
    18      misrepresented as being the original software.
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    20 */
    21 #include "../../SDL_internal.h"
    21 #include "../../SDL_internal.h"
    22 #include "SDL_dbus.h"
    22 #include "SDL_dbus.h"
    23 
       
    24 #if !SDL_THREADS_DISABLED
       
    25 #include <sys/time.h>
       
    26 #include <sys/resource.h>
       
    27 #include <pthread.h>
       
    28 #include "SDL_system.h"
       
    29 #endif
       
    30 
    23 
    31 #if SDL_USE_LIBDBUS
    24 #if SDL_USE_LIBDBUS
    32 /* we never link directly to libdbus. */
    25 /* we never link directly to libdbus. */
    33 #include "SDL_loadso.h"
    26 #include "SDL_loadso.h"
    34 static const char *dbus_library = "libdbus-1.so.3";
    27 static const char *dbus_library = "libdbus-1.so.3";
   347         }
   340         }
   348     }
   341     }
   349 
   342 
   350     return SDL_TRUE;
   343     return SDL_TRUE;
   351 }
   344 }
   352 
       
   353 #if !SDL_THREADS_DISABLED
       
   354 /* d-bus queries to org.freedesktop.RealtimeKit1. */
       
   355 #define RTKIT_DBUS_NODE "org.freedesktop.RealtimeKit1"
       
   356 #define RTKIT_DBUS_PATH "/org/freedesktop/RealtimeKit1"
       
   357 #define RTKIT_DBUS_INTERFACE "org.freedesktop.RealtimeKit1"
       
   358 
       
   359 static pthread_once_t rtkit_initialize_once = PTHREAD_ONCE_INIT;
       
   360 static Sint32 rtkit_min_nice_level = -20;
       
   361 
       
   362 static void
       
   363 rtkit_initialize()
       
   364 {
       
   365     SDL_DBusContext *dbus = SDL_DBus_GetContext();
       
   366 
       
   367     /* Try getting minimum nice level: this is often greater than PRIO_MIN (-20). */
       
   368     if (!dbus || !SDL_DBus_QueryPropertyOnConnection(dbus->system_conn, RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MinNiceLevel",
       
   369                                             DBUS_TYPE_INT32, &rtkit_min_nice_level)) {
       
   370         rtkit_min_nice_level = -20;
       
   371     }
       
   372 }
       
   373 
       
   374 static SDL_bool
       
   375 rtkit_setpriority(pid_t thread, int nice_level)
       
   376 {
       
   377     Uint64 ui64 = (Uint64)thread;
       
   378     Sint32 si32 = (Sint32)nice_level;
       
   379     SDL_DBusContext *dbus = SDL_DBus_GetContext();
       
   380 
       
   381     pthread_once(&rtkit_initialize_once, rtkit_initialize);
       
   382 
       
   383     if (si32 < rtkit_min_nice_level)
       
   384         si32 = rtkit_min_nice_level;
       
   385 
       
   386     if (!dbus || !SDL_DBus_CallMethodOnConnection(dbus->system_conn,
       
   387             RTKIT_DBUS_NODE, RTKIT_DBUS_PATH, RTKIT_DBUS_INTERFACE, "MakeThreadHighPriority",
       
   388             DBUS_TYPE_UINT64, &ui64, DBUS_TYPE_INT32, &si32, DBUS_TYPE_INVALID,
       
   389             DBUS_TYPE_INVALID)) {
       
   390         return SDL_FALSE;
       
   391     }
       
   392     return SDL_TRUE;
       
   393 }
       
   394 #endif
   345 #endif
   395 
   346 
   396 #endif
       
   397 
       
   398 /* this is a public symbol, so it has to exist even if threads are disabled. */
       
   399 int
       
   400 SDL_LinuxSetThreadPriority(Sint64 threadID, int priority)
       
   401 {
       
   402 #if SDL_THREADS_DISABLED
       
   403     return SDL_Unsupported();
       
   404 #else
       
   405     if (setpriority(PRIO_PROCESS, (id_t)threadID, priority) == 0) {
       
   406         return 0;
       
   407     }
       
   408 
       
   409 #if SDL_USE_LIBDBUS
       
   410     /* Note that this fails if you're trying to set high priority
       
   411        and you don't have root permission. BUT DON'T RUN AS ROOT!
       
   412 
       
   413        You can grant the ability to increase thread priority by
       
   414        running the following command on your application binary:
       
   415           sudo setcap 'cap_sys_nice=eip' <application>
       
   416 
       
   417        Let's try setting priority with RealtimeKit...
       
   418 
       
   419        README and sample code at: http://git.0pointer.net/rtkit.git
       
   420     */
       
   421     if (rtkit_setpriority((pid_t)threadID, priority)) {
       
   422         return 0;
       
   423     }
       
   424 #endif
       
   425 
       
   426     return SDL_SetError("setpriority() failed");
       
   427 #endif
       
   428 }
       
   429 
       
   430 /* vi: set ts=4 sw=4 expandtab: */
   347 /* vi: set ts=4 sw=4 expandtab: */