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
);
158 void error(const char *fmt
, ...
);
159 void error(const char *fmt
, ...
)
164 post(BULLET
" ERROR: ");
165 va_start(vargs
, fmt
);
172 void postText(const char *text
, long length
);
173 void postText(const char *text
, long length
)
176 vposttext(text
, length
);
180 void postChar(char c
);
181 void postChar(char c
)
192 // can't from other threads..
196 void PostBuf
::Flush()
199 long localwritepos
= wrpos
;
201 if (localwritepos
>= rdpos
) {
202 numtoread
= localwritepos
- rdpos
;
204 numtoread
= POSTBUFLEN
- (rdpos
- localwritepos
);
208 endpos
= rdpos
+ numtoread
;
209 if (endpos
> POSTBUFLEN
) {
210 // wrap around end in two copies
211 long firstpart
, secondpart
;
213 firstpart
= POSTBUFLEN
- rdpos
;
214 endpos
-= POSTBUFLEN
;
219 NSRange range
= [gPostView selectedRange
];
221 if ([gPostView shouldChangeTextInRange
: range replacementString
: nil]) {
222 string
= [NSString stringWithCString
: buf
+ rdpos length
: firstpart
];
223 [gPostView replaceCharactersInRange
: range withString
: string
];
225 string
= [NSString stringWithCString
: buf length
: secondpart
];
226 range
= [gPostView selectedRange
];
227 [gPostView replaceCharactersInRange
: range withString
: string
];
228 [gPostView didChangeText
];
230 range
= [gPostView selectedRange
];
231 [gPostView scrollRangeToVisible
: range
];
237 NSString
* string
= [NSString stringWithCString
: buf
+ rdpos length
: numtoread
];
238 NSRange range
= [gPostView selectedRange
];
240 if ([gPostView shouldChangeTextInRange
: range replacementString
: string
]) {
241 [gPostView replaceCharactersInRange
: range withString
: string
];
242 [gPostView setTextColor
: [NSColor colorWithCalibratedRed
: postTextColor.red
243 green
: postTextColor.green
244 blue
: postTextColor.blue
245 alpha
: postTextColor.alpha
]
246 range
: NSMakeRange(range.location
, [gPostView selectedRange
].location
- range.location
)];
247 [gPostView didChangeText
];
249 range
= [gPostView selectedRange
];
250 [gPostView scrollRangeToVisible
: range
];
253 if (endpos
== POSTBUFLEN
) rdpos
= 0;