📄 main.c
字号:
#include <windows.h>
#include <windowsx.h>
#include <commdlg.h>
#include <stdlib.h>
#include <winreg.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
//#include <sys/time.h>
//#include <sys/types.h>
#include "../lib/flash.h"
#include "../../resource.h"
HINSTANCE hInst;
int cbheight;
typedef struct {
FlashDisplay fd;
HWND target; // Target window
HDC hdc; // X11 Graphic context
HBITMAP hbmp;
HDC bmpDC; // X11 Graphic context
//Pixmap canvas; // Graphic buffer
} Win32Context;
Win32Context wc1, *wc=&wc1;
int shape_size,shape_nb,shaperecord_size,shaperecord_nb,style_size,style_nb;
long framerate = 0;
BOOL diag = FALSE;
LPTSTR fileDropped = NULL;
struct timeval {
long tv_sec;
long tv_usec;
};
#define TB_GO 2323
#define TB_OPEN 2324
#define TB_PAUSE 2325
#define TB_RESTART 2326
static TBBUTTON tbCBr1Button[] = {
//{STD_FILENEW, TB_STD_FILENEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0, -1},
{7, TB_GO, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0, -1},
{STD_FILEOPEN, TB_OPEN, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0, -1},
{7, TB_GO, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0, -1},
{4, TB_GO, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0, -1},
{2, TB_PAUSE, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0, -1},
{7, TB_GO, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0, -1},
{3, TB_RESTART, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0, -1},
};
/*
* This file is the entry of a very simple Flash Player
*/
#define WM_FLASHWAKEUP (WM_USER+501)
#define WM_SETFLASHHANDLE (WM_USER+502)
#define WM_UNLOADFLASH (WM_USER+503)
#define WM_FILEDROPPED (WM_USER+504)
#define WM_EVERYTHINGLOADED (WM_USER+505)
HWND frame,movie,control;
struct FlashInfo fi;
char *filename;
TCHAR frameName[256] = _T("the frame");
TCHAR controlName[256] = _T("the control");
TCHAR movieName[256] = _T("the movie");
HINSTANCE g_hInstance;
extern void exit(int c);
static OPENFILENAME ofn ;
void PalmFileInitialize (HWND hwnd)
{
static TCHAR szFilter[] = TEXT("Flash Files (*.SWF)\0*.swf\0All Files (*.*)\0*.*\0\0");
ofn.lStructSize = sizeof (OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = NULL;
ofn.lpstrFilter = szFilter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = NULL;
ofn.nMaxFile = _MAX_PATH;
ofn.lpstrFileTitle = NULL;
#ifdef x86
ofn.nMaxFileTitle = 257;
#else
ofn.nMaxFileTitle = MAX_PATH;
#endif
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = NULL;
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = TEXT("swf");
ofn.lCustData = 0L;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
}
BOOL PalmFileOpenDlg (HWND hwnd, LPTSTR pstrFileName, LPTSTR pstrTitleName)
{
ofn.hwndOwner = hwnd;
ofn.lpstrFile = pstrFileName;
ofn.lpstrFileTitle = pstrTitleName;
ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT;
return GetOpenFileName (&ofn) ;
}
void readFile(char *filename, char **buffer, long *size)
{
HANDLE hFile;
TCHAR filenameW[MAX_PATH+1];
BY_HANDLE_FILE_INFORMATION info;
DWORD cbRead;
LPBYTE buf;
mbstowcs(filenameW, filename, strlen(filename) + 1);
hFile = CreateFile(filenameW, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (hFile == NULL || hFile == INVALID_HANDLE_VALUE) {
MessageBox(NULL, filenameW, _T("Error Loading"), 0);
TerminateProcess(NULL, 0);
}
GetFileInformationByHandle(hFile, &info);
buf = malloc(info.nFileSizeLow);
ReadFile(hFile, buf, info.nFileSizeLow, &cbRead, 0);
*size = info.nFileSizeLow;
*buffer = buf;
CloseHandle(hFile);
}
void drawInfo(HWND hdlg)
{
TCHAR msg[1024];
TCHAR filenameW[MAX_PATH];
TCHAR finalmsg[1024];
HDC hdc;
RECT rect;
hdc = GetDC(control);
wcscpy(finalmsg, _T(""));
GetClientRect(control, &rect);
SelectObject(hdc, GetStockObject(WHITE_PEN));
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
Rectangle(hdc, 0, 0, rect.right, rect.bottom);
mbstowcs(filenameW, strchr(filename, '\\'), strlen(filename)+1);
wsprintf(msg, L"%s (v%d), Frames=%d, %d fps ",
filenameW,fi.version,fi.frameCount,fi.frameRate);
SelectObject(hdc, GetStockObject(WHITE_PEN));
SelectObject(hdc, GetStockObject(BLACK_BRUSH));
wcscat(finalmsg, msg);
wcscat(finalmsg, _T("\n"));
wsprintf(msg, L"icons: Open, Continue, Pause, Restart");
wcscat(finalmsg, msg);
SetDlgItemText(hdlg, IDC_INFO, finalmsg);
ReleaseDC(control, hdc);
}
long FlashTick(FlashHandle fh, long flag, MSG *event, struct timeval *wakeDate, long *actualFramerate) {
FlashEvent fe;
long ret;
long delayed = GetTickCount();
if (flag & FLASH_EVENT) {
// X to Flash event structure conversion
switch (event->message) {
case WM_LBUTTONDOWN:
fe.type = FeButtonPress;
break;
case WM_LBUTTONUP:
fe.type = FeButtonRelease;
break;
case WM_MOUSEMOVE:
fe.type = FeMouseMove;
fe.x = (int)(SHORT)LOWORD(event->lParam);
fe.y = (int)(SHORT)HIWORD(event->lParam);
break;
case WM_PAINT:
fe.type = FeRefresh;
break;
default:
fe.type = FeNone;
break;
}
}
ret = FlashExec(fh, flag, &fe, wakeDate);
if (actualFramerate) *actualFramerate = GetTickCount() - delayed;
return ret;
}
void playMovie(FlashHandle flashHandle, HANDLE *dpy, HWND movie)
{
struct timeval wd;//,de,now;
long cmd;
long delay = 0;
long wakeUp = 1;
long z = 1;
long x = 0;
long y = 0;
cmd = FLASH_WAKEUP;
wakeUp = FlashTick(flashHandle, cmd, 0, &wd, &framerate);
SetFocus(movie);
}
void
showUrl(char *url, char *target, void *client_data)
{
printf("GetURL : %s\n", url);
}
long FlashGraphicInitWin32(FlashHandle flashHandle, HWND movie) {
//XWindowAttributes wattr;
RECT wattr;
HDC hdc;
int bpp, pad, targetWidth, targetHeight, zoom, movieWidth, movieHeight, bpl;
HBITMAP canvas;
LPBYTE canvasBuffer;
GetWindowRect(movie, &wattr);
hdc = GetDC(movie);
bpp = GetDeviceCaps(hdc, BITSPIXEL) / 8;
pad = 4;//(512-480);
targetWidth = wattr.right - wattr.left; //wattr.width;
targetHeight = wattr.bottom - wattr.top; // wattr.height;
zoom = 20;
movieWidth = targetWidth;
movieHeight = targetHeight;
bpp = 2;
if (bpp) {
bpl = (targetWidth*bpp + pad-1)/pad*pad;
} else {
bpl = (targetWidth/8 + pad-1)/pad*pad;
}
//allocColor = allocColor16_565;
// create canvas == DIBSection
// TODO DIBsection code
{
BITMAPINFO * bmi;
LPDWORD masks;
bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO)+(sizeof(RGBQUAD)*256));
bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi->bmiHeader.biWidth = targetWidth;
bmi->bmiHeader.biHeight = -targetHeight;
bmi->bmiHeader.biPlanes = 1;
bmi->bmiHeader.biBitCount = bpp*8;
bmi->bmiHeader.biCompression = BI_BITFIELDS ;
bmi->bmiHeader.biSizeImage = targetWidth*targetHeight*bpp;
bmi->bmiHeader.biXPelsPerMeter = 0;
bmi->bmiHeader.biYPelsPerMeter = 0;
bmi->bmiHeader.biClrUsed = bpp*8;
bmi->bmiHeader.biClrImportant = bpp*8;
masks = (LPDWORD)((LPBYTE)&bmi->bmiHeader + bmi->bmiHeader.biSize);
masks[0] = 0x0000F800; // red (0000 0000 0000 0000 1111 1000 0000 0000)
masks[1] = 0x000007E0; // green (0000 0000 0000 0000 0000 0111 1110 0000)
masks[2] = 0x0000001F; // blue (0000 0000 0000 0000 0000 0000 0001 1111)
canvas = CreateDIBSection( hdc, bmi, DIB_RGB_COLORS, (void **)&canvasBuffer, NULL, 0);
if (!canvas) {
TCHAR buf[80];
wsprintf(buf, _T("DIBSection error == %d, bpp = %d, targetWidth = %d, targetHeight = %d"), GetLastError(), bpp, targetWidth, targetHeight);
MessageBox(movie, buf, _T("DIB error"), 0);
}
wc->hbmp = canvas;
free(bmi);
}
wc->fd.pixels = canvasBuffer;
wc->fd.width = targetWidth + 1;
wc->fd.height = targetHeight + 1;
wc->fd.bpl = targetWidth * 2;
wc->fd.depth = 16;
wc->hdc = hdc;
wc->bmpDC = CreateCompatibleDC(hdc);
SelectObject(wc->bmpDC, canvas);
GetClientRect(movie, &wattr);
FillRect(wc->bmpDC, &wattr, (HBRUSH)GetStockObject(WHITE_BRUSH));
wc->target = movie;
return FlashGraphicInit(flashHandle, &wc->fd);
}
void loadMovie(char* themovie) {
static char *buffer = NULL;
long size;
int status;
FlashHandle flashHandle;
if (themovie == NULL) {
return;
}
filename = themovie;
readFile(filename, &buffer, &size);
flashHandle = FlashNew();
if (flashHandle == 0) {
exit(1);
}
do {
status = FlashParse(flashHandle, 0, buffer, size);
} while (status & FLASH_PARSE_NEED_DATA);
free(buffer);
if (movie) {
SendMessage(movie, WM_UNLOADFLASH, 0, (LPARAM)flashHandle);
SendMessage(movie, WM_SETFLASHHANDLE, 0, (LPARAM)flashHandle);
}
FlashGetInfo(flashHandle, &fi);
FlashGraphicInitWin32(flashHandle, movie);
FlashSoundInit(flashHandle, "/dev/dsp");
FlashSetGetUrlMethod(flashHandle, showUrl, 0);
playMovie(flashHandle, NULL, movie);
}
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rt, rt1;
int DlgWidth, DlgHeight; // dialog width and height in pixel units
int NewPosX, NewPosY;
switch (message)
{
case WM_INITDIALOG:
// trying to center the About dialog
if (GetWindowRect(hDlg, &rt1)) {
GetClientRect(GetParent(hDlg), &rt);
DlgWidth = rt1.right - rt1.left;
DlgHeight = rt1.bottom - rt1.top ;
NewPosX = (rt.right - rt.left - DlgWidth)/2;
NewPosY = (rt.bottom - rt.top - DlgHeight)/2;
// if the About box is larger than the physical screen
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
drawInfo(hDlg);
return TRUE;
case WM_COMMAND:
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
void LoadFileDropped() {
if (fileDropped && (lstrlen(fileDropped) > 0) && wcschr(fileDropped, '\\')) {
char * buf;
buf = (char*)malloc(MAX_PATH);
//strcpy(buf, "\\test.swf");
wcstombs(buf, fileDropped, lstrlen(fileDropped) + 1);
loadMovie(buf);
}
}
LRESULT frameWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
static HWND hwndCB;
int wmId, wmEvent;
if (msg == WM_CREATE) {
hwndCB = CommandBar_Create(hInst, hwnd, 1);
CommandBar_InsertMenubar(hwndCB, hInst, IDM_MENU, 0);
CommandBar_AddBitmap(hwndCB, hInst, IDB_BITMAP1, 6, 16, 16);
CommandBar_AddBitmap(hwndCB, HINST_COMMCTRL, IDB_STD_SMALL_COLOR, 15, 16, 16);
CommandBar_AddButtons(hwndCB, sizeof(tbCBr1Button)/sizeof(TBBUTTON), tbCBr1Button);
CommandBar_AddAdornments(hwndCB, 0, 0);
cbheight = CommandBar_Height(hwndCB);
{
HMENU hmenu = CommandBar_GetMenu(hwndCB, 0);
if (hmenu) {
HMENU tools = GetSubMenu(hmenu, 1);
if (tools) {
CheckMenuItem(tools, ID_TOOLS_DIAGNOSTICS, diag);
}
}
}
} else if (msg == WM_COPYDATA) {
if (((PCOPYDATASTRUCT)lp)->dwData == WM_FILEDROPPED) {
fileDropped = (LPTSTR)((PCOPYDATASTRUCT)lp)->lpData;
LoadFileDropped();
}
fileDropped = NULL;
} else if (msg == WM_EVERYTHINGLOADED) {
// check for commandline
LoadFileDropped();
} else if (msg == WM_COMMAND) {
wmId = LOWORD(wp);
wmEvent = HIWORD(wp);
// Parse the menu selections:
switch (wmId)
{
case TB_GO:
SendMessage(movie, WM_KEYDOWN, (WPARAM)'C', 0);
break;
case TB_PAUSE:
SendMessage(movie, WM_KEYDOWN, (WPARAM)'P', 0);
break;
case TB_RESTART:
SendMessage(movie, WM_KEYDOWN, (WPARAM)'R', 0);
break;
case TB_OPEN:
case IDM_FILE_LOAD:
{
TCHAR g_szFileName[MAX_PATH*2];
TCHAR g_szTitleName[MAX_PATH*2];
SendMessage(movie, WM_KEYDOWN, (WPARAM)'P', 0);
PalmFileInitialize (hwnd) ;
if (PalmFileOpenDlg (hwnd, g_szFileName, g_szTitleName)) {
char * buf;
buf = (char*)malloc(MAX_PATH);
//strcpy(buf, "\\test.swf");
wcstombs(buf, g_szFileName, lstrlen(g_szFileName) + 1);
loadMovie(buf);
} else {
SendMessage(movie, WM_KEYDOWN, (WPARAM)'C', 0);
}
}
break;
case ID_TOOLS_DIAGNOSTICS:
{
HMENU hmenu = CommandBar_GetMenu(hwndCB, 0);
if (hmenu) {
HMENU tools = GetSubMenu(hmenu, 1);
if (tools) {
diag = !diag;
CheckMenuItem(tools, ID_TOOLS_DIAGNOSTICS, diag);
}
}
}
break;
case ID_HELP_ZOOMIN:
SendMessage(movie, WM_KEYDOWN, (WPARAM)'Z', 0);
SendMessage(movie, WM_KEYDOWN, (WPARAM)'C', 0);
break;
case ID_HELP_ZOOMOUT:
SendMessage(movie, WM_KEYDOWN, (WPARAM)'X', 0);
SendMessage(movie, WM_KEYDOWN, (WPARAM)'C', 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -