001/* [{
002Copyright 2008 Brien Colwell
003
004This file is part of jpen.
005
006jpen is free software: you can redistribute it and/or modify
007it under the terms of the GNU Lesser General Public License as published by
008the Free Software Foundation, either version 3 of the License,
009or (at your option) any later version.
010
011jpen is distributed in the hope that it will be useful,
012but WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014GNU Lesser General Public License for more details.
015
016You should have received a copy of the GNU Lesser General Public License
017along with jpen.  If not, see <http://www.gnu.org/licenses/>.
018}] */
019package jpen.provider.osx;
020
021import java.util.EnumMap;
022import java.util.Map;
023import jpen.PenManager;
024import jpen.PenProvider;
025import jpen.PKind;
026import jpen.provider.AbstractPenProvider;
027import jpen.provider.NativeLibraryLoader;
028import jpen.internal.BuildInfo;
029
030public class CocoaProvider extends AbstractPenProvider {
031
032        private static final NativeLibraryLoader LIB_LOADER=new NativeLibraryLoader(
033                                Integer.valueOf(BuildInfo.getProperties().getString("jpen.provider.osx.nativeVersion")));
034
035        public static class Constructor
036                extends AbstractPenProvider.AbstractConstructor{
037
038                public String getName() {
039                        return "Cocoa";
040                }
041
042                //@Override
043                public boolean constructable(PenManager penManager) {
044                        return System.getProperty("os.name").toLowerCase().contains("mac");
045                }
046
047                @Override
048                public PenProvider constructProvider() throws Throwable {
049                        LIB_LOADER.load();
050                        CocoaAccess cocoaAccess=new CocoaAccess();
051                        return new CocoaProvider(this, cocoaAccess);
052                }
053                @Override
054                public int getNativeVersion(){
055                        return LIB_LOADER.nativeVersion;
056                }
057                @Override
058                public int getNativeBuild(){
059                        LIB_LOADER.load();
060                        return CocoaAccess.getNativeBuild();
061                }
062                @Override
063                public int getExpectedNativeBuild(){
064                        return Integer.valueOf(BuildInfo.getProperties().
065                                                 getString("jpen.provider.osx.nativeBuild"));
066                }
067        }
068
069
070        private final CocoaAccess cocoaAccess;
071        private final Map<PKind.Type, CocoaDevice> deviceMap;
072
073        private CocoaProvider(final Constructor _constructor, final CocoaAccess _cocoaAccess) {
074                super(_constructor);
075                cocoaAccess = _cocoaAccess;
076                cocoaAccess.setProvider(this);
077
078                deviceMap = new EnumMap<PKind.Type, CocoaDevice>(PKind.Type.class);
079                for (PKind.Type type : PKind.Type.VALUES) {
080                        final CocoaDevice device = new CocoaDevice(this, type);
081                        deviceMap.put(type, device);
082                        devices.add(device);
083                }
084
085                cocoaAccess.start();
086        }
087
088        public CocoaDevice getDevice(final PKind.Type type) {
089                return deviceMap.get(type);
090        }
091
092        public void penManagerPaused(final boolean paused) {
093                if (! paused) {
094                        cocoaAccess.enable();
095                }
096                else {
097                        cocoaAccess.disable();
098                }
099        }
100
101
102        public void dispose() {
103                cocoaAccess.stop();
104        }
105}