forked from linuxmint/cinnamon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cinnamon-app.c
1533 lines (1277 loc) · 42.2 KB
/
cinnamon-app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
#include "config.h"
#include <string.h>
#include <glib/gi18n-lib.h>
#include <meta/display.h>
#include "cinnamon-app-private.h"
#include "cinnamon-enum-types.h"
#include "cinnamon-global.h"
#include "cinnamon-util.h"
#include "cinnamon-app-system-private.h"
#include "cinnamon-window-tracker-private.h"
#include "st.h"
typedef enum {
MATCH_NONE,
MATCH_SUBSTRING, /* Not prefix, substring */
MATCH_PREFIX, /* Strict prefix */
} CinnamonAppSearchMatch;
/* This is mainly a memory usage optimization - the user is going to
* be running far fewer of the applications at one time than they have
* installed. But it also just helps keep the code more logically
* separated.
*/
typedef struct {
guint refcount;
/* Last time the user interacted with any of this application's windows */
guint32 last_user_time;
/* Signal connection to dirty window sort list on workspace changes */
guint workspace_switch_id;
GSList *windows;
/* Whether or not we need to resort the windows; this is done on demand */
guint window_sort_stale : 1;
} CinnamonAppRunningState;
/**
* SECTION:cinnamon-app
* @short_description: Object representing an application
*
* This object wraps a #GMenuTreeEntry, providing methods and signals
* primarily useful for running applications.
*/
struct _CinnamonApp
{
GObject parent;
int started_on_workspace;
CinnamonAppState state;
GMenuTreeEntry *entry; /* If NULL, this app is backed by one or more
* MetaWindow. For purposes of app title
* etc., we use the first window added,
* because it's most likely to be what we
* want (e.g. it will be of TYPE_NORMAL from
* the way cinnamon-window-tracker.c works).
*/
CinnamonAppRunningState *running_state;
char *window_id_string;
char *casefolded_name;
char *name_collation_key;
char *casefolded_description;
char *casefolded_exec;
char *keywords;
};
G_DEFINE_TYPE (CinnamonApp, cinnamon_app, G_TYPE_OBJECT);
enum {
PROP_0,
PROP_STATE
};
enum {
WINDOWS_CHANGED,
LAST_SIGNAL
};
static guint cinnamon_app_signals[LAST_SIGNAL] = { 0 };
static void create_running_state (CinnamonApp *app);
static void unref_running_state (CinnamonAppRunningState *state);
static void
cinnamon_app_get_property (GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
CinnamonApp *app = CINNAMON_APP (gobject);
switch (prop_id)
{
case PROP_STATE:
g_value_set_enum (value, app->state);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
const char *
cinnamon_app_get_id (CinnamonApp *app)
{
if (app->entry)
return gmenu_tree_entry_get_desktop_file_id (app->entry);
return app->window_id_string;
}
static MetaWindow *
window_backed_app_get_window (CinnamonApp *app)
{
g_assert (app->running_state);
g_assert (app->running_state->windows);
return app->running_state->windows->data;
}
static ClutterActor *
get_actor_for_icon_name (CinnamonApp *app,
const gchar *icon_name,
gint size)
{
ClutterActor *actor;
GIcon *icon;
icon = NULL;
actor = NULL;
if (g_path_is_absolute (icon_name))
{
GFile *icon_file;
icon_file = g_file_new_for_path (icon_name);
icon = g_file_icon_new (icon_file);
g_object_unref (icon_file);
}
else
{
icon = g_themed_icon_new (icon_name);
}
if (icon != NULL)
{
actor = g_object_new (ST_TYPE_ICON, "gicon", icon, "icon-size", size, NULL);
g_object_unref (icon);
}
return actor;
}
static ClutterActor *
window_backed_app_get_icon (CinnamonApp *app,
int size)
{
MetaWindow *window;
ClutterActor *actor;
gint scale;
CinnamonGlobal *global;
StThemeContext *context;
const gchar *icon_name;
actor = NULL;
global = cinnamon_global_get ();
context = st_theme_context_get_for_stage (cinnamon_global_get_stage (global));
g_object_get (context, "scale-factor", &scale, NULL);
/* During a state transition from running to not-running for
* window-backend apps, it's possible we get a request for the icon.
* Avoid asserting here and just return an empty image.
*/
if (app->running_state == NULL)
{
actor = clutter_texture_new ();
g_object_set (actor, "opacity", 0, "width", (float) size, "height", (float) size, NULL);
return actor;
}
window = window_backed_app_get_window (app);
icon_name = meta_window_get_icon_name (window);
if (icon_name != NULL)
{
actor = get_actor_for_icon_name (app, icon_name, size);
}
if (actor == NULL)
{
size *= scale;
actor = st_texture_cache_bind_pixbuf_property (st_texture_cache_get_default (),
G_OBJECT (window), "icon");
g_object_set (actor, "width", (float) size, "height", (float) size, NULL);
}
return actor;
}
/**
* cinnamon_app_create_icon_texture:
*
* Look up the icon for this application, and create a #ClutterTexture
* for it at the given size.
*
* Return value: (transfer none): A floating #ClutterActor
*/
ClutterActor *
cinnamon_app_create_icon_texture (CinnamonApp *app,
int size)
{
GIcon *icon;
ClutterActor *ret;
gboolean has_custom_icon;
has_custom_icon = FALSE;
ret = NULL;
if (app->running_state != NULL)
{
MetaWindow *window;
const gchar *icon_name;
window = window_backed_app_get_window (app);
icon_name = meta_window_get_icon_name (window);
has_custom_icon = icon_name != NULL;
}
if (app->entry == NULL || has_custom_icon)
{
return window_backed_app_get_icon (app, size);
}
icon = g_app_info_get_icon (G_APP_INFO (gmenu_tree_entry_get_app_info (app->entry)));
if (icon != NULL)
{
ret = g_object_new (ST_TYPE_ICON, "gicon", icon, "icon-size", size, NULL);
}
if (ret == NULL)
{
icon = g_themed_icon_new ("application-x-executable");
ret = g_object_new (ST_TYPE_ICON, "gicon", icon, "icon-size", size, NULL);
g_object_unref (icon);
}
return ret;
}
typedef struct {
CinnamonApp *app;
int size;
int scale;
} CreateFadedIconData;
static CoglHandle
cinnamon_app_create_faded_icon_cpu (StTextureCache *cache,
const char *key,
void *datap,
GError **error)
{
CreateFadedIconData *data = datap;
CinnamonApp *app;
GdkPixbuf *pixbuf;
int size;
int scale;
CoglHandle texture;
gint width, height, rowstride;
guint8 n_channels;
gboolean have_alpha;
gint fade_start;
gint fade_range;
int i, j;
guint pixbuf_byte_size;
guint8 *orig_pixels;
guint8 *pixels;
GIcon *icon;
GtkIconInfo *info;
app = data->app;
size = data->size;
scale = data->scale;
info = NULL;
icon = g_app_info_get_icon (G_APP_INFO (gmenu_tree_entry_get_app_info (app->entry)));
if (icon != NULL)
{
info = gtk_icon_theme_lookup_by_gicon_for_scale (gtk_icon_theme_get_default (),
icon, size, scale,
GTK_ICON_LOOKUP_FORCE_SIZE);
}
if (info == NULL)
{
icon = g_themed_icon_new ("application-x-executable");
info = gtk_icon_theme_lookup_by_gicon_for_scale (gtk_icon_theme_get_default (),
icon, size, scale,
GTK_ICON_LOOKUP_FORCE_SIZE);
g_object_unref (icon);
}
if (info == NULL)
return COGL_INVALID_HANDLE;
pixbuf = gtk_icon_info_load_icon (info, NULL);
g_object_unref (info);
if (pixbuf == NULL)
return COGL_INVALID_HANDLE;
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
n_channels = gdk_pixbuf_get_n_channels (pixbuf);
orig_pixels = gdk_pixbuf_get_pixels (pixbuf);
have_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
pixbuf_byte_size = (height - 1) * rowstride +
+ width * ((n_channels * gdk_pixbuf_get_bits_per_sample (pixbuf) + 7) / 8);
pixels = g_malloc0 (rowstride * height);
memcpy (pixels, orig_pixels, pixbuf_byte_size);
fade_start = width / 2;
fade_range = width - fade_start;
for (i = fade_start; i < width; i++)
{
for (j = 0; j < height; j++)
{
guchar *pixel = &pixels[j * rowstride + i * n_channels];
float fade = 1.0 - ((float) i - fade_start) / fade_range;
pixel[0] = 0.5 + pixel[0] * fade;
pixel[1] = 0.5 + pixel[1] * fade;
pixel[2] = 0.5 + pixel[2] * fade;
if (have_alpha)
pixel[3] = 0.5 + pixel[3] * fade;
}
}
texture = st_cogl_texture_new_from_data_wrapper (width, height,
COGL_TEXTURE_NONE,
have_alpha ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888,
COGL_PIXEL_FORMAT_ANY,
rowstride, pixels);
g_free (pixels);
g_object_unref (pixbuf);
return texture;
}
/**
* cinnamon_app_get_faded_icon:
* @app: A #CinnamonApp
* @size: Size in pixels
*
* Return an actor with a horizontally faded look.
*
* Return value: (transfer none): A floating #ClutterActor, or %NULL if no icon
*/
ClutterActor *
cinnamon_app_get_faded_icon (CinnamonApp *app, int size)
{
CoglHandle texture;
ClutterActor *result;
char *cache_key;
CreateFadedIconData data;
gint scale;
CinnamonGlobal *global;
StThemeContext *context;
/* Don't fade for window backed apps for now...easier to reuse the
* property tracking bits, and this helps us visually distinguish
* app-tracked from not.
*/
if (!app->entry)
return window_backed_app_get_icon (app, size);
global = cinnamon_global_get ();
context = st_theme_context_get_for_stage (cinnamon_global_get_stage (global));
g_object_get (context, "scale-factor", &scale, NULL);
cache_key = g_strdup_printf ("faded-icon:%s,size=%d,scale=%d", cinnamon_app_get_id (app), size, scale);
data.app = app;
data.size = size;
data.scale = scale;
texture = st_texture_cache_load (st_texture_cache_get_default (),
cache_key,
ST_TEXTURE_CACHE_POLICY_FOREVER,
cinnamon_app_create_faded_icon_cpu,
&data,
NULL);
g_free (cache_key);
if (texture != COGL_INVALID_HANDLE)
{
result = clutter_texture_new ();
clutter_texture_set_cogl_texture (CLUTTER_TEXTURE (result), texture);
}
else
{
result = clutter_texture_new ();
g_object_set (result, "opacity", 0, "width", (float) size * scale, "height", (float) size * scale, NULL);
}
return result;
}
const char *
cinnamon_app_get_name (CinnamonApp *app)
{
if (app->entry)
return g_app_info_get_name (G_APP_INFO (gmenu_tree_entry_get_app_info (app->entry)));
else if (app->running_state == NULL)
return _("Unknown");
else
{
MetaWindow *window = window_backed_app_get_window (app);
const char *name;
name = meta_window_get_wm_class (window);
if (!name)
name = _("Unknown");
return name;
}
}
const char *
cinnamon_app_get_description (CinnamonApp *app)
{
if (app->entry)
return g_app_info_get_description (G_APP_INFO (gmenu_tree_entry_get_app_info (app->entry)));
else
return NULL;
}
const char *
cinnamon_app_get_keywords (CinnamonApp *app)
{
const char * const *keywords;
const char *keyword;
gint i;
gchar *ret = NULL;
if (app->keywords)
return app->keywords;
if (app->entry)
keywords = g_desktop_app_info_get_keywords (G_DESKTOP_APP_INFO (gmenu_tree_entry_get_app_info (app->entry)));
else
keywords = NULL;
if (keywords != NULL)
{
GString *keyword_list = g_string_new(NULL);
for (i = 0; keywords[i] != NULL; i++)
{
keyword = keywords[i];
g_string_append_printf (keyword_list, "%s;", keyword);
}
ret = g_string_free (keyword_list, FALSE);
}
app->keywords = ret;
return ret;
}
/**
* cinnamon_app_is_window_backed:
*
* A window backed application is one which represents just an open
* window, i.e. there's no .desktop file association, so we don't know
* how to launch it again.
*/
gboolean
cinnamon_app_is_window_backed (CinnamonApp *app)
{
return app->entry == NULL;
}
typedef struct {
MetaWorkspace *workspace;
GSList **transients;
} CollectTransientsData;
static gboolean
collect_transients_on_workspace (MetaWindow *window,
gpointer datap)
{
CollectTransientsData *data = datap;
if (data->workspace && meta_window_get_workspace (window) != data->workspace)
return TRUE;
*data->transients = g_slist_prepend (*data->transients, window);
return TRUE;
}
/* The basic idea here is that when we're targeting a window,
* if it has transients we want to pick the most recent one
* the user interacted with.
* This function makes raising GEdit with the file chooser
* open work correctly.
*/
static MetaWindow *
find_most_recent_transient_on_same_workspace (MetaDisplay *display,
MetaWindow *reference)
{
GSList *transients, *transients_sorted, *iter;
MetaWindow *result;
CollectTransientsData data;
transients = NULL;
data.workspace = meta_window_get_workspace (reference);
data.transients = &transients;
meta_window_foreach_transient (reference, collect_transients_on_workspace, &data);
transients_sorted = meta_display_sort_windows_by_stacking (display, transients);
/* Reverse this so we're top-to-bottom (yes, we should probably change the order
* returned from the sort_windows_by_stacking function)
*/
transients_sorted = g_slist_reverse (transients_sorted);
g_slist_free (transients);
transients = NULL;
result = NULL;
for (iter = transients_sorted; iter; iter = iter->next)
{
MetaWindow *window = iter->data;
MetaWindowType wintype = meta_window_get_window_type (window);
/* Don't want to focus UTILITY types, like the Gimp toolbars */
if (wintype == META_WINDOW_NORMAL ||
wintype == META_WINDOW_DIALOG)
{
result = window;
break;
}
}
g_slist_free (transients_sorted);
return result;
}
/**
* cinnamon_app_activate_window:
* @app: a #CinnamonApp
* @window: (allow-none): Window to be focused
* @timestamp: Event timestamp
*
* Bring all windows for the given app to the foreground,
* but ensure that @window is on top. If @window is %NULL,
* the window with the most recent user time for the app
* will be used.
*
* This function has no effect if @app is not currently running.
*/
void
cinnamon_app_activate_window (CinnamonApp *app,
MetaWindow *window,
guint32 timestamp)
{
GSList *windows;
if (cinnamon_app_get_state (app) != CINNAMON_APP_STATE_RUNNING)
return;
windows = cinnamon_app_get_windows (app);
if (window == NULL && windows)
window = windows->data;
if (!g_slist_find (windows, window))
return;
else
{
GSList *iter;
CinnamonGlobal *global = cinnamon_global_get ();
MetaScreen *screen = cinnamon_global_get_screen (global);
MetaDisplay *display = meta_screen_get_display (screen);
MetaWorkspace *active = meta_screen_get_active_workspace (screen);
MetaWorkspace *workspace = meta_window_get_workspace (window);
guint32 last_user_timestamp = meta_display_get_last_user_time (display);
MetaWindow *most_recent_transient;
if (meta_display_xserver_time_is_before (display, timestamp, last_user_timestamp))
{
meta_window_set_demands_attention (window);
return;
}
/* Now raise all the other windows for the app that are on
* the same workspace, in reverse order to preserve the stacking.
*/
for (iter = windows; iter; iter = iter->next)
{
MetaWindow *other_window = iter->data;
if (other_window != window)
meta_window_raise (other_window);
}
/* If we have a transient that the user's interacted with more recently than
* the window, pick that.
*/
most_recent_transient = find_most_recent_transient_on_same_workspace (display, window);
if (most_recent_transient
&& meta_display_xserver_time_is_before (display,
meta_window_get_user_time (window),
meta_window_get_user_time (most_recent_transient)))
window = most_recent_transient;
if (!cinnamon_window_tracker_is_window_interesting (cinnamon_window_tracker_get_default (), window))
{
/* We won't get notify::user-time signals for uninteresting windows,
* which means that an app's last_user_time won't get updated.
* Update it here instead.
*/
app->running_state->last_user_time = timestamp;
}
if (active != workspace)
meta_workspace_activate_with_focus (workspace, window, timestamp);
else
meta_window_activate (window, timestamp);
}
}
/**
* cinnamon_app_activate:
* @app: a #CinnamonApp
*
* Like cinnamon_app_activate_full(), but using the default workspace and
* event timestamp.
*/
void
cinnamon_app_activate (CinnamonApp *app)
{
return cinnamon_app_activate_full (app, -1, 0);
}
/**
* cinnamon_app_activate_full:
* @app: a #CinnamonApp
* @workspace: launch on this workspace, or -1 for default. Ignored if
* activating an existing window
* @timestamp: Event timestamp
*
* Perform an appropriate default action for operating on this application,
* dependent on its current state. For example, if the application is not
* currently running, launch it. If it is running, activate the most
* recently used NORMAL window (or if that window has a transient, the most
* recently used transient for that window).
*/
void
cinnamon_app_activate_full (CinnamonApp *app,
int workspace,
guint32 timestamp)
{
CinnamonGlobal *global;
global = cinnamon_global_get ();
if (timestamp == 0)
timestamp = cinnamon_global_get_current_time (global);
switch (app->state)
{
case CINNAMON_APP_STATE_STOPPED:
{
GError *error = NULL;
if (!cinnamon_app_launch (app,
timestamp,
NULL,
workspace,
NULL,
&error))
{
char *msg;
msg = g_strdup_printf (_("Failed to launch '%s'"), cinnamon_app_get_name (app));
cinnamon_global_notify_error (global,
msg,
error->message);
g_free (msg);
g_clear_error (&error);
}
}
break;
case CINNAMON_APP_STATE_STARTING:
break;
case CINNAMON_APP_STATE_RUNNING:
cinnamon_app_activate_window (app, NULL, timestamp);
break;
default:
g_warning("cinnamon_app_activate_full: default case");
break;
}
}
/**
* cinnamon_app_open_new_window:
* @app: a #CinnamonApp
* @workspace: open on this workspace, or -1 for default
*
* Request that the application create a new window.
*/
void
cinnamon_app_open_new_window (CinnamonApp *app,
int workspace)
{
g_return_if_fail (app->entry != NULL);
/* Here we just always launch the application again, even if we know
* it was already running. For most applications this
* should have the effect of creating a new window, whether that's
* a second process (in the case of Calculator) or IPC to existing
* instance (Firefox). There are a few less-sensical cases such
* as say Pidgin. Ideally, we have the application express to us
* that it supports an explicit new-window action.
*/
cinnamon_app_launch (app,
0,
NULL,
workspace,
NULL,
NULL);
}
/**
* cinnamon_app_get_state:
* @app: a #CinnamonApp
*
* Returns: State of the application
*/
CinnamonAppState
cinnamon_app_get_state (CinnamonApp *app)
{
return app->state;
}
typedef struct {
CinnamonApp *app;
MetaWorkspace *active_workspace;
} CompareWindowsData;
static int
cinnamon_app_compare_windows (gconstpointer a,
gconstpointer b,
gpointer datap)
{
MetaWindow *win_a = (gpointer)a;
MetaWindow *win_b = (gpointer)b;
CompareWindowsData *data = datap;
gboolean ws_a, ws_b;
gboolean vis_a, vis_b;
ws_a = meta_window_get_workspace (win_a) == data->active_workspace;
ws_b = meta_window_get_workspace (win_b) == data->active_workspace;
if (ws_a && !ws_b)
return -1;
else if (!ws_a && ws_b)
return 1;
vis_a = meta_window_showing_on_its_workspace (win_a);
vis_b = meta_window_showing_on_its_workspace (win_b);
if (vis_a && !vis_b)
return -1;
else if (!vis_a && vis_b)
return 1;
return meta_window_get_user_time (win_b) - meta_window_get_user_time (win_a);
}
/**
* cinnamon_app_get_windows:
* @app:
*
* Get the toplevel, interesting windows which are associated with this
* application. The returned list will be sorted first by whether
* they're on the active workspace, then by whether they're visible,
* and finally by the time the user last interacted with them.
*
* Returns: (transfer none) (element-type MetaWindow): List of windows
*/
GSList *
cinnamon_app_get_windows (CinnamonApp *app)
{
if (app->running_state == NULL)
return NULL;
if (app->running_state->window_sort_stale)
{
CompareWindowsData data;
data.app = app;
data.active_workspace = meta_screen_get_active_workspace (cinnamon_global_get_screen (cinnamon_global_get ()));
app->running_state->windows = g_slist_sort_with_data (app->running_state->windows, cinnamon_app_compare_windows, &data);
app->running_state->window_sort_stale = FALSE;
}
return app->running_state->windows;
}
guint
cinnamon_app_get_n_windows (CinnamonApp *app)
{
if (app->running_state == NULL)
return 0;
return g_slist_length (app->running_state->windows);
}
static gboolean
cinnamon_app_has_visible_windows (CinnamonApp *app)
{
GSList *iter;
if (app->running_state == NULL)
return FALSE;
for (iter = app->running_state->windows; iter; iter = iter->next)
{
MetaWindow *window = iter->data;
if (meta_window_showing_on_its_workspace (window))
return TRUE;
}
return FALSE;
}
gboolean
cinnamon_app_is_on_workspace (CinnamonApp *app,
MetaWorkspace *workspace)
{
GSList *iter;
if (cinnamon_app_get_state (app) == CINNAMON_APP_STATE_STARTING)
{
if (app->started_on_workspace == -1 ||
meta_workspace_index (workspace) == app->started_on_workspace)
return TRUE;
else
return FALSE;
}
if (app->running_state == NULL)
return FALSE;
for (iter = app->running_state->windows; iter; iter = iter->next)
{
if (meta_window_get_workspace (iter->data) == workspace)
return TRUE;
}
return FALSE;
}
/**
* cinnamon_app_compare:
* @app:
* @other: A #CinnamonApp
*
* Compare one #CinnamonApp instance to another, in the following way:
* - Running applications sort before not-running applications.
* - If one of them has visible windows and the other does not, the one
* with visible windows is first.
* - Finally, the application which the user interacted with most recently
* compares earlier.
*/
int
cinnamon_app_compare (CinnamonApp *app,
CinnamonApp *other)
{
gboolean vis_app, vis_other;
if (app->state != other->state)
{
if (app->state == CINNAMON_APP_STATE_RUNNING)
return -1;
return 1;
}
vis_app = cinnamon_app_has_visible_windows (app);
vis_other = cinnamon_app_has_visible_windows (other);
if (vis_app && !vis_other)
return -1;
else if (!vis_app && vis_other)
return 1;
if (app->state == CINNAMON_APP_STATE_RUNNING)
{
if (app->running_state->windows && !other->running_state->windows)
return -1;
else if (!app->running_state->windows && other->running_state->windows)
return 1;
return other->running_state->last_user_time - app->running_state->last_user_time;
}
return 0;
}
CinnamonApp *
_cinnamon_app_new_for_window (MetaWindow *window)
{
CinnamonApp *app;
app = g_object_new (CINNAMON_TYPE_APP, NULL);
app->window_id_string = g_strdup_printf ("window:%d", meta_window_get_stable_sequence (window));
_cinnamon_app_add_window (app, window);
return app;
}
CinnamonApp *
_cinnamon_app_new (GMenuTreeEntry *info)
{
CinnamonApp *app;
app = g_object_new (CINNAMON_TYPE_APP, NULL);
_cinnamon_app_set_entry (app, info);
return app;
}
void
_cinnamon_app_set_entry (CinnamonApp *app,
GMenuTreeEntry *entry)
{
if (app->entry != NULL)
gmenu_tree_item_unref (app->entry);
app->entry = gmenu_tree_item_ref (entry);
if (app->name_collation_key != NULL)
g_free (app->name_collation_key);
app->name_collation_key = g_utf8_collate_key (cinnamon_app_get_name (app), -1);
}
static void
cinnamon_app_state_transition (CinnamonApp *app,
CinnamonAppState state)
{
if (app->state == state)
return;
g_return_if_fail (!(app->state == CINNAMON_APP_STATE_RUNNING &&
state == CINNAMON_APP_STATE_STARTING));
app->state = state;
if (app->state == CINNAMON_APP_STATE_STOPPED && app->running_state)
{
unref_running_state (app->running_state);
app->running_state = NULL;
}
_cinnamon_app_system_notify_app_state_changed (cinnamon_app_system_get_default (), app);
g_object_notify (G_OBJECT (app), "state");
}
static void
cinnamon_app_on_unmanaged (MetaWindow *window,
CinnamonApp *app)
{
_cinnamon_app_remove_window (app, window);
}
static void
cinnamon_app_on_user_time_changed (MetaWindow *window,
GParamSpec *pspec,
CinnamonApp *app)
{
g_assert (app->running_state != NULL);
app->running_state->last_user_time = meta_window_get_user_time (window);