1 /*
2  * This file is part of libbluray
3  * Copyright (C) 2010  William Hahne
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
20 package org.havi.ui;
21 
22 import java.awt.Dimension;
23 import java.util.Arrays;
24 
25 import org.videolan.Logger;
26 
27 public abstract class HScreenConfigTemplate {
HScreenConfigTemplate()28     HScreenConfigTemplate() {
29         objectArray = new Object[getPreferenceObjectCount()];
30         priorityArray = new int[getPreferenceCount()];
31         Arrays.fill(priorityArray, DONT_CARE);
32     }
33 
setPreference(int preference, int priority)34     public void setPreference(int preference, int priority) {
35         if ((priority < REQUIRED) || (priority > REQUIRED_NOT)) {
36             logger.error("invalid priority " + priority);
37             throw new IllegalArgumentException("invalid priority");
38         }
39         int index = getPreferenceIndex(preference);
40         if (index < 0) {
41             logger.error("invalid preference " + preference);
42             throw new IllegalArgumentException("invalid preference");
43         }
44         priorityArray[index] = priority;
45     }
46 
setPreference(int preference, Object object, int priority)47     public void setPreference(int preference, Object object, int priority) {
48         if ((priority < REQUIRED) || (priority > REQUIRED_NOT)) {
49             logger.error("invalid priority " + priority);
50             throw new IllegalArgumentException("invalid priority");
51         }
52         int index = getPreferenceObjectIndex(preference);
53         if (index < 0) {
54             logger.error("invalid preference " + preference);
55             throw new IllegalArgumentException("invalid preference");
56         }
57         objectArray[index] = object;
58         priorityArray[getPreferenceIndex(preference)] = priority;
59     }
60 
getPreferencePriority(int preference)61     public int getPreferencePriority(int preference) {
62         int index = getPreferenceIndex(preference);
63         if (index < 0) {
64             logger.error("invalid preference " + preference);
65             throw new IllegalArgumentException("invalid preference");
66         }
67         return priorityArray[getPreferenceIndex(preference)];
68     }
69 
getPreferenceObject(int preference)70     public Object getPreferenceObject(int preference) {
71         int index = getPreferenceObjectIndex(preference);
72         if (index < 0) {
73             logger.error("invalid preference " + preference);
74             throw new IllegalArgumentException("invalid preference");
75         }
76         return objectArray[getPreferenceObjectIndex(preference)];
77     }
78 
getPreferenceCount()79     protected int getPreferenceCount() {
80         return SCREEN_RECTANGLE - ZERO_BACKGROUND_IMPACT + 1;
81     }
82 
getPreferenceObjectCount()83     protected int getPreferenceObjectCount() {
84         return SCREEN_RECTANGLE - VIDEO_GRAPHICS_PIXEL_ALIGNED + 1;
85     }
86 
getPreferenceIndex(int preference)87     protected int getPreferenceIndex(int preference) {
88         if ((preference < ZERO_BACKGROUND_IMPACT) || (preference > SCREEN_RECTANGLE))
89             return -1;
90         return preference - ZERO_BACKGROUND_IMPACT;
91     }
92 
getPreferenceObjectIndex(int preference)93     protected int getPreferenceObjectIndex(int preference) {
94         if ((preference < VIDEO_GRAPHICS_PIXEL_ALIGNED) || (preference > SCREEN_RECTANGLE))
95                 return -1;
96         return preference - VIDEO_GRAPHICS_PIXEL_ALIGNED;
97     }
98 
equals(Object obj1, Object obj2)99     protected boolean equals(Object obj1, Object obj2) {
100         return (obj1 == null) ? (obj2 == null) : obj1.equals(obj2);
101     }
102 
matchPreference(int preference, boolean supported)103     protected int matchPreference(int preference, boolean supported) {
104         int Priority = getPreferencePriority(preference);
105         if (Priority == REQUIRED) {
106             if (!supported)
107                 return -1;
108             return 1;
109         } else if (Priority == REQUIRED_NOT) {
110             if (supported)
111                 return -1;
112             return 1;
113         }
114         return 0;
115     }
116 
matchPreference(int preference, HScreenConfiguration hsc)117     protected int matchPreference(int preference, HScreenConfiguration hsc) {
118         switch (preference) {
119         case INTERLACED_DISPLAY:
120             return matchPreference(INTERLACED_DISPLAY, hsc.getInterlaced());
121         case FLICKER_FILTERING:
122             return matchPreference(FLICKER_FILTERING, hsc.getFlickerFilter());
123         case PIXEL_ASPECT_RATIO:
124             return matchPreference(PIXEL_ASPECT_RATIO, equals(hsc.getPixelAspectRatio(), getPreferenceObject(PIXEL_ASPECT_RATIO)));
125         case PIXEL_RESOLUTION:
126             return matchPreference(PIXEL_RESOLUTION, equals(hsc.getPixelResolution(), getPreferenceObject(PIXEL_RESOLUTION)));
127         case SCREEN_RECTANGLE:
128             return matchPreference(SCREEN_RECTANGLE, equals(hsc.getScreenArea(), getPreferenceObject(SCREEN_RECTANGLE)));
129         }
130         return 0;
131     }
132 
match(HScreenConfiguration hsc)133     protected int match(HScreenConfiguration hsc) {
134         int score = 0;
135         int length = getPreferenceCount();
136         for (int i = 0; i < length; i++) {
137             int s = matchPreference(i, hsc);
138             if (s < 0)
139                 return s;
140             score += s;
141         }
142         return score;
143     }
144 
initDefaultConfigTemplate(HScreenConfigTemplate hsct, int index)145     static void initDefaultConfigTemplate(HScreenConfigTemplate hsct, int index) {
146         hsct.setPreference(HScreenConfigTemplate.PIXEL_ASPECT_RATIO,
147                 new Dimension(defaultConfig[index][2], defaultConfig[index][3]),
148                 HScreenConfigTemplate.REQUIRED);
149         hsct.setPreference(HScreenConfigTemplate.PIXEL_RESOLUTION,
150             new Dimension(defaultConfig[index][0], defaultConfig[index][1]),
151             HScreenConfigTemplate.REQUIRED);
152         hsct.setPreference(HScreenConfigTemplate.SCREEN_RECTANGLE,
153             new HScreenRectangle(0.0f, 0.0f, 1.0f, 1.0f),
154             HScreenConfigTemplate.REQUIRED);
155     }
156 
157     public static final int REQUIRED = 0x01;
158     public static final int PREFERRED = 0x02;
159     public static final int DONT_CARE = 0x03;
160     public static final int PREFERRED_NOT = 0x04;
161     public static final int REQUIRED_NOT = 0x05;
162 
163     public static final int ZERO_BACKGROUND_IMPACT = 0x01;
164     public static final int ZERO_GRAPHICS_IMPACT = 0x02;
165     public static final int ZERO_VIDEO_IMPACT = 0x03;
166     public static final int INTERLACED_DISPLAY = 0x04;
167     public static final int FLICKER_FILTERING = 0x05;
168     public static final int VIDEO_GRAPHICS_PIXEL_ALIGNED = 0x06;
169     public static final int PIXEL_ASPECT_RATIO = 0x07;
170     public static final int PIXEL_RESOLUTION = 0x08;
171     public static final int SCREEN_RECTANGLE = 0x09;
172 
173     private Object[] objectArray;
174     private int[] priorityArray;
175 
176     static int[][] defaultConfig = {
177         { 1920, 1080, 16, 9 },
178         { 1280, 720, 16, 9 },
179         { 720, 480, 4, 3 },
180         { 720, 480, 16, 9 },
181         { 720, 576, 4, 3 },
182         { 720, 576, 16, 9 },
183 
184         { 960, 540, 16, 9 },
185     };
186 
187     private static final Logger logger = Logger.getLogger(HScreenConfigTemplate.class.getName());
188 }
189