aboutsummaryrefslogtreecommitdiffstats
path: root/applets/launcher.c
blob: aa5912b1ac7d45679ea701f479de9c2a2ee650b0 (plain)
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
/* 
 * (C) 2006 OpenedHand Ltd.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkbutton.h>
#include <gdk/gdkx.h>
#include <string.h>
#include <unistd.h>
#include <matchbox-panel/mb-panel.h>

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

typedef struct {
        gboolean use_sn;

        char *name;
        char **argv;
} LauncherData;

static void
launcher_data_free (LauncherData *data)
{
        g_free (data->name);
        g_strfreev (data->argv);

        g_slice_free (LauncherData, data);
}

#define MAX_ARGS 255

/* Convert command line to argv array, stripping % conversions on the way */
static 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;
}

/* Button clicked */
static void
clicked_cb (GtkButton    *button,
            LauncherData *data)
{
        pid_t child_pid = 0;
#ifdef USE_LIBSN
        SnLauncherContext *context;

        context = NULL;

        if (data->use_sn) {
                SnDisplay *sn_dpy;
                Display *display;
                int screen;

                display = gdk_x11_display_get_xdisplay
                                (gtk_widget_get_display (GTK_WIDGET (button)));
                sn_dpy = sn_display_new (display, NULL, NULL);

                screen = gdk_screen_get_number
                                (gtk_widget_get_screen (GTK_WIDGET (button)));
                context = sn_launcher_context_new (sn_dpy, screen);
                sn_display_unref (sn_dpy);
          
                sn_launcher_context_set_name (context, data->name);
                sn_launcher_context_set_binary_name (context,
                                                     data->argv[0]);
          
                sn_launcher_context_initiate (context,
                                              "matchbox-panel",
                                              data->argv[0],
                                              CurrentTime);
        }
#endif

        switch ((child_pid = fork ())) {
        case -1:
                g_warning ("Fork failed");
                break;
        case 0:
#ifdef USE_LIBSN
                if (data->use_sn)
                        sn_launcher_context_setup_child_process (context);
#endif
                execvp (data->argv[0], data->argv);

                g_warning ("Failed to execvp() %s", data->argv[0]);
                _exit (1);

                break;
        }

#ifdef USE_LIBSN
        if (data->use_sn)
                sn_launcher_context_unref (context);
#endif
}

G_MODULE_EXPORT GtkWidget *
mb_panel_applet_create (const char *id,
                        int         panel_width,
                        int         panel_height)
{
        char *filename;
        GKeyFile *key_file;
        GtkWidget *button, *image;
        GError *error;
        char *icon, *exec, *name;
        gboolean use_sn;
        int shortest_side;
        LauncherData *data;
        
        /* Try to find a .desktop file for @id */
        key_file = g_key_file_new ();

        filename = g_strdup_printf ("applications/%s.desktop", id);
        error = NULL;
        if (!g_key_file_load_from_data_dirs (key_file,
                                             filename,
                                             NULL,
                                             G_KEY_FILE_NONE,
                                             &error)) {
                /* An error occured */
                g_warning ("%s", error->message);
                
                g_error_free (error);

                g_free (filename);
                g_key_file_free (key_file);

                return NULL;
        }

        g_free (filename);

        /* Found and opened keyfile. Read the values we want. */

        /* Icon */
        error = NULL;
        icon = g_key_file_get_string (key_file,
                                      "Desktop Entry",
                                      "Icon",
                                      &error);
        if (!icon) {
                /* An error occured */
                g_warning ("%s", error->message);

                g_error_free (error);

                g_key_file_free (key_file);

                return NULL;
        }

        icon = g_strstrip (icon);

        /* Exec */
        error = NULL;
        exec = g_key_file_get_string (key_file,
                                      "Desktop Entry",
                                      "Exec",
                                      &error);
        if (!exec) {
                /* An error occured */
                g_warning ("%s", error->message);
                
                g_error_free (error);

                g_free (icon);
                g_key_file_free (key_file);

                return NULL;
        }

        exec = g_strstrip (exec);

        /* Name */
        name = g_key_file_get_string (key_file,
                                      "Desktop Entry",
                                      "Name",
                                      NULL);

        /* StartupNotify */
        use_sn = g_key_file_get_boolean (key_file,
                                         "Desktop Entry",
                                         "StartupNotify",
                                         NULL);
        
        /* Close key file */
        g_key_file_free (key_file);

        /* Create image widget */
        image = gtk_image_new ();

        /* Load icon */
        shortest_side = MIN (panel_width, panel_height);

        if (icon[0] == '/') {
                /* Absolute path specified: load icon directly */
                GdkPixbuf *pixbuf;

                error = NULL;
                pixbuf = gdk_pixbuf_new_from_file_at_scale (icon,
                                                            shortest_side,
                                                            shortest_side,
                                                            TRUE,
                                                            &error);
                if (!pixbuf) {
                        /* An error occured */
                        g_warning ("%s", error->message);

                        g_error_free (error);
                        
                        g_free (icon);
                        g_free (exec);
                        g_free (name);
                        gtk_widget_destroy (image);

                        return NULL;
                }

                gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
                g_object_unref (pixbuf);
        } else {
                /* No absolute path given. Load icon from icon theme */
                char *p;

                /* Strip extension, so that we can feed the filename to
                 * GTK+'s icon name handling. */
                p = strrchr (icon, '.');
                if (p)
                        *p = 0;

                gtk_image_set_pixel_size (GTK_IMAGE (image), shortest_side);
                gtk_image_set_from_icon_name (GTK_IMAGE (image),
                                              icon,
                                              0);
        }
        
        g_free (icon);

        /* Create button */
        button = gtk_button_new ();

        gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
        GTK_WIDGET_UNSET_FLAGS (button, GTK_CAN_FOCUS);

        /* Strip %-conversions from exec */

        /* Connect to "clicked" signal */
        data = g_slice_new (LauncherData);

        data->use_sn = use_sn;

        data->name = name;

        data->argv = exec_to_argv (exec);
        g_free (exec);
        
        g_signal_connect_data (button,
                               "clicked",
                               G_CALLBACK (clicked_cb),
                               data,
                               (GClosureNotify) launcher_data_free,
                               0);

        /* Add image to button */
        gtk_container_add (GTK_CONTAINER (button), image);

        /* Show! */
        gtk_widget_show_all (button);

        return button;
};