Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 373 lines (319 sloc) 7.82 KB
#ifndef com_sleepless_system_cpp
#define com_sleepless_system_cpp
/* Copyright 2004 Sleepless Software Inc. All Rights Reserved */
#ifdef WIN32
# include <windows.h>
# include <sys/timeb.h>
# include <stdlib.h>
# include "windows.cpp"
# include <shlobj.h>
#endif
#if defined(linux) //|| (defined(__APPLE__) && defined(__GNUC__))
# include <time.h>
# include <sys/time.h>
# include <unistd.h>
# include <stdlib.h>
# include <sys/types.h>
# include <signal.h>
#endif
#ifdef __CYGWIN__
# include <time.h>
# include <sys/time.h>
# include <unistd.h>
# include <stdlib.h>
# include <sys/types.h>
# include <signal.h>
#endif
#ifdef __APPLE__
# include "TargetConditionals.h"
# include <time.h>
# include <sys/time.h>
# include <unistd.h>
# include <stdlib.h>
# include <sys/types.h>
# include <signal.h>
#endif
#include "string.cpp"
#include "str.cpp"
#include "assert.cpp"
#include "file.cpp"
struct System
{
// not thread safe - buffer overwitten on every call
static const char *datetime_local(time_t tt = 0)
{
static char buf[80];
if(tt == 0)
tt = time(0);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", localtime(&tt));
return buf;
}
static int userName(char *buf, unsigned long buflen)
{
buf[0] = 0;
#ifdef WIN32
return GetUserName(buf, &buflen) ? 0 : 1;
#else
// XXX make *nux version of this please
return 0;
#endif
}
static unsigned long secs()
{
return (unsigned long)time(0);
}
static unsigned long currentTimeMillis()
{
#ifdef WIN32
struct timeb t;
ftime(&t);
return (unsigned long)((t.time * 1000) + t.millitm);
#endif
#if defined(linux) || (defined(__APPLE__) && defined(__GNUC__)) || __CYGWIN__
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
#endif
#ifdef __palmos__
return (unsigned long)(TimGetTicks() * (1000.0f / SysTicksPerSecond()));
#endif
}
static unsigned long millis()
{
return currentTimeMillis();
}
static const char *getHomeDir()
{
static char dir[1024];
#ifdef _WINDOWS
ITEMIDLIST* pidl;
HRESULT hRes = SHGetSpecialFolderLocation(NULL, CSIDL_PROFILE | CSIDL_FLAG_CREATE, &pidl);
if(hRes==NOERROR)
SHGetPathFromIDList(pidl, dir);
else
STR_CPY(dir, ".");
#else
STR_CPY(dir, ".");
const char *h = getenv("HOME");
if(h)
STR_CPY(dir, h);
#endif
return dir;
}
static const char *getAppDataDir()
{
static char dir[1024] = "";
#ifdef _WINDOWS
ITEMIDLIST* pidl;
HRESULT hRes = SHGetSpecialFolderLocation(NULL, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, &pidl);
if(hRes==NOERROR)
SHGetPathFromIDList(pidl, dir);
else
STR_CPY(dir, ".");
#else
STR_CPY(dir, ".");
const char *h = getenv("HOME");
if(h)
{
STR_CPY(dir, h);
# if defined(__APPLE__) && defined(__GNUC__)
if(File::exists(S(h, "/Library/Application Support")))
STR_APP(dir, "/Library/Application Support");
# endif
}
#endif
return dir;
}
static void alert(const char *prompt, const char *msg)
{
#ifdef WIN32
Windows::alert(prompt, msg);
#else
fprintf(stderr,
"************* ALERT *************\n"
"%s:\n"
" %s\n"
"*********************************\n",
prompt, msg
);
#endif
// XXX palm
}
static bool query(const char *prompt, const char *msg)
{
#ifdef WIN32
return Windows::query(prompt, msg);
#else
# warning "**** Sytem::query() has no code ****"
return 0;
#endif
}
static void info(const char *prompt, const char *msg)
{
#ifdef WIN32
Windows::msg(prompt, msg);
#endif
#if defined(linux) || (defined(__APPLE__) && defined(__GNUC__))
// XXX Use gui.cpp to make a real window?
fprintf(stderr,
"------------- INFO --------------\n"
"%s:\n"
" %s\n"
"---------------------------------\n",
prompt, msg
);
#endif
// XXX palm
}
static void ssleep(unsigned int secs)
{
#ifdef WIN32
Sleep(secs * 1000);
#endif
#if defined(linux) || (defined(__APPLE__) && defined(__GNUC__))
sleep(secs);
#endif
#ifdef __palmos__
SysTaskDelay(secs * SysTicksPerSecond());
#endif
}
static void msleep(unsigned int millis)
{
#ifdef WIN32
Sleep(millis);
#endif
#if defined(linux) || (defined(__APPLE__) && defined(__GNUC__))
usleep(millis * 1000);
#endif
#ifdef __palmos__
float millisPerTick = 1000.0f / SysTicksPerSecond();
unsigned long ticksToSleep = (unsigned long)(millis / millisPerTick);
SysTaskDelay(ticksToSleep);
#endif
}
/* XXX move this somewhere more appropriate */
static void invokeURL(const char *url)
{
#ifdef WIN32
// don't know what this returns ...
ShellExecute(NULL,"open",url,NULL,NULL,SW_SHOWNORMAL);
#endif
// XXX linux
// XXX palm
}
// return path of directory where applications should be installed
static bool getAppsPath(char *buf, int bufsize)
{
#ifdef WIN32
return Windows::getProgramFilesPath(buf, bufsize) == 1;
#elif defined(__APPLE__)
return "/Applications";
#else
# warning "**** Sytem::getAppsPath() has no code ****"
buf[0] = 0;
return false;
#endif
}
/*
Runs an executable given a full command line such as:
"\"c:\program files\sleepless\monkeys\butt.exe\" \"arg1\" arg2"
Note the embedded double quotes.
The exe is launched and runs independently of the calling prog.
The caller does not wait for the launched prog in any way, nor
is any return code from the launched prog returned or made available
in any way.
Returns 0 on success.
*/
static int launchProgram(const char *commandLine, bool waitforit = false, int *rcvpid = 0)
{
#ifdef WIN32
return Windows::launchProgram(commandLine, waitforit, rcvpid);
#else
# warning "**** Sytem::launchProgram() has no code ****"
return 0;
#endif
}
static int killProcess(int pid)
{
#ifdef WIN32
return Windows::killProcess(pid);
#else
return kill(SIGKILL, pid);
//# warning "**** Sytem::killProcess(int) has no code ****"
// return 0;
#endif
}
static int killProcess(const char *pat)
{
#ifdef WIN32
return Windows::killProcess(pat);
#else
# warning "**** Sytem::killProcess(const char *) has no code ****"
return 0;
#endif
}
static int createLink(const char *target, const char *sc, const char *icon)
{
#ifdef WIN32
return Windows::mkShortcut(target, sc, icon);
#else
return launchProgram(S("ln -s \"",target,"\" \"",sc,"\""), true);
#endif
}
#ifdef WIN32
static int screenCap(const char *file)
{
#ifdef WIN32
HBITMAP hDIB;
BITMAPINFOHEADER bmpih;
BITMAPFILEHEADER bmpfh;
HDC hdc, hdcOld, hdcMem;
BYTE* imgBuf;
HANDLE hFile;
DWORD written;
UINT w = GetSystemMetrics (SM_CXSCREEN),
h = GetSystemMetrics (SM_CYSCREEN),
size = w * h * 3;
// Allocate image buffer
imgBuf = new BYTE[size];
// Initialize bitmap information header
bmpih.biSize = sizeof(BITMAPINFOHEADER);
bmpih.biWidth = w;
bmpih.biHeight = h;
bmpih.biPlanes = 1;
bmpih.biBitCount = 24;
bmpih.biCompression = BI_RGB;
bmpih.biSizeImage = sizeof(imgBuf);
bmpih.biXPelsPerMeter = 0;
bmpih.biYPelsPerMeter = 0;
bmpih.biClrUsed = 0;
bmpih.biClrImportant = 0;
// Capture Screen
hdc = GetDC (NULL);
hDIB = CreateDIBSection (hdc, (BITMAPINFO*)&bmpih, DIB_RGB_COLORS, (void**)&imgBuf, NULL, 0);
hdcMem = CreateCompatibleDC (hdc);
hdcOld = (HDC) SelectObject (hdcMem, hDIB);
BitBlt (hdcMem, 0, 0, w, h, hdc, 0, 0, SRCCOPY);
SelectObject (hdcMem, hdcOld);
ReleaseDC (NULL, hdc);
// Initialize bitmap file header
bmpfh.bfType = *(WORD*)"BM";
bmpfh.bfOffBits = sizeof(BITMAPFILEHEADER) + bmpih.biSize;
bmpfh.bfSize = bmpfh.bfOffBits + bmpih.biSizeImage;
bmpfh.bfReserved1 = bmpfh.bfReserved2 = 0;
// Write bitmap to file
hFile = CreateFile (file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
return 1;
}
WriteFile (hFile, &bmpfh, sizeof(BITMAPFILEHEADER), &written, NULL);
WriteFile (hFile, &bmpih, sizeof(BITMAPINFOHEADER), &written, NULL);
WriteFile (hFile, imgBuf, size, &written, NULL);
CloseHandle (hFile);
return 0;
#endif
}
#endif
};
#endif