void KPR_system_saveFile(xsMachine* the) { FskErr err; xsIntegerValue argc = xsToInteger(xsArgc); xsStringValue name = NULL; xsStringValue prompt = NULL; xsStringValue initialPath = NULL; xsStringValue path = NULL; xsStringValue url = NULL; if ((argc > 0) && xsTest(xsArg(0))) { if (xsFindResult(xsArg(0), xsID_name)) name = xsToStringCopy(xsResult); if (xsFindResult(xsArg(0), xsID_prompt)) prompt = xsToStringCopy(xsResult); if (xsFindResult(xsArg(0), xsID_url)) KprURLToPath(xsToString(xsResult), &initialPath); } err = FskFileChooseSave(name, prompt, initialPath, &path); if ((kFskErrNone == err) && (NULL != path)) { KprPathToURL(path, &url); (void)xsCallFunction1(xsArg(1), xsNull, xsString(url)); } FskMemPtrDispose(url); FskMemPtrDispose(path); FskMemPtrDispose(initialPath); FskMemPtrDispose(prompt); FskMemPtrDispose(name); }
xsBooleanValue fxFindModuleLoadAsDirectory(xsMachine* the, xsStringValue base, xsStringValue name, xsIndex* id) { char node[1024]; UInt32 nodeSize; xsStringValue slash; FskErr err; FskFileMapping map = NULL; xsBooleanValue result = 0; xsStringValue path = NULL; unsigned char* data; FskInt64 size; xsSlot slot; nodeSize = sizeof(node); FskStrNCopy(node, base, nodeSize); slash = FskStrRChr(node, '/'); if (slash) *slash = 0; FskStrNCat(node, "/", nodeSize); FskStrNCat(node, name, nodeSize); if (FskStrTail(node, "/") != 0) FskStrNCat(node, "/", nodeSize); FskStrNCat(node, "package.json", nodeSize); err = KprURLToPath(node, &path); if (err == kFskErrNone) { err = FskFileMap(path, &data, &size, 0, &map); if (err == kFskErrNone) { xsTry { slot = xsNewInstanceOf(xsChunkPrototype); xsSetHostData(slot, data); xsSetHostDestructor(slot, NULL); xsSet(slot, xsID("length"), xsInteger(size)); slot = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("parse"), slot); } xsCatch { slot = xsUndefined; } if (xsTest(slot)) { slot = xsGet(slot, xsID("main")); if (xsTest(slot)) { FskStrNCopy(node, name, nodeSize); if (FskStrTail(node, "/") != 0) FskStrNCat(node, "/", nodeSize); FskStrNCat(node, xsToString(slot), nodeSize); if (fxFindModuleLoadAsFile(the, base, node, id)) result = 1; } } }
void KPR_launchURI(xsMachine *the) { FskErr err; xsStringValue uri = xsToString(xsArg(0)); xsStringValue path = NULL; if (FskStrCompareWithLength(uri, "file://", 7)) err = FskLauncherOpenDocument(uri, 1); else { err = KprURLToPath(uri, &path); if (!err) { err = FskLauncherOpenDocument(path, 0); FskMemPtrDispose(path); } } xsThrowIfFskErr(err); }
void on_drag_data_received(GtkWidget* widget, GdkDragContext* context, gint x, gint y, GtkSelectionData *data, guint info, guint time, gpointer user_data) { FskGtkWindow gtkWin = user_data; gboolean dnd_success = FALSE; FskDragDropFile dropFileList = NULL; FskDragDropFile droppedFile = NULL; char* path = NULL; if (gdk_drag_context_get_suggested_action(context) == GDK_ACTION_COPY) { char* string = (char*)gtk_selection_data_get_data(data); char* end; FskFileInfo itemInfo; for (end = FskStrStr(string, "\r\n"); end; end = FskStrStr(string, "\r\n")) { BAIL_IF_ERR(FskMemPtrNewClear(sizeof(FskDragDropFileRecord), (FskMemPtr*)&droppedFile)); FskListAppend((FskList *)&dropFileList, droppedFile); *end = 0; BAIL_IF_ERR(KprURLToPath(string, &path)); BAIL_IF_ERR(FskFileGetFileInfo(path, &itemInfo)); if (itemInfo.filetype == kFskDirectoryItemIsDirectory) { int length = FskStrLen(path); BAIL_IF_ERR(FskMemPtrNew(length + 2, &droppedFile->fullPathName)); FskMemCopy(droppedFile->fullPathName, path, length); droppedFile->fullPathName[length] = '/'; droppedFile->fullPathName[length + 1] = 0; FskMemPtrDispose(path); } else { droppedFile->fullPathName = path; } path = NULL; string = end + 2; *end = '\r'; } (*gDropTargetProc)(kFskDragDropTargetEnterWindow, x, y, dropFileList, gtkWin->owner); (*gDropTargetProc)(kFskDragDropTargetDropInWindow, x, y, dropFileList, gtkWin->owner); dnd_success = TRUE; } bail: gtk_drag_finish(context, dnd_success, TRUE, time); FskMemPtrDispose(path); while (NULL != dropFileList) { droppedFile = dropFileList; FskListRemove((FskList *)&dropFileList, droppedFile); FskMemPtrDispose(droppedFile->fullPathName); FskMemPtrDispose(droppedFile); } }
void fxLoadModule(txMachine* the, txID moduleID) { txArchive* archive = the->archive; txSlot* key = fxGetKey(the, moduleID); txString path = NULL; char buffer[PATH_MAX]; txString dot = NULL; txString* extension; txLoader* loader; KprURLToPath(key->value.key.string, &path); c_strcpy(buffer, path); c_free(path); path = buffer; if (archive) { if (!c_strncmp(path, archive->base, archive->baseLength)) { txInteger c = archive->scriptCount, i; txScript* script = archive->scripts; path += archive->baseLength; #if mxWindows { char* separator = path; while (*separator) { if (*separator == '/') *separator = mxSeparator; separator++; } } #endif for (i = 0; i < c; i++) { if (!c_strcmp(path, script->path)) { fxResolveModule(the, moduleID, script, C_NULL, C_NULL); return; } script++; } } } dot = FskStrRChr(path, '.'); for (extension = gxExtensions, loader = gxLoaders; *extension; extension++, loader++) { if (!FskStrCompare(dot, *extension)) { (**loader)(the, buffer, moduleID); break; } } }
void KPR_host_set_profilingDirectory(xsMachine* the) { KprHost self = xsGetHostData(xsThis); KprApplication application = (KprApplication)self->first; char* directory = NULL; if (xsTest(xsArg(0))) { xsThrowIfFskErr(KprURLToPath(xsToString(xsArg(0)), &directory)); } xsBeginHost(application->the); { if (directory) { xsResult = xsString(directory); FskMemPtrDispose(directory); } (void)xsCall1(xsGet(xsGlobal, xsID("xs")), xsID("setProfilingDirectory"), xsResult); } xsEndHost(application->the); }
void KprFILEServiceInvoke(KprService service UNUSED, KprMessage message) { FskErr err = kFskErrNone; char* path = NULL; FskFileInfo info; FskDirectoryIterator iterator = NULL; char* pathName = NULL; char* sniff = NULL; FskFile fref = NULL; char *name = NULL; if (KprMessageContinue(message)) { if (!message->method || FskStrCompare(message->method, "GET")) { bailIfError(KprURLToPath(message->url, &path)); bailIfError(FskFileGetFileInfo(path, &info)); if (kFskDirectoryItemIsDirectory == info.filetype) { unsigned char buffer[4096]; UInt32 itemType; double date; UInt32 size; xsBeginHostSandboxCode(gFILEService.machine, NULL); { xsVars(3); { xsTry { xsVar(0) = xsGet(xsGlobal, xsID("Files")); xsResult = xsNewInstanceOf(xsArrayPrototype); bailIfError(FskDirectoryIteratorNew(path, &iterator, 0)); while (kFskErrNone == FskDirectoryIteratorGetNext(iterator, &name, &itemType)) { if (name[0] == '.') { FskMemPtrDisposeAt(&name); continue; } if (kFskDirectoryItemIsFile == itemType) { pathName = FskStrDoCat(path, name); bailIfError(FskFileGetFileInfo(pathName, &info)); bailIfError(FskFileOpen(pathName, kFskFilePermissionReadOnly, &fref)); bailIfError(FskFileRead(fref, sizeof(buffer), buffer, &size)); FskFileClose(fref); fref = NULL; if (kFskErrNone == FskMediaPlayerSniffForMIME(buffer, size, NULL, pathName, &sniff)) { } else if (kFskErrNone == FskImageDecompressSniffForMIME(buffer, size, NULL, pathName, &sniff)) { } FskMemPtrDispose(pathName); pathName = NULL; } xsVar(1) = xsNewInstanceOf(xsObjectPrototype); xsNewHostProperty(xsVar(1), xsID("name"), xsString(name), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("path"), xsString(name), xsDefault, xsDontScript); FskStrCopy((char*)buffer, message->url); FskStrCat((char*)buffer, name); if (kFskDirectoryItemIsDirectory == itemType) { xsVar(2) = xsGet(xsVar(0), xsID("directoryType")); FskStrCat((char*)buffer, "/"); } else if (kFskDirectoryItemIsFile == itemType) { xsVar(2) = xsGet(xsVar(0), xsID("fileType")); if (info.fileModificationDate > info.fileCreationDate) date = info.fileModificationDate * 1000.0; else date = info.fileCreationDate * 1000.0; xsNewHostProperty(xsVar(1), xsID("date"), xsNumber(date), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("size"), xsNumber(info.filesize), xsDefault, xsDontScript); if (sniff) { xsNewHostProperty(xsVar(1), xsID("mime"), xsString(sniff), xsDefault, xsDontScript); FskMemPtrDispose(sniff); sniff = NULL; } } else xsVar(2) = xsGet(xsVar(0), xsID("linkType")); xsNewHostProperty(xsVar(1), xsID("type"), xsVar(2), xsDefault, xsDontScript); xsNewHostProperty(xsVar(1), xsID("url"), xsString((char*)buffer), xsDefault, xsDontScript); (void)xsCall1(xsResult, xsID("push"), xsVar(1)); FskMemPtrDisposeAt(&name); } xsResult = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("stringify"), xsResult); message->response.body = FskStrDoCopy(xsToString(xsResult)); message->response.size = FskStrLen(message->response.body); KprMessageTransform(message, gFILEService.machine); } xsCatch { } } } xsEndHostSandboxCode(); } else if (kFskDirectoryItemIsFile == info.filetype) {
FskErr KprSoundLoad(KprSound self) { FskErr err = kFskErrNone; char *path = NULL; FskFile fref = NULL; FskMediaSpoolerRecord spooler; KprSoundLoadSpoolerRefconRecord refcon; FskMediaReader reader = NULL; FskMediaReaderTrack track; FskMediaPropertyValueRecord property; char *audioFormat = NULL; double duration; unsigned char *formatInfo = NULL; UInt32 formatInfoSize; UInt16 numChannels; double sampleRate; double timeScale; FskMediaReaderSampleInfo info = NULL; unsigned char *buffer = NULL; KprSoundFrame frames = NULL; UInt32 frameCount = 0; unsigned char *data = NULL; UInt32 dataSize = 0; if (self->data) goto bail; bailIfError(KprURLToPath(self->url, &path)); if (!self->mime) { unsigned char buffer[4096]; UInt32 size; char *sniff = NULL; bailIfError(FskFileOpen(path, kFskFilePermissionReadOnly, &fref)); bailIfError(FskFileRead(fref, sizeof(buffer), buffer, &size)); FskFileClose(fref); fref = NULL; bailIfError(FskMediaPlayerSniffForMIME(buffer, size, NULL, self->url, &sniff)); self->mime = sniff; } FskMemSet(&spooler, 0, sizeof(spooler)); FskMemSet(&refcon, 0, sizeof(refcon)); refcon.path = path; spooler.refcon = &refcon; spooler.doOpen = KprSoundLoadSpoolerOpen; spooler.doClose = KprSoundLoadSpoolerClose; spooler.doRead = KprSoundLoadSpoolerRead; spooler.doGetSize = KprSoundLoadSpoolerGetSize; bailIfError((*FskMediaReaderNewProc)(&reader, self->mime, self->url, &spooler, NULL, NULL)); bailIfError((*FskMediaReaderGetPropertyProc)(reader, kFskMediaPropertyTimeScale, &property)); timeScale = property.value.integer; (*FskMediaReaderGetPropertyProc)(reader, kFskMediaPropertyDuration, &property); duration = property.value.number; (*FskMediaReaderGetTrackProc)(reader, 0, &track); (*FskMediaReaderTrackGetPropertyProc)(track, kFskMediaPropertyFormat, &property); audioFormat = property.value.str; if (kFskErrNone == (*FskMediaReaderTrackGetPropertyProc)(track, kFskMediaPropertyFormatInfo, &property)) { formatInfo = property.value.data.data; formatInfoSize = property.value.data.dataSize; } else formatInfoSize = 0; (*FskMediaReaderTrackGetPropertyProc)(track, kFskMediaPropertyChannelCount, &property); numChannels = property.value.integer; (*FskMediaReaderTrackGetPropertyProc)(track, kFskMediaPropertySampleRate, &property); sampleRate = property.value.integer; bailIfError((*FskMediaReaderStartProc)(reader, NULL, NULL)); for (;;) { UInt32 c = 0, i, s; unsigned char *d; err = (*FskMediaReaderExtractProc)(reader, &track, &c, &info, &buffer); if (err == kFskErrEndOfFile) { err = kFskErrNone; break; } if (err != kFskErrNone) goto bail; for (i = 0, d = buffer; i < c; i++, d += s) { s = info[i].sampleSize * info[i].samples; bailIfError(FskMemPtrRealloc((frameCount + 1) * sizeof(KprSoundFrameRecord), &frames)); frames[frameCount].count = info[i].samples; frames[frameCount].frameSize = info[i].sampleSize; frames[frameCount].samplesPerFrame = info[i].sampleDuration; frames[frameCount].frameOffset = dataSize; frameCount++; bailIfError(FskMemPtrRealloc(dataSize + s, &data)); FskMemCopy(data + dataSize, d, s); dataSize += s; } FskMemPtrDispose(buffer); buffer = NULL; FskMemPtrDispose(info); info = NULL; } (*FskMediaReaderStopProc)(reader); self->audioFormat = audioFormat; self->duration = duration; self->formatInfo = formatInfo; self->formatInfoSize = formatInfoSize; self->numChannels = numChannels; self->sampleRate = sampleRate; self->timeScale = timeScale; self->data = data; self->dataSize = dataSize; self->frames = frames; self->frameCount = frameCount; bail: if (err) { FskMemPtrDispose(data); FskMemPtrDispose(frames); FskMemPtrDispose(formatInfo); FskMemPtrDispose(audioFormat); } FskMemPtrDispose(buffer); FskMemPtrDispose(info); (*FskMediaReaderDisposeProc)(reader); FskFileClose(fref); FskMemPtrDispose(path); return err; }
void fxIncludeScript(txParser* parser, txString string) { txBoolean done = 0; char* base = NULL; char name[PATH_MAX]; char* slash = NULL; char* dot = NULL; char* extension = NULL; char* url = NULL; char* path = NULL; FskFileInfo fileInfo; FskFileMapping map= NULL; txFileMapStream fileMapStream; txStringStream stringStream; txMachine* the = parser->console; fxBeginHost(the); { mxTry(the) { xsThrowIfFskErr(KprPathToURL(parser->path->string, &base)); FskStrCopy(name, string); slash = FskStrRChr(name, '/'); if (!slash) slash = name; dot = FskStrRChr(slash, '.'); if (dot) extension = dot; else extension = name + FskStrLen(name); if (!dot) FskStrCopy(extension, ".js"); if (!FskStrCompare(extension, ".js")) { xsThrowIfFskErr(KprURLMerge(base, name, &url)); xsThrowIfFskErr(KprURLToPath(url, &path)); if (kFskErrNone == FskFileGetFileInfo(path, &fileInfo)) { xsThrowIfFskErr(FskFileMap(path, (unsigned char**)&(fileMapStream.buffer), &(fileMapStream.size), 0, &map)); fileMapStream.offset = 0; fxIncludeTree(parser, &fileMapStream, fxFileMapGetter, parser->flags, path); done = 1; FskFileDisposeMap(map); map = NULL; } FskMemPtrDisposeAt(&path); FskMemPtrDisposeAt(&url); } if (!dot) FskStrCopy(extension, ".xml"); if (!FskStrCompare(extension, ".xml")) { xsThrowIfFskErr(KprURLMerge(base, name, &url)); xsThrowIfFskErr(KprURLToPath(url, &path)); if (kFskErrNone == FskFileGetFileInfo(path, &fileInfo)) { xsThrowIfFskErr(FskFileMap(path, &fileMapStream.buffer, &fileMapStream.size, 0, &map)); fileMapStream.offset = 0; mxPushInteger(0); mxPushInteger(0); mxPushInteger(0); mxPushInteger(0); mxPushInteger(3); fxParse(the, &fileMapStream, fxFileMapGetter, path, 1, xsSourceFlag | xsDebugFlag); fxCallID(the, fxID(the, "generate")); fxToString(the, the->stack); stringStream.slot = the->stack; stringStream.size = c_strlen(stringStream.slot->value.string); stringStream.offset = 0; fxIncludeTree(parser, &stringStream, fxStringGetter, parser->flags, path); done = 1; FskFileDisposeMap(map); map = NULL; } FskMemPtrDisposeAt(&path); FskMemPtrDisposeAt(&url); } FskMemPtrDispose(base); } mxCatch(the) { FskFileDisposeMap(map); FskMemPtrDispose(path); FskMemPtrDispose(url); FskMemPtrDispose(base); break; } } fxEndHost(the); if (!done) fxReportParserError(parser, "include file not found: %s", string); }