1 /* Copyright (C) 2000  Free Software Foundation
2 
3    This file is part of libgcj.
4 
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8 
9 #include <config.h>
10 #include <platform.h>
11 
12 #include <gcj/javaprims.h>
13 #include <jvm.h>
14 
15 #include <X11/Xlib.h>
16 
17 #include <gcj/cni.h>
18 #include <gnu/gcj/RawData.h>
19 
20 #include <java/lang/RuntimeException.h>
21 
22 #include <java/lang/System.h>
23 #include <java/io/PrintStream.h>
24 
25 #include <gnu/gcj/xlib/Display.h>
26 #include <gnu/gcj/xlib/Window.h>
27 #include <gnu/gcj/xlib/XAnyEvent.h>
28 #include <gnu/gcj/xlib/XExposeEvent.h>
29 #include <gnu/gcj/xlib/XException.h>
30 
31 #include <unistd.h>
32 #include <posix.h>
33 
init()34 void gnu::gcj::xlib::XAnyEvent::init()
35 {
36   ::XEvent* event = new ::XEvent;
37   int *pipes = new int[2];
38   pipe(pipes);
39   structure = reinterpret_cast<gnu::gcj::RawData*>(event);
40   pipefds = reinterpret_cast<gnu::gcj::RawData*>(pipes);
41 }
42 
finalize()43 void gnu::gcj::xlib::XAnyEvent::finalize()
44 {
45   delete structure;
46   int *pipe = reinterpret_cast<int *>(pipefds);
47   close(pipe[0]);
48   close(pipe[1]);
49   delete [] pipefds;
50   pipefds = 0;
51   structure = 0;
52 }
53 
loadNext(jboolean block)54 jboolean gnu::gcj::xlib::XAnyEvent::loadNext(jboolean block)
55 {
56   ::Display* dpy = (::Display*) display->display;
57   ::XEvent* evt = (::XEvent*) structure;
58 
59   if (XPending(dpy))
60     {
61       XNextEvent(dpy, evt);
62       return true;
63     }
64 
65   if (!block)
66     return false;
67 
68   int *pipe = reinterpret_cast<int *>(pipefds);
69   int xfd = XConnectionNumber(dpy);
70   int pipefd = pipe[0];
71   int n = (xfd > pipefd ? xfd : pipefd) + 1;
72   fd_set rfds;
73   FD_ZERO(&rfds);
74   FD_SET(xfd, &rfds);
75   FD_SET(pipefd, &rfds);
76   int sel = _Jv_select (n, &rfds, NULL, NULL, NULL);
77   if (sel > 0)
78     {
79       if (FD_ISSET(xfd, &rfds))
80 	{
81 	  XNextEvent(dpy, evt);
82 	  return true;
83 	}
84       if (FD_ISSET(pipefd, &rfds))
85 	{
86 	  char c;
87 	  read(pipefd, &c, 1);
88 	}
89     }
90   return false;
91 }
92 
interrupt()93 void gnu::gcj::xlib::XAnyEvent::interrupt()
94 {
95   int *pipe = reinterpret_cast<int *>(pipefds);
96   write(pipe[1], "W", 1);
97 }
98 
getType()99 jint gnu::gcj::xlib::XAnyEvent::getType()
100 {
101   ::XEvent* event = (::XEvent*) structure;
102   return event->type;
103 }
104 
setType(jint type)105 void gnu::gcj::xlib::XAnyEvent::setType(jint type)
106 {
107   ::XEvent* event = (::XEvent*) structure;
108   event->type = type;
109 }
110 
getWindow()111 gnu::gcj::xlib::Window* gnu::gcj::xlib::XAnyEvent::getWindow()
112 {
113   ::XEvent* event = (::XEvent*) structure;
114   return display->getWindow(event->xany.window);
115 }
116 
setWindow(gnu::gcj::xlib::Window * window)117 void gnu::gcj::xlib::XAnyEvent::setWindow(gnu::gcj::xlib::Window* window)
118 {
119   ::XEvent* event = (::XEvent*) structure;
120   event->xany.window = window->getXID();
121 }
122 
getSerial()123 jlong gnu::gcj::xlib::XAnyEvent::getSerial()
124 {
125   ::XEvent* event = (::XEvent*) structure;
126   return event->xany.serial;
127 }
128 
send(gnu::gcj::xlib::Window * destination,jboolean propagate,jlong mask)129 void gnu::gcj::xlib::XAnyEvent::send(gnu::gcj::xlib::Window* destination,
130 				     jboolean propagate, jlong mask)
131 {
132   ::Display* dpy = (::Display*) display->display;
133   ::XEvent* event = (::XEvent*) structure;
134 
135   Status status =
136     XSendEvent(dpy, destination->getXID(), propagate ? True : False,
137 	       mask, event);
138 
139   switch (status)
140     {
141     case 0:
142       throw new XException(JvNewStringLatin1("conversion to wire "
143 					     "protocol failed"));
144     case BadWindow:
145     case BadValue:
146       throw new XException(display, status);
147 
148     default:
149       /* All other return values indicate success.  Ie. (status ==
150 	 1) indicates success, not BadRequest. */
151       ; // NOP
152     }
153 }
154