1 /* xscreensaver, Copyright (c) 2012-2016 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * Loading URLs and returning the underlying text.
13 * This is necessary because iOS doesn't have Perl installed, so we can't
14 * run "xscreensaver-text" to do this.
19 #ifdef USE_IPHONE // whole file
21 #include "textclient.h"
24 textclient_mobile_date_string (void)
26 UIDevice *dd = [UIDevice currentDevice];
27 NSString *name = [dd name]; // My iPhone
28 NSString *model = [dd model]; // iPad
29 // NSString *system = [dd systemName]; // iPhone OS
30 NSString *vers = [dd systemVersion]; // 5.0
33 localizedStringFromDate:[NSDate date]
34 dateStyle: NSDateFormatterMediumStyle
35 timeStyle: NSDateFormatterMediumStyle];
38 NSString *result = name;
39 result = [result stringByAppendingString: nl];
40 result = [result stringByAppendingString: model];
41 // result = [result stringByAppendingString: nl];
42 // result = [result stringByAppendingString: system];
43 result = [result stringByAppendingString: @" "];
44 result = [result stringByAppendingString: vers];
45 result = [result stringByAppendingString: nl];
46 result = [result stringByAppendingString: nl];
47 result = [result stringByAppendingString: date];
48 result = [result stringByAppendingString: nl];
49 result = [result stringByAppendingString: nl];
50 return strdup ([result cStringUsingEncoding:NSISOLatin1StringEncoding]);
54 @interface TextLoader : NSObject
55 @property (nonatomic, retain) NSURL *url;
56 @property (nonatomic, retain) NSString *result;
59 @implementation TextLoader
65 + (TextLoader *) sharedLoader
67 static TextLoader *singleton = nil;
70 singleton = [[self alloc] init];
77 // NSLog(@"textclient thread loading %@", self.url);
78 self.result = [NSString stringWithContentsOfURL: self.url
79 encoding: NSUTF8StringEncoding
81 // NSLog(@"textclient thread finished %@ (length %d)", self.url,
82 // (unsigned int) [self.result length]);
89 /* Returns the contents of the URL.
90 Loads the URL in a background thread: if the URL has not yet loaded,
91 this will return NULL. Once the URL has completely loaded, the full
92 contents will be returned. Calling this again after that starts the
96 textclient_mobile_url_string (Display *dpy, const char *url)
98 TextLoader *loader = [TextLoader sharedLoader];
99 NSString *result = [loader result];
101 // Since this is a singleton, it's possible that if hack #1 starts
102 // URL #1 loading and then quickly exits, and then hack #2 asks for
103 // URL #2, it might get URL #1 instead. Oh well, who cares.
105 if (result) { // Thread finished
106 // NSLog(@"textclient finished %s (length %d)", url,
107 // (unsigned int) [result length]);
108 char *s = strdup ([result cStringUsingEncoding:NSUTF8StringEncoding]);
113 } else if ([loader url]) { // Waiting on thread
114 // NSLog(@"textclient waiting...");
117 } else { // Launch thread
118 // NSLog(@"textclient launching %s...", url);
120 [NSURL URLWithString:
121 [NSString stringWithCString: url
122 encoding:NSISOLatin1StringEncoding]];
123 [NSThread detachNewThreadSelector: @selector(startLoading)
124 toTarget: loader withObject: nil];
129 #endif // USE_IPHONE -- whole file