RSS

(root)/iphone/common : 50 : 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-30 10:05:15
Revision ID: dognotdog@gmail.com-20091030090515-tzzgj0hpoq1qi7ik
tappity server is changed to automagically hook into the relevant classes when linked with -ObjC; network header is changed to contain msg id in addition to size

Show diffs side-by-side

added added

removed removed

10
10
#import "Tappity.h"
11
11
#import <QuartzCore/QuartzCore.h>
12
12
#import <OpenGLES/ES1/gl.h>
 
13
#import <OpenGLES/EAGL.h>
13
14
#import <CoreLocation/CoreLocation.h>
 
15
#import <objc/runtime.h>
14
16
 
15
17
 
16
18
static void _setStandardSocketOpts(int socket)
22
24
        setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
23
25
}
24
26
 
 
27
@interface CLLocationManager (TappityServer)
 
28
 
 
29
- (void) setDelegate_tappity: (id) delegate;
 
30
- (void) startUpdatingLocation_tappity;
 
31
- (void) stopUpdatingLocation_tappity;
 
32
- (void) startUpdatingHeading_tappity;
 
33
- (void) stopUpdatingHeading_tappity;
 
34
 
 
35
@end
 
36
 
 
37
@interface UIAccelerometer (TappityServer)
 
38
 
 
39
- (void) setUpdateInterval_tappity: (NSTimeInterval) dt;
 
40
 
 
41
@end
 
42
 
 
43
@interface EAGLContext (TappityServer)
 
44
 
 
45
- (BOOL)presentRenderbuffer_tappity:(NSUInteger)target;
 
46
 
 
47
 
 
48
@end
 
49
 
 
50
 
 
51
@implementation CLLocationManager (TappityServer)
 
52
 
 
53
- (void) setDelegate_tappity: (id) obj
 
54
{
 
55
        if ([obj respondsToSelector: @selector(locationManager:didUpdateToLocation:fromLocation:)])
 
56
        {
 
57
                [[TappityServer sharedServer] setLocationDelegate: obj];
 
58
        }
 
59
        if ([obj respondsToSelector: @selector(locationManager:didUpdateHeading:)])
 
60
        {
 
61
                [[TappityServer sharedServer] setHeadingDelegate: obj];
 
62
        }
 
63
}
 
64
 
 
65
- (void) startUpdatingLocation_tappity
 
66
{
 
67
        [[TappityServer sharedServer] sendData: [NSData data] withIdentifier: kTapStartUpdatingLocation];
 
68
}
 
69
 
 
70
- (void) stopUpdatingLocation_tappity
 
71
{
 
72
        [[TappityServer sharedServer] sendData: [NSData data] withIdentifier: kTapStopUpdatingLocation];
 
73
}
 
74
 
 
75
- (void) startUpdatingHeading_tappity
 
76
{
 
77
        [[TappityServer sharedServer] sendData: [NSData data] withIdentifier: kTapStartUpdatingHeading];
 
78
}
 
79
 
 
80
- (void) stopUpdatingHeading_tappity
 
81
{
 
82
        [[TappityServer sharedServer] sendData: [NSData data] withIdentifier: kTapStopUpdatingHeading];
 
83
}
 
84
 
 
85
 
 
86
@end
 
87
 
 
88
@implementation UIAccelerometer (TappityServer)
 
89
 
 
90
- (void) setUpdateInterval_tappity: (NSTimeInterval) dt
 
91
{
 
92
        [[TappityServer sharedServer] setAccelerometerUpdateInterval: dt];
 
93
        
 
94
        [self setUpdateInterval_tappity: dt];
 
95
}
 
96
 
 
97
@end
 
98
 
 
99
@implementation EAGLContext (TappityServer)
 
100
 
 
101
- (BOOL)presentRenderbuffer_tappity:(NSUInteger)target
 
102
{
 
103
        [TappityServer glGrabPoint];
 
104
        return [self presentRenderbuffer_tappity: target];
 
105
}
 
106
 
 
107
 
 
108
@end
 
109
 
 
110
static void swapMethods(Class c, SEL sela, SEL selb)
 
111
{
 
112
        Method ma = class_getInstanceMethod(c, sela);
 
113
        Method mb = class_getInstanceMethod(c, selb);
 
114
        method_exchangeImplementations(ma, mb);
 
115
        
 
116
}
 
117
 
 
118
static void catch_pipe(int sig_num)
 
119
{
 
120
    signal(SIGPIPE, catch_pipe);
 
121
}
 
122
 
 
123
__attribute__((constructor)) void initTappityServer(void)
 
124
{
 
125
//      __builtin_printf("MOOOOOOOOOOOOOOO\n");
 
126
        /* TODO:
 
127
                1. intercept UIAccelerometer
 
128
                2. intercept CLLocationManager
 
129
                class_getInstanceMethod(), method_setImplementation()
 
130
        */
 
131
 
 
132
        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
 
133
        
 
134
        NSString* name = [[NSBundle mainBundle] bundleIdentifier];
 
135
        
 
136
        [[TappityServer sharedServer] startWithName: name];
 
137
        
 
138
        Class lmc = NSClassFromString(@"CLLocationManager");
 
139
        if (lmc)
 
140
        {
 
141
                swapMethods(lmc, @selector(setDelegate:), @selector(setDelegate_tappity:));
 
142
                swapMethods(lmc, @selector(startUpdatingHeading:), @selector(startUpdatingHeading_tappity:));
 
143
                swapMethods(lmc, @selector(startUpdatingLocation:), @selector(startUpdatingLocation_tappity:));
 
144
        }
 
145
 
 
146
        Class accc = [UIAccelerometer class];
 
147
        swapMethods(accc, @selector(setUpdateInterval:), @selector(setUpdateInterval_tappity:));
 
148
 
 
149
        Class glc = NSClassFromString(@"EAGLContext");
 
150
        if (glc)
 
151
                swapMethods(glc, @selector(presentRenderbuffer:), @selector(presentRenderbuffer_tappity:));
 
152
 
 
153
        
 
154
        [pool drain];
 
155
        
 
156
        signal(SIGPIPE, catch_pipe);
 
157
 
 
158
}
 
159
 
 
160
 
 
161
 
25
162
 
26
163
@implementation TappityServer
27
164
 
 
165
+ (void) initialize
 
166
{
 
167
        
 
168
}
 
169
 
28
170
- (id) init
29
171
{
30
172
        if (!(self = [super init]))
318
460
                eventPlaybackBusy = NO;
319
461
}
320
462
 
321
 
- (void) dataReceived: (NSData*) tapData
 
463
- (void) dataReceived: (NSDictionary*) dataDict
322
464
{
323
 
        assert([tapData length] > 4);
324
 
        
325
 
        
326
 
        NSDictionary* tapDict = [NSPropertyListSerialization propertyListFromData: tapData mutabilityOption:NSPropertyListImmutable format: NULL errorDescription: NULL];
327
 
        assert(tapDict);
328
 
        switch([[tapDict objectForKey: TapIdKey] intValue])
 
465
        uint32_t messageId = [[dataDict objectForKey: TapIdKey] intValue];
 
466
 
 
467
        NSData* tapData = [dataDict objectForKey: TapDataKey];
 
468
        
 
469
//      NSDictionary* tapDict = [NSPropertyListSerialization propertyListFromData: tapData mutabilityOption:NSPropertyListImmutable format: NULL errorDescription: NULL];
 
470
        assert(tapData);
 
471
        switch(messageId)
329
472
        {
330
473
/*
331
474
                case kTapTouchEvent:
365
508
*/
366
509
                case kTapRecordedEvent:
367
510
                {                       
368
 
                        NSDictionary* eventDict = [tapDict objectForKey: TapRecordedEventKey];
 
511
                        NSDictionary* eventDict = [NSPropertyListSerialization propertyListFromData: tapData mutabilityOption:NSPropertyListImmutable format: NULL errorDescription: NULL];
369
512
                        
370
513
                        if (!queuedEvents)
371
514
                                queuedEvents = [[NSMutableArray alloc] init];
380
523
                }
381
524
                case kTapAcceleration:
382
525
                {                       
383
 
                        NSArray* accel = [tapDict objectForKey: TapAccelerationKey];
 
526
                        NSArray* accel = [NSPropertyListSerialization propertyListFromData: tapData mutabilityOption:NSPropertyListImmutable format: NULL errorDescription: NULL];
384
527
                        
385
528
                        UIAccelerometer* accm = [UIAccelerometer sharedAccelerometer];
386
529
                        float x = [[accel objectAtIndex: 0] floatValue];
393
536
                case kTapLocation:
394
537
                {
395
538
                        static CLLocation* oldLoc = nil;
396
 
                        CLLocation* loc = [NSKeyedUnarchiver unarchiveObjectWithData: [tapDict objectForKey: TapLocationKey]];
 
539
                        CLLocation* loc = [NSKeyedUnarchiver unarchiveObjectWithData: tapData];
397
540
                        if ([locationDelegate respondsToSelector: @selector(locationManager:didUpdateToLocation:fromLocation:)])
398
541
                        {
399
542
                                [locationDelegate locationManager: (id)self didUpdateToLocation: loc fromLocation: oldLoc];
403
546
                }
404
547
                case kTapCompass:
405
548
                {
406
 
                        CLHeading* hdg = [NSKeyedUnarchiver unarchiveObjectWithData: [tapDict objectForKey: TapCompassKey]];
 
549
                        CLHeading* hdg = [NSKeyedUnarchiver unarchiveObjectWithData: tapData];
407
550
                        if ([headingDelegate respondsToSelector: @selector(locationManager:didUpdateHeading:)])
408
551
                        {
409
552
                                [headingDelegate locationManager: (id)self didUpdateHeading: hdg];
491
634
                NSData* imgData = UIImagePNGRepresentation(img);
492
635
        
493
636
                if (imgData)
494
 
                        [self sendData: imgData];
 
637
                        [self sendData: imgData withIdentifier: kTapRawScreenshot];
495
638
 
496
639
                
497
640
                [pool drain];
549
692
}
550
693
 
551
694
 
 
695
- (void) setAccelerometerUpdateInterval: (NSTimeInterval) dt
 
696
{
 
697
        CFSwappedFloat64 sdt = CFConvertFloat64HostToSwapped(dt);
 
698
        NSData* data = [NSData dataWithBytes: &sdt length: sizeof(CFSwappedFloat64)];
 
699
        [self sendData: data withIdentifier: kTapAccelerometerUpdateInterval];
 
700
}
552
701
 
553
702
/*
554
703
- (void)performTouches: (NSSet*) touches inView: (UIView *)view
618
767
 
619
768
- (void) glGrabPoint
620
769
{
621
 
        if (!commsSocket)
 
770
        if (!commsSocket || (commsSocket == -1))
622
771
                return;
623
772
 
624
773
        NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
629
778
                
630
779
                NSData* data = UIImagePNGRepresentation(image);
631
780
                
632
 
                [self sendData: data];
 
781
                [self sendData: data withIdentifier: kTapRawScreenshot];
633
782
                
634
783
                lastGlUpdate = now;
635
784
        }

Loggerhead 1.17 is a web-based interface for Bazaar branches