summaryrefslogtreecommitdiff
path: root/utils.c
blob: d758f7939bc3a5e2d74bfd3e508b40517f5bdffe (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
#include <stdarg.h>
#include <stdio.h>

#include "ocm.h"

void
ocm_error (const char *fmt, ...)
{
    va_list ap;

    va_start (ap, fmt);
    fprintf (stderr, "** Fatal **: ");
    vfprintf (stderr, fmt, ap);
    fprintf (stderr, "\n");
    va_end (ap);
    
    exit (-1);
}

void *
ocm_malloc (size_t n)
{
    void *mem = malloc (n);

    if (!mem)
	ocm_error ("Out of memory allocating %d bytes\n", mem);

    return mem;
}

void
ocm_free (void *mem)
{
    free (mem);
}

static int
ignore_errors (Display *dpy, XErrorEvent *event)
{
    return 0;
}

void
ocm_begin_ignore_errors (ocm_app_t *app)
{
    app->error_nesting++;
    if (app->error_handler)
	return;
    
    XSync (app->display, FALSE);

    app->error_handler = XSetErrorHandler (ignore_errors);
}

void
ocm_end_ignore_errors (ocm_app_t *app)
{
    if (--app->error_nesting == 0)
    {
	XSync (app->display, FALSE);

	XSetErrorHandler (app->error_handler);
    }
}