File: launcher-util.c

package info (click to toggle)
matchbox-desktop 2.2%2Bgit20200512-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 412 kB
  • sloc: ansic: 3,161; makefile: 57; sh: 24
file content (283 lines) | stat: -rw-r--r-- 6,397 bytes parent folder | download | duplicates (2)
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
/* 
 * Copyright (C) 2007 OpenedHand Ltd
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 */

#include <config.h>

#include <string.h>
#include <unistd.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include "launcher-util.h"
#include "xutil.h"

#ifdef USE_LIBSN
#define SN_API_NOT_YET_FROZEN 1
#include <libsn/sn.h>
#endif

/* Convert command line to argv array, stripping % conversions on the way */
#define MAX_ARGS 255
char **
exec_to_argv (const char *exec)
{
  const char *p;
  char *buf, *bufp, **argv;
  int nargs;
  gboolean escape, single_quote, double_quote;
  
  argv = g_new (char *, MAX_ARGS + 1);
  buf = g_alloca (strlen (exec) + 1);
  bufp = buf;
  nargs = 0;
  escape = single_quote = double_quote = FALSE;
  
  for (p = exec; *p; p++) {
    if (escape) {
      *bufp++ = *p;
      
      escape = FALSE;
    } else {
      switch (*p) {
      case '\\':
        escape = TRUE;

        break;
      case '%':
        /* Strip '%' conversions */
        if (p[1] && p[1] == '%')
          *bufp++ = *p;
        
        p++;

        break;
      case '\'':
        if (double_quote)
          *bufp++ = *p;
        else
          single_quote = !single_quote;
        
        break;
      case '\"':
        if (single_quote)
          *bufp++ = *p;
        else
          double_quote = !double_quote;
        
        break;
      case ' ':
        if (single_quote || double_quote)
          *bufp++ = *p;
        else {
          *bufp = 0;
          
          if (nargs < MAX_ARGS)
            argv[nargs++] = g_strdup (buf);
          
          bufp = buf;
        }
        
        break;
      default:
        *bufp++ = *p;
        break;
      }
    }
  }
  
  if (bufp != buf) {
    *bufp = 0;
    
    if (nargs < MAX_ARGS)
      argv[nargs++] = g_strdup (buf);
  }
  
  argv[nargs] = NULL;
  
  return argv;
}

/* Strips extension off filename */
static char *
strip_extension (const char *file)
{
        char *stripped, *p;

        stripped = g_strdup (file);

        p = strrchr (stripped, '.');
        if (p &&
            (!strcmp (p, ".png") ||
             !strcmp (p, ".svg") ||
             !strcmp (p, ".xpm")))
	        *p = 0;

        return stripped;
}

#define MISSING_IMAGE "gtk-missing-image"
#define GENERIC_EXECUTABLE "application-x-executable"

GdkPixbuf*
get_icon (const gchar *name, gint pixel_size)
{
  static GtkIconTheme *theme = NULL;
  GdkPixbuf *pixbuf = NULL;
  GError *error = NULL;
  gchar *stripped = NULL;
  gint width, height;

  if (G_UNLIKELY (theme == NULL))
    theme = gtk_icon_theme_get_default ();

  if (name == NULL) {
    return get_icon (GENERIC_EXECUTABLE, pixel_size);
  }

  if (g_path_is_absolute (name)) {
    pixbuf = gdk_pixbuf_new_from_file_at_scale (name, pixel_size, pixel_size,
                                                TRUE, &error);
    if (error) {
      g_warning ("Error loading icon: %s", error->message);
      g_error_free (error);
      error = NULL;
    }
    return pixbuf;
  }

  stripped = strip_extension (name);
  
  pixbuf = gtk_icon_theme_load_icon (theme,
                                     stripped,
                                     pixel_size,
                                     0, &error);
  if (error) {
    g_warning ("Error loading icon: %s", error->message);
    g_error_free (error);
    error = NULL;
  }
  g_free (stripped);

  /* Fallback on generic executable, then missing image */
  if (pixbuf == NULL) {
    if (strcmp (name, MISSING_IMAGE) == 0) {
      return NULL;
    } else if (strcmp (name, GENERIC_EXECUTABLE) == 0) {
      return get_icon (MISSING_IMAGE, pixel_size);
    } else {
      return get_icon (GENERIC_EXECUTABLE, pixel_size);
    }
  }
 
  width = gdk_pixbuf_get_width (pixbuf);
  height = gdk_pixbuf_get_height (pixbuf);

  if (width != pixel_size || height != pixel_size) {
    GdkPixbuf *new;
    
    new = gdk_pixbuf_scale_simple (pixbuf,
                                   pixel_size, pixel_size,
                                   GDK_INTERP_BILINEAR);
    
    g_object_unref (pixbuf);
    pixbuf = new;
  }

  return pixbuf;
}


static void
child_setup (gpointer user_data)
{
#ifdef USE_LIBSN
  if (user_data) {
    sn_launcher_context_setup_child_process (user_data);
  }
#endif
}


/* TODO: optionally link to GtkUnique and directly handle that? */
void
launcher_start (GtkWidget *widget, 
                TakuMenuItem *item, 
                gchar **argv,
                gboolean use_sn,
                gboolean single_instance)
{
  GError *error = NULL;
#ifdef USE_LIBSN
  SnLauncherContext *context;
#endif

  /* Check for an existing instance if Matchbox single instance */
  if (single_instance) {
    Window win_found;

    if (mb_single_instance_is_starting (argv[0]))
      return;

    win_found = mb_single_instance_get_window (argv[0]);
    if (win_found != None) {
      x_window_activate (win_found, gtk_get_current_event_time ());

      return;
    }
  }
  
#ifdef USE_LIBSN
  context = NULL;
  
  if (use_sn) {
    SnDisplay *sn_dpy;
    Display *display;
    int screen;

    display = gdk_x11_display_get_xdisplay (gtk_widget_get_display (widget));
    sn_dpy = sn_display_new (display, NULL, NULL);
    
    screen = gdk_screen_get_number (gtk_widget_get_screen (widget));
    context = sn_launcher_context_new (sn_dpy, screen);
    sn_display_unref (sn_dpy);
    
    sn_launcher_context_set_name (context, taku_menu_item_get_name (item));
    sn_launcher_context_set_binary_name (context, argv[0]);
    /* TODO: set workspace, steal gedit_utils_get_current_workspace */
    
    sn_launcher_context_initiate (context,
                                  g_get_prgname () ?: "unknown",
                                  argv[0],
                                  gtk_get_current_event_time ());
  }
#endif

  /* TODO: use GAppInfo */
  if (!g_spawn_async (NULL, argv, NULL,
                      G_SPAWN_SEARCH_PATH,
                      child_setup,
#ifdef USE_LIBSN
                      use_sn ? context : NULL,
#else
                      NULL,
#endif
                      NULL,
                      &error)) {
    g_warning ("Cannot launch %s: %s", argv[0], error->message);
    g_error_free (error);
#ifdef USE_LIBSN
    if (context)
      sn_launcher_context_complete (context);
#endif
  }
  
#ifdef USE_LIBSN
  if (use_sn)
    sn_launcher_context_unref (context);
#endif
}