aboutsummaryrefslogtreecommitdiffstats
path: root/winspew.c
blob: e795c58aec03f27f002ce89f7c6736a1c6dcfc7b (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
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
/*
  winspew - a EWMH testing tool.

  Copyright 2003 Matthew Allum

  SPDX-License-Identifier: GPL-2.0-or-later
 
*/


#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAX_WINS 32

#define WIN_OPTS_NO_TITLE           (1<<1)
#define WIN_OPTS_STATE_FULLSCREEN   (1<<2)
#define WIN_OPTS_STATE_MODAL        (1<<3)
#define WIN_OPTS_WM_PROTO_ACCEPT    (1<<5)
#define WIN_OPTS_WM_PROTO_HELP      (1<<6)
/* XXX possibles */
#define WIN_OPTS_UTF8_TITLE         (1<<7)
#define WIN_OPTS_NO_DECOR           (1<<8)
#define WIN_OPTS_WM_PROTO_CUSTOM    (1<<9)
#define WIN_OPTS_CM_TRANS           (1<<10)
#define WIN_OPTS_URGENT             (1<<12)
#define WIN_OPTS_STATE_ABOVE        (1<<13)


enum { 				/* Window Atoms */

  WINDOW_TYPE_TOOLBAR = 1,
  WINDOW_TYPE_DOCK,
  WINDOW_TYPE_DIALOG,
  WINDOW_TYPE_SPLASH,
  WINDOW_TYPE_DESKTOP,
  WINDOW_TYPE,    

  WINDOW_STATE,
  WINDOW_STATE_FULLSCREEN,
  WINDOW_STATE_MODAL,
  WINDOW_STATE_ABOVE,
  
  _NET_CLOSE_WINDOW,
  
  _NET_WM_NAME,
  UTF8_STRING,

  _NET_SHOW_DESKTOP,
  _NET_WM_ICON,

  _MOTIF_WM_HINTS,

  WM_PROTOCOLS,
  WM_DELETE_WINDOW,
  _NET_WM_CONTEXT_HELP,
  _NET_WM_CONTEXT_ACCEPT,
  _NET_WM_CONTEXT_CUSTOM,

  _NET_SUPPORTED,

  CM_TRANSLUCENCY,

  ATOM_COUNT
};



static void paint(Window win, int width, int height);

static Display* dpy;	
static Window win_top_level[MAX_WINS], win_child[MAX_WINS], 
  win_current_top_level, win_root;
int win_top_level_idx = 0, win_child_idx = 0;
static int screen;
static Atom atoms[ATOM_COUNT];
static Bool WinIsFullscreen = False;
static Bool WantPattern     = True;
static int  WinTranslucency = -1;

static struct { 
  char *key; int  flag; char *desc;
} Options_lookup[] = {
  { "no_title",       WIN_OPTS_NO_TITLE,
    "Request no titlebar / border only\n"},
  { "no_decor",       WIN_OPTS_NO_DECOR,
    "Request no window decorations\n"},
  { "fullscreen",     WIN_OPTS_STATE_FULLSCREEN ,
    "Request Fullscreen\n"},
  { "modal",          WIN_OPTS_STATE_MODAL,
    "Request dialog be modal\n"},    
  { "help_button",    WIN_OPTS_WM_PROTO_HELP,
  "Request help button in window title bar\n"},
  { "accept_button",  WIN_OPTS_WM_PROTO_ACCEPT,
  "Request 'OK' button in window title bar\n"},  
  { "custom_button",  WIN_OPTS_WM_PROTO_CUSTOM,
  "Request 'custom' button in window title bar\n"},  
  { "urgent", WIN_OPTS_URGENT, "Set Urgency flag on window\n" },
  { "above", WIN_OPTS_STATE_ABOVE, "Set above state hint on window\n" },
  { NULL, 0, NULL }
};

static struct { 
  char *key; unsigned char *data; int len;
} UTF8_Names_lookup[] = {
  { "zh_CN.utf8", "\344\275\240\345\245\275\344\270\255\345\233\275!", 13 },
  { "ar_AE.utf8", "Arabic ( \357\273\262\357\272\221\357\272\256\357\273\213 )", 23 },
  { "mixed.utf8", "Hello from Arabic ( \357\273\262\357\272\221\357\272\256\357\273\213 ) and now from china : \344\275\240\345\245\275\344\270\255\345\233\275! clip this clip this clip this clip this clip this clip this clip this", 139 } ,
  /*   { "utf8-ar", "\357\273\262\357\272\221\357\272\256\357\273\213", 12 }, */

  { NULL, NULL, 0 }
};

void 
wm_check_supported_features(void)
{
  /* 
     XXX This functioniality here could be greatly enhances, though
         Im not sure how useful it would be. 
  */
  Atom type, *atoms_supported = NULL;
  int format;
  long bytes_after;
  long n_items;
  int result;
  unsigned char *atom_str_name;
  int i;
  Bool have_help_support = False, have_accept_support = False;

  result =  XGetWindowProperty (dpy, win_root, atoms[_NET_SUPPORTED],
				0, 1024L,
				False, XA_ATOM,
				&type, &format, &n_items,
				&bytes_after, 
				(unsigned char **)&atoms_supported);

  printf("winspew log: Checking wm features..\n");

  if (result != Success || atoms_supported == NULL)
    {
      if (atoms_supported) XFree (atoms_supported);
      printf("winspew log: *WARNING* looks like wm is not EWMH compliant\n"
	     "             Many features _will_not_ work!");
      return;
    }

  for (i=0; i<n_items; i++)
    {
      atom_str_name = XGetAtomName(dpy, atoms_supported[i]);
      printf("\t %s supported\n", atom_str_name); 
      if (atoms_supported[i] == atoms[_NET_WM_CONTEXT_HELP])
	have_help_support = True;
      if (atoms_supported[i] == atoms[_NET_WM_CONTEXT_ACCEPT])
	have_accept_support = True;
      if (atom_str_name) XFree(atom_str_name);
    }

  if (!have_help_support)
    printf("winspew log: *WARNING* This wm _does_not_ support help buttons\n"); 

  if (!have_accept_support)
    printf("winspew log: *WARNING* This wm _does_not_ support accept buttons\n"); 

  XFree (atoms_supported);
}

void
create_atoms(void)
{
  
  atoms[WINDOW_TYPE_TOOLBAR] 
    = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_TOOLBAR",False);
  atoms[WINDOW_TYPE_DOCK]
    = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DOCK",False);
  atoms[WINDOW_TYPE_DIALOG]
    = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG",False);
  atoms[WINDOW_TYPE_SPLASH]
    = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_SPLASH",False);
  atoms[WINDOW_TYPE_DESKTOP]
    = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DESKTOP",False);
  atoms[WINDOW_TYPE]
    = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);


  atoms[WINDOW_STATE]
    = XInternAtom(dpy, "_NET_WM_STATE",False);
  atoms[WINDOW_STATE_FULLSCREEN]
    = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN",False);
  atoms[WINDOW_STATE_MODAL]
    = XInternAtom(dpy, "_NET_WM_STATE_MODAL",False);

  atoms[WINDOW_STATE_ABOVE]
    = XInternAtom(dpy, "_NET_WM_STATE_ABOVE",False);

  atoms[_NET_WM_NAME]
    = XInternAtom(dpy, "_NET_WM_NAME",False);
  atoms[UTF8_STRING]
    = XInternAtom(dpy, "UTF8_STRING",False);

  atoms[_NET_CLOSE_WINDOW]
    = XInternAtom(dpy, "_NET_CLOSE_WINDOW",False);
  atoms[_NET_SHOW_DESKTOP]
    = XInternAtom(dpy, "_NET_SHOWING_DESKTOP",False);
  atoms[_NET_WM_ICON]
    = XInternAtom(dpy, "_NET_WM_ICON", False);


   atoms[_MOTIF_WM_HINTS]
      = XInternAtom(dpy, "_MOTIF_WM_HINTS", False);

   atoms[WM_PROTOCOLS]
      = XInternAtom(dpy, "WM_PROTOCOLS", False);
   atoms[WM_DELETE_WINDOW]
      = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
   atoms[_NET_WM_CONTEXT_HELP]
      = XInternAtom(dpy, "_NET_WM_CONTEXT_HELP", False);
   atoms[_NET_WM_CONTEXT_ACCEPT]
      = XInternAtom(dpy, "_NET_WM_CONTEXT_ACCEPT", False);
   atoms[_NET_WM_CONTEXT_CUSTOM]
      = XInternAtom(dpy, "_NET_WM_CONTEXT_CUSTOM", False);

   atoms[_NET_SUPPORTED]
      = XInternAtom(dpy, "_NET_SUPPORTED", False);

   atoms[CM_TRANSLUCENCY]
     = XInternAtom(dpy, "CM_TRANSLUCENCY", False);

}


void
usage(char *bin_name, char *error)
{
  int i = 0;
  if (!bin_name) {
    bin_name = "winspew";
  }
  printf("\n %s usage:\n"
	 "%s [ -display <X11 display> ] [ -nopaint] -top <options> [ -dialog <options> ] ...\n\n"
	 "-top, ( -t ) creates a top level window, <options> should consist of;\n\n"
         "   <title>,<window type>[,[geom],[extra opts]]\n\n"
	 "-dialog, ( -c) creates a dialog for the previous top level, <options> are;\n\n"
	 "   <title>[,[geom],[extra opts],]\n\n"

	 "   'window type' is one of normal, dock, toolbar, dialog, splash, desktop, message \n"

         "   'geom' is a X11 window geometry definition, like 100x100+100+100\n"
	 "   'extra opts' is a ':' seperated list of the following flags;\n\n",
	 bin_name, bin_name);

  while( Options_lookup[i].key != NULL )
    {
      printf("       %s - %s", Options_lookup[i].key, Options_lookup[i].desc);
      i++;
    }  

      printf("       trans=XX - Sets window translucency ( if supported ) 'XX'  is 0 - 255." );

  printf("\nNote, you may use as many instances of -top and -dialog as you like.\n"); 

  if (error) {
    printf("\nError: %s\n", error);
  }
  exit(1);
}

void
set_motif_no_title_decoration_hint(Window win, Bool border_only)
{

#define PROP_MOTIF_WM_HINTS_ELEMENTS    5
#define MWM_HINTS_DECORATIONS          (1L << 1)
#define MWM_DECOR_BORDER               (1L << 1)

  typedef struct
  {
    unsigned long       flags;
    unsigned long       functions;
    unsigned long       decorations;
    long                inputMode;
    unsigned long       status;
  } PropMotifWmHints;

  PropMotifWmHints *hints;

  hints = malloc(sizeof(PropMotifWmHints));
  memset(hints, 0, sizeof(PropMotifWmHints));

  hints->flags = MWM_HINTS_DECORATIONS;

  if (border_only)
    hints->decorations = MWM_DECOR_BORDER;
  else
    hints->decorations = 0;

  XChangeProperty(dpy, win, atoms[_MOTIF_WM_HINTS], 
		  XA_ATOM, 32, PropModeReplace, 
		  (unsigned char *)hints, PROP_MOTIF_WM_HINTS_ELEMENTS);

  free(hints);

  return;

}


int
parse_options(const char *options_spec)
{
  char *p = NULL;
  int   i = 0, result_flags = 0;
  Bool  have_trans = False;

  printf("parsing %s\n", options_spec);

  while( Options_lookup[i].key != NULL )
    {
      /* Grab translucent key/val if available */
      if (!have_trans && (p = strstr(options_spec, "trans=")) != NULL)
	{
	  char *val = NULL;
	  val = strdup(p+6); p = val;
	  while (*p != ',' && *p != '\0') p++;
	  *p = '\0';
	  have_trans = True;
	  result_flags |= WIN_OPTS_CM_TRANS;
	  WinTranslucency = atoi(val);
	  free(val);
	}

      if (strstr(options_spec, Options_lookup[i].key) != NULL)
	{
	  printf("parsed %s\n", Options_lookup[i].key);
	  result_flags |= Options_lookup[i].flag;
	}
      i++;
    }

  return result_flags;
}

void
win_set_utf8_name(Window         win, 
		  unsigned char *data, 
		  int            data_len)
{
  XChangeProperty(dpy, win, atoms[_NET_WM_NAME], 
		  atoms[UTF8_STRING], 8, PropModeReplace, 
		  data, data_len);
}

void
win_set_tranlucency(Window         win, 
		    int            level)
{
  XChangeProperty(dpy, win, atoms[CM_TRANSLUCENCY], 
		  XA_INTEGER, 32, PropModeReplace, 
		  (unsigned char *) &level, 1L);
}

void
win_set_standard_props(Window win, char *win_title, int x, int y, int h, int w)
{
  XSizeHints size_hints;

  size_hints.flags = PPosition | PSize | PMinSize;
  size_hints.x = x;
  size_hints.y = y;
  size_hints.width      =  h;
  size_hints.height     =  w;
  size_hints.min_width  =  w;
  size_hints.min_height =  h;
  
  XSetStandardProperties(dpy, win, 
			 win_title, win_title, None,
			 NULL, 0, &size_hints);
}

void
win_set_ewmh_type(Window win, Atom atom_type)
{
  printf("winspew log: Setting type\n");
  XChangeProperty(dpy, win, atoms[WINDOW_TYPE], XA_ATOM, 32, 
		  PropModeReplace, (unsigned char *) &atom_type, 1);
}

void
win_set_extened_options(Window win, int option_flags, int message_timeout)
{
  Atom wm_protocols[3], wm_states[3];
  int wm_protocols_idx = 1, wm_states_idx = 0;

  wm_protocols[0] = atoms[WM_DELETE_WINDOW];

  if (option_flags & WIN_OPTS_NO_TITLE)
    {
      printf("winspew log: Setting no decor hint\n"); 
      set_motif_no_title_decoration_hint(win, True);
    }

  if (option_flags & WIN_OPTS_NO_DECOR)
    {
      printf("winspew log: Setting no decor hint\n"); 
      set_motif_no_title_decoration_hint(win, False);
    }

  
  if (option_flags & WIN_OPTS_STATE_FULLSCREEN)
    {
      printf("winspew log: Setting fullscreen hint\n"); 
      wm_states[wm_states_idx] = atoms[WINDOW_STATE_FULLSCREEN];
      wm_states_idx++;
      WinIsFullscreen = True;
    }

  if (option_flags & WIN_OPTS_CM_TRANS)
    {
      printf("winspew log: Setting window translucency to %i\n", 
	     WinTranslucency); 

      win_set_tranlucency(win, WinTranslucency); 
    }

  if (option_flags & WIN_OPTS_STATE_MODAL)
    {
      printf("winspew log: Setting modal hint\n"); 
      wm_states[wm_states_idx] = atoms[WINDOW_STATE_MODAL];
      wm_states_idx++;
    }

  if (option_flags & WIN_OPTS_STATE_ABOVE)
    {
      printf("winspew log: Setting state above hint\n");
      wm_states[wm_states_idx] = atoms[WINDOW_STATE_ABOVE];
      wm_states_idx++;
    }

  if (wm_states_idx)
    {
      XChangeProperty(dpy, win,
                      atoms[WINDOW_STATE], XA_ATOM, 32,
                      PropModeReplace,
                      (unsigned char *)wm_states, wm_states_idx);
    }

  if (option_flags & WIN_OPTS_URGENT)
    {
      XWMHints *wmhints;

      wmhints = XAllocWMHints();

      wmhints->flags = XUrgencyHint;

      printf("winspew log: Setting urgency hint\n");

      XSetWMHints(dpy, win, wmhints);

      XFree(wmhints);
    }

  if (option_flags & WIN_OPTS_WM_PROTO_ACCEPT)
    {
      printf("winspew log: Setting accept protocol\n"); 
      wm_protocols[wm_protocols_idx] = atoms[_NET_WM_CONTEXT_ACCEPT];
      wm_protocols_idx++;
    }
  
  if (option_flags & WIN_OPTS_WM_PROTO_HELP)
    {
      printf("winspew log: Setting help protocol\n"); 
      wm_protocols[wm_protocols_idx] = atoms[_NET_WM_CONTEXT_HELP];
      wm_protocols_idx++;
    }

  if (option_flags & WIN_OPTS_WM_PROTO_CUSTOM)
    {
      printf("winspew log: Setting help protocol\n"); 
      wm_protocols[wm_protocols_idx] = atoms[_NET_WM_CONTEXT_CUSTOM];
      wm_protocols_idx++;
    }
  
  if (wm_protocols_idx)
    XSetWMProtocols(dpy, win, wm_protocols, wm_protocols_idx );

}


void
win_realize(Window win)
{
  XSelectInput(dpy, win,
	       KeyPressMask		
	       |KeyReleaseMask		
	       |ButtonPressMask
	       |ButtonReleaseMask	
	       |EnterWindowMask
	       |LeaveWindowMask
	       |PointerMotionMask	
	       |PointerMotionHintMask	
	       |Button1MotionMask	
	       |Button2MotionMask	
	       |Button3MotionMask	
	       |Button4MotionMask	
	       |Button5MotionMask	
	       |ButtonMotionMask	
	       |KeymapStateMask
	       |ExposureMask		
	       |VisibilityChangeMask	
	       |StructureNotifyMask	
	       |SubstructureNotifyMask	
	       |FocusChangeMask	
	       |PropertyChangeMask	
	       |ColormapChangeMask	
	       |OwnerGrabButtonMask	
	       ); 		/* XXX really need all this ? */
     
  XMapWindow(dpy, win);
}

void
win_destroy(Window win) 	
{
  Window win_tmp;
  int i;

  for (i=0; i<win_child_idx; i++) /* destroy any transient dialogs */
    {
      if (win_child[i] != None 
	  && XGetTransientForHint(dpy, win_child[i], &win_tmp)
	  && win_tmp == win)
	{
	  XDestroyWindow(dpy, win_child[i]);
	  win_child[i] = None;
	}
    }

  XDestroyWindow(dpy,win);

  /* Update window arrays */

  for (i=0; i<win_top_level_idx; i++)
    if (win_top_level[i] == win)
      {
	win_top_level[i] = None;
	return;
      }

  for (i=0; i<win_child_idx; i++)
    if (win_child[i] == win)
      win_child[i] = None;

}

void
create_top_level(int argc, char **argv, char *spec)
{
  char *str;
  const char delim[] = ",";
  char *tmp;
  int i;

  char *win_title;
  int win_type = 0, win_x = 0, win_y = 0, win_w = 100, win_h = 50;

  int option_flags    = 0;
  int message_timeout = 0;
  int geom_bitmask    = 0;

  struct {
    char *name;
    int id;
  } win_types[] = {
    {"normal",  0},
    {"dock",    WINDOW_TYPE_DOCK},
    {"dialog",  WINDOW_TYPE_DIALOG},
    {"splash",  WINDOW_TYPE_SPLASH},
    {"toolbar", WINDOW_TYPE_TOOLBAR},
    {"desktop", WINDOW_TYPE_DESKTOP},
    {NULL, 0}
  };

  if ((strchr(spec, delim[0]) != NULL))
  {
     str = strdup(spec);

     if ((win_title = strsep (&str, delim)) == NULL)
       usage(NULL, "top title missing");

     if ((tmp = strsep (&str, delim)) == NULL)
       usage(NULL, "top type missing");
     
     if (atoi(tmp) > 0)
       win_type = atoi(tmp);
     else
       {
	 Bool found = False;
	 i = 0;
	 while(win_types[i].name != NULL)
	   {
	     if (!strcmp(tmp, win_types[i].name))
	       {
		 win_type =  win_types[i].id;
		 found = True;
		 break;
	       }
	     i++;
	   }
       }
     
     /* Rest are optional */

     if ((tmp = strsep (&str, delim)) != NULL)
       {
	 geom_bitmask = XParseGeometry(tmp, &win_x, &win_y, &win_w, &win_h);

	 if (!win_w) win_w = 100;
	 if (!win_h) win_h = 100;

	 /* XXX see code for dialogs
	 if (!(geom_bitmask & XNegative))
	   {
	     win_x = (-1 * DisplayWidth(dpy, screen)) + win_x;
	   }

	 if (!(geom_bitmask & YNegative)) 
	   {
	     win_y = (-1 * DisplayHeight(dpy, screen)) + win_y;
	   }
	 */
       }

     if ((tmp = strsep (&str, delim)) != NULL)
       {
	 option_flags = parse_options(tmp);
       }

     if ((tmp = strsep (&str, delim)) != NULL)
       {
	 /* This is actually now ignored */
	 message_timeout = atoi(tmp);
	 fprintf(stderr, "*** Message timeout depreciated ***\n");
       }

     free(str);

     /* Now create actual window */

     printf("winspew log: creating top level x:%i, y:%i, w:%i, h:%i\n",
	    win_x, win_y, win_w, win_h);

     win_top_level[win_top_level_idx] 
       = XCreateSimpleWindow(dpy, win_root, win_x, win_y,
			     win_w, win_h, 0,
			     BlackPixel(dpy, screen),
			     WhitePixel(dpy, screen));

     printf("winspew log: window id is %li\n", 
	    win_top_level[win_top_level_idx]);

     win_current_top_level = win_top_level[win_top_level_idx];

     win_set_standard_props(win_top_level[win_top_level_idx], win_title,
			    win_x, win_y, win_h, win_w);

     if (win_type) 		/* something other than a normal window */
       win_set_ewmh_type(win_top_level[win_top_level_idx], atoms[win_type]);

     win_set_extened_options(win_top_level[win_top_level_idx], 
			     option_flags, message_timeout);

     /* Set utf8 name from lookup */
     i = 0;
     while( UTF8_Names_lookup[i].key != NULL )
       {
	 if (!strcmp(win_title, UTF8_Names_lookup[i].key))
	   {
	     win_set_utf8_name(win_top_level[win_top_level_idx],
			       UTF8_Names_lookup[i].data, 
			       UTF8_Names_lookup[i].len);
	   }
	 i++;
       }

     win_realize(win_top_level[win_top_level_idx]);


      win_top_level_idx++;
     
  } else {
    /* Failed */
    usage("winspew", "top options missing");
  }
}

void 				/* XXX this should be merged into above */
create_child_dialog(int argc, char **argv, char *spec)
{
  char *str, *str_start;
  const char delim[] = ",";
  char *tmp;

  char *win_title;
  int win_x = 0, win_y = 0, win_w = 100, win_h = 50;
  int option_flags = 0, geom_bitmask = 0;

  if (spec == NULL) usage("winspew", "child options missing");

  if ((strchr(spec, delim[0]) != NULL))
  {
     str_start = str = strdup(spec);

     if ((win_title = strsep (&str, delim)) == NULL)
       usage(NULL, "child title missing");

     /* Rest are optional */

     if ((tmp = strsep (&str, delim)) != NULL)
       {
	 geom_bitmask = XParseGeometry(tmp, &win_x, &win_y, &win_w, &win_h);

	 if (!win_w) win_w = 100;
	 if (!win_h) win_h = 100;

	 /* XXX we need to do gravitys for the below to work correctly. 
	 if (!(geom_bitmask & XNegative))
	   {
	     win_x = (-1 * DisplayWidth(dpy, screen)) - win_x - win_w;
	   }

	 if (!(geom_bitmask & YNegative)) 
	   {
	     win_y = (-1 * DisplayHeight(dpy, screen)) - win_y - win_h;
	   }
	 */
       }

     if ((tmp = strsep (&str, delim)) != NULL)
       {
	 option_flags = parse_options(tmp);
       }

     free(str_start);
  } 
  else win_title = spec;

  /* Now create actual window */

  printf("winspew log: creating dialog x:%i, y:%i, w:%i, h:%i\n",
	 win_x, win_y, win_w, win_h);

  win_child[win_child_idx] 
    = XCreateSimpleWindow(dpy, win_root, win_x, win_y,
			  win_w, win_h, 0,
			  BlackPixel(dpy, screen),
			  WhitePixel(dpy, screen));

  printf("winspew log: window id is %li\n", 
	 win_child[win_child_idx]);

  XSetTransientForHint(dpy, win_child[win_child_idx], 
		       win_current_top_level);

  win_set_standard_props(win_child[win_child_idx], win_title,
			 win_x, win_y, win_h, win_w);

  win_set_extened_options(win_child[win_child_idx], option_flags, 0);
  
  win_realize(win_child[win_child_idx]);

  win_child_idx++;

}

static void  /* Fill the window with a simple pattern  */
paint(Window win, int width, int height)
{
  XWindowAttributes win_attr;
  XGCValues gc_values;
  GC gc;
  int x, y;
  Pixmap pxm_backing;

  XGetWindowAttributes(dpy, win, &win_attr);

  gc_values.graphics_exposures = False;
  gc_values.function           = GXcopy;
  gc_values.background         = WhitePixel(dpy, DefaultScreen(dpy));
  gc = XCreateGC(dpy, win, GCGraphicsExposures|GCFunction|GCForeground, 
		 &gc_values);

  pxm_backing = XCreatePixmap(dpy, win, width, height, 
			      win_attr.depth);

  XSetForeground(dpy, gc, WhitePixel(dpy, DefaultScreen(dpy)));
  XFillRectangle(dpy, pxm_backing, gc, 0, 0, width, height);

  XSetForeground(dpy, gc, BlackPixel(dpy, DefaultScreen(dpy)));

  /* Quick hack for a window pattern, using window ID  */
  if (win % 2)
    {
      for (y = 0; y < height; y += win % 12)
	XDrawLine(dpy, pxm_backing, gc, 0, y, width, y);
    } else {
      for (x = 0; x < width; x += win % 12)
	    XDrawLine(dpy, pxm_backing, gc, x, 0, x, height);
    }
  
  XSetWindowBackgroundPixmap(dpy, win, pxm_backing);
   

  XClearWindow(dpy, win);

  XFreePixmap(dpy, pxm_backing);
  XFreeGC(dpy, gc);
} 

void
toggle_ewmh_fullscreen (Window win_to_toggle)
{

#define _NET_WM_STATE_TOGGLE        2    /* toggle property  */

  XEvent ev;
  Atom atom_win_state, atom_win_state_fullscreen;

  atom_win_state = XInternAtom(dpy, "_NET_WM_STATE",False);
  atom_win_state_fullscreen = XInternAtom(dpy,
                                          "_NET_WM_STATE_FULLSCREEN",False);

  printf("winspew-log: attempting toggle on %li\n", win_to_toggle);

  memset(&ev, 0, sizeof(ev));
  ev.xclient.type = ClientMessage;
  ev.xclient.window = win_to_toggle;
  ev.xclient.message_type = atom_win_state;
  ev.xclient.format = 32;
  ev.xclient.data.l[1] = atom_win_state_fullscreen;
  ev.xclient.data.l[0] = _NET_WM_STATE_TOGGLE;

  XSendEvent(dpy, win_root, False, SubstructureRedirectMask , &ev);

  XSync(dpy, False);
}


int 
main(int argc, char **argv)
{
  int i, j = 1;
  char *dpy_name = NULL;
  XEvent xevent;

  for (i = 1; i < argc; i++) {
    if (!strcmp ("-display", argv[i]) || !strcmp ("-d", argv[i])) {
      if (++i>=argc) usage (argv[0], "display missing");
      dpy_name = argv[i++];
      j++;
      continue;
    }
    if (!strcmp ("--nopattern", argv[i]) || !strcmp ("-np", argv[i])) 
      {
	j++;
	WantPattern = False;
	continue;
      }
  }

  if ((argc - j) == 0) 
    usage(argv[0], "no options");

  if ((dpy = XOpenDisplay(dpy_name)) == NULL) {
    fprintf(stderr, "Cannot connect to X server on display %s.",
	    dpy_name);
    exit(1);
  }
  
  screen   = DefaultScreen(dpy);
  win_root = DefaultRootWindow(dpy);

  create_atoms();

  wm_check_supported_features();

  win_current_top_level = win_root;

  for (i = j; i < argc; i++) {
    if (!strcmp ("-top", argv[i]) || !strcmp ("-t", argv[i])) {
      if (++i>=argc) usage (argv[0], "no -top options");
      create_top_level(argc, argv, argv[i]);
      continue;
    }
    if (!strcmp ("-dialog", argv[i]) || !strcmp ("-c", argv[i])) {
      if (++i>=argc) usage (argv[0], "no -child options");
      create_child_dialog(argc, argv, argv[i]);
      continue;
    }
    usage(argv[0], argv[i]);
  }

  while(1)
    {
      char *tmp;
      fflush(stdout);
      XNextEvent(dpy, &xevent);
      switch (xevent.type) 
	{
	case ButtonPress:
	  printf("winspew log: %li, Button press event\n", 
		 xevent.xbutton.window);
	  if (WinIsFullscreen) 
	    toggle_ewmh_fullscreen (xevent.xbutton.window);
	  // XLowerWindow(dpy, win_top_level[0]);
	  break;
	case Expose:
	  printf("winspew log: %li, Expose event\n", 
		 xevent.xexpose.window);
	  /*
	  paint(xevent.xexpose.window, xevent.xexpose.width, 
		xevent.xexpose.height);
	  */
	  break;
	case MapNotify:
	  printf("winspew log: %li, MapNotify event\n", 
		 xevent.xmap.window);
	  break;
	case UnmapNotify:
	  printf("winspew log: %li, UnmapNotify event\n", 
		 xevent.xunmap.window);
	  break;
	case ReparentNotify:
	  printf("winspew log: %li, ReparentNotify\n", 
		 xevent.xreparent.window);
	  break;
	  
	case ConfigureNotify:
	  printf("winspew log: %li, ConfigureNotify event\n", 
		 xevent.xconfigure.window);
	  /* XXX compress configure notifys ? */
	  if (WantPattern)
	    paint(xevent.xconfigure.window, xevent.xconfigure.width, 
		  xevent.xconfigure.height);
	  break;
	case ClientMessage:
	  printf("winspew log: %li, ClientMessage event\n", 
		 xevent.xclient.window);
	  if ((xevent.xclient.message_type == atoms[WM_PROTOCOLS])
	      && (xevent.xclient.data.l[0] == atoms[WM_DELETE_WINDOW])) 
	    {
	      printf("\tis WM_DELETE, deleting...\n");
	      win_destroy(xevent.xclient.window);
	    }
	  break;
	case KeyPress:
	  break;
	case FocusIn:
	  printf("winspew log: %li, FocusIn event\n", 
		 xevent.xfocus.window);
	  break;
	case FocusOut:
	  printf("winspew log: %li, FocusOut event\n", 
		 xevent.xfocus.window);
	  break;
	case PropertyNotify:
	  tmp = XGetAtomName(dpy, xevent.xproperty.atom);
	  printf("winspew log: %li, PropertyNotify event. Atom '%s' ( ID: %li )\n", 
		 xevent.xproperty.window, tmp, xevent.xproperty.atom);
	  if (tmp) XFree(tmp);
	  break;
	default:
	  break;
	}
    }
  
}