Ticket #9063: psp_suspend_cleaned.diff

File psp_suspend_cleaned.diff, 22.8 KB (added by joostp, 15 years ago)

Slightly cleaned up (mostly formatting) version of the patch against trunk

  • backends/platform/psp/psp_main.cpp

     
    3131#include <pspdebug.h>
    3232#endif
    3333
     34#include <psppower.h>
     35
    3436#include <common/system.h>
    3537#include <engines/engine.h>
    3638#include <base/main.h>
    3739#include <base/plugins.h>
     40#include "backends/platform/psp/powerman.h"
    3841
     42
    3943#include "osys_psp_gu.h"
    4044#include "./trace.h"
    4145
     
    9195#endif
    9296
    9397/* Exit callback */
    94 SceKernelCallbackFunction exit_callback(int /*arg1*/, int /*arg2*/, void * /*common*/) {
     98int exit_callback(void) {
    9599        sceKernelExitGame();
    96100        return 0;
    97101}
    98102
     103/* Function for handling suspend/resume */
     104void power_callback(int , int powerinfo)
     105{
     106        PowerManager *pm = &(PowerManager::instance());
     107
     108        if (powerinfo & PSP_POWER_CB_POWER_SWITCH || powerinfo & PSP_POWER_CB_SUSPENDING) {
     109                pm->suspend();
     110        } else if (powerinfo & PSP_POWER_CB_RESUME_COMPLETE) {
     111                pm->resume();
     112        }
     113}
     114
    99115/* Callback thread */
    100116int CallbackThread(SceSize /*size*/, void *arg) {
    101117        int cbid;
    102118
    103119        cbid = sceKernelCreateCallback("Exit Callback", (SceKernelCallbackFunction)exit_callback, NULL);
    104120        sceKernelRegisterExitCallback(cbid);
     121        /* Set up callbacks for PSPIoStream */
    105122
     123        cbid = sceKernelCreateCallback("Power Callback", (SceKernelCallbackFunction)power_callback, 0);
     124        if (cbid >= 0) {
     125                if(scePowerRegisterCallback(-1, cbid) < 0) {
     126                        PSPDebugTrace("SetupCallbacks(): Couldn't register callback for power_callback\n");
     127                }
     128        }
     129        else {
     130                PSPDebugTrace("SetupCallbacks(): Couldn't create a callback for power_callback\n");
     131        }
     132
    106133        sceKernelSleepThreadCB();
    107134        return 0;
    108135}
     
    119146
    120147#undef main
    121148int main(void) {
     149
     150        PowerManager::instance();       // Setup power manager
     151
    122152        SetupCallbacks();
    123153
    124154        static const char *argv[] = { "scummvm", NULL };
     
    131161
    132162        g_system->quit();       // TODO: Consider removing / replacing this!
    133163
     164        PowerManager::destroy();        // get rid of PowerManager
     165
    134166        sceKernelSleepThread();
    135167
    136168        return res;
  • backends/platform/psp/module.mk

     
    11MODULE := backends/platform/psp
    22
    33MODULE_OBJS := \
     4        powerman.o \
    45        psp_main.o \
    56        osys_psp.o \
    67        osys_psp_gu.o \
  • backends/platform/psp/psp.spec

     
    11%rename lib     old_lib
    22*lib:
    3 %(old_lib) -lpspdebug -lpspgu -lpspctrl -lpspge -lpspdisplay -lpsphprm -lpspsdk -lpsprtc -lpspaudio -lc -lpspuser -lpsputility -lpspkernel -lpspnet_inet -lz -lstdc++ -lc -lpspdisplay -lpspgu -lpspctrl -lpspsdk -lpspnet -lpspnet_inet -lpsputility -lpspsdk -lpspuser
     3%(old_lib) -lpspdebug -lpspgu -lpspctrl -lpspge -lpspdisplay -lpsphprm -lpspsdk -lpsprtc -lpspaudio -lc -lpspuser -lpsputility -lpspkernel -lpspnet_inet -lz -lstdc++ -lc -lpspdisplay -lpspgu -lpspctrl -lpspsdk -lpspnet -lpspnet_inet -lpsputility -lpspuser -lpsppower
  • backends/platform/psp/powerman.cpp

     
     1/* ScummVM - Graphic Adventure Engine
     2 *
     3 * ScummVM is the legal property of its developers, whose names
     4 * are too numerous to list here. Please refer to the COPYRIGHT
     5 * file distributed with this source distribution.
     6 *
     7 * This program is free software; you can redistribute it and/or
     8 * modify it under the terms of the GNU General Public License
     9 * as published by the Free Software Foundation; either version 2
     10 * of the License, or (at your option) any later version.
     11
     12 * This program is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16
     17 * You should have received a copy of the GNU General Public License
     18 * along with this program; if not, write to the Free Software
     19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
     20 *
     21 * $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/tags/release-0-13-1/backends/platform/psp/psp_main.cpp $
     22 * $Id: psp_main.cpp 34827 2008-10-19 21:06:26Z fingolfin $
     23 *
     24 */
     25
     26#include "powerman.h"
     27#include "trace.h"
     28
     29DECLARE_SINGLETON(PowerManager);
     30
     31 /*******************************************
     32*
     33*       Constructor
     34*
     35********************************************/
     36 PowerManager::PowerManager() {
     37        _flagMutex = NULL;              /* Init mutex handle */
     38        _listMutex = NULL;              /* Init mutex handle */
     39        _cond = NULL;                   /* Init condition variable */
     40       
     41        _cond = SDL_CreateCond();
     42        if (_cond <= 0) {
     43                PSPDebugTrace("PowerManager::PowerManager(): Couldn't create cond\n");
     44        }
     45
     46        _flagMutex = SDL_CreateMutex();
     47        if (_flagMutex <= 0) {
     48                PSPDebugTrace("PowerManager::PowerManager(): Couldn't create flagMutex\n");
     49        }
     50
     51        _listMutex = SDL_CreateMutex();
     52        if (_listMutex <= 0) {
     53                PSPDebugTrace("PowerManager::PowerManager(): Couldn't create listMutex\n");
     54        }
     55
     56        _suspendFlag = false;
     57 }
     58 
     59/*******************************************
     60*
     61*       Function to register to be notified when suspend/resume time comes
     62*
     63********************************************/
     64int PowerManager::registerSuspend(Suspendable *item) {
     65        // Register in list
     66        if (SDL_mutexP(_listMutex) != 0) {
     67                PSPDebugTrace("PowerManager::registerSuspend(): Couldn't lock _listMutex %d\n", _listMutex);
     68        }
     69
     70        _suspendList.push_front(item);
     71
     72        if (SDL_mutexV(_listMutex) != 0) {
     73                PSPDebugTrace("PowerManager::registerSuspend(): Couldn't unlock _listMutex %d\n", _listMutex);
     74        }
     75
     76        return 0;
     77}
     78
     79/*******************************************
     80*
     81*       Function to unregister to be notified when suspend/resume time comes
     82*
     83********************************************/ 
     84 int PowerManager::unregisterSuspend(Suspendable *item) {
     85        // Unregister from stream list
     86        if (SDL_mutexP(_listMutex) != 0) {
     87                PSPDebugTrace("PowerManager::unregisterSuspend(): Couldn't unlock _listMutex %d\n", _listMutex);
     88        }
     89
     90        _suspendList.remove(item);
     91
     92        if (SDL_mutexV(_listMutex) != 0) {
     93                PSPDebugTrace("PowerManager::unregisterSuspend(): Couldn't unlock _listMutex %d\n", _listMutex);
     94        }
     95
     96        return 0;
     97 }
     98 
     99 /*******************************************
     100*
     101*       Destructor
     102*
     103********************************************/
     104 PowerManager::~PowerManager() {
     105        SDL_DestroyCond(_cond);
     106        _cond = 0;
     107
     108        SDL_DestroyMutex(_flagMutex);
     109        _flagMutex = 0;
     110
     111        SDL_DestroyMutex(_listMutex);
     112        _listMutex = 0;
     113 }
     114 
     115 
     116 /*******************************************
     117*
     118*       Function to be called by threads wanting to block on the PSP entering suspend
     119*
     120********************************************/ 
     121 int PowerManager::blockOnSuspend() const {
     122        int ret = PowerManager::NotBlocked;
     123
     124        if (SDL_mutexP(_flagMutex) != 0) {
     125                PSPDebugTrace("PowerManager::blockOnSuspend(): Couldn't lock flagMutex %d\n", _flagMutex);
     126                ret = PowerManager::Error;
     127        }
     128
     129        // Check the access flag
     130        if (_suspendFlag) {
     131                ret = PowerManager::Blocked;
     132
     133                // If it's true, we wait for a signal to continue
     134                if (SDL_CondWait(_cond, _flagMutex) != 0) {
     135                        PSPDebugTrace("PowerManager::blockOnSuspend(): Couldn't wait on cond %d\n", _cond);
     136                }
     137        }
     138
     139        if (SDL_mutexV(_flagMutex) != 0) {
     140                PSPDebugTrace("PowerManager::blockOnSuspend(): Couldn't unlock flagMutex %d\n", _flagMutex);
     141                ret = PowerManager::Error;
     142        }
     143
     144        return ret;
     145}
     146
     147 /*******************************************
     148*
     149*       Callback function to be called to put every Suspendable to suspend
     150*
     151********************************************/ 
     152int PowerManager::suspend() {
     153        int ret = 0;
     154
     155        // First we set the suspend flag to true
     156        if (SDL_mutexP(_flagMutex) != 0) {
     157                PSPDebugTrace("PowerManager::suspend(): Couldn't lock flagMutex %d\n", _flagMutex);
     158                ret = -1;
     159        }
     160
     161        _suspendFlag = true;
     162
     163        if (SDL_mutexV(_flagMutex) != 0) {
     164                PSPDebugTrace("PowerManager::suspend(): Couldn't unlock flagMutex %d\n", _flagMutex);
     165                ret = -1;
     166        }
     167
     168        // Loop over list, calling suspend()
     169        if (SDL_mutexP(_listMutex) != 0) {
     170                PSPDebugTrace("PowerManager::suspend(): Couldn't lock listMutex %d\n", _listMutex);
     171                ret = -1;
     172        }
     173
     174        Common::List<Suspendable *>::iterator i = _suspendList.begin();
     175
     176        for(; i != _suspendList.end(); i++) {
     177                (*i)->suspend();
     178        }
     179
     180        if (SDL_mutexV(_listMutex) != 0) {
     181                PSPDebugTrace("PowerManager::suspend(): Couldn't unlock listMutex %d\n", _listMutex);
     182                ret = -1;
     183        }
     184
     185        return ret;
     186}
     187
     188 /*******************************************
     189*
     190*       Callback function to resume every Suspendable
     191*
     192********************************************/ 
     193int PowerManager::resume() {
     194        int ret = 0;
     195
     196        // First we notify our Suspendables. Loop over list, calling resume()
     197        if (SDL_mutexP(_listMutex) != 0) {
     198                PSPDebugTrace("PowerManager::resume(): Couldn't lock listMutex %d\n", _listMutex);
     199                ret = -1;
     200        }
     201
     202        Common::List<Suspendable *>::iterator i = _suspendList.begin();
     203
     204        for (; i != _suspendList.end(); i++) {
     205                (*i)->resume();
     206        }
     207
     208        if (SDL_mutexV(_listMutex) != 0) {
     209                PSPDebugTrace("PowerManager::resume(): Couldn't unlock listMutex %d\n", _listMutex);
     210                ret = -1;
     211        }
     212
     213        // Now we set the suspend flag to false
     214        if (SDL_mutexP(_flagMutex) != 0) {
     215                PSPDebugTrace("PowerManager::resume(): Couldn't lock flagMutex %d\n", _flagMutex);
     216                ret = -1;
     217        }
     218        _suspendFlag = false;
     219
     220        // Signal the other threads to wake up
     221        if( SDL_CondBroadcast(_cond) != 0) {
     222                PSPDebugTrace("PowerManager::resume(): Couldn't broadcast condition %d\n", _cond);
     223                ret = -1;
     224        }
     225
     226        if (SDL_mutexV(_flagMutex) != 0) {
     227                PSPDebugTrace("PowerManager::resume(): Couldn't unlock flagMutex %d\n", _flagMutex);
     228                ret = -1;
     229        }
     230
     231        return ret;
     232}
  • backends/platform/psp/Makefile

     
    7575CXXFLAGS+= -DUSE_VORBIS -DUSE_TREMOR
    7676LIBS    += -lvorbisidec
    7777
    78 LIBS    += `$(PSPBIN)/sdl-config --libs` -lz -lstdc++ -lc -lpspdisplay -lpspgu -lpspctrl -lpspsdk -lpspnet -lpspnet_inet -lpsputility -lpspsdk -lpspuser
     78LIBS    += `$(PSPBIN)/sdl-config --libs` -lz -lstdc++ -lc -lpspdisplay -lpspgu -lpspctrl -lpspsdk -lpspnet -lpspnet_inet -lpsputility -lpspuser -lpsppower
    7979
    8080CXXFLAGS := $(CXXFLAGS) -fno-exceptions -fno-rtti
    8181
    8282TARGET = scummvm-psp
    83 OBJS := psp_main.o \
     83OBJS := powerman.o \
     84        psp_main.o \
    8485        osys_psp.o \
    8586        osys_psp_gu.o \
    8687        kbd_ss_c.o \
  • backends/platform/psp/powerman.h

     
     1/* ScummVM - Graphic Adventure Engine
     2 *
     3 * ScummVM is the legal property of its developers, whose names
     4 * are too numerous to list here. Please refer to the COPYRIGHT
     5 * file distributed with this source distribution.
     6 *
     7 * This program is free software; you can redistribute it and/or
     8 * modify it under the terms of the GNU General Public License
     9 * as published by the Free Software Foundation; either version 2
     10 * of the License, or (at your option) any later version.
     11
     12 * This program is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16
     17 * You should have received a copy of the GNU General Public License
     18 * along with this program; if not, write to the Free Software
     19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
     20 *
     21 * $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/tags/release-0-13-1/backends/platform/psp/psp_main.cpp $
     22 * $Id: psp_main.cpp 34827 2008-10-19 21:06:26Z fingolfin $
     23 *
     24 */
     25
     26#ifndef POWERMAN_H
     27#define POWERMAN_H
     28 
     29#include <SDL/SDL_thread.h>
     30#include <SDL/SDL_mutex.h>
     31#include "common/singleton.h"
     32#include "common/list.h"
     33 
     34 /* Implement this class (interface) if you want to use PowerManager's suspend callback functionality */
     35 class Suspendable
     36 {
     37 public:
     38        Suspendable() {}
     39        virtual ~Suspendable() {}
     40        virtual int suspend() = 0;
     41        virtual int resume() = 0;
     42 };
     43 
     44 /******************************************************************************************************
     45 *
     46 *  This class will call a Suspendable when the PSP goes to suspend/resumes. It also provides the ability to block
     47 *  a thread when the PSP is going to suspend/suspending, and to wake it up when the PSP is resumed.
     48 *      This ability is very useful for managing the PSPIoStream class, but may be found useful by other classes as well.
     49 *
     50 *******************************************************************************************************/
     51 class PowerManager: public Common::Singleton<PowerManager>
     52 {
     53private:
     54        friend class Common::Singleton<PowerManager>;
     55        PowerManager();
     56        virtual ~PowerManager();
     57
     58        Common::List<Suspendable *> _suspendList;               /* list to register in */
     59
     60        bool _suspendFlag;                                                              /* protected variable */
     61        SDL_mutex *_flagMutex;                                                  /* mutex to access access flag */
     62        SDL_mutex *_listMutex;                                                  /* mutex to access Suspendable list */
     63        SDL_cond *_cond;                                                                /* signal to synchronize accessing threads */
     64
     65public:
     66        int blockOnSuspend() const;                                             /* block if suspending */
     67        int registerSuspend(Suspendable *item);                 /* register to be called to suspend/resume */
     68        int unregisterSuspend(Suspendable *item);               /* remove from suspend/resume list */
     69        int suspend();                                                                  /* callback to have all items in list suspend */
     70        int resume();                                                                   /* callback to have all items in list resume */
     71       
     72        enum
     73        {
     74                Error = -1,
     75                NotBlocked = 0,
     76                Blocked = 1             
     77        };
     78               
     79 };
     80 
     81 #endif /* POWERMAN_H */
  • backends/fs/psp/psp-stream.h

     
     1/* ScummVM - Graphic Adventure Engine
     2 *
     3 * ScummVM is the legal property of its developers, whose names
     4 * are too numerous to list here. Please refer to the COPYRIGHT
     5 * file distributed with this source distribution.
     6 *
     7 * This program is free software; you can redistribute it and/or
     8 * modify it under the terms of the GNU General Public License
     9 * as published by the Free Software Foundation; either version 2
     10 * of the License, or (at your option) any later version.
     11
     12 * This program is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16
     17 * You should have received a copy of the GNU General Public License
     18 * along with this program; if not, write to the Free Software
     19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
     20 *
     21 * $URL: https://scummvm.svn.sourceforge.net:443/svnroot/scummvm/scummvm/trunk/backends/fs/stdiostream.h $
     22 * $Id: stdiostream.h 34549 2008-09-14 22:28:53Z wjpalenstijn $
     23 *
     24 */
     25
     26#ifndef PSPSTREAM_H_
     27#define PSPSTREAM_H_
     28
     29#include "backends/fs/stdiostream.h"
     30#include "backends/platform/psp/powerman.h"
     31#include "common/list.h"
     32
     33
     34class PSPIoStream : public StdioStream, public Suspendable {
     35protected:
     36        /** File handle to the actual file. */
     37        Common::String _path;                   /* Need to maintain for reopening after suspend */
     38        bool _writeMode;                                /* "" */
     39        unsigned int _pos;                              /* "" */
     40        static PowerManager *pm;               
     41
     42
     43public:
     44        /**
     45         * Given a path, invoke fopen on that path and wrap the result in a
     46         * StdioStream instance.
     47         */
     48        static PSPIoStream *makeFromPath(const Common::String &path, bool writeMode);
     49
     50        PSPIoStream(void *handle, const Common::String &path, bool writeMode);
     51        virtual ~PSPIoStream();
     52
     53        bool err() const;
     54        void clearErr();
     55        bool eos() const;
     56
     57        virtual uint32 write(const void *dataPtr, uint32 dataSize);
     58        virtual bool flush();
     59
     60        virtual int32 pos() const;
     61        virtual int32 size() const;
     62        virtual bool seek(int32 offs, int whence = SEEK_SET);
     63        virtual uint32 read(void *dataPtr, uint32 dataSize);
     64
     65        int suspend();          /* Suspendable interface */
     66        int resume();           /* " " */
     67};
     68
     69#endif /* PSPSTREAM_H_ */
  • backends/fs/psp/psp-fs.cpp

     
    2626
    2727#include "engines/engine.h"
    2828#include "backends/fs/abstract-fs.h"
    29 #include "backends/fs/stdiostream.h"
     29#include "backends/fs/psp/psp-stream.h"
    3030
    3131#include <sys/stat.h>
    3232#include <unistd.h>
     
    3535
    3636#define ROOT_PATH       "ms0:/"
    3737
     38#include "backends/platform/psp/trace.h"
    3839/**
    3940 * Implementation of the ScummVM file system API based on PSPSDK API.
    4041 *
     
    166167}
    167168
    168169Common::SeekableReadStream *PSPFilesystemNode::createReadStream() {
    169         return StdioStream::makeFromPath(getPath().c_str(), false);
     170        return PSPIoStream::makeFromPath(getPath(), false);
    170171}
    171172
    172173Common::WriteStream *PSPFilesystemNode::createWriteStream() {
    173         return StdioStream::makeFromPath(getPath().c_str(), true);
     174        return PSPIoStream::makeFromPath(getPath(), true);
    174175}
    175176
    176177#endif //#ifdef __PSP__
  • backends/fs/psp/psp-stream.cpp

     
     1/* ScummVM - Graphic Adventure Engine
     2 *
     3 * ScummVM is the legal property of its developers, whose names
     4 * are too numerous to list here. Please refer to the COPYRIGHT
     5 * file distributed with this source distribution.
     6 *
     7 * This program is free software; you can redistribute it and/or
     8 * modify it under the terms of the GNU General Public License
     9 * as published by the Free Software Foundation; either version 2
     10 * of the License, or (at your option) any later version.
     11
     12 * This program is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16
     17 * You should have received a copy of the GNU General Public License
     18 * along with this program; if not, write to the Free Software
     19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
     20 *
     21 * $URL: https://scummvm.svn.sourceforge.net:443/svnroot/scummvm/scummvm/trunk/backends/fs/stdiostream.cpp $
     22 * $Id: stdiostream.cpp 39143 2009-03-06 00:26:48Z sunmax $
     23 *
     24 */
     25
     26#ifdef __PSP__
     27
     28#include "backends/fs/psp/psp-stream.h"
     29#include "backends/platform/psp/trace.h"
     30#include <errno.h>
     31
     32/* For static variable */
     33PowerManager *PSPIoStream::pm = 0;
     34
     35PSPIoStream::PSPIoStream(void *handle, const Common::String &path, bool writeMode)
     36: StdioStream(handle), _path(path), _writeMode(writeMode) {
     37        assert(!path.empty());
     38
     39        if(pm == 0) pm = &(PowerManager::instance());
     40       
     41        pm->registerSuspend(this);
     42}
     43
     44PSPIoStream::~PSPIoStream() {
     45        pm->unregisterSuspend(this);
     46
     47        fclose((FILE *)_handle);
     48}
     49
     50bool PSPIoStream::err() const {
     51        pm->blockOnSuspend();
     52        return ferror((FILE *)_handle) != 0;
     53}
     54
     55void PSPIoStream::clearErr() {
     56        pm->blockOnSuspend();
     57        clearerr((FILE *)_handle);
     58}
     59
     60bool PSPIoStream::eos() const {
     61        pm->blockOnSuspend();
     62        return feof((FILE *)_handle) != 0;
     63}
     64
     65int32 PSPIoStream::pos() const {
     66        pm->blockOnSuspend();
     67        return ftell((FILE *)_handle);
     68}
     69
     70int32 PSPIoStream::size() const {
     71        pm->blockOnSuspend();
     72
     73        int32 oldPos = ftell((FILE *)_handle);
     74        fseek((FILE *)_handle, 0, SEEK_END);
     75        int32 length = ftell((FILE *)_handle);
     76        fseek((FILE *)_handle, oldPos, SEEK_SET);
     77
     78        return length;
     79}
     80
     81bool PSPIoStream::seek(int32 offs, int whence) {
     82        int ret = 0;
     83
     84        // Check if we can access the file
     85        pm->blockOnSuspend();
     86
     87        // Act. If we fail, maybe it was because of a suspend
     88        if ((ret = fseek((FILE *)_handle, offs, whence)) != 0 && (pm->blockOnSuspend() == PowerManager::Blocked)) {
     89                ret = fseek((FILE *)_handle, offs, whence);
     90        }
     91
     92        return ret == 0;
     93}
     94
     95uint32 PSPIoStream::read(void *ptr, uint32 len) {
     96        int ret = 0;
     97        pm->blockOnSuspend();
     98
     99        // Act. If we fail, maybe it was because of a suspend
     100        ret = fread((byte *)ptr, 1, len, (FILE *)_handle);
     101        if (ferror((FILE *)_handle) && pm->blockOnSuspend() == PowerManager::Blocked) {
     102                clearerr((FILE *)_handle);
     103                fseek((FILE *)_handle, -ret, SEEK_CUR);
     104                ret = fread((byte *)ptr, 1, len, (FILE *)_handle);
     105        }
     106        return ret;
     107
     108}
     109
     110uint32 PSPIoStream::write(const void *ptr, uint32 len) {
     111        int ret = 0;
     112
     113        pm->blockOnSuspend();
     114
     115        // Act. If we fail, maybe it was because of a suspend
     116        ret = fwrite(ptr, 1, len, (FILE *)_handle);
     117        if (ferror((FILE *)_handle) && (pm->blockOnSuspend() == PowerManager::Blocked)) {
     118                clearerr((FILE *)_handle);
     119                fseek((FILE *)_handle, -ret, SEEK_CUR);
     120                ret = fwrite(ptr, 1, len, (FILE *)_handle);
     121        }
     122
     123        return ret;
     124}
     125
     126bool PSPIoStream::flush() {
     127        int ret = 0;
     128
     129        pm->blockOnSuspend();
     130
     131        // Act. If we fail, maybe it was because of a suspend
     132        if ((ret = fflush((FILE *)_handle)) != 0 && pm->blockOnSuspend() == PowerManager::Blocked) {
     133                ret = fflush((FILE *)_handle);
     134        }
     135       
     136        return ret == 0;
     137}
     138
     139PSPIoStream *PSPIoStream::makeFromPath(const Common::String &path, bool writeMode) {
     140        FILE *handle = fopen(path.c_str(), writeMode ? "wb" : "rb");
     141
     142        if (handle)
     143                return new PSPIoStream(handle, path, writeMode);
     144
     145        return 0;
     146}
     147
     148
     149int PSPIoStream::suspend() {
     150        // Save our position
     151        _pos = ftell((FILE *)_handle);
     152
     153        // close our file descriptor
     154        fclose((FILE *)_handle);
     155
     156        return 0;
     157}
     158
     159int PSPIoStream::resume() {
     160        int ret = 0;
     161
     162        // We reopen our file descriptor
     163        _handle = fopen(_path.c_str(), _writeMode ? "wb" : "rb");
     164        if (_handle <= 0) {
     165                PSPDebugTrace("PSPIoStream::resume(): Couldn't reopen file %s\n", _path.c_str());
     166                ret = -1;;
     167        }
     168
     169        // Resume our previous position
     170        fseek((FILE *)_handle, _pos, SEEK_SET);
     171
     172        return ret;
     173}
     174
     175
     176
     177#endif /* __PSP__ */
  • backends/module.mk

     
    1111        fs/posix/posix-fs-factory.o \
    1212        fs/ps2/ps2-fs-factory.o \
    1313        fs/psp/psp-fs-factory.o \
     14        fs/psp/psp-stream.o \
    1415        fs/symbian/symbian-fs-factory.o \
    1516        fs/windows/windows-fs-factory.o \
    1617        fs/wii/wii-fs-factory.o \