-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsl.m
509 lines (432 loc) · 14.9 KB
/
sl.m
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*========================================
* sl.m:
* Copyright 1993,1999 Toyoda Masashi
* [Mac OS X version] Copyright 2003 KITADAI Yukinori
* Last Modified: 2003/ 6/24
*========================================
*/
/* sl version 3.03 : Mac OS X version 1.0.4 */
/* Drawing-speed is up(?) */
/* by KITADAI Yukinori 2003/ 6/24 */
/* sl version 3.03 : Mac OS X version 1.0.3 */
/* Changes font to Monaco */
/* by KITADAI Yukinori 2003/ 6/20 */
/* sl version 3.03 : Mac OS X version 1.0.2 */
/* from Yen symbol to backslash */
/* by KITADAI Yukinori 2003/ 6/18 */
/* sl version 3.03 : Mac OS X version 1.0.1 */
/* Only /dev/console owner meets slMacOSX.(Thanx T.Sumiya) */
/* Puts white under sl. */
/* by KITADAI Yukinori 2003/ 6/14 */
/* sl version 3.03 : Mac OS X version 1.0.0 */
/* by KITADAI Yukinori 2003/ 6/12 */
/* sl version 3.03 : add usleep(20000) */
/* by Toyoda Masashi 1998/ 7/22 */
/* sl version 3.02 : D51 flies! Change options. */
/* by Toyoda Masashi 1993/ 1/19 */
/* sl version 3.01 : Wheel turns smoother */
/* by Toyoda Masashi 1992/12/25 */
/* sl version 3.00 : Add d(D51) option */
/* by Toyoda Masashi 1992/12/24 */
/* sl version 2.02 : Bug fixed.(dust remains in screen) */
/* by Toyoda Masashi 1992/12/17 */
/* sl version 2.01 : Smoke run and disappear. */
/* Change '-a' to accident option. */
/* by Toyoda Masashi 1992/12/16 */
/* sl version 2.00 : Add a(all),l(long),F(Fly!) options. */
/* by Toyoda Masashi 1992/12/15 */
/* sl version 1.02 : Add turning wheel. */
/* by Toyoda Masashi 1992/12/14 */
/* sl version 1.01 : Add more complex smoke. */
/* by Toyoda Masashi 1992/12/14 */
/* sl version 1.00 : SL runs vomitting out smoke. */
/* by Toyoda Masashi 1992/12/11 */
#import <Appkit/Appkit.h>
#include <curses.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "sl.h"
int ACCIDENT = 0;
int LOGO = 0;
int FLY = 0;
int NEXT = 1;
char *buffer;
int *usedlines;
int lineMax,lineMin,colMax,colMin;
int lineMax0, lineMin0;
int lineDiff;
float fontWidth, fontHeight;
@interface SlView : NSView {
NSTextStorage *textStorage;
NSLayoutManager *layoutManager;
NSTextContainer *textContainer;
}
- (id)initWithFrame:(NSRect)frame;
- (void)dealloc;
- (void)drawRect:(NSRect)rect;
- (BOOL)isOpaque;
- (BOOL)isFlipped;
- (void)setColor:(NSColor *)color;
- (void)setString:(NSString *)string;
@end
@implementation SlView
- (id)initWithFrame:(NSRect)frame {
[super initWithFrame:frame];
textStorage = [[NSTextStorage alloc] init];
layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager:layoutManager];
[textStorage replaceCharactersInRange:NSMakeRange(0, [textStorage length]) withString:@""];
[layoutManager release];
textContainer = [[NSTextContainer alloc] init];
//textContainer = [[NSTextContainer alloc] initWithContainerSize:frame.size];
/* [textContainer lineFragmentRectForProposedRect: frame
sweepDirection: NSLineSweepRight
movementDirection: NSLineMovesDown
remainingRect: NULL
];
*/
[layoutManager addTextContainer:textContainer];
[textContainer release];
[layoutManager setUsesScreenFonts:NO];
return self;
}
- (void)dealloc {
[textStorage release];
[super dealloc];
}
- (void)drawRect:(NSRect)rect {
unsigned glyphIndex;
NSRange glyphRange;
NSRect usedRect;
int i;
NSFont *slFont = [NSFont fontWithName:@"Monaco" size:16];
NSDictionary *attr =
[NSDictionary dictionaryWithObjectsAndKeys:
slFont, NSFontAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
nil ];
NSDictionary *attrW =
[NSDictionary dictionaryWithObjectsAndKeys:
slFont, NSFontAttributeName,
[NSColor whiteColor], NSForegroundColorAttributeName,
nil ];
[[NSColor clearColor] set];
//[[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.4 alpha:0.5] set];
NSRectFill([self bounds]);
glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
usedRect = [layoutManager usedRectForTextContainer:textContainer];
[textStorage setAttributes:attr range:NSMakeRange(0, [textStorage length])];
[textStorage addAttribute:NSForegroundColorAttributeName value:[NSColor whiteColor] range:NSMakeRange(0, [textStorage length])];
[layoutManager drawGlyphsForGlyphRange: glyphRange
//atPoint:usedRect.origin];
atPoint:NSMakePoint(-5+1,1)];
[textStorage addAttribute:NSForegroundColorAttributeName value:[NSColor blackColor] range:NSMakeRange(0, [textStorage length])];
[layoutManager drawGlyphsForGlyphRange: glyphRange
//atPoint:usedRect.origin];
atPoint:NSMakePoint(-5,0)];
}
- (BOOL)isOpaque {
return YES;
}
- (BOOL) isFlipped {
return YES;
}
- (void)setString:(NSString *)string {
[textStorage replaceCharactersInRange:NSMakeRange(0, [textStorage length]) withString:string];
[self setNeedsDisplay:YES];
}
- (void)setColor:(NSColor *)color {
[textStorage addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, [textStorage length])];
[self setNeedsDisplay:YES];
}
@end
int check_console( int argc, char *argv[] ) {
struct stat sb;
uid_t uid = getuid();
stat ( "/dev/console", &sb );
if ( sb.st_uid != uid ){
NEXT=0;
original_main ( argc, argv );
}
return 0;
}
void checkUsedArea(void){
int i,j;
lineMax = 0;
lineMin = LINES-1;
colMax = 0;
colMin = COLS-1;
for(i=0;i<LINES;i++)
for(j=0;j<COLS;j++)
if(buffer[i*(COLS+1)+j]!=' '){
if(i>lineMax) lineMax = i;
if(i<lineMin) lineMin = i;
if(j>colMax) colMax = j;
if(j<colMin) colMin = j;
}
}
int my_mvaddstr(int y, int x, char *str)
{
for ( ; x < 0; ++x, ++str)
if (*str == '\0') return ERR;
for ( ; *str != '\0'; ++str, ++x){
if(NEXT==0){
if (mvaddch(y, x, *str) == ERR) return ERR;
}else{
if (x>=COLS || y*COLS+x< 0 || y*COLS+x >= COLS*LINES) return ERR;
else{
buffer[y*(COLS+1)+x]=*str;
}
}
}
return OK;
}
void option(char *str)
{
extern int ACCIDENT, FLY, LONG;
while (*str != '\0') {
switch (*str++) {
case 'a': ACCIDENT = 1; break;
case 'F': FLY = 1; break;
case 'l': LOGO = 1; break;
default: break;
}
}
}
int original_main(int argc, char *argv[])
{
int x, i;
for (i = 1; i < argc; ++i) {
if (*argv[i] == '-') {
option(argv[i] + 1);
}
}
initscr();
signal(SIGINT, SIG_IGN);
noecho();
leaveok(stdscr, TRUE);
scrollok(stdscr, FALSE);
for (x = COLS - 1; ; --x) {
if (LOGO == 0) {
if (add_D51(x) == ERR) break;
} else {
if (add_sl(x) == ERR) break;
}
refresh();
usleep(20000);
}
mvcur(0, COLS - 1, LINES - 1, 0);
endwin();
exit(0);
}
int main(int argc, char *argv[])
{
int dummy = check_console( argc, argv );
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id NSApp = [NSApplication new];
NSWindow *slWindow;
NSString *string;
SlView *slView;
int x, i, j=0, k=0;
NSRect rect;
NSFont *slFont = [NSFont fontWithName:@"Monaco" size:16];
NSDictionary *attr =
[NSDictionary dictionaryWithObjectsAndKeys:
slFont, NSFontAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
nil ];
NSDictionary *attrW =
[NSDictionary dictionaryWithObjectsAndKeys:
slFont, NSFontAttributeName,
[NSColor whiteColor], NSForegroundColorAttributeName,
nil ];
for (i = 1; i < argc; ++i) {
if (*argv[i] == '-') {
option(argv[i] + 1);
}
}
initscr();
signal(SIGINT, SIG_IGN);
noecho();
leaveok(stdscr, TRUE);
scrollok(stdscr, FALSE);
rect = [[NSScreen mainScreen] frame];
//rect.size.height*=0.2;
#ifdef __MAC_OS_X_VERSION_MAX_ALLOWED
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
NSSize size = [@"-" sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [NSFont messageFontOfSize:[slFont pointSize]], NSFontAttributeName, nil]];
fontWidth = size.width;
fontHeight = size.height;
#else
fontWidth = [slFont widthOfString:@" "];
fontHeight = [slFont defaultLineHeightForFont];
#endif
#endif
COLS = (float)rect.size.width/fontWidth;
LINES = (float)rect.size.height/fontHeight;
// NSLog(@"%f %f %f", (float)rect.size.width,fontWidth, COLS);
// NSLog(@"%f %f %f", (float)rect.size.height,fontHeight, LINES);
buffer = (char *)malloc((COLS+1)*LINES*sizeof(char));
for(i=0;i<(COLS+1)*LINES;i++) buffer[i]=' ';
for(i=0;i<LINES-1;i++) buffer[COLS*(i+1)+i] = '\n';
buffer[(COLS+1)*LINES-1] = 0;
slWindow = [[NSWindow alloc]
initWithContentRect:rect
styleMask:NSBorderlessWindowMask //| NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[slWindow setBackgroundColor: [NSColor clearColor]];
[slWindow setOpaque:NO];
[slWindow setLevel:NSScreenSaverWindowLevel];
[slWindow setHasShadow:NO];
slView = [[SlView alloc] initWithFrame:rect];
[[slWindow contentView] addSubview:slView];
[slView release];
[slWindow makeKeyAndOrderFront:nil];
for (x = COLS - 1; ; --x) {
unsigned glyphIndex;
NSRange glyphRange;
NSRect usedRect;
if (LOGO == 0) {
if (add_D51(x) == ERR) break;
} else {
if (add_sl(x) == ERR) break;
}
checkUsedArea();
string = [[NSString alloc] initWithData:[NSData dataWithBytes:buffer length:(COLS+1)*LINES-1] encoding:NSASCIIStringEncoding];
[slView setString:string];
[string release];
[slView display];
}
mvcur(0, COLS - 1, LINES - 1, 0);
endwin();
[slWindow release];
[pool release];
// [NSApp release];
return 0;
}
int add_sl(int x)
{
static char *sl[LOGOPATTERNS][LOGOHIGHT + 1]
= {{LOGO1, LOGO2, LOGO3, LOGO4, LWHL11, LWHL12, DELLN},
{LOGO1, LOGO2, LOGO3, LOGO4, LWHL21, LWHL22, DELLN},
{LOGO1, LOGO2, LOGO3, LOGO4, LWHL31, LWHL32, DELLN},
{LOGO1, LOGO2, LOGO3, LOGO4, LWHL41, LWHL42, DELLN},
{LOGO1, LOGO2, LOGO3, LOGO4, LWHL51, LWHL52, DELLN},
{LOGO1, LOGO2, LOGO3, LOGO4, LWHL61, LWHL62, DELLN}};
static char *coal[LOGOHIGHT + 1]
= {LCOAL1, LCOAL2, LCOAL3, LCOAL4, LCOAL5, LCOAL6, DELLN};
static char *car[LOGOHIGHT + 1]
= {LCAR1, LCAR2, LCAR3, LCAR4, LCAR5, LCAR6, DELLN};
int i, y, py1 = 0, py2 = 0, py3 = 0;
if (x < - LOGOLENGTH) return ERR;
y = LINES / 2 - 3;
if (FLY == 1) {
y = (x / 6) + LINES - (COLS / 6) - LOGOHIGHT;
py1 = 2; py2 = 4; py3 = 6;
}
for (i = 0; i <= LOGOHIGHT; ++i) {
my_mvaddstr(y + i, x, sl[(LOGOLENGTH + x) / 3 % LOGOPATTERNS][i]);
my_mvaddstr(y + i + py1, x + 21, coal[i]);
my_mvaddstr(y + i + py2, x + 42, car[i]);
my_mvaddstr(y + i + py3, x + 63, car[i]);
}
if (ACCIDENT == 1) {
add_man(y + 1, x + 14);
add_man(y + 1 + py2, x + 45); add_man(y + 1 + py2, x + 53);
add_man(y + 1 + py3, x + 66); add_man(y + 1 + py3, x + 74);
}
add_smoke(y - 1, x + LOGOFUNNEL);
return OK;
}
add_D51(int x)
{
static char *d51[D51PATTERNS][D51HIGHT + 1]
= {{D51STR1, D51STR2, D51STR3, D51STR4, D51STR5, D51STR6, D51STR7,
D51WHL11, D51WHL12, D51WHL13, D51DEL},
{D51STR1, D51STR2, D51STR3, D51STR4, D51STR5, D51STR6, D51STR7,
D51WHL21, D51WHL22, D51WHL23, D51DEL},
{D51STR1, D51STR2, D51STR3, D51STR4, D51STR5, D51STR6, D51STR7,
D51WHL31, D51WHL32, D51WHL33, D51DEL},
{D51STR1, D51STR2, D51STR3, D51STR4, D51STR5, D51STR6, D51STR7,
D51WHL41, D51WHL42, D51WHL43, D51DEL},
{D51STR1, D51STR2, D51STR3, D51STR4, D51STR5, D51STR6, D51STR7,
D51WHL51, D51WHL52, D51WHL53, D51DEL},
{D51STR1, D51STR2, D51STR3, D51STR4, D51STR5, D51STR6, D51STR7,
D51WHL61, D51WHL62, D51WHL63, D51DEL}};
static char *coal[D51HIGHT + 1]
= {COAL01, COAL02, COAL03, COAL04, COAL05,
COAL06, COAL07, COAL08, COAL09, COAL10, COALDEL};
int y, i, dy = 0;
if (x < - D51LENGTH) return ERR;
y = LINES / 2 - 5;
if (FLY == 1) {
y = (x / 7) + LINES - (COLS / 7) - D51HIGHT;
dy = 1;
}
for (i = 0; i <= D51HIGHT; ++i) {
my_mvaddstr(y + i, x, d51[(D51LENGTH + x) % D51PATTERNS][i]);
my_mvaddstr(y + i + dy, x + 53, coal[i]);
}
if (ACCIDENT == 1) {
add_man(y + 2, x + 43);
add_man(y + 2, x + 47);
}
add_smoke(y - 1, x + D51FUNNEL);
return OK;
}
int add_man(int y, int x)
{
static char *man[2][2] = {{"", "(O)"}, {"Help!", "\\O/"}};
int i;
for (i = 0; i < 2; ++i) {
my_mvaddstr(y + i, x, man[(LOGOLENGTH + x) / 12 % 2][i]);
}
}
int add_smoke(int y, int x)
#define SMOKEPTNS 16
{
static struct smokes {
int y, x;
int ptrn, kind;
} S[1000];
static int sum = 0;
static char *Smoke[2][SMOKEPTNS]
= {{"( )", "( )", "( )", "( )", "( )",
"( )" , "( )" , "( )" , "()" , "()" ,
"O" , "O" , "O" , "O" , "O" ,
" " },
{"(@@@)", "(@@@@)", "(@@@@)", "(@@@)", "(@@)",
"(@@)" , "(@)" , "(@)" , "@@" , "@@" ,
"@" , "@" , "@" , "@" , "@" ,
" " }};
static char *Eraser[SMOKEPTNS]
= {" ", " ", " ", " ", " ",
" " , " " , " " , " " , " " ,
" " , " " , " " , " " , " " ,
" " };
static int dy[SMOKEPTNS] = { 2, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0 };
static int dx[SMOKEPTNS] = {-2, -1, 0, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 3, 3, 3 };
int i;
if (x % 4 == 0) {
for (i = 0; i < sum; ++i) {
my_mvaddstr(S[i].y, S[i].x, Eraser[S[i].ptrn]);
S[i].y -= dy[S[i].ptrn];
S[i].x += dx[S[i].ptrn];
S[i].ptrn += (S[i].ptrn < SMOKEPTNS - 1) ? 1 : 0;
my_mvaddstr(S[i].y, S[i].x, Smoke[S[i].kind][S[i].ptrn]);
}
my_mvaddstr(y, x, Smoke[sum % 2][0]);
S[sum].y = y; S[sum].x = x;
S[sum].ptrn = 0; S[sum].kind = sum % 2;
sum ++;
}
}