summaryrefslogtreecommitdiff
path: root/iconized.c
blob: e31339e7e93cd3c2387992e9c5e0c2a3f22f17f0 (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
/* Compile with "gcc -o iconized -Wall iconized.c -lX11" */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

/* This exercises some window state transitions per ICCCM 4.1.4 */

/* Run as ./initially_iconized [0|1] to select initial state */
/* Click middle mouse button on window to toggle */

int main(int argc, char** argv)
{
  if (argc != 2 || strcmp(argv[1], "-h") == 0)
    {
      fprintf(stderr, "Usage: %s [iconized]\n", argv[0]);
      return 1;
    }

  Display* display = XOpenDisplay(NULL);
  if (!display) {
    fprintf(stderr, "Couldn't open display\n");
    return 1;
  }
  int screen = DefaultScreen(display);

  const int width = 300, height = 200;

  unsigned long black = BlackPixel(display, screen);
  unsigned long white = WhitePixel(display, screen);

  Window win = XCreateSimpleWindow(display,
                                   RootWindow(display, screen),
                                   0,       /* x */
                                   0,       /* y */
                                   width,
                                   height,
                                   0,       /* border_width */
                                   black,   /* border */
                                   white);  /* background */

  Bool toggle = (strcmp(argv[1], "0") != 0);

  fprintf(stderr, "Setting WM_HINT to %s\n", (toggle ? "iconized" : "normal"));

  XWMHints *hints = XAllocWMHints();
  hints->flags = StateHint;
  hints->initial_state = toggle ? IconicState : NormalState;
  XSetWMHints(display, win, hints);
  XFree(hints);

  XSelectInput(display, win, ButtonPressMask);
  XMapWindow(display, win);

  while (1) {
    XEvent event;
    XNextEvent(display, &event);

    if (event.type == ButtonPress) {
      XButtonEvent be = event.xbutton;
      if (be.button == 2) {
        fprintf(stderr, "XIconifyWindow()\n");
        XIconifyWindow(display, win, screen);
        XFlush(display);

        {
          int i;
          fprintf(stderr, "I'll be back!\n");
          for (i = 10; i > 0; i--)
            {
              sleep(1);
              printf("%d...\n", i);
            }
        }

        fprintf(stderr, "XMapWindow()\n");
        XMapWindow(display, win);
        XFlush(display);
      }
    }
  }

  return 0;
}