2ba781c431239e29c956413b0c24cea426fc0d9e
[platform/upstream/at-spi2-core.git] / registryd / display.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2009 Nokia.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <glib.h>
24
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <X11/Xos.h>
28 #include <X11/Xatom.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "display.h"
34
35 static Display *default_display = NULL;
36
37 Display *spi_set_display (const char *display_name)
38 {
39         /* 
40          * TODO - Should we ever do anything different might need to
41          * close previous display.
42          */
43         default_display = XOpenDisplay (display_name);  
44         if (!default_display)
45         {
46                 g_warning ("AT-SPI: Cannot open default display");
47                 exit (1);
48         }
49  return default_display;
50 }
51
52 Display *spi_get_display ()
53 {
54         if (!default_display)
55                 spi_set_display (NULL);
56
57         return default_display;
58 }
59
60 Window spi_get_root_window ()
61 {
62         if (!default_display)
63                 spi_set_display (NULL);
64
65         return DefaultRootWindow (default_display);
66 }
67
68 static int (*old_x_error_handler) (Display *, XErrorEvent *);
69 static int x_error_code;
70
71 static int spi_x_error_handler (Display *display, XErrorEvent *error)
72 {
73         if (error->error_code)
74                 x_error_code = error->error_code;
75         else
76                 x_error_code = 0;
77   
78         return 0;
79 }
80
81 void spi_x_error_trap (void)
82 {
83         old_x_error_handler = XSetErrorHandler (spi_x_error_handler);
84 }
85
86 int spi_x_error_release (void)
87 {
88         XSetErrorHandler (old_x_error_handler);
89         return x_error_code;
90 }