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

/* Compile with "gcc -o bell -lX11 -Wall bell.c" */

/* Set the decorations part of a window's _MOTIF_WM_HINTS property to
 * enable or disable window decorations. */
static void SetMotifDecorationsHint(
    Display* display, Window win, int show_decorations) {
  fprintf(stderr, "Setting hint to %s decorations on 0x%x\n",
          (show_decorations ? "show" : "hide"), (unsigned int) win);
  int values[3] = {
    1 << 1,  /* flags */
    0,       /* functions */
    show_decorations
  };
  Atom atom = XInternAtom(display, "_MOTIF_WM_HINTS", False);
  XChangeProperty(
      display,
      win,
      atom,  /* property */
      atom,  /* type */
      32,    /* format */
      PropModeReplace,
      (unsigned char*) values,
      3);    /* nelements */
}

int main(int argc, char** argv) {
  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 */

  SetMotifDecorationsHint(display, win, 0);

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

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

    if (event.type == ButtonPress) {
      fprintf(stderr, "Sending bell for window 0x%x\n", (unsigned int) win);
      XkbBell(display, win, 0, None);
    }
  }

  return 0;
}
