Browse files

holy crap this code is fugly but it works

  • Loading branch information...
1 parent b39da64 commit 7fbc87890301e4136567292031f167e0f6d978a1 chendo committed Aug 17, 2009
Showing with 343 additions and 1 deletion.
  1. +119 −0 Keymap.plist
  2. +12 −0 MethodSwizzle.h
  3. +94 −0 MethodSwizzle.m
  4. +16 −0 TPServerControllerPatch.h
  5. +19 −0 TPServerControllerPatch.m
  6. +8 −0 Telefrag.h
  7. +65 −1 Telefrag.m
  8. +10 −0 Telefrag.xcodeproj/project.pbxproj
View
119 Keymap.plist
@@ -0,0 +1,119 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<!--
+ Keymap.plist
+ Telefrag
+
+ Created by chendo on 18/08/09.
+ Copyright 2009 __MyCompanyName__. All rights reserved.
+-->
+<plist version="1.0">
+ <dict>
+<key>123</key><integer>123</integer>
+<key>99</key><integer>122</integer>
+<key>15</key><integer>35</integer>
+<key>36</key><integer>36</integer>
+<key>8</key><integer>38</integer>
+<key>78</key><integer>78</integer>
+<key>124</key><integer>124</integer>
+<key>103</key><integer>122</integer>
+<key>16</key><integer>3</integer>
+<key>37</key><integer>45</integer>
+<key>9</key><integer>40</integer>
+<key>81</key><integer>81</integer>
+<key>125</key><integer>125</integer>
+<key>82</key><integer>82</integer>
+<key>17</key><integer>16</integer>
+<key>40</key><integer>17</integer>
+<key>38</key><integer>4</integer>
+<key>126</key><integer>126</integer>
+<key>105</key><integer>122</integer>
+<key>83</key><integer>83</integer>
+<key>20</key><integer>85</integer>
+<key>18</key><integer>83</integer>
+<key>41</key><integer>1</integer>
+<key>39</key><integer>78</integer>
+<key>106</key><integer>122</integer>
+<key>84</key><integer>84</integer>
+<key>21</key><integer>86</integer>
+<key>19</key><integer>84</integer>
+<key>42</key><integer>42</integer>
+<key>107</key><integer>122</integer>
+<key>85</key><integer>85</integer>
+<key>43</key><integer>13</integer>
+<key>108</key><integer>122</integer>
+<key>86</key><integer>86</integer>
+<key>65</key><integer>65</integer>
+<key>22</key><integer>88</integer>
+<key>110</key><integer>122</integer>
+<key>109</key><integer>122</integer>
+<key>87</key><integer>87</integer>
+<key>23</key><integer>87</integer>
+<key>44</key><integer>6</integer>
+<key>111</key><integer>122</integer>
+<key>24</key><integer>30</integer>
+<key>45</key><integer>11</integer>
+<key>66</key><integer>124</integer>
+<key>112</key><integer>122</integer>
+<key>88</key><integer>88</integer>
+<key>25</key><integer>92</integer>
+<key>46</key><integer>46</integer>
+<key>67</key><integer>67</integer>
+<key>113</key><integer>122</integer>
+<key>91</key><integer>91</integer>
+<key>89</key><integer>89</integer>
+<key>26</key><integer>89</integer>
+<key>47</key><integer>9</integer>
+<key>70</key><integer>123</integer>
+<key>114</key><integer>114</integer>
+<key>92</key><integer>92</integer>
+<key>0</key><integer>0</integer>
+<key>27</key><integer>33</integer>
+<key>50</key><integer>50</integer>
+<key>48</key><integer>48</integer>
+<key>71</key><integer>71</integer>
+<key>69</key><integer>69</integer>
+<key>115</key><integer>115</integer>
+<key>30</key><integer>81</integer>
+<key>1</key><integer>31</integer>
+<key>28</key><integer>91</integer>
+<key>51</key><integer>51</integer>
+<key>49</key><integer>49</integer>
+<key>72</key><integer>125</integer>
+<key>116</key><integer>116</integer>
+<key>10</key><integer>10</integer>
+<key>31</key><integer>15</integer>
+<key>2</key><integer>14</integer>
+<key>29</key><integer>82</integer>
+<key>52</key><integer>76</integer>
+<key>117</key><integer>117</integer>
+<key>95</key><integer>95</integer>
+<key>32</key><integer>5</integer>
+<key>3</key><integer>32</integer>
+<key>53</key><integer>71</integer>
+<key>118</key><integer>122</integer>
+<key>96</key><integer>122</integer>
+<key>4</key><integer>2</integer>
+<key>75</key><integer>75</integer>
+<key>11</key><integer>7</integer>
+<key>120</key><integer>122</integer>
+<key>119</key><integer>119</integer>
+<key>97</key><integer>122</integer>
+<key>5</key><integer>34</integer>
+<key>76</key><integer>76</integer>
+<key>12</key><integer>39</integer>
+<key>33</key><integer>75</integer>
+<key>121</key><integer>121</integer>
+<key>100</key><integer>122</integer>
+<key>98</key><integer>122</integer>
+<key>13</key><integer>95</integer>
+<key>34</key><integer>8</integer>
+<key>6</key><integer>41</integer>
+<key>122</key><integer>122</integer>
+<key>101</key><integer>122</integer>
+<key>14</key><integer>65</integer>
+<key>35</key><integer>37</integer>
+<key>7</key><integer>12</integer>
+<key>77</key><integer>126</integer>
+ </dict>
+</plist>
View
12 MethodSwizzle.h
@@ -0,0 +1,12 @@
+//
+// MethodSwizzle.h
+// Telefrag
+//
+// Created by chendo on 17/08/09.
+// Copyright 2009 __MyCompanyName__. All rights reserved.
+//
+
+#import <objc/objc.h>
+
+BOOL ClassMethodSwizzle(Class klass, SEL origSel, SEL altSel);
+BOOL MethodSwizzle(Class klass, SEL origSel, SEL altSel);
View
94 MethodSwizzle.m
@@ -0,0 +1,94 @@
+#import "MethodSwizzle.h"
+#import <stdlib.h>
+#import <string.h>
+#import <objc/objc-runtime.h>
+#import <objc/objc-class.h>
+#import <uuid/uuid.h>
+
+static BOOL _PerformSwizzle(Class klass, SEL origSel, SEL altSel, BOOL forInstance);
+
+BOOL ClassMethodSwizzle(Class klass, SEL origSel, SEL altSel) {
+ return _PerformSwizzle(klass, origSel, altSel, NO);
+}
+
+BOOL MethodSwizzle(Class klass, SEL origSel, SEL altSel) {
+ return _PerformSwizzle(klass, origSel, altSel, YES);
+}
+
+// if the origSel isn't present in the class, pull it up from where it exists
+// then do the swizzle
+BOOL _PerformSwizzle(Class klass, SEL origSel, SEL altSel, BOOL forInstance) {
+ // First, make sure the class isn't nil
+ if (klass != nil) {
+ Method origMethod = NULL, altMethod = NULL;
+
+ // Next, look for the methods
+ Class iterKlass = (forInstance ? klass : klass->isa);
+ void *iterator = NULL;
+ struct objc_method_list *mlist = class_nextMethodList(iterKlass, &iterator);
+ while (mlist != NULL) {
+ int i;
+ for (i = 0; i < mlist->method_count; ++i) {
+ if (mlist->method_list[i].method_name == origSel) {
+ origMethod = &mlist->method_list[i];
+ break;
+ }
+ if (mlist->method_list[i].method_name == altSel) {
+ altMethod = &mlist->method_list[i];
+ break;
+ }
+ }
+ mlist = class_nextMethodList(iterKlass, &iterator);
+ }
+
+ if (origMethod == NULL || altMethod == NULL) {
+ // one or both methods are not in the immediate class
+ // try searching the entire hierarchy
+ // remember, iterKlass is the class we care about - klass || klass->isa
+ // class_getInstanceMethod on a metaclass is the same as class_getClassMethod on the real class
+ BOOL pullOrig = NO, pullAlt = NO;
+ if (origMethod == NULL) {
+ origMethod = class_getInstanceMethod(iterKlass, origSel);
+ pullOrig = YES;
+ }
+ if (altMethod == NULL) {
+ altMethod = class_getInstanceMethod(iterKlass, altSel);
+ pullAlt = YES;
+ }
+
+ // die now if one of the methods doesn't exist anywhere in the hierarchy
+ // this way we won't make any changes to the class if we can't finish
+ if (origMethod == NULL || altMethod == NULL) {
+ return NO;
+ }
+
+ // we can safely assume one of the two methods, at least, will be pulled
+ // pull them up
+ size_t listSize = sizeof(struct objc_method_list);
+ if (pullOrig && pullAlt) listSize += sizeof(struct objc_method); // need 2 methods
+ struct objc_method_list *mlist = malloc(listSize);
+ mlist->obsolete = NULL;
+ int i = 0;
+ if (pullOrig) {
+ memcpy(&mlist->method_list[i], origMethod, sizeof(struct objc_method));
+ origMethod = &mlist->method_list[i];
+ i++;
+ }
+ if (pullAlt) {
+ memcpy(&mlist->method_list[i], altMethod, sizeof(struct objc_method));
+ altMethod = &mlist->method_list[i];
+ i++;
+ }
+ mlist->method_count = i;
+ class_addMethods(iterKlass, mlist);
+ }
+
+ // now swizzle
+ IMP temp = origMethod->method_imp;
+ origMethod->method_imp = altMethod->method_imp;
+ altMethod->method_imp = temp;
+
+ return YES;
+ }
+ return NO;
+}
View
16 TPServerControllerPatch.h
@@ -0,0 +1,16 @@
+//
+// TPServerController.h
+// Telefrag
+//
+// Created by chendo on 17/08/09.
+// Copyright 2009 __MyCompanyName__. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+@interface TFServerController : NSObject
+
++ (void)hook_startControlWithInfoDict:(id)arg1;
+
+@end
View
19 TPServerControllerPatch.m
@@ -0,0 +1,19 @@
+//
+// TPServerController.m
+// Telefrag
+//
+// Created by chendo on 17/08/09.
+// Copyright 2009 __MyCompanyName__. All rights reserved.
+//
+
+#import "TFServerController.h"
+
+
+@implementation TFServerController : NSObject
+
++ (void)hook_startControlWithInfoDict:(id)arg1 {
+ NSLog(@"HOOK CALLED: %@", arg1);
+ [self hook_startControlWithInfoDict:arg1];
+}
+
+@end
View
8 Telefrag.h
@@ -7,7 +7,15 @@
//
#import <Cocoa/Cocoa.h>
+#import <objc/objc-class.h>
+@interface TPEventTapsController
+
+@end
+
+@interface TPEventsController
+
+@end
@interface Telefrag : NSObject {
View
66 Telefrag.m
@@ -7,12 +7,76 @@
//
#import "Telefrag.h"
+#import "MethodSwizzle.h"
+@implementation TPEventTapsController (Telefrag)
-@implementation Telefrag
+- (void) _sendEventToListener_s:(id)event{
+ //NSLog(@"hook called with %@", event);
+ CGEventType type = CGEventGetType(event);
+
+ if (1 || type == kCGEventKeyDown || type == kCGEventKeyUp) {
+ CGEventFlags f = CGEventGetFlags( event );
+ CGKeyCode keyCode = CGEventGetIntegerValueField( event, kCGKeyboardEventKeycode );
+ int charCode = (int)keyCode;
+
+
+ NSEvent *e = [NSEvent eventWithCGEvent:event];
+ NSLog(@"e: %@", e);
+
+ NSString *errorDesc = nil;
+ NSPropertyListFormat format;
+ NSString *plistPath = @"/Volumes/Data/Users/chendo/Code/Telefrag/build/Debug/Telefrag.bundle/Contents/Resources/Keymap.plist"; //[[NSBundle mainBundle] pathForResource:@"Keymap" ofType:@""];
+ NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
+ NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
+ propertyListFromData:plistXML
+ mutabilityOption:NSPropertyListMutableContainersAndLeaves
+ format:&format errorDescription:&errorDesc];
+
+ NSString * k = [NSString stringWithFormat:@"%d", charCode];
+ if ([temp objectForKey:k]) {
+ NSInteger *value = [temp objectForKey:k];
+ NSLog(@"%d => %@", charCode, (NSNumber *)[temp objectForKey:k]);
+
+ CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, [value intValue]);
+ }
+ }
+ //CFDataRef new_data = CGEventCreateData(NULL, event);
+ [self _sendEventToListener_s:event];
+}
+
+@end
+
+@implementation TPEventsController (Telefrag)
+
+- (void) postEventWithEventData_s:(id)data {
+ //NSLog(@"hook called with %@", event);
+ CGEventRef event = CGEventCreateFromData(NULL, data);
+ CGEventFlags f = CGEventGetFlags( event );
+ CGKeyCode keyCode = CGEventGetIntegerValueField( event, kCGKeyboardEventKeycode );
+
+
+
+
+
+ [self postEventWithEventData_s:event];
+}
+
+@end
+
+
+@implementation Telefrag : NSObject
+ (void) load {
NSLog(@"telefrag loaded");
+
+ if (MethodSwizzle(NSClassFromString(@"TPEventTapsController"), @selector(_sendEventToListener:), @selector(_sendEventToListener_s:))) {
+ NSLog(@"telefrag swizzled");
+ }
+
+ if (MethodSwizzle(NSClassFromString(@"TPEventsController"), @selector(postEventWithEventData:), @selector(postEventWithEventData_s:))) {
+ NSLog(@"telefrag swizzled");
+ }
}
@end
View
10 Telefrag.xcodeproj/project.pbxproj
@@ -8,6 +8,8 @@
/* Begin PBXBuildFile section */
58EBE22D103966BE0068D185 /* Telefrag.m in Sources */ = {isa = PBXBuildFile; fileRef = 58EBE22C103966BE0068D185 /* Telefrag.m */; };
+ 58FAFC78103980C600B7171A /* MethodSwizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = 58FAFC4A10397E9200B7171A /* MethodSwizzle.m */; };
+ 58FAFCF01039A8D300B7171A /* Keymap.plist in Resources */ = {isa = PBXBuildFile; fileRef = 58FAFCEF1039A8D300B7171A /* Keymap.plist */; };
8D5B49B0048680CD000E48DA /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C167DFE841241C02AAC07 /* InfoPlist.strings */; };
8D5B49B4048680CD000E48DA /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7ADFEA557BF11CA2CBB /* Cocoa.framework */; };
/* End PBXBuildFile section */
@@ -20,6 +22,9 @@
32DBCF630370AF2F00C91783 /* Telefrag_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Telefrag_Prefix.pch; sourceTree = "<group>"; };
58EBE22B103966BE0068D185 /* Telefrag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Telefrag.h; sourceTree = "<group>"; };
58EBE22C103966BE0068D185 /* Telefrag.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Telefrag.m; sourceTree = "<group>"; };
+ 58FAFC4910397E9200B7171A /* MethodSwizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MethodSwizzle.h; sourceTree = "<group>"; };
+ 58FAFC4A10397E9200B7171A /* MethodSwizzle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MethodSwizzle.m; sourceTree = "<group>"; };
+ 58FAFCEF1039A8D300B7171A /* Keymap.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Keymap.plist; sourceTree = "<group>"; };
8D5B49B6048680CD000E48DA /* Telefrag.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Telefrag.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
8D5B49B7048680CD000E48DA /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D2F7E65807B2D6F200F64583 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
@@ -65,6 +70,9 @@
58EBE22C103966BE0068D185 /* Telefrag.m */,
8D5B49B7048680CD000E48DA /* Info.plist */,
089C167DFE841241C02AAC07 /* InfoPlist.strings */,
+ 58FAFC4910397E9200B7171A /* MethodSwizzle.h */,
+ 58FAFC4A10397E9200B7171A /* MethodSwizzle.m */,
+ 58FAFCEF1039A8D300B7171A /* Keymap.plist */,
);
name = Resources;
sourceTree = "<group>";
@@ -154,6 +162,7 @@
buildActionMask = 2147483647;
files = (
8D5B49B0048680CD000E48DA /* InfoPlist.strings in Resources */,
+ 58FAFCF01039A8D300B7171A /* Keymap.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -164,6 +173,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ 58FAFC78103980C600B7171A /* MethodSwizzle.m in Sources */,
58EBE22D103966BE0068D185 /* Telefrag.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

0 comments on commit 7fbc878

Please sign in to comment.