~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/libdisplay/DisplayMacAqua.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cstdlib>
 
2
#include <cstring>
 
3
#include <fstream>
 
4
#include <sstream>
 
5
using namespace std;
 
6
 
 
7
#include "keywords.h"
 
8
#include "Options.h"
 
9
#include "PlanetProperties.h"
 
10
#include "xpUtil.h"
 
11
 
 
12
#include "DisplayMacAqua.h"
 
13
 
 
14
#include "libimage/Image.h"
 
15
 
 
16
#include <Carbon/Carbon.h>
 
17
 
 
18
extern "C" {
 
19
    bool SetDesktopPictureFromCharString(const char *file);
 
20
}
 
21
 
 
22
DisplayMacAqua::DisplayMacAqua(const int tr) : DisplayBase(tr)
 
23
{
 
24
    fullWidth_ = static_cast<int> (CGDisplayPixelsWide(kCGDirectMainDisplay));
 
25
    fullHeight_ = static_cast<int> (CGDisplayPixelsHigh(kCGDirectMainDisplay));
 
26
 
 
27
    if (fullWidth_ == 0 || fullHeight_ == 0)
 
28
        xpExit("Can't set Aqua display\n", __FILE__, __LINE__);
 
29
 
 
30
    Options *options = Options::getInstance();
 
31
    switch (options->DisplayMode())
 
32
    {
 
33
    case WINDOW:
 
34
        xpWarn("-window option not supported for Aqua.\n", 
 
35
               __FILE__, __LINE__);
 
36
        // fall through
 
37
    case ROOT:
 
38
        if (options->GeometrySelected())
 
39
        {
 
40
            width_ = options->getWidth();
 
41
            height_ = options->getHeight();
 
42
        }
 
43
        else
 
44
        {
 
45
            width_ = fullWidth_;
 
46
            height_ = fullHeight_;
 
47
        }
 
48
        
 
49
        break;
 
50
    }
 
51
  
 
52
    if (!options->CenterSelected())
 
53
    {
 
54
        if (width_ % 2 == 0)
 
55
            options->setCenterX(width_/2 - 0.5);
 
56
        else
 
57
            options->setCenterX(width_/2);
 
58
 
 
59
        if (height_ % 2 == 0)
 
60
            options->setCenterY(height_/2 - 0.5);
 
61
        else
 
62
            options->setCenterY(height_/2);
 
63
    }
 
64
 
 
65
    allocateRGBData();
 
66
 
 
67
}
 
68
 
 
69
DisplayMacAqua::~DisplayMacAqua()
 
70
{
 
71
}
 
72
 
 
73
// This was pretty much written by trial and error once I found
 
74
// DesktopPicture.m on developer.apple.com
 
75
void 
 
76
DisplayMacAqua::renderImage(PlanetProperties *planetProperties[])
 
77
{
 
78
    drawLabel(planetProperties);
 
79
 
 
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);
 
85
 
 
86
    ostringstream outputStream;
 
87
    outputStream << TmpDir() << "/" << tmpFile << ".png";
 
88
 
 
89
    Options *options = Options::getInstance();
 
90
    if (options->GeometrySelected()) PlaceImageOnRoot();
 
91
 
 
92
    Image i(fullWidth_, fullHeight_, rgb_data, alpha);
 
93
    if (!i.Write(outputStream.str().c_str()))
 
94
    {
 
95
        ostringstream errStr;
 
96
        errStr << "Can't create image file " << outputStream.str() << "\n";
 
97
        xpExit(errStr.str(), __FILE__, __LINE__);
 
98
    }
 
99
 
 
100
    if (options->Verbosity() > 1)
 
101
    {
 
102
        ostringstream msg;
 
103
        msg << "Created image file " << outputStream.str() << "\n";
 
104
        xpMsg(msg.str(), __FILE__, __LINE__);
 
105
    }
 
106
 
 
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.
 
110
    sleep(1);
 
111
    if (!SetDesktopPictureFromCharString(outputStream.str().c_str()))
 
112
    {
 
113
        ostringstream errStr;
 
114
        errStr << "Failed to set desktop from " 
 
115
               << outputStream.str() << "\n";
 
116
        xpWarn(errStr.str(), __FILE__, __LINE__);
 
117
    }
 
118
 
 
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
 
122
    // it some time.
 
123
    sleep(1);
 
124
    if (!options->SaveDesktopFile())
 
125
        unlinkFile(outputStream.str().c_str());
 
126
}