-
Notifications
You must be signed in to change notification settings - Fork 744
/
cinnamon-app.c
1461 lines (1217 loc) · 39.4 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 <glib/gstdio.h>
#define GMENU_I_KNOW_THIS_IS_UNSTABLE
#include <gmenu-desktopappinfo.h>
#include <libxapp/xapp-gpu-offload-helper.h>
#include <meta/display.h>
#include <meta/meta-workspace-manager.h>
#include "cinnamon-app-private.h"
#include "cinnamon-enum-types.h"
#include "cinnamon-global-private.h"
#include "cinnamon-util.h"
#include "cinnamon-app-system-private.h"
#include "cinnamon-window-tracker-private.h"
#include "st.h"
/* 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;
/* 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;
CinnamonGlobal *global;
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).
*/
GMenuDesktopAppInfo *info;
CinnamonAppRunningState *running_state;
char *window_id_string;
char *keywords;
char *unique_name;
gboolean hidden_as_duplicate;
gboolean is_flatpak;
};
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;
}
char *
cinnamon_app_get_flatpak_app_id (CinnamonApp *app)
{
if (app->info)
{
gchar *id;
id = g_strdup (gmenu_desktopappinfo_get_flatpak_app_id (app->info));
if (id != NULL)
{
return id;
}
else
{
const gchar *desktop_file = cinnamon_app_get_id (app);
gchar **split = g_strsplit (desktop_file, ".desktop", -1);
id = g_strdup (split[0]);
g_strfreev (split);
return id;
}
}
// This should never occur
return NULL;
}
static MetaWindow *
window_backed_app_get_window (CinnamonApp *app)
{
g_assert (app->info == NULL);
if (app->running_state)
{
g_assert (app->running_state->windows);
return app->running_state->windows->data;
}
else
return NULL;
}
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-type", ST_ICON_FULLCOLOR, "icon-size", size, NULL);
g_object_unref (icon);
}
return actor;
}
static ClutterActor *
get_failsafe_icon (int size)
{
GIcon *icon = g_themed_icon_new ("application-x-executable");
ClutterActor *actor = g_object_new (ST_TYPE_ICON, "gicon", icon, "icon-type", ST_ICON_FULLCOLOR, "icon-size", size, NULL);
g_object_unref (icon);
return actor;
}
static ClutterActor *
window_backed_app_get_icon (CinnamonApp *app,
int size)
{
MetaWindow *window = NULL;
StWidget *widget;
int scale, scaled_size;
CinnamonGlobal *global;
StThemeContext *context;
global = cinnamon_global_get ();
context = st_theme_context_get_for_stage (cinnamon_global_get_stage (global));
g_object_get (context, "scale-factor", &scale, NULL);
scaled_size = size * scale;
/* 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)
window = window_backed_app_get_window (app);
if (window == NULL)
{
ClutterActor *actor;
actor = clutter_actor_new ();
g_object_set (actor,
"opacity", 0,
"width", (float) scaled_size,
"height", (float) scaled_size,
NULL);
return actor;
}
widget = NULL;
if (meta_window_get_client_type (window) == META_WINDOW_CLIENT_TYPE_X11)
{
cairo_surface_t *icon;
g_object_get (G_OBJECT (window), "icon", &icon, NULL);
if (icon != NULL)
{
StWidget *texture_actor;
texture_actor =
st_texture_cache_bind_cairo_surface_property (st_texture_cache_get_default (),
G_OBJECT (window),
"icon",
scaled_size);
widget = g_object_new (ST_TYPE_BIN,
"child", texture_actor,
NULL);
}
}
if (widget == NULL)
{
widget = g_object_new (ST_TYPE_ICON,
"icon-size", size,
"icon-type", ST_ICON_FULLCOLOR,
"icon-name", "application-x-executable",
NULL);
}
st_widget_add_style_class_name (widget, "fallback-app-icon");
return CLUTTER_ACTOR (widget);
}
/**
* cinnamon_app_create_icon_texture:
*
* Look up the icon for this application, and create a #ClutterActor
* 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;
ret = NULL;
if (app->info == NULL)
return window_backed_app_get_icon (app, size);
icon = g_app_info_get_icon (G_APP_INFO (app->info));
if (icon != NULL)
ret = g_object_new (ST_TYPE_ICON, "gicon", icon, "icon-size", size, NULL);
if (ret == NULL)
ret = get_failsafe_icon (size);
return ret;
}
/**
* cinnamon_app_create_icon_texture_for_window:
* @app: a #CinnamonApp
* @size: the size of the icon to create
* @for_window: (nullable): Optional - the backing MetaWindow to look up for.
*
* Look up the icon for this application, and create a #ClutterTexture
* for it at the given size. If for_window is NULL, it bases the icon
* off the most-recently-used window for the app, otherwise it attempts to
* use for_window for determining the icon.
*
* Return value: (transfer none): A floating #ClutterActor
*/
ClutterActor *
cinnamon_app_create_icon_texture_for_window (CinnamonApp *app,
int size,
MetaWindow *for_window)
{
MetaWindow *window;
window = NULL;
if (app->running_state != NULL)
{
const gchar *icon_name;
if (for_window != NULL)
{
if (g_slist_find (app->running_state->windows, for_window) != NULL)
{
window = for_window;
}
else
{
g_warning ("cinnamon_app_create_icon_texture: MetaWindow %p provided that does not match App %p",
for_window, app);
}
}
if (window != NULL)
{
icon_name = meta_window_get_icon_name (window);
if (icon_name != NULL)
{
return get_actor_for_icon_name (app, icon_name, size);
}
}
}
return cinnamon_app_create_icon_texture (app, size);
}
static const char *
get_common_name (CinnamonApp *app)
{
if (app->entry)
return g_app_info_get_name (G_APP_INFO (app->info));
else if (app->running_state == NULL)
return _("Unknown");
else
{
MetaWindow *window = window_backed_app_get_window (app);
const char *name = NULL;
if (window)
name = meta_window_get_wm_class (window);
if (!name)
name = _("Unknown");
return name;
}
}
const char *
cinnamon_app_get_name (CinnamonApp *app)
{
if (app->unique_name)
return app->unique_name;
return get_common_name (app);
}
const char *
cinnamon_app_get_description (CinnamonApp *app)
{
if (app->entry)
return g_app_info_get_description (G_APP_INFO (app->info));
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->info)
keywords = gmenu_desktopappinfo_get_keywords (app->info);
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;
}
gboolean
cinnamon_app_get_nodisplay (CinnamonApp *app)
{
if (app->hidden_as_duplicate)
{
return TRUE;
}
if (app->entry)
{
g_return_val_if_fail (app->info != NULL, TRUE);
return gmenu_desktopappinfo_get_nodisplay (app->info);
// return !g_app_info_should_show (G_APP_INFO (app->info));
}
return FALSE;
}
/**
* 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: (nullable): 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 (app->state != 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 = app->global;
MetaWorkspaceManager *workspace_manager = global->workspace_manager;
MetaDisplay *display = global->meta_display;
MetaWorkspace *active = meta_workspace_manager_get_active_workspace (workspace_manager);
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 (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 = app->global;
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_can_open_new_window:
* @app: a #CinnamonApp
*
* Returns %TRUE if the app supports opening a new window through
* cinnamon_app_open_new_window() (ie, if calling that function will
* result in actually opening a new window and not something else,
* like presenting the most recently active one)
*/
gboolean
cinnamon_app_can_open_new_window (CinnamonApp *app)
{
/* Apps that are not running can always open new windows, because
activating them would open the first one */
if (!app->running_state)
return TRUE;
/* If the app doesn't have a desktop file, then nothing is possible */
if (!app->info)
return FALSE;
/* If the app is explicitly telling us, then we know for sure */
if (gmenu_desktopappinfo_has_key (GMENU_DESKTOPAPPINFO (app->info),
"X-GNOME-SingleWindow"))
return !gmenu_desktopappinfo_get_boolean (GMENU_DESKTOPAPPINFO (app->info),
"X-GNOME-SingleWindow");
/* In all other cases, we don't have a reliable source of information
or a decent heuristic, so we err on the compatibility side and say
yes.
*/
return TRUE;
}
/**
* cinnamon_app_get_state:
* @app: a #CinnamonApp
*
* Returns: State of the application
*/
CinnamonAppState
cinnamon_app_get_state (CinnamonApp *app)
{
return app->state;
}
/**
* cinnamon_app_get_is_flatpak:
* @app: a #CinnamonApp
*
* Returns: TRUE if #app is a flatpak app, FALSE if not
*/
gboolean
cinnamon_app_get_is_flatpak (CinnamonApp *app)
{
return app->is_flatpak;
}
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_workspace_manager_get_active_workspace (app->global->workspace_manager);
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);
}
gboolean
cinnamon_app_is_on_workspace (CinnamonApp *app,
MetaWorkspace *workspace)
{
GSList *iter;
if (app->state == 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;
}
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)
{
g_clear_pointer (&app->entry, gmenu_tree_item_unref);
g_clear_object (&app->info);
/* If our entry has changed, our name may have as well, so clear
* anything set by appsys while deduplicating desktop items. */
g_clear_pointer (&app->unique_name, g_free);
app->hidden_as_duplicate = FALSE;
app->entry = gmenu_tree_item_ref (entry);
if (entry != NULL)
{
app->info = g_object_ref (gmenu_tree_entry_get_app_info (entry));
app->is_flatpak = app->info && gmenu_desktopappinfo_get_is_flatpak (app->info);
}
}
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_ws_switch (MetaWorkspaceManager *workspace_manager,
int from,
int to,
MetaMotionDirection direction,
gpointer data)
{
CinnamonApp *app = CINNAMON_APP (data);
g_assert (app->running_state != NULL);
app->running_state->window_sort_stale = TRUE;
g_signal_emit (app, cinnamon_app_signals[WINDOWS_CHANGED], 0);
}
void
_cinnamon_app_add_window (CinnamonApp *app,
MetaWindow *window)
{
if (app->running_state && g_slist_find (app->running_state->windows, window))
return;
g_object_freeze_notify (G_OBJECT (app));
if (!app->running_state)
create_running_state (app);
app->running_state->window_sort_stale = TRUE;
app->running_state->windows = g_slist_prepend (app->running_state->windows, g_object_ref (window));
g_signal_connect (window, "unmanaged", G_CALLBACK(cinnamon_app_on_unmanaged), app);
if (app->state != CINNAMON_APP_STATE_STARTING)
cinnamon_app_state_transition (app, CINNAMON_APP_STATE_RUNNING);
g_object_thaw_notify (G_OBJECT (app));
g_signal_emit (app, cinnamon_app_signals[WINDOWS_CHANGED], 0);
}
void
_cinnamon_app_remove_window (CinnamonApp *app,
MetaWindow *window)
{
g_assert (app->running_state != NULL);
if (!g_slist_find (app->running_state->windows, window))
return;
g_signal_handlers_disconnect_by_func (window, G_CALLBACK(cinnamon_app_on_unmanaged), app);
g_object_unref (window);
app->running_state->windows = g_slist_remove (app->running_state->windows, window);
if (app->running_state->windows == NULL)
cinnamon_app_state_transition (app, CINNAMON_APP_STATE_STOPPED);
g_signal_emit (app, cinnamon_app_signals[WINDOWS_CHANGED], 0);
}
/**