From http://www.jwz.org/xscreensaver/xscreensaver-5.36.tar.gz
[xscreensaver] / OSX / textclient-ios.m
1 /* xscreensaver, Copyright (c) 2012-2016 Jamie Zawinski <jwz@jwz.org>
2  *
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 
9  * implied warranty.
10  *
11  * Loading URLs and returning the underlying text.
12  *
13  * This is necessary because iOS doesn't have Perl installed, so we can't
14  * run "xscreensaver-text" to do this.
15  */
16
17 #include "utils.h"
18
19 #ifdef USE_IPHONE // whole file
20
21 #include "textclient.h"
22
23 char *
24 textclient_mobile_date_string (void)
25 {
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
31   NSString *date =
32     [NSDateFormatter
33       localizedStringFromDate:[NSDate date]
34       dateStyle: NSDateFormatterMediumStyle
35       timeStyle: NSDateFormatterMediumStyle];
36   NSString *nl = @"\n";
37
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]);
51 }
52
53
54 @interface TextLoader : NSObject
55 @property (nonatomic, retain) NSURL *url;
56 @property (nonatomic, retain) NSString *result;
57 @end
58
59 @implementation TextLoader
60 {
61   NSURL    *_url;
62   NSString *_result;
63 }
64
65 + (TextLoader *) sharedLoader
66 {
67   static TextLoader *singleton = nil;
68   @synchronized(self) {
69     if (!singleton)
70       singleton = [[self alloc] init];
71   }
72   return singleton;
73 }
74
75 - (void) startLoading
76 {
77   // NSLog(@"textclient thread loading %@", self.url);
78   self.result = [NSString stringWithContentsOfURL: self.url
79                           encoding: NSUTF8StringEncoding
80                           error: nil];
81   // NSLog(@"textclient thread finished %@ (length %d)", self.url,
82   //      (unsigned int) [self.result length]);
83 }
84
85 @end
86
87
88
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
93    URL loading again.
94  */
95 char *
96 textclient_mobile_url_string (Display *dpy, const char *url)
97 {
98   TextLoader *loader = [TextLoader sharedLoader];
99   NSString *result = [loader result];
100
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.
104
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]);
109     loader.url    = nil;
110     loader.result = nil;
111     return s;
112
113   } else if ([loader url]) {                            // Waiting on thread
114     // NSLog(@"textclient waiting...");
115     return 0;
116
117   } else {                                              // Launch thread
118     // NSLog(@"textclient launching %s...", url);
119     loader.url =
120       [NSURL URLWithString:
121                [NSString stringWithCString: url
122                          encoding:NSISOLatin1StringEncoding]];
123     [NSThread detachNewThreadSelector: @selector(startLoading)
124               toTarget: loader withObject: nil];
125     return 0;
126   }
127 }
128
129 #endif // USE_IPHONE -- whole file