2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #import <Cocoa/Cocoa.h>
31 CFBundleRef gMainBundle
;
32 NSTextView
*gPostView
;
33 SCColor postTextColor
;
35 void SetupHomeDirectory();
36 void SetupHomeDirectory()
39 gMainBundle
= CFBundleGetMainBundle();
40 CFURLRef mainURL
= CFBundleCopyBundleURL(gMainBundle
);
41 CFStringRef mainPath
= CFURLCopyFileSystemPath(mainURL
, kCFURLPOSIXPathStyle
);
42 CFStringGetCString(mainPath
, path
, 2048, kCFStringEncodingUTF8
);
43 *strrchr(path
, '/') = 0;
44 int mainPathLen
= strlen(path
);
47 path[mainPathLen] = '/';
49 CFURLRef sharedURL = CFBundleCopySharedSupportURL(gMainBundle);
50 CFStringRef sharedPath = CFURLCopyFileSystemPath(sharedURL, kCFURLPOSIXPathStyle);
51 CFStringGetCString(sharedPath, path+mainPathLen+1, 2048-mainPathLen-1, kCFStringEncodingUTF8);
54 gHomePath
= (char*)malloc(mainPathLen
+ 1);
55 strcpy(gHomePath
, path
);
65 pthread_mutex_t mutex
;
71 static PostBuf mainPostBuf
;
73 #define POSTBUFLEN 131072
74 #define POSTBUFMASK 131071
80 buf
= (char *)malloc(POSTBUFLEN
);
81 //HoldMemory(buf, POSTBUFLEN);
82 postTextColor
= SCMakeColor(0.0, 0.0, 0.0, 1.0); //default black
85 pthread_mutex_init(&mutex
, NULL
);
88 void initPostBuffer();
94 void setPostFile(FILE *file
)
99 void vposttext(const char *str
, int length
);
100 void vposttext(const char *str
, int length
)
102 pthread_mutex_lock(&mainPostBuf.mutex
);
105 fwrite(str
, length
, 1, postfile
);
109 for (int i
=0; i
<length
&& str
[i
]; ++i
) {
110 if (((mainPostBuf.wrpos
+1) & POSTBUFMASK
) == mainPostBuf.rdpos
) {
112 //mainPostBuf.Flush(); CANNOT DO THIS FROM OTHER THAN COCOA'S THREAD!
114 if (str
[i
] == '\n') mainPostBuf.buf
[mainPostBuf.wrpos
] = '\r';
115 else mainPostBuf.buf
[mainPostBuf.wrpos
] = str
[i
];
116 mainPostBuf.wrpos
= (mainPostBuf.wrpos
+1) & POSTBUFMASK
;
118 pthread_mutex_unlock(&mainPostBuf.mutex
);
122 int vpost(const char *fmt
, va_list vargs
);
125 int vpost(const char *fmt
, va_list vargs
)
129 int len
= vsnprintf(str
, 512, fmt
, vargs
);
134 void postfl(const char *fmt
, ...
);
135 void postfl(const char *fmt
, ...
)
140 va_start(vargs
, fmt
);
145 void post(const char *fmt
, ...
);
146 void post(const char *fmt
, ...
)
151 va_start(vargs
, fmt
);
156 void error(const char *fmt
, ...
);
157 void error(const char *fmt
, ...
)
162 post("\xa5 ERROR: ");
163 va_start(vargs
, fmt
);
170 void postText(const char *text
, long length
);
171 void postText(const char *text
, long length
)
174 vposttext(text
, length
);
178 void postChar(char c
);
179 void postChar(char c
)
190 // can't from other threads..
194 void PostBuf
::Flush()
197 long localwritepos
= wrpos
;
199 if (localwritepos
>= rdpos
) {
200 numtoread
= localwritepos
- rdpos
;
202 numtoread
= POSTBUFLEN
- (rdpos
- localwritepos
);
206 endpos
= rdpos
+ numtoread
;
207 if (endpos
> POSTBUFLEN
) {
208 // wrap around end in two copies
209 long firstpart
, secondpart
;
211 firstpart
= POSTBUFLEN
- rdpos
;
212 endpos
-= POSTBUFLEN
;
217 NSRange range
= [gPostView selectedRange
];
219 if ([gPostView shouldChangeTextInRange
: range replacementString
: nil]) {
220 // string = [NSString stringWithCString: buf + rdpos length: firstpart];
221 string
= [NSString stringWithCString
: buf
+ rdpos encoding
:NSASCIIStringEncoding
];
222 string
= [string substringToIndex
:firstpart
];
223 [gPostView replaceCharactersInRange
: range withString
: string
];
225 // string = [NSString stringWithCString: buf length: secondpart];
226 string
= [NSString stringWithCString
: buf encoding
:NSASCIIStringEncoding
];
227 string
= [string substringToIndex
: secondpart
];
228 range
= [gPostView selectedRange
];
229 [gPostView replaceCharactersInRange
: range withString
: string
];
230 [gPostView didChangeText
];
232 range
= [gPostView selectedRange
];
233 [gPostView scrollRangeToVisible
: range
];
239 // NSString* string = [NSString stringWithCString: buf + rdpos length: numtoread];
240 NSString
* string
= [NSString stringWithCString
:buf
+ rdpos encoding
:NSASCIIStringEncoding
];
241 string
= [string substringToIndex
:numtoread
];
242 NSRange range
= [gPostView selectedRange
];
244 if ([gPostView shouldChangeTextInRange
: range replacementString
: string
]) {
245 [gPostView replaceCharactersInRange
: range withString
: string
];
246 [gPostView setTextColor
: [NSColor colorWithCalibratedRed
: postTextColor.red
247 green
: postTextColor.green
248 blue
: postTextColor.blue
249 alpha
: postTextColor.alpha
]
250 range
: NSMakeRange(range.location
, [gPostView selectedRange
].location
- range.location
)];
251 [gPostView didChangeText
];
253 range
= [gPostView selectedRange
];
254 [gPostView scrollRangeToVisible
: range
];
257 if (endpos
== POSTBUFLEN
) rdpos
= 0;