Toshihiro Shimizu 890ddd
#include "toonz/screensavermaker.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 2a7129
#include "texception.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 2a7129
#include <memory></memory>
Shinya Kitaoka 2a7129
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Shinya Kitaoka d4642c
#include <shellapi.h></shellapi.h>
Toshihiro Shimizu 890ddd
//#include <iostream.h></iostream.h>
Toshihiro Shimizu 890ddd
#include <sys stat.h=""></sys>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qurl></qurl>
Toshihiro Shimizu 890ddd
#include <qdesktopservices></qdesktopservices>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void makeScreenSaver(TFilePath scrFn, TFilePath swfFn,
Shinya Kitaoka 120a6e
                     std::string screenSaverName) {
Shinya Kitaoka 120a6e
  struct _stat results;
Shinya Kitaoka 120a6e
  if (_wstat(swfFn.getWideString().c_str(), &results) != 0)
Shinya Kitaoka 120a6e
    throw TException(L"Can't stat file " + swfFn.getWideString());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int swfSize = results.st_size;
Shinya Kitaoka 2a7129
  std::unique_ptr<char> swf(new char[swfSize]);</char>
Shinya Kitaoka 120a6e
  FILE *chan = _wfopen(swfFn.getWideString().c_str(), L"rb");
Shinya Kitaoka 120a6e
  if (!chan) throw TException(L"fopen failed on " + swfFn.getWideString());
Shinya Kitaoka 120a6e
  fread(swf.get(), swfSize, 1, chan);
Shinya Kitaoka 120a6e
  fclose(chan);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFilePath svscrn = TSystem::getBinDir() + "screensaver.dat";
Shinya Kitaoka 120a6e
  if (!TFileStatus(svscrn).doesExist()) {
Shinya Kitaoka 120a6e
    throw TException(std::wstring(L"Screensaver template not found: ") +
Shinya Kitaoka 120a6e
                     svscrn.getWideString());
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TSystem::copyFile(scrFn, svscrn);
Shinya Kitaoka 120a6e
  HANDLE hUpdateRes =
Shinya Kitaoka 120a6e
      BeginUpdateResourceW(scrFn.getWideString().c_str(), FALSE);
Shinya Kitaoka 120a6e
  if (hUpdateRes == NULL)
Shinya Kitaoka 120a6e
    throw TException(L"can't write " + scrFn.getWideString());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  BOOL result = UpdateResource(hUpdateRes, "FLASHFILE", MAKEINTRESOURCE(101),
Shinya Kitaoka 120a6e
                               MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
Shinya Kitaoka 120a6e
                               swf.get(), swfSize);
Shinya Kitaoka 120a6e
  if (result == FALSE)
Shinya Kitaoka 120a6e
    throw TException(L"can't add resource to " + scrFn.getWideString());
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
result = UpdateResource(
Shinya Kitaoka 120a6e
hUpdateRes,
Shinya Kitaoka 120a6e
RT_STRING,
Shinya Kitaoka 120a6e
MAKEINTRESOURCE(1),
Shinya Kitaoka 120a6e
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
Shinya Kitaoka 120a6e
(void*)screenSaverName.c_str(),
Shinya Kitaoka 120a6e
screenSaverName.size());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
if (result == FALSE)
Shinya Kitaoka 120a6e
throw TException(L"can't add name to "+scrFn.getWideString());
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!EndUpdateResource(hUpdateRes, FALSE))
Shinya Kitaoka 120a6e
    throw TException(L"Couldn't write " + scrFn.getWideString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void previewScreenSaver(TFilePath scr) {
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TSystem::showDocument(scr);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void installScreenSaver(TFilePath scr) {
Shinya Kitaoka 120a6e
  std::wstring cmd = L"desk.cpl,InstallScreenSaver " + scr.getWideString();
Shinya Kitaoka 120a6e
  int ret = (int)ShellExecuteW(0, L"open", L"rundll32.exe", cmd.c_str(), 0,
Shinya Kitaoka 120a6e
                               SW_SHOWNORMAL);
Shinya Kitaoka 120a6e
  if (ret <= 32) throw;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void makeScreenSaver(TFilePath scrFn, TFilePath swfFn,
Shinya Kitaoka 120a6e
                     std::string screenSaverName) {}
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void previewScreenSaver(TFilePath scr)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void installScreenSaver(TFilePath scr) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif