Archive for the 'Apple' Category

Bluetooth Keyboard Caps

Saturday, June 22nd, 2013
Redesigned Keyboard Cap

v1.0, pictured below, proved lacking.

v2.0, in red, appears at the left.

The size difference between the end cap and the bit that rests against the end of the keyboard was widened considerably. The old piece would slip over the keyboard and the closed end would activate the power button. The open end combined with the wider overhang, seems, so far, to be a better, more durable, design.

Yes, my printer’s belts need to be tightened.

Thingiverse Updated.


Apple BT Keyboard End Caps

Recently, I’ve been carrying an Apple Bluetooth Keyboard with my iPad mini so I can compose relatively long emails. Nothing beats a keyboard for text entry!

While I love the keyboard, it does have an annoying habit of turning on when floating about my murse.

A few minutes in Autodesk Inventor Fusion and I whipped up some printable caps that slide over the ends of the keyboard. The cap protects the power button from accidental activation (there are two styles of caps, one more defensive than the other) and by placing a cap at both ends, they can be left in place while using the keyboard and it remains level.

The STL files and some more photos can be found on thingiverse.com.

Aside: I clearly need to reprint that piece. I had some schmutz on the print bed, leading to the end not being smooth. That and it looks like my belts need tightening. I’ll switch colors to clear and re-print someday soon.

Aside^2: Something snapped in my brain since the last time I messed with Inventor Fusion. In particular, I went from nerver using to completely embracing the construction feature. Basically, construction allows you to place axis or planes relative to features on the model. Thus, if you want to bisect the model to, say, make the inside wall of a tube a bit fatter for a few millimeters near the end, you simply place a plane parallel to the end face, offset a few millimeters into the tube and then bisect the model with the plane.


Xcode: Sometimes a return is not a return (emacs brain damage)

Sunday, December 23rd, 2012
ExpectedExpression
Indention and Insertion Prefs

Every now and then, I’ll be coding along merrily in Xcode and I’ll get an error much like the one at left. Or “expected identifier or ‘(‘” is another variant.

Huh? That code is fine. Maybe it is an invisible character? Nope. Nothing shown.

Took a bit, but I figured out the cause; 25 years of using emacs as my command line editor of choice, along with the folks at NeXT that implemented the AppKit’s text editor.

In emacs, you quite commonly navigate about by holding down the ctrl- key and banging on various keys to go to the beginning/end of lines, etc. Many of these control sequences are honored by Cocoa’s text editing system and quite a few more are supported in Xcode’s editor.

Seemingly unrelated, ctrl-return is mapped to Insert Line Break.

Thus, if you are an emacs head and you commonly hit ctrl-e<return> to start a new line of code and you happen to hold down the return key just a tad too long, it causes the error shown (or a variant depending on where the insert happens).

The easiest way to tell if this is the case is to go to the line of code after the line reporting the error and hit ctrl-a. If the cursor ends up at the beginning of the previous line, that line is ended by a line break and not a true newline. (ctrl-n – backspace – return to quickly fix).

While it is easy enough to fix once you know the ctrl-a trick, a better fix is one that makes it such that it’ll never happen again.

To do that, go to Xcode’s Key Bindings Preferences, click on “Text”, and scroll down to Insertions and Indetions. On Insert Line Break, delete the ctrl-return (hat + u-turn arrow) key sequence. For convenience add the same to Insert Newline.

Problem solved.

Arduino on Mountain Lion

Tuesday, August 21st, 2012

In my “spare” time (hah!), I hack on Arduino a bit. Mostly because there are tons and tons of 3rd party libraries that make hacking up a hardware solution mostly a bit of soldering followed by gluing together some pre-made software bits. The Arduino IDE is Java based and… well… not terribly awesome (to be fair — it isn’t awful, just quite lacking beyond the basics).

With the release of Mountain Lion, most Arduino installations were broken. Fortunately, this can be fixed by grabbing the latest bits from here and there.

  • Grab the latest Arduino.app for Mac OS X
  • Run it and it’ll insist on installing the latest Java VM. Do so.
  • If you use Teensyduino, grab the latest installer and install it. If Mac OS X (rightly) complains that the software is from an unidentified source and can’t be opened, you can ctrl-click on the installer, select “open” and it will present the option to bypass the security check. Do so, but not without a bit of misgivings.
  • Install the latest FTDI driver.
  • If all went well, you should see the device show up in /dev/ as something like /dev/tty.usbmodem12341.

    retainCount is useless.

    Sunday, December 18th, 2011

    The retainCount method just isn’t very useful as has been indicated in the answers to about a zillion StackOverflow questions.

    The documentation has this to say:

    Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

    Makes it pretty clear that you shouldn’t be calling retainCount, but it really doesn’t illuminate exactly how useless the method is outside of a very narrow context.

    So, let us count the ways:

    1. The absolute retain count of an object may change at any time once an object has been passed through any system API.
    2. Any subclass of any system provided class counts as “through system API”; the retain count may be impacted by implementation details.
    3. The retain count never reflects whether an object is autoreleased.
    4. Autorelease is a per-thread concept whereas retain counts are global; race condition derived hilarity can easily ensue.
    5. The retainCount method can never return 0.
    6. Some classes are implemented as singletons some of the time.
    7. Some classes may internally manipulate their retain count directly (I.e. no swizzle for you!).
    8. While retain/release are effectively thread safe, there is always a race between calling retainCount and having the actual retain count change in some other execution context.

    Bottom line: the only time the absolute retainCount can be conclusively used for analytic purposes is if you have the backtrace of every retain and release that contributed to the current retain count’s value (there is another use case, documented at the end). If you have that, then you don’t need the retain count and, fortunately, Instruments is already generally quite adept at producing a per-object inventory of retains and releases for you (see Analyzing Data with the Allocations Instrument“.

    In general, you should consider the retain count as a delta. Your code causes the retain count to increase and decrease. You don’t +alloc an object with a retain count of 1. Instead, you +alloc an object with a retain count of +1. If you want that object to go away, you need to do something — release, always and eventually — that causes the retain count to be decremented by 1.

    It really is that simple.

    Almost.

    Concurrency — whether through threading or GCD — throws a bit of a wrench in the works (as it always does). Namely, the retain count should really be considered as a per concurrency context delta. If thread or queue A wants to do something with object X, it should hold a +1 retainCount on X for the duration of said operation. One subtle detail that will bear repeating later; an autorelease‘d object does not contribute to thread safety. That is, if thread or queue A wants to pass X to thread or queue B, there must be an explicit retain in A that is balanced by a release (or synchronization event back to A) in B.

    Now, some of the enumerated claims may either seem specious at best or, certainly, not obvious as to exactly how it undermines the validity of the retain count. Read the rest of this entry »

    iOS 4.3: imp_implementationWithBlock()

    Thursday, March 17th, 2011

    In iOS 4.3, there are 3 rather low level functions in the runtime that provide a new kind of bridge between Objective-C and Blocks with a specific goal of facilitating the dynamic generation of method implementations.

    Specifically:

    IMP imp_implementationWithBlock(void *block);
    void *imp_getBlock(IMP anImp);
    BOOL imp_removeBlock(IMP anImp);
    

    In particular, imp_implementationWithBlock() takes a block as a parameter, copies it to the heap, and returns a trampoline that allows the block to be used as the implementation — the IMP — of a method in any Objective-C class (as long as the block’s arguments and the method’s arguments are compatible).

    Quite literally, this allows:

    int j = 12;
    IMP skewIMP = imp_implementationWithBlock(^(id _s, int k) { return k*j; });
    

    Where skewIMP would then contain a function pointer that could be used as the IMP for a method declared like:

    - (int)skew:(int)k;
    

    Details on the flip side…. Read the rest of this entry »

    When is a Leak not a Leak? Using Heapshot Analysis to Find Undesirable Memory Growth

    Sunday, October 17th, 2010

    The other day, I was in need of a Cocoa application that launches quickly that has a standard document model. At random, I chose the rather awesome Hex Fiend. As I often do, I also had top -u -o pid running in a Terminal window.

    And I noticed something odd. As expected, the RPRVT of Hex Fiend was growing on each cmd-n. However, the RPRVT was not decreasing the same amount every time I hit cmd-w.

    That ain’t right. Or it might be. Beyond evidence that a memory use problem may exist, top is a horrible tool for determining if a problem is real or what the actual problem might be.

    In this case, the issue looks like a simple memory leak. Hex Fiend is allocating and retaining some set of objects, but not releasing them. The easiest first step is to use the leaks command line tool:

    % leaks "Hex Fiend"
    leaks Report Version:  2.0
    Process:         Hex Fiend [3435]
    Path:            /Volumes/Data/Applications/Hex Fiend.app/Contents/MacOS/Hex Fiend
    Load Address:    0x100000000
    Identifier:      com.ridiculousfish.HexFiend
    Version:         2.0.0 (200)
    Code Type:       X86-64 (Native)
    Parent Process:  launchd [122]
    Date/Time:       2010-10-16 20:47:09.935 -0700
    OS Version:      Mac OS X 10.6.4
    Report Version:  7
    Process 3435: 22980 nodes malloced for 2600 KB
    Process 3435: 0 leaks for 0 total leaked bytes.
    

    OK; whatever the problem is, it isn’t “leaked” memory in the traditional definition of “leaked memory”.

    That is, whatever memory is being allocated and never released is still being referenced somewhere. Maybe a circular retain. Maybe something that has a weak reference from the rest of the App’s object graph such that leaks can’t detect.

    In other words, this isn’t just a simple memory leak and it will require more advanced tools to fix.

    Read the rest of this entry »

    The iPad & Reading (Free Books, too!)

    Sunday, April 11th, 2010

    Update:I’m keeping a list of ebook publishers/sources for which I’ve found inFeel free to send me suggestions. This is, by no means, a complete list — I’m just taking notes as my OCD-compulsive nature kicks in and I build up a huge set of books to read.

    • The Baen Free Library contains quite an amazing selection of donationware ebooks from many well known science fiction and fantasy authors.
    • Feedbooks contains a ton of public domain and original content as they are also a publisher of ebooks. Their blog is pretty interesting, too.
    • A one-off; Charlie Stross’s Accelerando comes highly recommended.
    • In the meta-category; a weblog post claiming to point to the “top 20 websites for DRM-free Sci-Fi Books”.
    • This list is impressive and also leads to cheap sources for ebooks, too. I’m perfectly happy paying for ebooks (just like real books), though I’m not at all happy about paying more than the paperback price for an older book.
    • Tor books — publishers of Jordan’s Wheel of Time series — has embraced ebooks to a large degree.

    iBooks-SciFi.PNG

    I know lots of people that have picked up iPads — no surprises there. What is surprising is that just about everyone has something for which their reaction is “the iPad changes everything”.

    I have several of those, but — at the moment — the biggest is reading. I used to read tons and tons of books, but gradually tapered off because I carrying around a couple of books was a pain in the ass and, for vacations, I would need to take up to a dozen, depending on duration.

    That and, frankly, it has been bloody obvious for years that an e-book read that is “good enough” would provide a portable library and a decent reading experience. The Kindle was almost the one, but having 40% of the front surface area covered by a keyboard seemed like a complete waste to me. I did, however, use the Kindle app on the iPhone to read a couple of books — good, but not great.

    I find iBooks to be a wonderful reading experience. Easy on the eyes, very nice user interface and — with the versatility of the iPad — I can read Kindle books, and do a myriad other things on the device. Haven’t spent much time with the Kindle app, but if it is like the iPhone, it’ll be just fine, too!

    I, however, am a cheapskate. I haven’t quite brought myself to drop money on books. Fortunately, there is a large number of freely available books in both the iTunes and Kindle stores.

    With a bit of hunting, I have also hit upon a treasure trove of mostly Science Fiction and Fantasy — my favorite genres — books! In particular, Baen Books made available a large number of their books in many formats! In particular, you can find a list of the participating authors, click through to their titles, then select the EPUB/Nook/Stanza Format on the download page.

    Note that the books have covers as in the picture on the left, but the cover art doesn’t show up in the iBooks application on the iPad.

    I donated $51 in return for a ton of books.

    Geotagging Photos With Aperture & QStarz BT-1300S

    Thursday, March 11th, 2010

    With the release of Aperture 3, geotagging photos is now an integral part of the application’s workflow. Aperture grew the Faces & Places features like iPhotos!

    In particular, the Places feature allows you to import GPS data from iPhone photos or from GPS data captured by pretty much any device that can spew a standard GPX format data file.

    ImportFromiPhone.png

    Tagging from the iPhone is straightforward. With the iPhone connected to your computer, go to Places in Aperture and then select Import from iPhone Photos…. Aperture will then display all the photos on your iPhone that have GPS metadata and you can pick the photos from which the GPS data is to be imported. Once picked, Aperture will apply the GPS data to photos taken near the same time as the imported data.

    However, one issue with the iPhone is that it really isn’t a terribly good GPS logging device. Using it as one eats the battery and the data generated often has holes. And, because the iPhone uses A-GPS (GPS assisted by cellular signal), it doesn’t work at all when hiking in areas without cell signal. Apparently, I’m mistaken about A-GPS — it should fall back to regular GPS behavior. My experience, though, is that the iPhone just isn’t a terribly good GPS device when it doesn’t have a cell signal and has often been off by miles when in the hinterlands. It works great when on the road or near cities, though.

    Read the rest of this entry »

    No, iTunes, I don’t want to listen to holiday music in February….

    Monday, February 22nd, 2010

    We have a ton of Christmas music — 400 or so tracks, with it growing by about 50-75 tracks each year — that ranges across all genres. It is some awesome stuff; blues, reggae, pop, traditional, you name it…

    However, it is music that should only be in the rotation starting December 1st, heavy rotation by about the 15th-20th and then no more from December 26th to the next December 1st.

    Of course, iTunes is agnostic and, thus, does not follow my holiday music filtration wishes.

    Easy to fix.

    1. On all Christmas/Holiday music, add a grouping of “Holiday” or “Christmas” (or whatever you want).
    2. Create a smart playlist that finds all music in said grouping. Make sure the smart playlist matches unchecked items.
    3. On December 26th, select all, ctrl-click, and select uncheck selection
    4. On Decmber 1st of the next year, select the smart playlist, select all, ctrl-clik, and select check selection

    Done. iTunes will not select unchecked songs when constructing genius playlists or when playing through the library on shuffle play. Smart playlists can optionally include checked songs.

    objc_msgSend() Tour Part 4: Method Lookup & Some Odds and Ends

    Thursday, February 4th, 2010

    Table of Contents

    1. objc_msgSend() Tour Part 1: The Road Map
    2. objc_msgSend() Tour Part 2: Setting the Stage
    3. objc_msgSend() Tour Part 3: The Fast Path
    4. objc_msgSend() Tour Part 4: Method Lookup & Some Odds and Ends

    In the first three parts, I gave an overview, explained a bit of the ABI used by Objective-C, and took a near instruction by instruction tour of what happens on the fast path of Objective-C method dispatch. By fast path, I mean what happens 99.9% of the time; a very fast, no overhead, no function call, no locking, set of instructions that grabs the method implementation from the cache and does a tail-call jump to that implementation.

    The slow path is used rarely. As little as once per unique selector invoked per class with a goal of filling the cache such that the slow path is never used again for that selector/class combination. A handful of operations will cause a class’s cache to be flushed; method swizzling, category loading, and the like.

    Note that during +initialize, methods won’t always be cached. Yet another reason to not do any real work during +initialize! Read the rest of this entry »