Reply To: Prevent sleep on OSX when sharing in progress

#18886
Anonymous
Inactive

I’ve been programming for ~30 years.. but haven’t touched C in over 10 years, and my last real Mac programming was using Lightspeed(?) Pascal in the early 90’s. My work now is all in HDLs, and Perl/html, etc. There are probably cleaner/better ways to accomplish what I did… on the other hand, it appears to accomplish what I wanted to do. I am VERY open to (re)learning what those better ways are if anyone wants to make suggestions.

My home network is set up such that my Mac has my music (iTunes) library.

My HTPC (and mail/web server) is linux (ubuntu). Since Apple encrypted DAAP library sharing after iTunes version 7, I was using Firefly (mt-daapd) to serve my music (it can read iTunes).

My linux box stays on 24/7 (as my mail/web server)… my Mac, I want to sleep (to save power) when not in use.

The issue I had was that the act of serving music (via Firefly) was not sufficient to keep the Mac from falling asleep.

Following up ob a generous tip (thanks BH!) I was referred to this Apple doc:

http://developer.apple.com/mac/library/qa/qa2004/qa1160.html

NOT being familiar with the Firefly code structure, I thought as a first approximation, if I could “poke” the OS every time a new song was started, that would be sufficient prodding to keep the Mac awake… so I hunted thru the Firefly code, and came up with this strategy (attempting to do this in a cross-platform-friendly way).

In a non-cross-platform friendly world, the quick and dirty waste add at the top of plugin.c:

#include

and then at the top of the routine pi_stream I would add

UpdateSystemActivity(OverallAct);

Seemed simple enough… but plugin.c is not Mac-specific, so the other OSes would not like seeing Mac calls there, so to make it more cross-platform-friendly, I

a) added (plugin.c, line 895)
prevent_sleep();

b) add (rend.h, line 30)
extern void prevent_sleep(void);

c) added (rend-osx.c, line 35)
#include

d) added (rend-osx.c, line 45) (message will print to the log window in the Preference panel)
/* prevent Mac from sleeping while serving music */
void prevent_sleep(void) {
UpdateSystemActivity(OverallAct);
DPRINTF(E_WARN,L_WS, “Sending keepAwaken”);
}

e) edit configure to add CoreServices, change line 21910 to
LDFLAGS=”$LDFLAGS -framework CoreFoundation -framework CoreServices”

f) will need to add a placeholder (or equivalent function) to the non OSX flavors of the rend-XXX files.
/* placeholder to enable Mac function.. could add equivalent here for each OS if sleep is an issue */
void prevent_sleep(void) {
}

Once this was done I could compile. I followed the hints from Ron in this thread; viewtopic.php?f=8&t=7353

including installing MacPorts (http://www.macports.org/install.php) (I personally didn’t care about flac, but the id3tags lib from Fink did not work in the compile for me)

sudo port install libid3tag sqlite3 flac

mkdir firefly
cd firefly
wget http://nightlies.fireflymediaserver.org/nightlies/svn-1586/mt-daapd-svn-1586.tar.gz
tar -xvzf mt-daapd-svn-1586.tar.gz
cd mt-daapd-svn-1586
./configure –enable-flac –enable-sqlite3 –with-id3tag=/opt/local
make

(stop Firefly if it is running)

cp src/mt-daapd /Library/PreferencePanes/Firefly.prefPane/Contents/Resources/Firefly Helper.app/Contents/Resources/Server/firefly
cp src/plugins/.libs/*.so /Library/PreferencePanes/Firefly.prefPane/Contents/Resources/Firefly Helper.app/Contents/Resources/Server/plugins/
vi ~/Library/Application Support/Firefly/firefly.conf (and change sqlite to sqlite3)

Now restart Firefly and enjoy.

So far in testing, my Mac will not go to sleep when serving music. When the client stops asking for songs, the Mac does go to sleep. Desired behavior accomplished.

Based on the way I did this, your sleep timer must be no shorter than your longest song.. so I suppose if you were playing an audiobook, ripped as one long track, this might not suffice.

Details are here for anyone that wants to duplicate, and/or Ron is welcome to pull this in (if its desired) (and/or do it in a better way), or I’d be happy to add the changes if someone would help me understand the proper way to do so (and we’d need to finish out step f) above). The other complication is that I did this against 1586, as the latest 16xx build does not work on OSX(?) (I forget why.. I saw that 1586 worked so I stuck with that).

Again, I’m glad Firefly exists (kudos to Ron) it makes a great tool for my HTPC setup.

PS. since the forum appears to disable direct user-to-user contact I can be reached at zimcomm at h_o_t_mail.com (remove the underscores, just trying to avoid spam)