RSS

(root)/iphone/common : 48 : tappity/source/TappityServer.m

To get this branch, use:
bzr branch /browse/iphone/common

« back to all changes in this revision

Viewing changes to tappity/source/TappityServer.m

Dömötör Gulyás
2009-10-28 02:04:32
Revision ID: dognotdog@gmail.com-20091028010432-ncgjtcsyxavch41q
tappity improvements

Show diffs side-by-side

added added

removed removed

9
9
#import "TappityServer.h"
10
10
#import "Tappity.h"
11
11
#import <QuartzCore/QuartzCore.h>
 
12
#import <OpenGLES/ES1/gl.h>
12
13
 
13
14
 
14
15
static void _setStandardSocketOpts(int socket)
26
27
 
27
28
@implementation TappityServer
28
29
 
 
30
- (id) init
 
31
{
 
32
        if (!(self = [super init]))
 
33
                return nil;
 
34
 
 
35
        desiredFrameInterval = 0.333;
 
36
        
 
37
        receiveDataOnMainThread = YES;
 
38
        
 
39
        return self;
 
40
}
29
41
 
30
42
- (void) acceptThread: (id) info
31
43
{
101
113
                        commsSocket = newSocket;
102
114
                        
103
115
                        
 
116
                        [netService stop];
 
117
                        [netService release];
 
118
                        netService = nil;
 
119
 
104
120
                        if (!sendLock)
105
121
                                sendLock = [[NSCondition alloc] init];
106
122
                        
112
128
 
113
129
                        //[self runThreadWithSelector: @selector(screenieThread:)];
114
130
                        
 
131
                        
115
132
                        NSLog(@"Accepted connection.");
116
133
                }
117
134
 
128
145
 
129
146
- (void) startScreenieTimer
130
147
{
131
 
        captureTimer = [NSTimer scheduledTimerWithTimeInterval: 0.2 target: self selector: @selector(captureScreen) userInfo: nil repeats: YES];
 
148
        captureTimer = [NSTimer scheduledTimerWithTimeInterval: desiredFrameInterval target: self selector: @selector(captureScreen) userInfo: nil repeats: YES];
132
149
        
133
150
        
134
151
        if (!capturedImagesCondition)
159
176
        return YES;
160
177
}
161
178
 
162
 
- (void) start
 
179
- (void) startWithName: (NSString*) name
163
180
{
164
181
        NSLog(@"starting tappity server");
 
182
        
 
183
        [serviceName release];
 
184
        serviceName = [name retain];
 
185
 
165
186
        myAddress.sin_family = AF_INET;                                 // host byte order
166
187
        myAddress.sin_port = htons(1234);                               // short, network byte order, any port
167
188
        myAddress.sin_addr.s_addr = htonl(INADDR_ANY);  // auto-fill with my IP
179
200
 
180
201
        [self runThreadWithSelector: @selector(acceptThread:)];
181
202
        
182
 
        if([self enableBonjourWithDomain: @"" applicationProtocol: @"_tappity._tcp" name: @"tappity"])
 
203
        if([self enableBonjourWithDomain: @"" applicationProtocol: @"_tappity._tcp" name: serviceName])
183
204
                NSLog(@"tappity bounjour advertisments up and running");
184
205
 
185
206
}
325
346
 
326
347
                        break;
327
348
                }
 
349
                case kTapAcceleration:
 
350
                {
 
351
//                      NSLog(@"RECORDED EVENT");
 
352
                        
 
353
                        NSArray* accel = [tapDict objectForKey: TapAccelerationKey];
 
354
                        
 
355
                        UIAccelerometer* accm = [UIAccelerometer sharedAccelerometer];
 
356
                        float x = [[accel objectAtIndex: 0] floatValue];
 
357
                        float y = [[accel objectAtIndex: 1] floatValue];
 
358
                        float z = [[accel objectAtIndex: 2] floatValue];
 
359
                        [accm _acceleratedInX: x y: y z: z timestamp: [[accel objectAtIndex: 3] doubleValue]];
 
360
                        
 
361
                        break;
 
362
                }
328
363
        }
329
364
        
330
365
        [tapData release];
358
393
 
359
394
- (void) captureScreen
360
395
{
 
396
        NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
 
397
        
 
398
        if (now < lastGlUpdate + 2.0*desiredFrameInterval)
 
399
                return;
 
400
                
 
401
        // return if any gl has been updated at all
 
402
        if (lastGlUpdate)
 
403
                return;
 
404
        
361
405
        [self performSelectorOnMainThread: @selector(getWindowToCapture) withObject: nil waitUntilDone: NO];
362
406
 
363
407
        UIWindow* window = windowToCapture;
364
408
        
365
409
        UIImage* img = [self imageFromView: window];
366
410
        
 
411
        lastQuartzUpdate = now;
 
412
        
367
413
        if (img)
368
414
                [self convertAndSendImage: img];
369
415
}
441
487
                usleep(200);
442
488
        }
443
489
 
 
490
        [self threadWillExit: info];
 
491
 
444
492
        @synchronized(self)
445
493
        {
446
494
                [self release];
471
519
}
472
520
*/
473
521
 
 
522
static UIImage* imageFromGLData(NSData* data)
 
523
{
 
524
    // make data provider with data.
 
525
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, [data bytes], [data length], NULL);
 
526
   
 
527
    // prep the ingredients
 
528
    int bitsPerComponent = 8;
 
529
    int bitsPerPixel = 32;
 
530
    int bytesPerRow = 4 * 320;
 
531
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
 
532
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
 
533
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
 
534
   
 
535
    // make the cgimage
 
536
    CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
 
537
   
 
538
    // then make the uiimage from that
 
539
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
 
540
    return myImage;
 
541
}
 
542
 
 
543
static NSData* dataFromGL(void)
 
544
{
 
545
    NSInteger myDataLength = 320 * 480 * 4;
 
546
        
 
547
        NSMutableData* data = [NSMutableData dataWithLength: myDataLength];
 
548
   
 
549
    // allocate array and read pixels into it.
 
550
    void* buffer = [data mutableBytes];
 
551
    glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
 
552
        
 
553
        size_t bpr = 4*320;
 
554
        void* rowbuf = calloc(1, bpr);
 
555
        
 
556
        // swap image in Y
 
557
        for (size_t i = 0; i < 480/2; ++i)
 
558
        {
 
559
                memcpy(rowbuf, buffer + i*bpr, bpr);
 
560
                memcpy(buffer + i*bpr, buffer + (480-i-1)*bpr, bpr);
 
561
                memcpy(buffer + (480-i-1)*bpr, rowbuf, bpr);
 
562
        }
 
563
        
 
564
        free(rowbuf);
 
565
 
 
566
        return data;
 
567
}
 
568
 
 
569
- (void) glGrabPoint
 
570
{
 
571
        if (!commsSocket)
 
572
                return;
 
573
 
 
574
        NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
 
575
        if (lastGlUpdate + desiredFrameInterval < now)
 
576
        {
 
577
                NSData* rawData = dataFromGL();
 
578
                UIImage* image = imageFromGLData(rawData);
 
579
                
 
580
                NSData* data = UIImagePNGRepresentation(image);
 
581
                
 
582
                [self sendData: data];
 
583
                
 
584
                lastGlUpdate = now;
 
585
        }
 
586
}
 
587
 
 
588
+ (void) glGrabPoint
 
589
{
 
590
        [[self sharedServer] glGrabPoint];
 
591
}
 
592
 
 
593
+ (id) sharedServer
 
594
{
 
595
        static id sharedInstance = nil;
 
596
        if (!sharedInstance)
 
597
                sharedInstance = [[TappityServer alloc] init];
 
598
        
 
599
        
 
600
        return sharedInstance;
 
601
}
 
602
 
474
603
- (void) dealloc
475
604
{
476
 
        
 
605
        [serviceName release];
 
606
        [captureTimer invalidate];
 
607
        [capturedImagesCondition release];
 
608
        [capturedImagesQueue release];
 
609
 
477
610
        [netService stop];
478
611
        [netService release];
479
612
 

Loggerhead 1.17 is a web-based interface for Bazaar branches