9
#include "PlanetProperties.h"
12
#include "DisplayMacAqua.h"
14
#include "libimage/Image.h"
16
#include <Carbon/Carbon.h>
19
bool SetDesktopPictureFromCharString(const char *file);
22
DisplayMacAqua::DisplayMacAqua(const int tr) : DisplayBase(tr)
24
fullWidth_ = static_cast<int> (CGDisplayPixelsWide(kCGDirectMainDisplay));
25
fullHeight_ = static_cast<int> (CGDisplayPixelsHigh(kCGDirectMainDisplay));
27
if (fullWidth_ == 0 || fullHeight_ == 0)
28
xpExit("Can't set Aqua display\n", __FILE__, __LINE__);
30
Options *options = Options::getInstance();
31
switch (options->DisplayMode())
34
xpWarn("-window option not supported for Aqua.\n",
38
if (options->GeometrySelected())
40
width_ = options->getWidth();
41
height_ = options->getHeight();
46
height_ = fullHeight_;
52
if (!options->CenterSelected())
55
options->setCenterX(width_/2 - 0.5);
57
options->setCenterX(width_/2);
60
options->setCenterY(height_/2 - 0.5);
62
options->setCenterY(height_/2);
69
DisplayMacAqua::~DisplayMacAqua()
73
// This was pretty much written by trial and error once I found
74
// DesktopPicture.m on developer.apple.com
76
DisplayMacAqua::renderImage(PlanetProperties *planetProperties[])
78
drawLabel(planetProperties);
80
// Setting the desktop picture doesn't seem to work if you give it
81
// the same filename over and over again.
82
char templateFile[16];
83
strncpy(templateFile, "Xplanet.XXXXXX", 16);
84
char *tmpFile = mktemp(templateFile);
86
ostringstream outputStream;
87
outputStream << TmpDir() << "/" << tmpFile << ".png";
89
Options *options = Options::getInstance();
90
if (options->GeometrySelected()) PlaceImageOnRoot();
92
Image i(fullWidth_, fullHeight_, rgb_data, alpha);
93
if (!i.Write(outputStream.str().c_str()))
96
errStr << "Can't create image file " << outputStream.str() << "\n";
97
xpExit(errStr.str(), __FILE__, __LINE__);
100
if (options->Verbosity() > 1)
103
msg << "Created image file " << outputStream.str() << "\n";
104
xpMsg(msg.str(), __FILE__, __LINE__);
107
// This sometimes doesn't set the background correctly, but
108
// doesn't return false in those cases. Hopefully the real API to
109
// set the desktop will be available soon.
111
if (!SetDesktopPictureFromCharString(outputStream.str().c_str()))
113
ostringstream errStr;
114
errStr << "Failed to set desktop from "
115
<< outputStream.str() << "\n";
116
xpWarn(errStr.str(), __FILE__, __LINE__);
119
// I have no idea, but maybe the failure to set the desktop is
120
// because the Apple Event runs inside a thread or something.
121
// Sleep for a second before removing the temporary file to give
124
if (!options->SaveDesktopFile())
125
unlinkFile(outputStream.str().c_str());