jnserver.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 238 行

JAVA
238
字号
/*
 * $Id: JNServer.java,v 1.8 2004/02/28 09:20:53 epr Exp $
 */
package org.jnode.wt.server;

import java.awt.Dimension;

import javax.naming.NameNotFoundException;

import org.jnode.driver.ApiNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.DeviceException;
import org.jnode.driver.DeviceNotFoundException;
import org.jnode.driver.DeviceUtils;
import org.jnode.driver.console.ConsoleManager;
import org.jnode.driver.video.AlreadyOpenException;
import org.jnode.driver.video.FrameBufferAPI;
import org.jnode.driver.video.FrameBufferConfiguration;
import org.jnode.driver.video.Surface;
import org.jnode.driver.video.UnknownConfigurationException;
import org.jnode.naming.InitialNaming;
import org.jnode.wt.desktop.JNDesktop;
import org.jnode.wt.desktop.JNDesktopManager;
import org.jnode.wt.events.JNDesktopManagerListener;

/**
 * Server for JNComponents. This class creates the connection between
 * JNDesktopManager and FrameBuffer devices.
 * 
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public class JNServer implements JNDesktopManagerListener {

    private Surface surface;

    private int screenWidth;

    private int screenHeight;

    private JNKeyboardHandler keyboardHandler;

    private JNMouseHandler mouseHandler;

    private JNDesktopManager mgr;

    private boolean active;

    private JNConsole console;

    /**
     * Start the graphics server.
     */
    public synchronized void start() throws org.jnode.wt.components.JNException {
        if (surface != null) { throw new IllegalStateException(
                "Server already started"); }
        try {
            openConsole();
            openGraphics();
            keyboardHandler = new JNKeyboardHandler(this);
            mgr = JNDesktopManager.getDesktopManager();
            mgr.setScreenSize(new Dimension(screenWidth, screenHeight));
            mgr.addDesktopManagerListener(this);
            mgr.setGraphics(new JNGraphics(surface, screenWidth, screenHeight));
            JNDesktop desktop = mgr.addDesktop();
            active = true;
            notifyAll();
            desktop.repaint();
        } catch (NameNotFoundException ex) {
            throw new org.jnode.wt.components.JNException(ex);
        }
    }

    /**
     * Stop the graphics server.
     */
    public synchronized void stop() {
        if (surface == null) { throw new IllegalStateException(
                "Server not started"); }

        mgr.removeDesktopManagerListener(this);
        mouseHandler.close();
        keyboardHandler.close();
        surface.close();
        try {
            closeConsole();
        } catch (NameNotFoundException ex) {
            // Ignore
        }
        surface = null;
        mgr = null;
        this.active = false;
        notifyAll();
    }

    protected synchronized void focusGained() {
        if (surface != null) {
            try {
                final Device dev = getVideoDevice();
                final FrameBufferAPI api = (FrameBufferAPI) dev
                        .getAPI(FrameBufferAPI.class);
                final FrameBufferConfiguration conf = api.getConfigurations()[ 0];
                screenWidth = conf.getScreenWidth();
                screenHeight = conf.getScreenHeight();
                surface = api.open(conf);
            } catch (DeviceNotFoundException ex) {
                throw new RuntimeException(ex);
            } catch (ApiNotFoundException ex) {
                throw new RuntimeException(ex);
            } catch (UnknownConfigurationException ex) {
                throw new RuntimeException(ex);
            } catch (AlreadyOpenException ex) {
                throw new RuntimeException(ex);
            } catch (DeviceException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    protected synchronized void focusLost() {
        if (surface != null) {
            surface.close();
        }
    }

    /**
     * Block the current thread, until this server is not active any more.
     */
    public synchronized void waitUntilStopped() {
        while (active) {
            try {
                wait();
            } catch (InterruptedException e) {
                // Ignore
            }
        }
    }

    /**
     * Find and open the default frame buffer device.
     * 
     * @throws org.jnode.wt.components.JNException
     */
    private void openGraphics() throws org.jnode.wt.components.JNException,
            NameNotFoundException {
        try {
            final Device dev = getVideoDevice();
            final FrameBufferAPI api = (FrameBufferAPI) dev
                    .getAPI(FrameBufferAPI.class);
            final FrameBufferConfiguration conf = api.getConfigurations()[ 0];
            screenWidth = conf.getScreenWidth();
            screenHeight = conf.getScreenHeight();
            surface = api.open(conf);
            mouseHandler = new JNMouseHandler(this, dev, new Dimension(
                    screenWidth, screenHeight));
        } catch (AlreadyOpenException ex) {
            throw new org.jnode.wt.components.JNException(
                    "Device is already open", ex);
        } catch (ApiNotFoundException ex) {
            throw new org.jnode.wt.components.JNException(
                    "Device does not seem to be a video device", ex);
        } catch (DeviceNotFoundException ex) {
            throw new org.jnode.wt.components.JNException(
                    "Video device not found", ex);
        } catch (UnknownConfigurationException ex) {
            throw new org.jnode.wt.components.JNException(
                    "Unknown video configuration", ex);
        } catch (DeviceException ex) {
            throw new org.jnode.wt.components.JNException(
                    "Unknown device exception", ex);
        }
    }

    /**
     * Gets the video device.
     * 
     * @return Device
     * @throws DeviceNotFoundException
     */
    private Device getVideoDevice() throws DeviceNotFoundException {
        final String devId = System.getProperty("jnode.awt.device", "fb0");
        return DeviceUtils.getDevice(devId);
    }

    /**
     * @see org.jnode.wt.events.JNDesktopManagerListener#desktopClosed(org.jnode.wt.desktop.JNDesktop)
     */
    public void desktopClosed(JNDesktop desktop) {
        if (mgr.getDesktopCount() == 0) {
            stop();
        }
    }

    /**
     * @see org.jnode.wt.events.JNDesktopManagerListener#desktopCreated(org.jnode.wt.desktop.JNDesktop)
     */
    public void desktopCreated(JNDesktop desktop) {
        // Do nothing
    }

    /**
     * Is this server active.
     * 
     * @return Returns the active.
     */
    public final boolean isActive() {
        return this.active;
    }

    private void openConsole() throws NameNotFoundException {
        final ConsoleManager cm = (ConsoleManager) InitialNaming
                .lookup(ConsoleManager.NAME);
        console = (JNConsole) cm.getConsole(JNConsole.NAME);
        if (console == null) {
            console = new JNConsole();
            cm.registerConsole(console);
        }
        cm.focus(console);
        console.setServer(this);
    }

    private void closeConsole() throws NameNotFoundException {
        if (console != null) {
            final ConsoleManager cm = (ConsoleManager) InitialNaming
                    .lookup(ConsoleManager.NAME);
            cm.unregisterConsole(console);
            console.setServer(null);
            console = null;
        }
    }

    /**
     * @return Returns the console.
     */
    final JNConsole getConsole() {
        return this.console;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?