001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov
019: * @version $Revision$
020: */package org.apache.harmony.awt.wtk.linux;
021:
022: import java.awt.Dimension;
023: import java.awt.Frame;
024: import java.awt.Point;
025: import java.util.Collections;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import org.apache.harmony.awt.nativebridge.CLongPointer;
030: import org.apache.harmony.awt.nativebridge.Int32Pointer;
031: import org.apache.harmony.awt.nativebridge.Int8Pointer;
032: import org.apache.harmony.awt.nativebridge.NativeBridge;
033: import org.apache.harmony.awt.nativebridge.linux.X11;
034: import org.apache.harmony.awt.nativebridge.linux.X11Defs;
035: import org.apache.harmony.awt.wtk.CreationParams;
036: import org.apache.harmony.awt.wtk.NativeWindow;
037: import org.apache.harmony.awt.wtk.WindowFactory;
038:
039: public final class LinuxWindowFactory implements WindowFactory {
040:
041: private static final X11 x11 = X11.getInstance();
042: private static final NativeBridge bridge = NativeBridge
043: .getInstance();
044:
045: private final XServerConnection xConnection = XServerConnection
046: .getInstance();
047: private final long display = xConnection.getDisplay();
048: private final int screen = xConnection.getScreen();
049: final WindowManager wm;
050:
051: private final long javaWindow;
052: private final LinuxWindowMap allWindows = new LinuxWindowMap();
053:
054: /**
055: * Returns current root window id.
056: * @return root window id
057: */
058: long getRootWindow() {
059: return x11.XRootWindow(display, screen);
060: }
061:
062: public long getDisplay() {
063: return display;
064: }
065:
066: public int getScreen() {
067: return screen;
068: }
069:
070: public long getDisplayImpl() {
071: return display;
072: }
073:
074: public int getScreenImpl() {
075: return screen;
076: }
077:
078: public LinuxWindowFactory() {
079: javaWindow = x11.XCreateSimpleWindow(display, x11
080: .XDefaultRootWindow(display), 0, 0, 1, 1, 0, 0, 0);
081: x11.XSelectInput(display, javaWindow,
082: X11Defs.StructureNotifyMask);
083:
084: long rootWindow = getRootWindow();
085: X11.XWindowAttributes attributes = x11
086: .createXWindowAttributes(false);
087: x11.XGetWindowAttributes(display, rootWindow, attributes);
088: x11.XSelectInput(display, rootWindow, attributes
089: .get_your_event_mask()
090: | X11Defs.StructureNotifyMask);
091:
092: wm = new WindowManager(this );
093: }
094:
095: public NativeWindow createWindow(CreationParams p) {
096: LinuxWindow lw = new LinuxWindow(this , p);
097: allWindows.put(lw);
098: if (!lw.isChild() && !lw.isUndecorated()) {
099: p.child = true;
100: p.parentId = lw.getId();
101: p.x = 0;
102: p.y = 0;
103: ContentWindow cw = new ContentWindow(this , p);
104: allWindows.put(cw);
105: lw.setContentWindow(cw);
106: lw = cw;
107: }
108: x11.XFlush(display);
109: return lw;
110: }
111:
112: public NativeWindow getWindowById(long id) {
113: return allWindows.get(id);
114: }
115:
116: boolean validWindowId(long id) {
117: return allWindows.contains(id);
118: }
119:
120: void onWindowDispose(long windowID) {
121: allWindows.remove(windowID);
122: }
123:
124: public String getAtomName(long atom) {
125: long atomNamePtr = x11.XGetAtomName(display, atom);
126: Int8Pointer rawName = bridge.createInt8Pointer(atomNamePtr);
127: String atomName = rawName.getStringUTF();
128: x11.XFree(atomNamePtr);
129:
130: return atomName;
131: }
132:
133: public long internAtom(String name) {
134: return x11.XInternAtom(display, name, 0);
135: }
136:
137: /**
138: * @see org.apache.harmony.awt.wtk.WindowFactory#getWindowFromPoint(java.awt.Point)
139: */
140: public NativeWindow getWindowFromPoint(Point p) {
141: long rootID = getRootWindow();
142: long childID = rootID;
143: Int32Pointer x = bridge.createInt32Pointer(1, false);
144: Int32Pointer y = bridge.createInt32Pointer(1, false);
145: CLongPointer childWindow = bridge.createCLongPointer(1, false);
146:
147: //recursevily ask for child containing p
148: //until the deepest child is found
149: //or until our top-level window is found
150: while (childID != X11Defs.None) {
151: x11.XTranslateCoordinates(display, rootID, childID, p.x,
152: p.y, x, y, childWindow);
153: long nextID = childWindow.get(0);
154: // avoid endless loop
155: if (childID == nextID) {
156: return null;
157: }
158: childID = nextID;
159: if (validWindowId(childID)) {
160: break;
161: }
162: }
163:
164: LinuxWindow win = ((childID != 0) ? (LinuxWindow) getWindowById(childID)
165: : null);
166: if (win != null) {
167: LinuxWindow content = win.getContentWindow();
168: if ((content != null) && validWindowId(content.getId())) {
169: return content;
170: }
171: }
172: return win;
173:
174: }
175:
176: public boolean isWindowStateSupported(int state) {
177: switch (state) {
178: case Frame.NORMAL:
179: case Frame.ICONIFIED:
180: case Frame.MAXIMIZED_BOTH:
181: return true;
182: default:
183: return false;
184: }
185: }
186:
187: public NativeWindow attachWindow(long nativeWindowId) {
188: return new LinuxWindow(nativeWindowId, this );
189: }
190:
191: public void setCaretPosition(int x, int y) {
192: }
193:
194: /**
195: * @see org.apache.harmony.awt.wtk.WindowFactory#getWindowSizeById(long)
196: */
197: public Dimension getWindowSizeById(long id) {
198: Int32Pointer x = bridge.createInt32Pointer(1, false);
199: Int32Pointer y = bridge.createInt32Pointer(1, false);
200: Int32Pointer w = bridge.createInt32Pointer(1, false);
201: Int32Pointer h = bridge.createInt32Pointer(1, false);
202: CLongPointer root = bridge.createCLongPointer(1, false);
203: Int32Pointer border = bridge.createInt32Pointer(1, false);
204: Int32Pointer depth = bridge.createInt32Pointer(1, false);
205:
206: x11.XGetGeometry(display, id, root, x, y, w, h, border, depth);
207: return new Dimension(w.get(0), h.get(0));
208: }
209:
210: public long getJavaWindow() {
211: return javaWindow;
212: }
213:
214: }
|