FskErr FskTextFreeTypeInstallFonts(char* fontsPath, char* defaultFont) { FskErr err = kFskErrNone; FskTextEngine fte; char *fromDirectory = NULL; char *fromPath = NULL; FskDirectoryIterator iterator = NULL; char *name = NULL; UInt32 type; err = FskTextEngineNew(&fte, NULL); if (err) goto bail; fromDirectory = FskEnvironmentDoApply(FskStrDoCopy(fontsPath)); if (!fromDirectory) { err = kFskErrMemFull; goto bail; } err = FskDirectoryIteratorNew(fromDirectory, &iterator, 0); if (err) goto bail; while (kFskErrNone == FskDirectoryIteratorGetNext(iterator, &name, &type)) { if (type == kFskDirectoryItemIsFile) { fromPath = FskStrDoCat(fromDirectory, name); if (!fromPath) { err = kFskErrMemFull; goto bail; } FskTextAddFontFile(fte, fromPath); FskMemPtrDisposeAt(&fromPath); } FskMemPtrDisposeAt(&name); } bail: FskTextEngineDispose(fte); FskMemPtrDispose(fromDirectory); FskDirectoryIteratorDispose(iterator); return err; }
FskErr mp3ReaderTrackGetProfile(void *readerState, void *track, UInt32 propertyID, FskMediaPropertyValue property) { mp3Reader state = readerState; int profile = state->mi.profile; int level = state->mi.level; const char *profile_str; char level_str[16]; if (kFskAudioFormatAACADTS == state->mi.codec ) { if( profile == 1 ) profile_str = "AAC Main,"; else if( profile == 2 ) profile_str = "AAC LC,"; else if( profile == 3 ) profile_str = "AAC SSR,"; else if( profile == 4 ) profile_str = "AAC LTP,"; else if( profile == 5 ) profile_str = "SBR,"; else if( profile == 6 ) profile_str = "AAC Scalable,"; else if( profile == 7 ) profile_str = "TwinVQ,"; else if( profile == 8 ) profile_str = "CELP,"; else if( profile == 9 ) profile_str = "HXVC,"; else profile_str = "unknown,"; } else profile_str = "unknown,"; FskStrNumToStr( level, level_str, sizeof(level_str)); property->type = kFskMediaPropertyTypeString; property->value.str = FskStrDoCat( profile_str, level_str ); return kFskErrNone; }
void unpackAndroid() { char * src, *dst, buf[4096]; int doit=0; UInt32 amt, amtWrt; FskFileInfo info, infoSrc; FskFile srcFref, dstFref; FskErr err; dst = FskStrDoCat(gAndroidCallbacks->getStaticDataDirCB(), "ca-bundle.crt"); src = FskStrDoCat(gAndroidCallbacks->getStaticAppDirCB(), "/res/raw/kinoma.jet/ca-bundle.crt"); if (kFskErrFileNotFound == FskFileGetFileInfo(dst, &info)) { fprintf(stderr, "dst: %s not found\n", dst); doit = 1; } else if (kFskErrNone == FskFileGetFileInfo(src, &infoSrc)) { if (infoSrc.filesize != info.filesize) { fprintf(stderr, "src size: %lld, dstSize: %lld\n", infoSrc.filesize, info.filesize); doit = 1; } } if (doit) { fprintf(stderr, "Need to copy it over.\n"); err = FskFileOpen(src, kFskFilePermissionReadOnly, &srcFref); if (err) fprintf(stderr, "open %s failed %d\n", src, err); err = FskFileCreate(dst); if (err) fprintf(stderr, "create %s failed %d\n", dst, err); err = FskFileOpen(dst, kFskFilePermissionReadWrite, &dstFref); if (err) fprintf(stderr, "open %s failed %d\n", dst, err); while (kFskErrNone == err) { err = FskFileRead(srcFref, 4096, buf, &amt); if (err) fprintf(stderr, "read %s (%lu bytes) failed %d\n", src, amt, err); if (0 >= amt) break; while (amt) { err = FskFileWrite(dstFref, amt, buf, &amtWrt); if (err) fprintf(stderr, "write %s (%lu bytes) failed %d\n", dst, amt, err); amt -= amtWrt; } } err = FskFileClose(dstFref); if (err) fprintf(stderr, "close %s failed %d\n", src, err); err = FskFileClose(srcFref); if (err) fprintf(stderr, "close %s failed %d\n", src, err); } FskMemPtrDispose(src); FskMemPtrDispose(dst); }
void fxOpenProfileFile(txMachine* the, char* theName) { FskFile fref; char *applicationPath = NULL; char *profilePath = NULL; if (the->profileDirectory) profilePath = FskStrDoCat(the->profileDirectory, theName); else { applicationPath = FskEnvironmentDoApply(FskStrDoCopy("[applicationPath]")); profilePath = FskStrDoCat(applicationPath, theName); } FskFileDelete(profilePath); FskFileCreate(profilePath); if (kFskErrNone == FskFileOpen(profilePath, kFskFilePermissionReadWrite, &fref)) the->profileFile = fref; FskMemPtrDispose(profilePath); FskMemPtrDispose(applicationPath); }
void fxReadAddressAutomatic(txMachine* the) { /* read debugAddress and debugAutomatic from a file... */ #if TARGET_OS_ANDROID char *folder = NULL; char *file = FskStrDoCat(folder, "/sdcard/debug.txt"); { #else char *folder; if (kFskErrNone == FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &folder)) { char *file = FskStrDoCat(folder, "debug.txt"); #endif char *data; if (kFskErrNone == FskFileLoad(file, (FskMemPtr *)&data, NULL)) { char *lf = FskStrChr(data, 10); if ((NULL != lf) && (FskStrLen(data) < sizeof(debugAddress))) { *lf = 0; FskStrCopy(debugAddress, data); debugAutomatic = '1' == lf[1]; } FskMemPtrDispose(data); } else { FskECMAScriptGetDebug(&data, NULL, NULL); if (data) { FskStrCopy(debugAddress, data); FskStrCat(debugAddress, ":5002"); debugAutomatic = 1; } } FskMemPtrDispose(file); FskMemPtrDispose(folder); } } void fxSetAddress(txMachine* the, char* theAddress) { c_strcpy(debugAddress, theAddress); fxWriteAddressAutomatic(the); }
void addVolume(char *path, int type, int removable) { pMount mnt; char *tmp; FskErr err; if ('/' == path[FskStrLen(path)-1]) tmp = FskStrDoCopy(path); else tmp = FskStrDoCat(path, "/"); if (NULL != volumesContain(tmp)) { FskMemPtrDispose(tmp); return; } err = FskMemPtrNewClear(sizeof(sMountRec), &mnt); BAIL_IF_ERR(err); mnt->mountPoint = tmp; if (sNumMounts == 0) { mnt->name = FskStrDoCopy(kInternalDataStorageName); } else { gNumCards++; if (gNumCards > 1) { char *name = FskStrDoCat("Card (", path); mnt->name = FskStrDoCat(name, ")"); FskMemPtrDispose(name); } else { mnt->name = FskStrDoCopy("Card"); } } mnt->type = type; mnt->removable = removable; mnt->fsID = ++sNumMounts; FskListAppend(&gMounts, mnt); bail: ; }
FskErr FskSSLInitialize(char *CA_List) { char *calistpath; FskErr err = kFskErrNone; #if TARGET_OS_ANDROID unpackAndroid(); calistpath = FskStrDoCat(gAndroidCallbacks->getStaticDataDirCB(), CA_List); #else calistpath = FskStrDoCat(FskEnvironmentGet("applicationPath"), CA_List); #endif #if OPEN_SSL (void)FskOpenSSLInitialize(calistpath); #endif #if CLOSED_SSL err = makeSSLRootVM(calistpath); #endif FskMemPtrDispose(calistpath); return err; }
void androidVolumeEvent(int what, char *path) { UInt32 vid; FskFSVolumeNotifier walker = NULL; char *tmp = NULL; FskAndroidFilesPrintfDebug("androidVolumeEvent - %d, %s\n", what, path); if (NULL == gVolNotifiersMutex) return; FskMutexAcquire(gVolNotifiersMutex); if (0 == FskStrCompareWithLength(path, "file://", 7)) path += 7; if (path[FskStrLen(path)-1] != '/') tmp = FskStrDoCat(path, "/"); else tmp = FskStrDoCopy(path); FskFSVolumeGetID(tmp, &vid); FskAndroidFilesPrintfDebug("androidVolumeEvent - getvolumeid returned %d\n", vid); if (vid == kFskUInt32Max) { FskAndroidFilesPrintfDebug("couldn't find a mount for %s - try to scan again###\n", tmp); scanProcMount(); FskFSVolumeGetID(tmp, &vid); if (vid == 0) { FskAndroidFilesPrintfDebug("#### still no volume id? %s\n", tmp); } } while (gVolNotifiers) { FskFSVolumeNotifier notifier; walker = (FskFSVolumeNotifier)FskListGetNext(gVolNotifiers, walker); if (NULL == walker) break; FskAndroidFilesPrintfDebug("androidVolumeEvent - notifying %x\n", walker); notifier = (FskFSVolumeNotifier)(((char *)walker) - offsetof(FskFSVolumeNotifierRecord, next)); FskThreadPostCallback(notifier->callbackThread, callVolumeNotifier, notifier, (void*)what, (void*)vid, NULL); } FskMutexRelease(gVolNotifiersMutex); FskMemPtrDispose(tmp); }
void KPR_system_set_backGesture(xsMachine *the) { FskErr err = kFskErrNone; char* path = NULL; char* pathName = NULL; FskFile file = NULL; char* data = xsToString(xsArg(0)); xsIntegerValue size = FskStrLen(data); #if MINITV pathName = TOUCHD_CONF; // system("/etc/init.d/touchd.sh stop"); #else bailIfError(FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &path)); pathName = FskStrDoCat(path, "touchd.conf"); bailIfNULL(pathName); #endif err = FskFileOpen(pathName, kFskFilePermissionReadWrite, &file); if (kFskErrFileNotFound == err) { bailIfError(FskFileCreate(pathName)); err = FskFileOpen(pathName, kFskFilePermissionReadWrite, &file); } else { FskInt64 zero = 0; err = FskFileSetSize(file, &zero); } bailIfError(err); bailIfError(FskFileWrite(file, size, data, NULL)); bail: FskFileClose(file); #if MINITV int pipe = open(TOUCH_PIPE, O_WRONLY | O_NONBLOCK); if (pipe >= 0) { char data = 1; write(pipe, &data, 1); close(pipe); } // system("/etc/init.d/touchd.sh start"); #endif if (path) { FskMemPtrDispose(path); FskMemPtrDispose(pathName); } xsThrowIfFskErr(err); }
void KPR_system_get_backGesture(xsMachine *the) { FskErr err = kFskErrNone; char* path = NULL; char* pathName = NULL; FskFileMapping map = NULL; unsigned char *data; FskInt64 size; #if MINITV pathName = TOUCHD_CONF; #else bailIfError(FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &path)); pathName = FskStrDoCat(path, "touchd.conf"); bailIfNULL(pathName); #endif bailIfError(FskFileMap(pathName, &data, &size, 0, &map)); xsResult = xsStringBuffer((xsStringValue)data, (xsIntegerValue)size); bail: FskFileDisposeMap(map); if (path) { FskMemPtrDispose(path); FskMemPtrDispose(pathName); } }
////////////// Main and Init ///////////////// static void FskGtkWindowInit(void *refcon) { FskWindow fskWindow = (FskWindow)refcon; FskGtkWindow gtkWin = (FskGtkWindow)fskWindow->gtkWin; GtkWidget *win; GdkPixbuf *iconBuf; // Ubuntu Unity prevent signals to reach menu inside menubars. Disable it! setenv("UBUNTU_MENUPROXY", "0", true); // Start GTK special initialization XInitThreads(); //fix a xcb issue happened when using multithread gtk, see http://stackoverflow.com/a/18690540/472927 gtk_init(NULL, NULL); gtkWin->owner = fskWindow; gtkWin->window = win = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size((GtkWindow*)win, 800, 600); // Set App Icon char *iconPath = FskStrDoCat(FskGetApplicationPath(), "fsk.png"); iconBuf = gdk_pixbuf_new_from_file(iconPath, NULL); if(iconBuf != NULL) { gtk_window_set_icon(GTK_WINDOW(win), iconBuf); g_object_unref(G_OBJECT(iconBuf)); } FskMemPtrDispose(iconPath); gtkWin->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); gtk_container_add(GTK_CONTAINER(win), gtkWin->vbox); gtk_widget_show(gtkWin->vbox); gtkWin->menubar = gtk_menu_bar_new(); gtk_widget_show(gtkWin->menubar); gtkWin->da = gtk_drawing_area_new(); gtk_widget_show(gtkWin->da); gtkWin->accelGroup = gtk_accel_group_new(); gtk_window_add_accel_group(GTK_WINDOW(win), gtkWin->accelGroup); FskMutexNew(>kWin->bufMutex, "drawAreaBuffer"); FskMutexNew(>kWin->menuMutex, "GTKMenuItems"); gtkWin->menuStatus = true; gtkWin->queue = g_async_queue_new(); gtk_widget_add_events(GTK_WIDGET(win), GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK); gtk_widget_add_events(GTK_WIDGET(gtkWin->da), GDK_ALL_EVENTS_MASK); g_signal_connect(win, "destroy", G_CALLBACK(app_quit), gtkWin); g_signal_connect(win, "key-press-event", G_CALLBACK(on_key_press), gtkWin); g_signal_connect(win, "key-release-event", G_CALLBACK(on_key_release), gtkWin); g_signal_connect(win, "configure-event", G_CALLBACK(configure_event_win), gtkWin); g_signal_connect(win, "window-state-event", G_CALLBACK(window_state_cb), fskWindow); // If want to resize draw-area, it will be the event receiver. g_signal_connect(gtkWin->da, "configure-event", G_CALLBACK(on_configure_event_cb), gtkWin); g_signal_connect(gtkWin->da, "draw", G_CALLBACK(draw_callback), gtkWin); g_signal_connect(gtkWin->da, "button-press-event", G_CALLBACK(on_button_press), gtkWin); g_signal_connect(gtkWin->da, "button-release-event",G_CALLBACK(on_button_release), gtkWin); g_signal_connect(gtkWin->da, "motion-notify-event", G_CALLBACK(on_motion_notify), gtkWin); g_signal_connect(gtkWin->da, "scroll-event", G_CALLBACK(on_scroll_cb), gtkWin); gtk_box_pack_start(GTK_BOX(gtkWin->vbox), gtkWin->menubar, FALSE, TRUE, 0); gtk_box_pack_start(GTK_BOX(gtkWin->vbox), gtkWin->da, TRUE, TRUE, 0); // Set all to true! gtk_widget_show_all(win); }
FskErr FskTextFreeTypeInstallFonts(char* fontsPath, char* defaultFont) { FskErr err = kFskErrNone; FskTextEngine fte; char *fromDirectory = NULL; char* fromPath = NULL; FskDirectoryIterator iterator = NULL; char *name = NULL; UInt32 type; #if TARGET_OS_ANDROID char *toDirectory = NULL; char* toPath = NULL; FskFileInfo itemInfo; FskFileMapping map = NULL; unsigned char *data; FskInt64 dataSize; FskFile file = NULL; #endif err = FskTextEngineNew(&fte, NULL); if (err) goto bail; fromDirectory = FskEnvironmentDoApply(FskStrDoCopy(fontsPath)); if (!fromDirectory) { err = kFskErrMemFull; goto bail; } #if TARGET_OS_ANDROID if (!FskStrCompareWithLength(fromDirectory, "/data/app/", 10)) { err = FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, false, NULL, &toDirectory); if (err) goto bail; } #endif err = FskDirectoryIteratorNew(fromDirectory, &iterator, 0); if (err) goto bail; while (kFskErrNone == FskDirectoryIteratorGetNext(iterator, &name, &type)) { if (type == kFskDirectoryItemIsFile) { fromPath = FskStrDoCat(fromDirectory, name); if (!fromPath) { err = kFskErrMemFull; goto bail; } FskDebugStr("from %s", fromPath); #if TARGET_OS_ANDROID if (toDirectory) { toPath = FskStrDoCat(toDirectory, name); FskDebugStr("to %s", toPath); if (kFskErrNone != FskFileGetFileInfo(toPath, &itemInfo)) { err = FskFileMap(fromPath, &data, &dataSize, 0, &map); if (err) goto bail; err = FskFileCreate(toPath); if (err) goto bail; err = FskFileOpen(toPath, kFskFilePermissionReadWrite, &file); if (err) goto bail; err = FskFileWrite(file, dataSize, data, NULL); if (err) goto bail; FskFileClose(file); file = NULL; FskFileDisposeMap(map); map = NULL; } FskDebugStr("add %s", toPath); FskTextAddFontFile(fte, toPath); FskMemPtrDisposeAt(&toPath); } else #endif FskTextAddFontFile(fte, fromPath); FskMemPtrDisposeAt(&fromPath); } FskMemPtrDisposeAt(&name); } #if TARGET_OS_ANDROID if (gAndroidCallbacks->getModelInfoCB) { char* osVersion; gAndroidCallbacks->getModelInfoCB(NULL, &osVersion, NULL, NULL, NULL); if ((FskStrStr(osVersion, "android.5") == osVersion) // for KPR applications || (FskStrStr(osVersion, "5.") == osVersion)) { // for tests BAIL_IF_ERR(err = FskFTAddMapping("/system/etc/fonts.xml")); } if ((FskStrStr(osVersion, "android.4") == osVersion) // for KPR applications || (FskStrStr(osVersion, "4.") == osVersion)) { // for tests BAIL_IF_ERR(err = FskFTAddMapping("/system/etc/system_fonts.xml")); err = FskFTAddMapping("/vendor/etc/fallback_fonts.xml"); if (err != kFskErrFileNotFound) BAIL(err); BAIL_IF_ERR(err = FskFTAddMapping("/system/etc/fallback_fonts.xml")); } else { defaultFont = "Droid Sans"; BAIL_IF_ERR(err = FskFTAddMapping(NULL)); } } #endif if (defaultFont) FskTextDefaultFontSet(fte, defaultFont); bail: #if TARGET_OS_ANDROID if (file) FskFileClose(file); if (map) FskFileDisposeMap(map); FskMemPtrDispose(toPath); FskMemPtrDispose(toDirectory); #endif FskMemPtrDispose(name); FskDirectoryIteratorDispose(iterator); FskMemPtrDispose(fromPath); FskMemPtrDispose(fromDirectory); FskTextEngineDispose(fte); return err; }
FskErr FskECMAScriptLoadLibrary(const char *name) { FskErr err = kFskErrBadData; FskLibraryLoadProc libLoad; char *extension = NULL, *symbolName = NULL, *dot, *slash, *fullPath = NULL, *temp = NULL; FskLibrary library = NULL; char* root = NULL; FskFileInfo itemInfo; FskDebugStr("FskECMAScriptLoadLibrary: %s", name); dot = FskStrRChr(name, '.'); slash = FskStrRChr(name, '/'); if ((NULL == dot) || (slash && (slash > dot))) { #if TARGET_OS_WIN32 extension = ".dll"; #elif TARGET_OS_MAC || TARGET_OS_LINUX extension = ".so"; #elif TARGET_OS_KPL extension = (char*)KplECMAScriptGetExtension(); #endif } if (extension) fullPath = FskStrDoCat(name, extension); else fullPath = FskStrDoCopy(name); if (kFskErrNone != FskFileGetFileInfo(fullPath, &itemInfo)) { #if TARGET_OS_ANDROID char *libDir; libDir = FskStrDoCat(gAndroidCallbacks->getStaticDataDirCB(), "../lib/lib"); temp = FskStrDoCat(libDir, fullPath); FskMemPtrDispose(libDir); #else root = FskGetApplicationPath(); temp = FskStrDoCat(root, fullPath); FskMemPtrDispose(root); #endif if (kFskErrNone != FskFileGetFileInfo(temp, &itemInfo)) { FskDebugStr(" - no file: %s", temp); goto bail; } FskMemPtrDispose(fullPath); fullPath = temp; temp = NULL; } err = FskLibraryLoad(&library, fullPath); FskDebugStr(" - try: %s -> %d", fullPath, err); if (err) goto bail; symbolName = FskStrDoCat(name, "_fskLoad"); if ((kFskErrNone == FskLibraryGetSymbolAddress(library, symbolName, &libLoad)) || (kFskErrNone == FskLibraryGetSymbolAddress(library, "fskLoad", &libLoad))) { err = (libLoad)(library); FskDebugStr(" - symbolName: %x -> %d", libLoad, err); if (err) goto bail; err = kFskErrNone; } bail: if (err) { FskLibraryUnload(library); } FskMemPtrDispose(symbolName); FskMemPtrDispose(fullPath); return err; }
static FskErr makeSSLRootVM(const char *calistpath) { #ifdef KPR_CONFIG FskErr err = kFskErrNone; xsBeginHost(gShell->root); xsTry { xsCall1_noResult(xsGet(xsGlobal, xsID("FskSSL")), xsID("loadRootCerts"), xsString((xsStringValue)calistpath)); } xsCatch { err = kFskErrOperationFailed; } xsEndHost(gShell->root); return err; #else char *rootPath, *xsbPath, *xsbName; xsGrammar *grammar; FskErr err; if ((err = FskMemPtrNewClear(sizeof(FskECMAScriptRecord), &gSSLVM)) != kFskErrNone) return err; #if FSK_EMBED FskExtensionsEmbedLoad(SSL_VMNAME); FskExtensionsEmbedGrammar(SSL_VMNAME, &xsbName, &grammar); #else xsbName = "FskCore.xsb"; grammar = &FskCoreGrammar; #endif rootPath = FskEnvironmentDoApply(FskStrDoCopy("[applicationPath]")); xsbPath = FskStrDoCat(rootPath, xsbName); FskMemPtrDispose(rootPath); grammar->name = (xsStringValue)SSL_VMNAME; if ((err = loadGrammar(xsbPath, grammar)) == kFskErrNone) { if ((gSSLVM->the = xsNewMachine(&anAllocation, grammar, gSSLVM)) == NULL) err = kFskErrMemFull; FskMemPtrDispose(grammar->symbols); FskMemPtrDispose(grammar->code); } FskMemPtrDispose(xsbPath); BAIL_IF_ERR(err); xsBeginHost(gSSLVM->the); xsTry { #if !FSK_EMBED /* load Crypt and FskSSL extensions which are only needed for Fsk SSL */ xsCall3_noResult(xsGet(xsGlobal, xsID("System")), xsID("loadExtension"), xsString("Crypt"), xsString("[applicationPath]"), xsInteger(0)); xsCall3_noResult(xsGet(xsGlobal, xsID("System")), xsID("loadExtension"), xsString("FskSSL"), xsString("[applicationPath]"), xsInteger(1)); #endif xsCall1_noResult(xsGet(xsGlobal, xsID("FskSSL")), xsID("loadRootCerts"), xsString((xsStringValue)calistpath)); } xsCatch { err = kFskErrOperationFailed; } xsEndHost(gSSLVM->the); if (err == kFskErrNone) xsShareMachine(gSSLVM->the); bail: if (err != kFskErrNone) disposeSSLRootVM(); return err; #endif }
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) {
void fxWriteAddressAutomatic(txMachine* the) { /* write debugAddress and debugAutomatic to a file... */ #if TARGET_OS_ANDROID char *folder = NULL; char *file = FskStrDoCat(folder, "/sdcard/debug.txt"); { #else char *folder; if (kFskErrNone == FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &folder)) { char *file = FskStrDoCat(folder, "debug.txt"); #endif FskFile fref; FskFileDelete(file); FskFileCreate(file); if (kFskErrNone == FskFileOpen(file, kFskFilePermissionReadWrite, &fref)) { char *data = FskStrDoCat(debugAddress, debugAutomatic ? "\n1\n" : "\n0\n"); if (data) { FskFileWrite(fref, FskStrLen(data), data, NULL); FskFileClose(fref); FskMemPtrDispose(data); } } } } #endif /* !KPR_CONFIG */ void fxReadBreakpoints(txMachine* the) { char *breakpoints; FskMemPtr file = NULL; FskInt64 fileSize; char *p; FskECMAScriptGetDebug(NULL, &breakpoints, NULL); if ((NULL == breakpoints) || (0 == *breakpoints)) goto bail; if (kFskErrNone != FskFileLoad(breakpoints, &file, &fileSize)) goto bail; p = (char *)file; while (true) { char *path = p; char *line = FskStrChr(p, '\t'); char *cr; if (NULL == line) break; cr = FskStrChr(line, '\n'); if (NULL == cr) break; *line++ = 0; *cr = 0; fxSetBreakpoint(the, path, line); p = cr + 1; } bail: FskMemPtrDispose(file); }
char *FskUUIDGetForKey(const char *key) { char *uuidStr; FskUUIDRecord uuid; char *uuidCachePath = NULL, *prefFolder; FskFile fref; if (kFskErrNone == FskDirectoryGetSpecialPath(kFskDirectorySpecialTypeApplicationPreference, true, NULL, &prefFolder)) { uuidCachePath = FskStrDoCat(prefFolder, "fskuuidcache.txt"); FskMemPtrDispose(prefFolder); } // load the cache if (NULL == gUUIDCache) { char *uuidCache; FskInt64 cacheSize; gUUIDCache = FskAssociativeArrayNew(); if ((NULL != uuidCachePath) && (kFskErrNone == FskFileLoad(uuidCachePath, (unsigned char **)(void *)&uuidCache, &cacheSize))) { char *p = uuidCache; while (true) { char *uuid = p; char *mykey = FskStrChr(p, '\t'); char *cr; if (NULL == mykey) break; cr = FskStrChr(mykey, '\n'); if (NULL == cr) break; *mykey++ = 0; *cr = 0; FskAssociativeArrayElementSetString(gUUIDCache, mykey, uuid); p = cr + 1; } FskMemPtrDispose(uuidCache); } } // check the cache uuidStr = FskAssociativeArrayElementGetString(gUUIDCache, key); if (uuidStr) { FskMemPtrDispose(uuidCachePath); return uuidStr; } // not in cache FskUUIDCreate(&uuid); uuidStr = FskUUIDtoString_844412(&uuid); FskAssociativeArrayElementSetString(gUUIDCache, key, uuidStr); FskMemPtrDispose(uuidStr); // flush cache FskFileDelete(uuidCachePath); FskFileCreate(uuidCachePath); if (kFskErrNone == FskFileOpen(uuidCachePath, kFskFilePermissionReadWrite, &fref)) { FskAssociativeArrayIterator iterate = FskAssociativeArrayIteratorNew(gUUIDCache); char tab = '\t', cr = '\n'; while (iterate) { FskFileWrite(fref, FskStrLen(iterate->value), iterate->value, NULL); FskFileWrite(fref, 1, &tab, NULL); FskFileWrite(fref, FskStrLen(iterate->name), iterate->name, NULL); FskFileWrite(fref, 1, &cr, NULL); iterate = FskAssociativeArrayIteratorNext(iterate); } FskFileClose(fref); } FskMemPtrDispose(uuidCachePath); return FskAssociativeArrayElementGetString(gUUIDCache, key); // caller doesn't have to dispose }
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int argc, i; UInt16 **argvUnicode; UInt8 **argvUTF8; int result; HANDLE startupCheck; UInt16 *moduleFileName, *mutexName; DWORD lastError; const UInt16 *kMutexBaseName = L"Fsk-startup-check-"; #if USE_TIMEBOMB if (!CheckDate()) return -1; #endif FskMainSetHInstance(hInstance); argvUnicode = CommandLineToArgvW(GetCommandLineW(), &argc); argvUTF8 = (UInt8 **)calloc(sizeof(UInt16 *), argc); for (i= 0; i< argc; i++) { int charCount = wcslen(argvUnicode[i]); argvUTF8[i] = malloc((charCount + 1) * 3); WideCharToMultiByte(CP_UTF8, 0, argvUnicode[i], charCount + 1, argvUTF8[i], (charCount + 1) * 3, NULL, NULL); } GlobalFree(argvUnicode); moduleFileName = (UInt16 *)malloc(sizeof(UInt16) * 520); GetModuleFileNameW(0, moduleFileName, 512); mutexName = malloc((1 + wcslen(moduleFileName) + wcslen(kMutexBaseName)) * 2); wcscpy(mutexName, kMutexBaseName); wcscat(mutexName, wcsrchr(moduleFileName, '\\') + 1); free(moduleFileName); startupCheck = CreateMutexW(0, true, mutexName); lastError = GetLastError(); free(mutexName); for (i = 0; i < argc; i++){ if(strcmp(argvUTF8[i],"-new-instance")==0) { lastError=0; break; } } switch (lastError) { case 0: result = doMain(kFskMainNetwork | kFskMainServer, argc, argvUTF8); break; case ERROR_ALREADY_EXISTS: { char *fileList; UInt32 fileListSize; result = 0; FskUtilsSetArgs(argc, argvUTF8); fileList = FskUtilsGetFileArgs(&fileListSize); if (NULL != fileList) { char *fileName; HANDLE hMapFile; char number[64]; SInt32 val; val = FskRandom() ^ GetTickCount(); FskStrNumToStr(val, number, sizeof(number)); fileName = FskStrDoCat("FskFileOpen-", number); hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, fileListSize, fileName); if ((NULL != hMapFile) && (INVALID_HANDLE_VALUE != hMapFile)) { unsigned char *pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, fileListSize); if (NULL != pBuf) { memmove(pBuf, fileList, fileListSize); UnmapViewOfFile(pBuf); PostMessage(FindWindow("projectf-utils", NULL), RegisterWindowMessage("FskOpenFiles"), 0, val); } CloseHandle(hMapFile); } FskMemPtrDispose(fileName); } } break; default: result = -1; break; } CloseHandle(startupCheck); for (i = 0; i < argc; i++) free(argvUTF8[i]); free(argvUTF8); return result; }
FskErr KplDirectoryGetSpecialPath(UInt32 type, const Boolean create, const char *volumeName, char **fullPath) { FskErr err = kFskErrFileNotFound; KplFileInfo finfo; char *tmp = NULL, *specialDirEnv = NULL; *fullPath = NULL; switch (type & ~kFskDirectorySpecialTypeSharedFlag) { case kFskDirectorySpecialTypeDocument: specialDirEnv = "DocumentsDir"; break; case kFskDirectorySpecialTypePhoto: specialDirEnv = "PhotosDir"; break; case kFskDirectorySpecialTypeMusic: specialDirEnv = "MusicDir"; break; case kFskDirectorySpecialTypeVideo: specialDirEnv = "VideoDir"; break; case kFskDirectorySpecialTypeTV: specialDirEnv = "TVDir"; break; #ifdef ANDROID_PLATFORM case kFskDirectorySpecialTypeApplicationPreference: tmp = FskEnvironmentGet("PreferencesDir"); if (!tmp) *fullPath = FskStrDoCopy("/data/local/tmp/.kinoma/"); else *fullPath = FskStrDoCopy(tmp); err = kFskErrNone; goto bail; case kFskDirectorySpecialTypeTemporary: *fullPath = FskStrDoCopy("/data/local/tmp/"); err = kFskErrNone; goto bail; case kFskDirectorySpecialTypeCache: *fullPath = FskStrDoCopy("/data/local/tmp/"); err = kFskErrNone; goto bail; #else case kFskDirectorySpecialTypeApplicationPreference: tmp = FskEnvironmentGet("PreferencesDir"); if (!tmp) #if BG3CDP { *fullPath = FskStrDoCopy("/data/.kinoma/"); } #else { if (0 == FskStrCompare("/", getenv("HOME"))) *fullPath = FskStrDoCopy("/.kinoma/"); else *fullPath = FskStrDoCat(getenv("HOME"), "/.kinoma/"); } #endif else *fullPath = FskStrDoCopy(tmp); err = kFskErrNone; goto bail; case kFskDirectorySpecialTypeTemporary: *fullPath = FskStrDoCopy("/tmp/"); err = kFskErrNone; goto bail; case kFskDirectorySpecialTypeCache: *fullPath = FskStrDoCopy("/tmp/"); err = kFskErrNone; goto bail; #endif default: err = kFskErrInvalidParameter; goto bail; }
FskErr FskFSDirectoryGetSpecialPath(UInt32 type, const Boolean create, const char *volumeName, char **fullPath) { FskErr err = kFskErrFileNotFound; FskFileInfo finfo; char *tmp = NULL, *specialDir = NULL; char *dataDir = gAndroidCallbacks->getStaticDataDirCB(); char *externalDir = gAndroidCallbacks->getStaticExternalDirCB(); Boolean doCreate = create; *fullPath = NULL; FskAndroidFilesPrintfDebug("for SpecialPath - type %d, use dataDir as %s (volumeName %s)\n", type, dataDir, volumeName); switch (type & ~kFskDirectorySpecialTypeSharedFlag) { case kFskDirectorySpecialTypeDownload: if (androidSystemDownloadsDir) tmp = FskStrDoCat(androidSystemDownloadsDir, "/"); else if (externalDir) tmp = FskStrDoCat(externalDir, "/Kinoma/Downloads/"); else BAIL(kFskErrNotDirectory); break; case kFskDirectorySpecialTypeDocument: doCreate = true; if (externalDir) tmp = FskStrDoCat(externalDir, "/"); else if (androidSystemDownloadsDir) tmp = FskStrDoCat(androidSystemDownloadsDir, "/"); else specialDir = "Download"; break; case kFskDirectorySpecialTypePhoto: if (androidSystemPicturesDir) tmp = FskStrDoCat(androidSystemPicturesDir, "/"); else specialDir = "Pictures"; break; case kFskDirectorySpecialTypeMusic: if (androidSystemMusicDir) tmp = FskStrDoCat(androidSystemMusicDir, "/"); else specialDir = "Music"; break; case kFskDirectorySpecialTypeVideo: if (androidSystemMoviesDir) tmp = FskStrDoCat(androidSystemMoviesDir, "/"); else specialDir = "Movies"; break; case kFskDirectorySpecialTypeTV: specialDir = "TV"; break; case kFskDirectorySpecialTypeApplicationPreference: *fullPath = FskStrDoCat(dataDir, "kinoma/"); err = FskFSFileCreateDirectory(*fullPath); err = kFskErrNone; goto makeit; case kFskDirectorySpecialTypeTemporary: *fullPath = FskStrDoCat(dataDir, "tmp/"); err = FskFSFileCreateDirectory(*fullPath); err = kFskErrNone; goto makeit; case kFskDirectorySpecialTypeCache: *fullPath = FskStrDoCat(dataDir, "tmp/"); err = FskFSFileCreateDirectory(*fullPath); err = kFskErrNone; goto makeit; default: FskAndroidFilesPrintfDebug("SpecialDirectory - bad special directory\n"); BAIL(kFskErrInvalidParameter); } if (specialDir) tmp = FskStrDoCopy(FskEnvironmentGet(specialDir)); if (!tmp) { char *home; home = dataDir; err = FskMemPtrNewClear(FskStrLen(home) + FskStrLen(specialDir) + 3, (FskMemPtr*)(void*)&tmp); BAIL_IF_ERR(err); FskStrCopy(tmp, home); if (tmp[FskStrLen(tmp)-1] != '/') FskStrCat(tmp, "/"); if (specialDir) { FskStrCat(tmp, specialDir); FskStrCat(tmp, "/"); } } FskAndroidFilesPrintfDebug("looking for %s - got %s", specialDir, tmp); *fullPath = tmp; makeit: if (doCreate) { err = FskFSFileCreateDirectory(*fullPath); } else { err = FskFSFileGetFileInfo(*fullPath, &finfo); if (kFskErrNone == err) { if (kFskDirectoryItemIsDirectory != finfo.filetype) BAIL(kFskErrNotDirectory); } } bail: if (kFskErrFileExists == err) { err = kFskErrNone; FskAndroidFilesPrintfDebug("DIRECTORY EXISTS specialDirectory - looking for type %d - got [%s]", type, *fullPath); } else if (kFskErrNone != err) { FskAndroidFilesPrintfDebug("DIRECGOTRY DIDN'T EXIST - specialDirectory - looking for type %d - got [%s] err: %d", type, *fullPath, err); } return err; }