File: docker.c

package info (click to toggle)
docker 1.5-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 244 kB
  • sloc: ansic: 1,544; makefile: 129
file content (417 lines) | stat: -rw-r--r-- 10,862 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
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
#include "version.h"
#include "kde.h"
#include "icons.h"
#include "docker.h"
#include "net.h"

#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xutil.h>

int argc;
char **argv;

Window win = None, hint_win = None, root = None;
gboolean wmaker = FALSE; /* WindowMakerMode!!! wheeee */
Display *display = NULL;
GSList *icons = NULL;
int width = 0, height = 0;
int border = 1; /* blank area around icons. must be > 0 */
gboolean horizontal = TRUE; /* layout direction */
int icon_size = 24; /* width and height of systray icons */
static const char *colortext = NULL;

static char *display_string = NULL;
/* excluding the border. sum of all child apps */
static gboolean exit_app = FALSE;

void signal_handler(int signal)
{
  switch (signal)
  {
  case SIGSEGV:
  case SIGFPE:
    g_printerr("Segmentation Fault or Critical Error encountered. "
               "Dumping core and aborting.\n");
    abort();
    
  case SIGPIPE:
  case SIGTERM:
  case SIGINT:
  case SIGHUP:
    exit_app = TRUE;
  }
}


void parse_cmd_line()
{
  int i;
  gboolean help = FALSE;
  
  for (i = 1; i < argc; i++) {
    if (0 == strcasecmp(argv[i], "-display")) {
      ++i;
      if (i < argc) {
        display_string = argv[i];
      } else { /* argument doesn't exist */
        g_printerr("-display requires a parameter\n");
        help = TRUE;
      }
    } else if (0 == strcasecmp(argv[i], "-wmaker")) {
      wmaker = TRUE;
    } else if (0 == strcasecmp(argv[i], "-vertical")) {
      horizontal = FALSE;
    } else if (0 == strcasecmp(argv[i], "-border")) {
      ++i;

      if (i < argc) {
        int b = atoi(argv[i]);
        if (b > 0) {
          border = b;
        } else {
          g_printerr("-border must be a value greater than 0\n");
          help = TRUE;
        }
      } else { /* argument doesn't exist */
        g_printerr("-border requires a parameter\n");
        help = TRUE;
      }
    } else if (0 == strcasecmp(argv[i], "-iconsize")) {
      ++i;
      if (i < argc) {
        int s = atoi(argv[i]);
        if (s > 0) {
          icon_size = s;
        } else {
          g_printerr("-iconsize must be a value greater than 0\n");
          help = TRUE;
        }
      } else { /* argument doesn't exist */
        g_printerr("-iconsize requires a parameter\n");
        help = TRUE;
      }
    } else if (0 == strcasecmp(argv[i], "-color")) {
      ++i;

      if (i < argc) {
        colortext = argv[i];
      } else { /* argument doesn't exist */
        g_printerr("-color requires a parameter\n");
        help = TRUE;
      }
    } else {
      if (argv[i][0] == '-')
        help = TRUE;
    }


    if (help) {
      /* show usage help */
      g_print("%s - version %s\n", argv[0], VERSION);
      g_print("Copyright 2003, Ben Jansens <ben@orodu.net>\n\n");
      g_print("Usage: %s [OPTIONS]\n\n", argv[0]);
      g_print("Options:\n");
      g_print("  -help             Show this help.\n");
      g_print("  -display DISPLAY  The X display to connect to.\n");
      g_print("  -border           The width of the border to put around the\n"
              "                    system tray icons. Defaults to 1.\n");
      g_print("  -vertical         Line up the icons vertically. Defaults to\n"
              "                    horizontally.\n");
      g_print("  -wmaker           WindowMaker mode. This makes docker a\n"
              "                    fixed size (64x64) to appear nicely in\n"
              "                    in WindowMaker.\n"
              "                    Note: In this mode, you have a fixed\n"
              "                    number of icons that docker can hold.\n");
      g_print("  -iconsize SIZE    The size (width and height) to display\n"
              "                    icons as in the system tray. Defaults to\n"
              "                    24.\n");
      g_print("  -color COLOR      The background color to use for the tray.\n"
              "                    Defaults to whatever color the window\n"
              "                    manager specifies.\n");
      exit(1);
    }
  }
}


void create_hint_win()
{
  XWMHints hints;
  XClassHint classhints;

  hint_win = XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0);
  assert(hint_win);

  hints.flags = StateHint | WindowGroupHint | IconWindowHint;
  hints.initial_state = WithdrawnState;
  hints.window_group = hint_win;
  hints.icon_window = win;

  classhints.res_name = "docker";
  classhints.res_class = "Docker";

  XSetWMProperties(display, hint_win, NULL, NULL, argv, argc,
                   NULL, &hints, &classhints);

  XMapWindow(display, hint_win);
}


void create_main_window()
{
  XWMHints hints;
  XTextProperty text;
  Colormap colormap;
  XColor xcolor;
  char *name = "Docker";

  /* the border must be > 0 if not in wmaker mode */
  assert(wmaker || border > 0);

  if (!wmaker)
    win = XCreateSimpleWindow(display, root, 0, 0,
                              border * 2, border * 2, 0, 0, 0);
  else
    win = XCreateSimpleWindow(display, root, 0, 0,
                              64, 64, 0, 0, 0);
    
  assert(win);

  XStringListToTextProperty(&name, 1, &text);
  XSetWMName(display, win, &text);

  hints.flags = StateHint;
  hints.initial_state = WithdrawnState;
  XSetWMHints(display, win, &hints);
  
  create_hint_win();
  
  XSync(display, False);

  if (colortext) {
    colormap = DefaultColormap(display, DefaultScreen(display));
    if (!XParseColor(display, colormap, colortext, &xcolor)) {
      g_printerr("Couldn't find color in database: %s\n", colortext);
      exit(1);
    }
    assert(XAllocColor(display, colormap, &xcolor));
    XSetWindowBackground(display, win, xcolor.pixel);
  } else {
    XSetWindowBackgroundPixmap(display, win, ParentRelative);
  }

  XClearWindow(display, win);
}


void reposition_icons()
{
  int x = border + ((width % icon_size) / 2),
      y = border + ((height % icon_size) / 2);
  GSList *it;
  
  for (it = icons; it != NULL; it = g_slist_next(it)) {
    TrayWindow *traywin = it->data;
    traywin->x = x;
    traywin->y = y;
    XMoveWindow(display, traywin->id, x, y);
    XSync(display, False);
    if (wmaker) {
      x += icon_size;
      if (x + icon_size > width) {
        x = border;
        y += icon_size;
      }
    } else if (horizontal)
      x += icon_size;
    else
      y += icon_size;
  }
}


void fix_geometry()
{
  GSList *it;

  /* in wmaker mode we're a fixed size */
  if (wmaker) return;
  
  /* find the proper width and height */
  width = horizontal ? 0 : icon_size;
  height = horizontal ? icon_size : 0;
  for (it = icons; it != NULL; it = g_slist_next(it)) {
    if (horizontal)
      width += icon_size;
    else
      height += icon_size;
  }

  XResizeWindow(display, win, width + border * 2, height + border * 2);
}


gboolean next_event(XEvent *event)
{
  int fd;
  fd_set rfds;

  if (XPending(display)) {
    XNextEvent(display, event);
    return TRUE;
  }

  fd = ConnectionNumber(display);
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);

  if (select(fd + 1, &rfds, NULL, NULL, NULL) == -1) {
    if (errno != EINTR) {
      g_printerr("Error waiting for events (%s)", strerror(errno));
      exit(1);
    }
    return FALSE; /* interrupted by a signal */
  }

  if (!FD_ISSET(fd, &rfds) || !XPending(display))
    return FALSE; /* false positive */

  XNextEvent(display, event);
  return TRUE;
}


void event_loop()
{
  XEvent e;
  Window cover;
  GSList *it;
  
  while (!exit_app) {
    if (next_event(&e)) {
      switch (e.type)
      {
      case PropertyNotify:
        /* systray window list has changed? */
        if (e.xproperty.atom == kde_systray_prop) {
          XSelectInput(display, win, NoEventMask);
          kde_update_icons();
          XSelectInput(display, win, StructureNotifyMask);

          while (XCheckTypedEvent(display, PropertyNotify, &e));
        }

        break;

      case ConfigureNotify:
        if (e.xany.window != win) {
          /* find the icon it pertains to and beat it into submission */
          GSList *it;
  
          for (it = icons; it != NULL; it = g_slist_next(it)) {
            TrayWindow *traywin = it->data;
            if (traywin->id == e.xany.window) {
              XMoveResizeWindow(display, traywin->id, traywin->x, traywin->y,
                                icon_size, icon_size);
              break;
            }
          }
          break;
        }
        
        /* briefly cover the entire containing window, which causes it and
           all of the icons to refresh their windows. finally, they update
           themselves when the background of the main window's parent changes.
        */
        cover = XCreateSimpleWindow(display, win, 0, 0,
                                    border * 2 + width, border * 2 + height,
                                    0, 0, 0);
        XMapWindow(display, cover);
        XDestroyWindow(display, cover);
        
        break;

      case ReparentNotify:
        if (e.xany.window == win) /* reparented to us */
          break;
      case UnmapNotify:
      case DestroyNotify:
        for (it = icons; it; it = g_slist_next(it)) {
          if (((TrayWindow*)it->data)->id == e.xany.window) {
            icon_remove(it);
            break;
          }
        }
        break;

      case ClientMessage:
        if (e.xclient.message_type == net_opcode_atom &&
            e.xclient.format == 32 &&
            e.xclient.window == net_sel_win)
          net_message(&e.xclient);
        
      default:
        break;
      }
    }
  }

  /* remove/unparent all the icons */
  while (icons) {
    /* do the remove here explicitly, cuz the event handler isn't going to
       happen anymore. */
    icon_remove(icons);
  }
}

int main(int c, char **v)
{
  struct sigaction act;
  
  argc = c; argv = v;
  
  act.sa_handler = signal_handler;
  act.sa_flags = 0;
  sigaction(SIGSEGV, &act, NULL);
  sigaction(SIGPIPE, &act, NULL);
  sigaction(SIGFPE, &act, NULL);
  sigaction(SIGTERM, &act, NULL);
  sigaction(SIGINT, &act, NULL);
  sigaction(SIGHUP, &act, NULL);

  parse_cmd_line(argc, argv);

  display = XOpenDisplay(display_string);
  if (!display) {
    g_printerr("Unable to open Display %s. Exiting.\n",
               DisplayString(display_string));
  }

  root = RootWindow(display, DefaultScreen(display));
  assert(root);

  if (wmaker)
    width = height = 64 - border * 2;
  
  create_main_window();

  /* set up to find KDE systray icons, and get any that already exist */
  kde_init();

  net_init();

  /* we want to get ConfigureNotify events, and assume our parent's background
     has changed when we do, so we need to refresh ourself to match */
  XSelectInput(display, win, StructureNotifyMask);
  
  event_loop();

  XCloseDisplay(display);

  return 0;
}