aboutsummaryrefslogtreecommitdiff
path: root/error.c
blob: 694f0805a8455593c778a6f144f0b26074d224b5 (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
/* Copyright (c) 1994-1996 David Hogan, see README for licence details */
#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include "dat.h"
#include "fns.h"

int ignore_badwindow;

void fatal(char* s) {
  fprintf(stderr, "ryudo: ");
  perror(s);
  fprintf(stderr, "\n");
  exit(1);
}

int handler(Display* d, XErrorEvent* e) {
  char msg[80], req[80], number[80];

  if (
    initting && (e->request_code == X_ChangeWindowAttributes) &&
    (e->error_code == BadAccess)) {
    fprintf(
      stderr,
      "ryudo: it looks like there's already a window manager running;  ryudo "
      "not started\n");
    exit(1);
  }

  if (
    ignore_badwindow &&
    (e->error_code == BadWindow || e->error_code == BadColor))
    return 0;

  XGetErrorText(d, e->error_code, msg, sizeof(msg));
  sprintf(number, "%d", e->request_code);
  XGetErrorDatabaseText(d, "XRequest", number, "", req, sizeof(req));
  if (req[0] == '\0')
    sprintf(req, "<request-code-%d>", (int)e->request_code);

  fprintf(stderr, "ryudo: %s(0x%x): %s\n", req, (int)e->resourceid, msg);

  if (initting) {
    fprintf(stderr, "ryudo: failure during initialisation; aborting\n");
    exit(1);
  }
  return 0;
}

void graberror(char* f, int err) {
#ifdef DEBUG /* sick of "bug" reports; grab errors "just happen" */
  char* s;

  switch (err) {
    case GrabNotViewable:
      s = "not viewable";
      break;
    case AlreadyGrabbed:
      s = "already grabbed";
      break;
    case GrabFrozen:
      s = "grab frozen";
      break;
    case GrabInvalidTime:
      s = "invalid time";
      break;
    case GrabSuccess:
      return;
    default:
      fprintf(stderr, "ryudo: %s: grab error: %d\n", f, err);
      return;
  }
  fprintf(stderr, "ryudo: %s: grab error: %s\n", f, s);
#endif
}

#ifdef DEBUG_EV
#include "showevent/ShowEvent.c"
#endif

#ifdef DEBUG

void dotrace(char* s, Client* c, XEvent* e) {
  if (debug == 0)
    return;
  setbuf(stdout, 0);
  fprintf(stderr, "ryudo: %s: c=%p", s, (void*)c);
  if (c)
    fprintf(
      stderr,
      " x %d y %d dx %d dy %d w 0x%x parent 0x%x",
      c->x,
      c->y,
      c->dx,
      c->dy,
      (int)c->window,
      (int)c->parent);
#ifdef DEBUG_EV
  if (e) {
    fprintf(stderr, "\n\t");
    ShowEvent(e);
  }
#endif
  fprintf(stderr, "\n");
}
#endif