File: launchermodule.c

package info (click to toggle)
amiwm 0.22pl2-5
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 1,048 kB
  • sloc: ansic: 10,195; perl: 443; makefile: 258; yacc: 245; lex: 215; sh: 211
file content (251 lines) | stat: -rw-r--r-- 5,495 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include "libami.h"
#include "drawinfo.h"

char *progname;

Display *dpy;
XContext launchercontext;
struct DrawInfo dri;
Atom appiconmsg;
char *icondir;

struct launcher {
  struct ColorStore colorstore1, colorstore2;
  char cmdline[1];
};

#ifdef AMIGAOS
void spawn(const char *cmd)
{
  char *line=malloc(strlen(cmd)+12);
  if(line) {
    sprintf(line, "RUN <>NIL: %s", cmd);
    system(line);
    free(line);
  }
}
#else
void spawn(const char *cmd)
{
#ifdef HAVE_ALLOCA
  char *line=alloca(strlen(cmd)+4);
#else
  char *line=malloc(strlen(cmd)+4);
  if(line) {
#endif
  sprintf(line, "%s &", cmd);
#ifdef __ultrix
  {
    int pid, status;
    if ((pid = fork ()) == 0) {
      (void) setsid();
      execl ("/bin/sh", "sh", "-c", line, 0);
    } else
      waitpid (pid, &status, 0);
  }
#else
  (void)! system(line);
#endif
#ifndef HAVE_ALLOCA
    free(line);
  }
#endif
}
#endif

static void broker_cb(XEvent *evt, unsigned long mask)
{
  XPointer ret;

  if(evt->type != ClientMessage || evt->xclient.message_type != appiconmsg)
    return;

  if(!XFindContext(dpy, evt->xclient.window, launchercontext, &ret)) {
    struct launcher *l = (struct launcher*)ret;
    spawn(l->cmdline);
  }
}

static void create_broker()
{
  cx_broker(0, broker_cb);
}

static void create_launcher(char *label, char *icon, char *cmdline)
{
  struct DiskObject *icon_do = NULL;
  Pixmap icon_icon1, icon_icon2;
  Window win;

  struct launcher *l = malloc(sizeof(struct launcher)+strlen(cmdline));
  if (l == NULL) {
    fprintf(stderr, "%s: out of memory!\n", progname);
    exit(1);
  }

  memset(l, 0, sizeof(*l));
  strcpy(l->cmdline, cmdline);

  if (icon != NULL && *icon != 0) {
#ifdef AMIGAOS
    char fn[256];
    strncpy(fn, icondir, sizeof(fn)-1);
    fn[sizeof(fn)-1]='\0';
    AddPart(fn,icon,sizeof(fn));
#else
    int rl=strlen(icon)+strlen(icondir)+2;
#ifdef HAVE_ALLOCA
    char *fn=alloca(rl);
#else
    char fn[1024];
#endif
    sprintf(fn, "%s/%s", icondir, icon);
#endif
    fn[strlen(fn)-5]=0;
    icon_do = GetDiskObject(fn);
  }

  if(icon_do == NULL)
    icon_do = GetDefDiskObject(3/*WBTOOL*/);

  if(icon_do == NULL) {
    md_errormsg(md_root, "Failed to load icon");
    md_exit(0);
  }

  icon_icon1 =
    md_image_to_pixmap(md_root, dri.dri_Pens[BACKGROUNDPEN],
		       (struct Image *)icon_do->do_Gadget.GadgetRender,
		       icon_do->do_Gadget.Width, icon_do->do_Gadget.Height,
		       &l->colorstore1);

  icon_icon2 =
    md_image_to_pixmap(md_root, dri.dri_Pens[BACKGROUNDPEN],
		       (struct Image *)icon_do->do_Gadget.SelectRender,
		       icon_do->do_Gadget.Width, icon_do->do_Gadget.Height,
		       &l->colorstore2);

  FreeDiskObject(icon_do);
  XSync(dpy, False);
  win = md_create_appicon(md_root, 0x80000000, 0x80000000,
			  label, icon_icon1, icon_icon2, None);
  XSaveContext(dpy, win, launchercontext, (XPointer)l);
}

static int skip_ws(char *arg, int len)
{
  int n = 0;
  while(len > 0 && isspace(*(unsigned char *)arg)) { n++; arg++; --len; }
  return n;
}

static int get_string(char *arg, int len, int lineno)
{
  int n;
  if (len < 1 || *arg != '(') {
    fprintf(stderr, "%s: missing '(' on line %d\n", progname, lineno);
    return 0;
  }
  for (n=1; n<len; n++)
    if (arg[n] == ')')
      break;
  if (n >= len) {
    fprintf(stderr, "%s: missing ')' on line %d\n", progname, lineno);
    return 0;
  }
  return n+1;
}

static char *create_string(char *arg, int l)
{
  char *p = malloc(l+1);
  if (!p) {
    fprintf(stderr, "%s: out of memory!\n", progname);
    exit(1);
  }
  if(l>0) memcpy(p, arg, l);
  p[l] = 0;
  return p;
}

static void create_appicons_line(char *arg, int len, int lineno)
{
  int l;
  char *label, *icon, *cmdline;
  if (*arg == '#')
    return;
  l = skip_ws(arg, len);
  arg += l; len -= l;
  if (!len) return;
  l = get_string(arg, len, lineno);
  if (!l) return;
  label = create_string(arg+1, l-2);
  arg += l; len -= l;
  l = skip_ws(arg, len);
  arg += l; len -= l;
  l = get_string(arg, len, lineno);
  if (!l) { free(label); return; }
  icon = create_string(arg+1, l-2);
  arg += l; len -= l;
  l = skip_ws(arg, len);
  arg += l; len -= l;
  l = get_string(arg, len, lineno);
  if (!l) { free(icon); free(label); return; }
  cmdline = create_string(arg+1, l-2);
  arg += l; len -= l;
  l = skip_ws(arg, len);
  arg += l; len -= l;
  if (len) fprintf(stderr, "%s: junk at end of line %d\n", progname, lineno);
  create_launcher(label, icon, cmdline);
  free(cmdline);
  free(icon);
  free(label);
}

static void create_appicons(char *arg)
{
  char *p;
  int l = 1;
  while((p = strchr(arg, '\n'))) {
    create_appicons_line(arg, p-arg, l++);
    arg = p+1;
  }
  create_appicons_line(arg, strlen(arg), l);
}

static void setup()
{
  XWindowAttributes attr;
  launchercontext = XUniqueContext();
  XGetWindowAttributes(dpy, md_root, &attr);
  init_dri(&dri, dpy, md_root, attr.colormap, False);
  appiconmsg = XInternAtom(dpy, "AMIWM_APPICONMSG", False);
  icondir = get_current_icondir();
  if(!icondir) icondir="";
}

int main(int argc, char *argv[])
{
  char *arg=md_init(argc, argv);
  progname=argv[0];

  if(!(dpy = md_display())) {
    fprintf(stderr, "%s: cannot connect to X server %s\n", progname,
	    XDisplayName(NULL));
    exit(1);
  }
  if(arg) {
    setup();
    create_broker();
    create_appicons(arg);
    md_main_loop();
  }
  return 0;
}