Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
204 chris 1
/*
2
 *      Copyright (c) 2005-2006 Philip Lamb (PRL) phil@eden.net.nz. All rights reserved.
3
 *     
4
 *      Rev             Date            Who             Changes
5
 *      1.0.0   2005-03-08      PRL             Written.
6
 *
7
 */
8
/*
9
 *
10
 * This file is part of ARToolKit.
11
 *
12
 * ARToolKit is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * ARToolKit is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with ARToolKit; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 *
26
 */
27
 
28
#import "ARVideoSettingsController.h"
29
 
30
@implementation ARVideoSettingsController
31
 
32
- (id)initInput:(int)inInputIndex withSeqGrabComponent:(SeqGrabComponent)inSeqGrab withSGChannel:(SGChannel)inSgchanVideo
33
{
34
        ComponentResult err;
35
 
36
        self = [super init];
37
 
38
        // initialize NSApplication, using an entry point that is specific to bundles
39
    // this is a no-op for Cocoa apps, but is required by Carbon apps
40
        NSApplicationLoad();
41
 
42
        inputIndex = inInputIndex;
43
        seqGrab = inSeqGrab;
44
        sgchanVideo = inSgchanVideo;
45
 
46
        // Grab the defaults.
47
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
48
 
49
        // Try to load the version key, used to see if we have any saved settings.
50
        mVersion = [defaults floatForKey:@"version"];
51
        if (!mVersion) {
52
                // No previous defaults so define new.
53
                mVersion = 1;
54
 
55
                // Write out the defaults.
56
                [defaults setInteger:mVersion forKey:@"version"];
57
                [defaults synchronize];
58
        }
59
 
60
        // Load defaults, first time though mUserData may be 0 which is fine.
61
        [self loadUserData:&mUserData fromDefaults:defaults forKey:[NSString stringWithFormat:@"sgVideoSettings%03i", inputIndex]];
62
        if (mUserData) {
63
                if ((err = SGSetChannelSettings(seqGrab, sgchanVideo, mUserData, 0)) != noErr) {
64
                        fprintf(stderr, "-sgConfigurationDialog: error %ld in SGSetChannelSettings().\n", err);
65
                }
66
        }
67
 
68
        return self;
69
 
70
}
71
 
72
- (void)dealloc
73
{
74
        if (mUserData) DisposeUserData(mUserData);
75
        [super dealloc];
76
}
77
 
78
// NOTE: There is a know bug in QuickTime 6.4 in which the
79
// Settings Dialog pops up in random locations...sigh...
80
- (IBAction) sgConfigurationDialog:(id)sender withStandardDialog:(int)standardDialog
81
{
82
        // Set up the settings panel list removing the "Compression" panel.
83
        ComponentResult                 err;
84
 
85
        if (standardDialog) {
86
                err = SGSettingsDialog(seqGrab, sgchanVideo, 0, 0, seqGrabSettingsPreviewOnly, NULL, 0L);
87
        } else {
88
                ComponentDescription    cDesc;
89
                long                                    cCount;
90
                ComponentDescription    cDesc2;
91
 
92
                cDesc.componentType = SeqGrabPanelType;
93
                cDesc.componentSubType = VideoMediaType;
94
                cDesc.componentManufacturer = cDesc.componentFlags = cDesc.componentFlagsMask = 0L;
95
                cCount = CountComponents(&cDesc);
96
                if (cCount == 0) {
97
                        fprintf(stderr, "-sgConfigurationDialog: error in CountComponents().\n");
98
                        goto bail;
99
                }
100
 
101
                Component *PanelListPtr = (Component *)NewPtr(sizeof(Component) * (cCount + 1));
102
                if (err = MemError() || NULL == PanelListPtr) {
103
                        fprintf(stderr, "-sgConfigurationDialog: error in NewPtr().\n");
104
                        goto bail;
105
                }
106
                Component *cPtr = PanelListPtr;
107
                long PanelCount = 0L;  
108
                Component c = NULL;
109
                while ((c = FindNextComponent(c, &cDesc)) != NULL) {
110
                        Handle hName = NewHandleClear(0);
111
                        if (err = MemError() || NULL == hName) {
112
                                fprintf(stderr, "-sgConfigurationDialog: error in NewHandle().\n");
113
                                goto bail;
114
                        }
115
 
116
                        GetComponentInfo(c, &cDesc2, hName, NULL, NULL);
117
                        if (PLstrcmp(*(unsigned char **)hName, "\pCompression") != 0) {
118
                                *cPtr = c;
119
                                cPtr++;
120
                                PanelCount++;
121
                        }
122
                        DisposeHandle(hName);
123
                }
124
 
125
                // Bring up the dialog and if the user didn't cancel
126
                // save the new channel settings for later.
127
                err = SGSettingsDialog(seqGrab, sgchanVideo, PanelCount, PanelListPtr, seqGrabSettingsPreviewOnly, NULL, 0L);
128
bail:
129
                DisposePtr((Ptr)PanelListPtr);
130
        }
131
 
132
        if (err == noErr) {
133
                // Dispose the old settings and get the new channel settings.
134
                if (mUserData) DisposeUserData(mUserData);
135
                if ((err = SGGetChannelSettings(seqGrab, sgchanVideo, &mUserData, 0)) != noErr) {
136
                        fprintf(stderr, "-sgConfigurationDialog: error %ld in SGGetChannelSettings().\n", err);
137
                }
138
 
139
                // Grab the defaults.
140
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
141
 
142
                // Write the defaults.
143
                [self saveUserData:mUserData toDefaults:defaults withKey:[NSString stringWithFormat:@"sgVideoSettings%03i", inputIndex]];
144
                [defaults synchronize];
145
 
146
        } else if (err != userCanceledErr) {
147
                fprintf(stderr, "-sgConfigurationDialog: error %ld in SGSettingsDialog().\n", err);
148
        }
149
}
150
 
151
// Get the Channel Settings as UserData from the preferences
152
- (OSErr)loadUserData:(UserData *)outUserData fromDefaults:(NSUserDefaults *)inDefaults forKey:(NSString *)inKey
153
{
154
        NSData   *theSettings;
155
        Handle   theHandle = NULL;
156
        UserData theUserData = NULL;
157
        OSErr    err = paramErr;
158
 
159
        // read the new setttings from our preferences
160
        theSettings = [inDefaults objectForKey:inKey];
161
 
162
        if (theSettings) {
163
                err = PtrToHand([theSettings bytes], &theHandle, [theSettings length]);
164
 
165
                if (theHandle) {
166
                        err = NewUserDataFromHandle(theHandle, &theUserData);
167
                        if (theUserData) {
168
                                *outUserData = theUserData;
169
                        }
170
                        DisposeHandle(theHandle);
171
                }
172
        }
173
 
174
        return err;
175
}
176
 
177
// Save the Channel Settings from UserData in the preferences
178
- (OSErr)saveUserData:(UserData)inUserData toDefaults:(NSUserDefaults *)inDefaults withKey:(NSString *)outKey
179
{
180
        NSData *theSettings;
181
        Handle hSettings;
182
        OSErr  err;
183
 
184
        if (NULL == inUserData) return paramErr;
185
 
186
        hSettings = NewHandle(0);
187
        err = MemError();
188
 
189
        if (noErr == err) {
190
                err = PutUserDataIntoHandle(inUserData, hSettings);
191
 
192
                if (noErr == err) {
193
                        HLock(hSettings);
194
                        theSettings = [NSData dataWithBytes:(UInt8 *)*hSettings length:GetHandleSize(hSettings)];
195
 
196
                        // save the new setttings to our preferences
197
                        if (theSettings) {
198
                                [inDefaults setObject:theSettings forKey:outKey];
199
                                [inDefaults synchronize];
200
                        }
201
                }
202
 
203
                DisposeHandle(hSettings);
204
        }
205
 
206
        return err;
207
}
208
 
209
@end
210
 
211
OSStatus RequestSGSettings(const int inputIndex, SeqGrabComponent seqGrab, SGChannel sgchanVideo, const int showDialog, const int standardDialog)
212
{
213
    NSAutoreleasePool *localPool;
214
        ARVideoSettingsController *localController;
215
 
216
        // Sanity check.
217
        if (inputIndex < 0 || !seqGrab || !sgchanVideo) return (paramErr);
218
 
219
    // Calling Objective-C from C necessitates an autorelease pool.
220
        localPool = [[NSAutoreleasePool alloc] init];
221
 
222
    localController = [[ARVideoSettingsController alloc] initInput:inputIndex withSeqGrabComponent:seqGrab withSGChannel:sgchanVideo];
223
        if (showDialog) [localController sgConfigurationDialog:NULL withStandardDialog:standardDialog];
224
        [localController release];
225
 
226
    [localPool release];
227
    return (noErr);
228
}
229