app-manager optimizations
- simplify lookup by pid - less calls to wnck_window_get_pid - use a gint for the pid
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2010 Mike Massonnet, <mmassonnet@xfce.org>
|
* Copyright (c) 2010 Mike Massonnet, <mmassonnet@xfce.org>
|
||||||
|
* Copyright (c) 2018 Rozhuk Ivan <rozhuk.im@gmail.com>
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify
|
* This program is free software; you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
@@ -11,6 +12,7 @@
|
|||||||
#include <config.h>
|
#include <config.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <glib-object.h>
|
#include <glib-object.h>
|
||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
#define WNCK_I_KNOW_THIS_IS_UNSTABLE
|
#define WNCK_I_KNOW_THIS_IS_UNSTABLE
|
||||||
@@ -36,7 +38,10 @@ G_DEFINE_TYPE (XtmAppManager, xtm_app_manager, G_TYPE_OBJECT)
|
|||||||
|
|
||||||
static void xtm_app_manager_finalize (GObject *object);
|
static void xtm_app_manager_finalize (GObject *object);
|
||||||
|
|
||||||
static void apps_add_application (GArray *apps, WnckApplication *application);
|
static gint app_get_pid (WnckApplication *application);
|
||||||
|
static gint app_pid_compare_fn (gconstpointer a, gconstpointer b);
|
||||||
|
|
||||||
|
static void apps_add_application (GArray *apps, WnckApplication *application, gint pid);
|
||||||
static void apps_remove_application (GArray *apps, WnckApplication *application);
|
static void apps_remove_application (GArray *apps, WnckApplication *application);
|
||||||
static App * apps_lookup_pid (GArray *apps, gint pid);
|
static App * apps_lookup_pid (GArray *apps, gint pid);
|
||||||
static void application_opened (WnckScreen *screen, WnckApplication *application, XtmAppManager *manager);
|
static void application_opened (WnckScreen *screen, WnckApplication *application, XtmAppManager *manager);
|
||||||
@@ -67,16 +72,11 @@ xtm_app_manager_init (XtmAppManager *manager)
|
|||||||
for (l = windows; l != NULL; l = l->next)
|
for (l = windows; l != NULL; l = l->next)
|
||||||
{
|
{
|
||||||
WnckWindow *window = WNCK_WINDOW (l->data);
|
WnckWindow *window = WNCK_WINDOW (l->data);
|
||||||
WnckApplication *application = wnck_window_get_application (window);
|
|
||||||
gint pid = wnck_application_get_pid (application);
|
|
||||||
|
|
||||||
if (wnck_window_get_window_type (window) != WNCK_WINDOW_NORMAL)
|
if (wnck_window_get_window_type (window) != WNCK_WINDOW_NORMAL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (apps_lookup_pid (manager->apps, pid) != NULL)
|
WnckApplication *application = wnck_window_get_application (window);
|
||||||
continue;
|
apps_add_application (manager->apps, application, app_get_pid (application));
|
||||||
|
|
||||||
apps_add_application (manager->apps, application);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
G_DEBUG_FMT ("Initial applications: %d", manager->apps->len);
|
G_DEBUG_FMT ("Initial applications: %d", manager->apps->len);
|
||||||
@@ -92,18 +92,27 @@ xtm_app_manager_finalize (GObject *object)
|
|||||||
g_array_free (XTM_APP_MANAGER (object)->apps, TRUE);
|
g_array_free (XTM_APP_MANAGER (object)->apps, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static gint
|
||||||
|
app_get_pid(WnckApplication *application)
|
||||||
|
{
|
||||||
|
if (NULL == application)
|
||||||
|
return (0);
|
||||||
|
gint pid = wnck_application_get_pid (application);
|
||||||
|
if (pid != 0)
|
||||||
|
return (pid);
|
||||||
|
return (wnck_window_get_pid (WNCK_WINDOW (wnck_application_get_windows (application)->data)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static gint
|
||||||
|
app_pid_compare_fn(gconstpointer a, gconstpointer b)
|
||||||
|
{
|
||||||
|
return (((const App*)a)->pid - ((const App*)b)->pid);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
apps_add_application (GArray *apps, WnckApplication *application)
|
apps_add_application (GArray *apps, WnckApplication *application, gint pid)
|
||||||
{
|
{
|
||||||
App app;
|
App app;
|
||||||
gint pid;
|
|
||||||
|
|
||||||
pid = wnck_application_get_pid (application);
|
|
||||||
if (pid == 0)
|
|
||||||
{
|
|
||||||
WnckWindow *window = WNCK_WINDOW (wnck_application_get_windows (application)->data);
|
|
||||||
pid = wnck_window_get_pid (window);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (apps_lookup_pid (apps, pid))
|
if (apps_lookup_pid (apps, pid))
|
||||||
return;
|
return;
|
||||||
@@ -115,56 +124,42 @@ apps_add_application (GArray *apps, WnckApplication *application)
|
|||||||
g_object_ref (app.icon);
|
g_object_ref (app.icon);
|
||||||
|
|
||||||
g_array_append_val (apps, app);
|
g_array_append_val (apps, app);
|
||||||
|
g_array_sort (apps, app_pid_compare_fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
apps_remove_application (GArray *apps, WnckApplication *application)
|
apps_remove_application (GArray *apps, WnckApplication *application)
|
||||||
{
|
{
|
||||||
App *app = NULL;
|
App *app = apps_lookup_pid(apps, app_get_pid (application));
|
||||||
guint i;
|
|
||||||
|
|
||||||
for (i = 0; i < apps->len; i++)
|
if (app == NULL)
|
||||||
{
|
return;
|
||||||
app = &g_array_index (apps, App, i);
|
g_object_unref (app->icon);
|
||||||
if (app->application == application)
|
g_array_remove_index (apps, (guint)(((size_t)app - (size_t)apps->data) / sizeof(App)));
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (app != NULL)
|
|
||||||
{
|
|
||||||
g_object_unref (app->icon);
|
|
||||||
g_array_remove_index (apps, i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static App *
|
static App *
|
||||||
apps_lookup_pid (GArray *apps, gint pid)
|
apps_lookup_pid (GArray *apps, gint pid)
|
||||||
{
|
{
|
||||||
App *app;
|
App tapp;
|
||||||
guint i;
|
|
||||||
|
|
||||||
for (app = NULL, i = 0; i < apps->len; i++)
|
tapp.pid = pid;
|
||||||
{
|
|
||||||
app = &g_array_index (apps, App, i);
|
|
||||||
if (app->pid == (guint)pid)
|
|
||||||
break;
|
|
||||||
app = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return app;
|
return (bsearch(&tapp, apps->data, apps->len, sizeof(App), app_pid_compare_fn));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
application_opened (WnckScreen *screen, WnckApplication *application, XtmAppManager *manager)
|
application_opened (WnckScreen *screen, WnckApplication *application, XtmAppManager *manager)
|
||||||
{
|
{
|
||||||
G_DEBUG_FMT ("Application opened %p %d", application, wnck_application_get_pid (application));
|
gint pid = app_get_pid (application);
|
||||||
apps_add_application (manager->apps, application);
|
G_DEBUG_FMT ("Application opened %p %d", (void*)application, pid);
|
||||||
|
apps_add_application (manager->apps, application, pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
application_closed (WnckScreen *screen, WnckApplication *application, XtmAppManager *manager)
|
application_closed (WnckScreen *screen, WnckApplication *application, XtmAppManager *manager)
|
||||||
{
|
{
|
||||||
G_DEBUG_FMT ("Application closed %p", application);
|
G_DEBUG_FMT ("Application closed %p", (void*)application);
|
||||||
apps_remove_application (manager->apps, application);
|
apps_remove_application (manager->apps, application);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,16 +171,8 @@ xtm_app_manager_new (void)
|
|||||||
return g_object_new (XTM_TYPE_APP_MANAGER, NULL);
|
return g_object_new (XTM_TYPE_APP_MANAGER, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
const GArray *
|
|
||||||
xtm_app_manager_get_app_list (XtmAppManager *manager)
|
|
||||||
{
|
|
||||||
g_return_val_if_fail (XTM_IS_APP_MANAGER (manager), NULL);
|
|
||||||
return manager->apps;
|
|
||||||
}
|
|
||||||
|
|
||||||
App *
|
App *
|
||||||
xtm_app_manager_get_app_from_pid (XtmAppManager *manager, gint pid)
|
xtm_app_manager_get_app_from_pid (XtmAppManager *manager, gint pid)
|
||||||
{
|
{
|
||||||
return apps_lookup_pid (manager->apps, pid);
|
return apps_lookup_pid (manager->apps, pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ typedef struct _App App;
|
|||||||
struct _App
|
struct _App
|
||||||
{
|
{
|
||||||
WnckApplication * application;
|
WnckApplication * application;
|
||||||
guint pid;
|
gint pid;
|
||||||
gchar name[1024];
|
gchar name[1024];
|
||||||
GdkPixbuf * icon;
|
GdkPixbuf * icon;
|
||||||
};
|
};
|
||||||
@@ -39,7 +39,6 @@ typedef struct _XtmAppManager XtmAppManager;
|
|||||||
|
|
||||||
GType xtm_app_manager_get_type (void);
|
GType xtm_app_manager_get_type (void);
|
||||||
XtmAppManager * xtm_app_manager_new (void);
|
XtmAppManager * xtm_app_manager_new (void);
|
||||||
const GArray * xtm_app_manager_get_app_list (XtmAppManager *manager);
|
|
||||||
App * xtm_app_manager_get_app_from_pid (XtmAppManager *manager, gint pid);
|
App * xtm_app_manager_get_app_from_pid (XtmAppManager *manager, gint pid);
|
||||||
|
|
||||||
#endif /* !APP_MANAGER_H */
|
#endif /* !APP_MANAGER_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user