16#include "IPlugAPP_host.h"
32#define STBIW_WINDOWS_UTF8
33#define STB_IMAGE_WRITE_IMPLEMENTATION
34#include "../../Dependencies/IGraphics/STB/stb_image_write.h"
36extern WDL_DLGRET MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
39extern HINSTANCE gHINSTANCE;
43bool SaveWindowScreenshot(HWND hwnd,
const char* path)
50 if (!GetClientRect(hwnd, &clientRect))
53 int width = clientRect.right - clientRect.left;
54 int height = clientRect.bottom - clientRect.top;
56 if (width <= 0 || height <= 0)
60 HDC hdcWindow = GetDC(hwnd);
64 HDC hdcMem = CreateCompatibleDC(hdcWindow);
67 ReleaseDC(hwnd, hdcWindow);
73 bmi.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
74 bmi.bmiHeader.biWidth = width;
75 bmi.bmiHeader.biHeight = -height;
76 bmi.bmiHeader.biPlanes = 1;
77 bmi.bmiHeader.biBitCount = 32;
78 bmi.bmiHeader.biCompression = BI_RGB;
80 void* pBits =
nullptr;
81 HBITMAP hBitmap = CreateDIBSection(hdcMem, &bmi, DIB_RGB_COLORS, &pBits,
nullptr, 0);
83 if (!hBitmap || !pBits)
86 ReleaseDC(hwnd, hdcWindow);
90 HGDIOBJ hOldBitmap = SelectObject(hdcMem, hBitmap);
95#ifndef PW_RENDERFULLCONTENT
96 #define PW_RENDERFULLCONTENT 0x00000002
98 BOOL captured = PrintWindow(hwnd, hdcMem, PW_CLIENTONLY | PW_RENDERFULLCONTENT);
103 captured = BitBlt(hdcMem, 0, 0, width, height, hdcWindow, 0, 0, SRCCOPY);
106 SelectObject(hdcMem, hOldBitmap);
108 ReleaseDC(hwnd, hdcWindow);
112 DeleteObject(hBitmap);
117 uint8_t* pixels =
static_cast<uint8_t*
>(pBits);
118 for (
int i = 0; i < width * height; i++)
121 uint8_t temp = pixels[i * 4 + 0];
122 pixels[i * 4 + 0] = pixels[i * 4 + 2];
123 pixels[i * 4 + 2] = temp;
125 pixels[i * 4 + 3] = 255;
129 int result = stbi_write_png(path, width, height, 4, pixels, width * 4);
131 DeleteObject(hBitmap);
136int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam,
int nShowCmd)
140#ifndef APP_ALLOW_MULTIPLE_INSTANCES
141 HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, 0, BUNDLE_NAME);
144 hMutex = CreateMutex(0, 0, BUNDLE_NAME);
147 HWND hWnd = FindWindow(0, BUNDLE_NAME);
148 SetForegroundWindow(hWnd);
152 gHINSTANCE = hInstance;
154 InitCommonControls();
155 gScrollMessage = RegisterWindowMessage(
"MSWHEEL_ROLLMSG");
162 LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
165 for (
int i = 1; i < argc; i++)
167 if (wcscmp(argv[i], L
"--screenshot") == 0 && (i + 1) < argc)
171 else if (wcscmp(argv[i], L
"--no-io") == 0)
184 pAppHost->TryToChangeAudio();
186 HACCEL hAccel = LoadAccelerators(gHINSTANCE, MAKEINTRESOURCE(IDR_ACCELERATOR1));
188 static UINT(WINAPI *__SetProcessDpiAwarenessContext)(DPI_AWARENESS_CONTEXT);
190 if (!__SetProcessDpiAwarenessContext)
192 HINSTANCE h = LoadLibrary(
"user32.dll");
193 if (h) *(
void **)&__SetProcessDpiAwarenessContext = GetProcAddress(h,
"SetProcessDpiAwarenessContext");
194 if (!__SetProcessDpiAwarenessContext)
195 *(
void **)&__SetProcessDpiAwarenessContext = (
void*)(INT_PTR)1;
197 if ((UINT_PTR)__SetProcessDpiAwarenessContext > (UINT_PTR)1)
199 __SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
202 CreateDialog(gHINSTANCE, MAKEINTRESOURCE(IDD_DIALOG_MAIN), GetDesktopWindow(), IPlugAPPHost::MainDlgProc);
204#if !defined _DEBUG || defined NO_IGRAPHICS
205 HMENU menu = GetMenu(gHWND);
206 RemoveMenu(menu, 1, MF_BYPOSITION);
213 int vvv = GetMessage(&msg, NULL, 0, 0);
226 DispatchMessage(&msg);
230 if (gHWND && (TranslateAccelerator(gHWND, hAccel, &msg) || IsDialogMessage(gHWND, &msg)))
234 HWND hWndParent = NULL;
235 HWND temphwnd = msg.hwnd;
239 if (GetClassLong(temphwnd, GCW_ATOM) == (INT)32770)
241 hWndParent = temphwnd;
242 if (!(GetWindowLong(temphwnd, GWL_STYLE) & WS_CHILD))
246 while (temphwnd = GetParent(temphwnd));
248 if (hWndParent && IsDialogMessage(hWndParent,&msg))
251 TranslateMessage(&msg);
252 DispatchMessage(&msg);
257 DestroyWindow(gHWND);
259#ifndef APP_ALLOW_MULTIPLE_INSTANCES
260 ReleaseMutex(hMutex);
263 catch(
const std::exception& e)
265 fprintf(stderr,
"Fatal exception: %s\n", e.what());
266 DBGMSG(
"Exception: %s", e.what());
273#import <Cocoa/Cocoa.h>
276#include "IPlugSWELL.h"
282typedef CGImageRef (*CGWindowListCreateImageFunc)(CGRect, uint32_t, uint32_t, uint32_t);
285extern "C" bool SaveWindowScreenshot(
void* hwnd,
const char* path)
290 NSView* view = (__bridge NSView*)hwnd;
291 NSWindow* window = [view window];
298 static CGWindowListCreateImageFunc pCGWindowListCreateImage =
nullptr;
299 if (!pCGWindowListCreateImage)
301 void* handle = dlopen(
"/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics", RTLD_LAZY);
303 pCGWindowListCreateImage = (CGWindowListCreateImageFunc)dlsym(handle,
"CGWindowListCreateImage");
306 if (!pCGWindowListCreateImage)
310 CGWindowID windowID = (CGWindowID)[window windowNumber];
313 CGImageRef cgImage = pCGWindowListCreateImage(
315 kCGWindowListOptionIncludingWindow,
317 kCGWindowImageBoundsIgnoreFraming
324 NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
325 CGImageRelease(cgImage);
330 NSData* pngData = [bitmap representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
334 NSString* filePath = [NSString stringWithUTF8String:path];
335 return [pngData writeToFile:filePath atomically:YES];
337extern HMENU SWELL_app_stocksysmenu;
339static WDL_String gScreenshotPath;
340static bool gNoIO =
false;
342int main(
int argc,
char *argv[])
346 if (argc > 2 && std::string_view(argv[2]) ==
"registerauv3")
348 WDL_String appexPath;
349 appexPath.SetFormatted(1024,
"pluginkit -a %s%s%s.appex", argv[0],
"/../../Plugins/", appexPath.get_filepart());
350 if (system(appexPath.Get()) > -1)
351 NSLog(
@"Registered audiounit app extension\n");
353 NSLog(
@"Failed to register audiounit app extension\n");
358 for (
int i = 1; i < argc; i++)
360 if (strcmp(argv[i],
"--screenshot") == 0 && i + 1 < argc)
362 gScreenshotPath.Set(argv[i + 1]);
365 else if (strcmp(argv[i],
"--no-io") == 0)
371 if (AppIsSandboxed())
372 DBGMSG(
"App is sandboxed, file system access etc restricted!\n");
374 return NSApplicationMain(argc, (
const char**) argv);
377INT_PTR SWELLAppMain(
int msg, INT_PTR parm1, INT_PTR parm2)
383 case SWELLAPP_ONLOAD:
385 pAppHost = IPlugAPPHost::Create();
388 if (gScreenshotPath.GetLength() > 0)
399 pAppHost->TryToChangeAudio();
402 case SWELLAPP_LOADED:
404 pAppHost = IPlugAPPHost::sInstance.get();
406 HMENU menu = SWELL_GetCurrentMenu();
411 menu = SWELL_DuplicateMenu(menu);
412 HMENU src = LoadMenu(NULL, MAKEINTRESOURCE(IDR_MENU1));
414 for (
int x = 0; x < GetMenuItemCount(src)-1; x++)
416 HMENU sm = GetSubMenu(src,x);
421 MENUITEMINFO mii = {
sizeof(mii), MIIM_TYPE};
422 mii.dwTypeData = str;
423 mii.cch =
sizeof(str);
425 GetMenuItemInfo(src, x, TRUE, &mii);
426 MENUITEMINFO mi= {
sizeof(mi), MIIM_STATE|MIIM_SUBMENU|MIIM_TYPE,MFT_STRING, 0, 0, SWELL_DuplicateMenu(sm), NULL, NULL, 0, str};
427 InsertMenuItem(menu, x+1, TRUE, &mi);
434 HMENU sm = GetSubMenu(menu, 1);
435 DeleteMenu(sm, ID_QUIT, MF_BYCOMMAND);
436 DeleteMenu(sm, ID_PREFERENCES, MF_BYCOMMAND);
439 int a = GetMenuItemCount(sm);
441 while (a > 0 && GetMenuItemID(sm, a-1) == 0)
442 DeleteMenu(sm, --a, MF_BYPOSITION);
444 DeleteMenu(menu, 1, MF_BYPOSITION);
449 SetMenuItemModifier(menu, ID_SCREENSHOT, MF_BYCOMMAND,
'S', FCONTROL | FSHIFT);
452#if !defined _DEBUG || defined NO_IGRAPHICS
455 HMENU sm = GetSubMenu(menu, 1);
456 DeleteMenu(sm, ID_LIVE_EDIT, MF_BYCOMMAND);
457 DeleteMenu(sm, ID_SHOW_BOUNDS, MF_BYCOMMAND);
458 DeleteMenu(sm, ID_SHOW_DRAWN, MF_BYCOMMAND);
459 DeleteMenu(sm, ID_SHOW_FPS, MF_BYCOMMAND);
462 int a = GetMenuItemCount(sm);
464 while (a > 0 && GetMenuItemID(sm, a-1) == 0)
465 DeleteMenu(sm, --a, MF_BYPOSITION);
468 if (GetMenuItemCount(sm) == 0)
469 DeleteMenu(menu, 1, MF_BYPOSITION);
472 SetMenuItemModifier(menu, ID_LIVE_EDIT, MF_BYCOMMAND,
'E', FCONTROL);
473 SetMenuItemModifier(menu, ID_SHOW_DRAWN, MF_BYCOMMAND,
'D', FCONTROL);
474 SetMenuItemModifier(menu, ID_SHOW_BOUNDS, MF_BYCOMMAND,
'B', FCONTROL);
475 SetMenuItemModifier(menu, ID_SHOW_FPS, MF_BYCOMMAND,
'F', FCONTROL);
478 HWND hwnd = CreateDialog(gHINST, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, IPlugAPPHost::MainDlgProc);
483 SWELL_SetDefaultModalWindowMenu(menu);
488 case SWELLAPP_ONCOMMAND:
490 if (gHWND && (parm1&0xffff))
491 SendMessage(gHWND, WM_COMMAND, parm1 & 0xffff, 0);
493 case SWELLAPP_DESTROY:
495 DestroyWindow(gHWND);
497 case SWELLAPP_PROCESSMESSAGE:
498 MSG* pMSG = (MSG*) parm1;
499 NSView* pContentView = (NSView*) pMSG->hwnd;
500 NSEvent* pEvent = (NSEvent*) parm2;
501 int etype = (int) [pEvent type];
503 bool textField = [pContentView isKindOfClass:[NSText
class]];
505 if (!textField && etype == NSKeyDown)
507 int flag, code = SWELL_MacKeyToWindowsKey(pEvent, &flag);
509 if (!(flag&~FVIRTKEY) && (code == VK_RETURN || code == VK_ESCAPE))
511 [pContentView keyDown: pEvent];
520#define CBS_HASSTRINGS 0
521#define SWELL_DLG_SCALE_AUTOGEN 1
522#define SET_IDD_DIALOG_PREF_SCALE 1.5
524#define SWELL_DLG_FLAGS_AUTOGEN SWELL_DLG_WS_FLIPPED|SWELL_DLG_WS_RESIZABLE
526#include "swell-dlggen.h"
527#include "resources/main.rc_mac_dlg"
528#include "swell-menugen.h"
529#include "resources/main.rc_mac_menu"
532#elif defined(OS_LINUX)
533#include "IPlugSWELL.h"
537int gStandaloneExitStatus = EXIT_SUCCESS;
538bool gStandaloneCloseRequested =
false;
540static bool gNoIO =
false;
542int main(
int argc,
char** argv)
544 SWELL_initargs(&argc, &argv);
546 SWELL_ExtendedAPI(
"LOAD_GTK",
nullptr);
547 SWELL_Internal_PostMessage_Init();
548 SWELL_ExtendedAPI(
"APPNAME", (
void*) BUNDLE_NAME);
551 for (
int i = 1; i < argc; i++)
553 if (strcmp(argv[i],
"--no-io") == 0)
562 if (!pAppHost->Init())
564 fprintf(stderr,
"Standalone initialization failed; audio and editor startup cancelled\n");
567 if (!pAppHost->TryToChangeAudio())
568 fprintf(stderr,
"Audio startup failed; check the standalone audio settings\n");
570 HMENU menu = LoadMenu(NULL, MAKEINTRESOURCE(IDR_MENU1));
571 if (!CreateDialog(gHINSTANCE, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, IPlugAPPHost::MainDlgProc))
573 fprintf(stderr,
"Could not create the standalone window\n");
579 SetMenu(gHWND, menu);
582 fprintf(stderr,
"Standalone menu could not be loaded; opening the editor without it\n");
585 if (!PostMessage(gHWND, WM_USER_OPENWINDOW, 0, 0))
587 fprintf(stderr,
"Could not schedule standalone editor startup\n");
588 DestroyWindow(gHWND);
594 SWELL_RunMessageLoop();
595 if (gStandaloneCloseRequested && gHWND)
597 gStandaloneCloseRequested =
false;
598 DestroyWindow(gHWND);
604 DestroyWindow(gHWND);
606 return gStandaloneExitStatus;
609INT_PTR SWELLAppMain(
int msg, INT_PTR parm1, INT_PTR parm2)
614#define CBS_HASSTRINGS 0
615#define SWELL_DLG_SCALE_AUTOGEN 1
616#define SET_IDD_DIALOG_PREF_SCALE 1.5
618#define SWELL_DLG_FLAGS_AUTOGEN SWELL_DLG_WS_FLIPPED|SWELL_DLG_WS_RESIZABLE
620#include "swell-dlggen.h"
621#ifndef SWELL_DLG_WS_DEFAULT_SCALING
622#define SWELL_DLG_WS_DEFAULT_SCALING 0
624#include "resources/main.rc_mac_dlg"
625#include "swell-menugen.h"
626#include "resources/main.rc_mac_menu"
Common paths useful for plug-ins.
A class that hosts an IPlug as a standalone app and provides Audio/Midi I/O.
void SetScreenshotPath(const char *path)
Set screenshot path for CLI screenshot mode.
void SetNoIO(bool noIO)
Set no-I/O mode (disables audio and MIDI initialization)
bool IsScreenshotMode() const
Check if in screenshot mode.