File: CFreej.mm

package info (click to toggle)
freej 0.10git20100110-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 32,080 kB
  • ctags: 22,705
  • sloc: cpp: 156,254; ansic: 25,531; sh: 13,538; perl: 4,624; makefile: 3,278; python: 2,889; objc: 1,284; asm: 1,125; ruby: 126
file content (253 lines) | stat: -rw-r--r-- 7,219 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*  FreeJ
 *  (c) Copyright 2009 Andrea Guzzo <xant@dyne.org>
 *
 * This source code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Public License as published 
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * This source code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * Please refer to the GNU Public License for more details.
 *
 * You should have received a copy of the GNU Public License along with
 * this source code; if not, write to:
 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#import <CFreej.h>
#include <CVLayer.h>
#include <CVScreen.h>
#include <jsparser.h>

#define DEFAULT_FREEJ_WIDTH 352
#define DEFAULT_FREEJ_HEIGHT 288


@implementation CFreej


// bridge stdout and stderr with the NSTextView outlet (if any)
- (void) consoleOutput:(id)object
{
    NSAutoreleasePool * p = [[NSAutoreleasePool alloc] init];
    char buf[1024];
    struct timeval timeout;
    fd_set rfds;
    
    fcntl(stdout_pipe[0], F_SETFL, O_NONBLOCK);
    fcntl(stderr_pipe[0], F_SETFL, O_NONBLOCK);

    buf[1024] = 0;
    for (;;) {
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;
        memset(buf, 0, sizeof(buf));
        if ([outputPanel isEditable])
            [outputPanel setEditable:NO];
        FD_ZERO(&rfds);
        FD_SET(stdout_pipe[0], &rfds);
        FD_SET(stderr_pipe[0], &rfds);
        int maxfd = ((stdout_pipe[0] > stderr_pipe[0])?stdout_pipe[0]:stderr_pipe[0]) +1;
        switch (select(maxfd, &rfds, NULL, NULL, &timeout)) {
        case -1:
        case 0:
            break;
        default:
            if (FD_ISSET(stdout_pipe[0], &rfds)) {
                while (read(stdout_pipe[0], buf, sizeof(buf)-1) > 0) {
                    NSString *msg = [[NSString alloc] initWithCString:buf encoding:NSASCIIStringEncoding];
                    @synchronized (outputPanel)
                    {
                        [outputPanel setEditable:YES];
                        [outputPanel insertText:msg];
                        [outputPanel setEditable:NO];
                    }
                    [msg release];
                }
            }
            if (FD_ISSET(stderr_pipe[0], &rfds)) {
                while (read(stderr_pipe[0], buf, sizeof(buf)-1) > 0) {
                    NSString *msg = [[NSString alloc] initWithCString:buf encoding:NSASCIIStringEncoding];
                    @synchronized (outputPanel)
                    {
                        [outputPanel setEditable:YES];
                        [outputPanel insertText:msg];
                        [outputPanel setEditable:NO];
                    }
                    [msg release];
                }
            }
        }
    }
    [p release];
}
 

-(void)awakeFromNib
{
    lock = [[NSRecursiveLock alloc] init];
}

-(id)init
{
    if (self = [super init]) 
    {
        pipe(stdout_pipe);
        pipe(stderr_pipe);
        dup2(stdout_pipe[1], fileno(stdout));
        dup2(stderr_pipe[1], fileno(stderr));
        close(stdout_pipe[1]);
        close(stderr_pipe[1]);
        [NSThread detachNewThreadSelector:@selector(consoleOutput:) 
            toTarget:self withObject:nil];
        return self;
    }
    return nil;
}

- (void)openScriptPanelDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode  contextInfo:(void  *)contextInfo
{
     if(returnCode == NSOKButton){
         func("openScript we have an OK button");    
     } else if(returnCode == NSCancelButton) {
         func("openScript we have a Cancel button");
         return;
     } else {
         error("doOpen tvarInt not equal 1 or zero = %3d",returnCode);
         return;
     } // end if     
     NSString * tvarDirectory = [panel directory];
     func("openScript directory = %@",tvarDirectory);

     NSString * tvarFilename = [panel filename];
     func("openScript filename = %@",tvarFilename);

    [scriptPath setStringValue:tvarFilename];
    freej->open_script((char *)[tvarFilename UTF8String]);

}

- (IBAction)openScript:(id)sender 
{    
     NSOpenPanel *tvarNSOpenPanelObj = [NSOpenPanel openPanel];
     NSArray *types = [NSArray arrayWithObject:[NSString stringWithUTF8String:"js"]];
     [tvarNSOpenPanelObj 
        beginSheetForDirectory:nil 
        file:nil
        types:types 
        modalForWindow:[sender window]
        modalDelegate:self 
        didEndSelector:@selector(openScriptPanelDidEnd: returnCode: contextInfo:) 
        contextInfo:nil];    
    [tvarNSOpenPanelObj setCanChooseFiles:YES];
}

- (Context *)getContext
{
    return freej;
}

- (NSRecursiveLock *)getLock
{
    return lock;
}

 - (void)updateLayerList:(id)object
{
    NSAutoreleasePool * p = [[NSAutoreleasePool alloc] init];
    
    if (layerSelect) {
        [layerSelect removeAllItems];
        NSMenu *menu = [layerSelect menu];
        screen->layers.lock();
        Layer *lay = (Layer *)screen->layers.begin ();
        while (lay) {
            if (lay->type !=  Layer::GL_COCOA)
                [menu addItemWithTitle:[NSString stringWithUTF8String:lay->name] action:nil keyEquivalent:@""];
            lay = (Layer *)lay->next;
        }
        screen->layers.unlock();
    }
    [layerSelect synchronizeTitleAndSelectedItem];
    [p release];
}

- (void)start
{
    if (!freej) {
        //freej = new CVContext(self);
        freej = new Context();
        
        // override factory defaults
        Factory<Controller>::set_default_classtype("KeyboardController", "cocoa");
        Factory<ViewPort>::set_default_classtype("Screen", "cocoa");
        
        freej->quit = false;
        NSRect frame = [screenView frame];
        screen = (CVScreen *)Factory<ViewPort>::get_instance("Screen");
        screen->init(frame.size.width, frame.size.height, 32);
        freej->add_screen(screen);
        freej->plugger.refresh(freej);
        //freej->config_check("keyboard.js");
        [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(updateLayerList:)
             name:NSPopUpButtonWillPopUpNotification
             object:layerSelect];  
    }    
}

- (bool)isVisible:(CVLayer *)layer
{
    bool ret = NO;
    screen->layers.lock();
    Layer *lay = (Layer *)screen->layers.begin ();
    while (lay) {
        // TODO - should check against something more unique than the layer name
        if (strcmp(layer->name, lay->name) == 0) {
            ret = YES;
            break;
        }
        lay =  (Layer *)lay->next;
    }
    screen->layers.unlock();
    return ret;
}

- (IBAction)reset:(id)sender
{
    freej->reset();
    // give the engine some time to stop all layers
    Delay(20, NULL); 

    [screenView reset];
}

@end

/*

CVContext::CVContext(CFreej *cfreej) :
    Context()
{
    _cfreej = cfreej;
}

CVContext::~CVContext()
{
}

void CVContext::add_layer(Layer *lay)
{
    Context::add_layer(lay);
}

bool CVContext::init(int wx, int hx, VideoMode videomode, int audiomode)
{
    bool ret = Context::init(wx, hx, videomode, audiomode);
    return ret;
}
*/