diff options
author | Dan Gudmundsson <[email protected]> | 2014-01-27 14:23:15 +0100 |
---|---|---|
committer | Dan Gudmundsson <[email protected]> | 2014-01-27 14:23:15 +0100 |
commit | 75bfbe5a61fa1a1dd204fdb21dc4bf7df8e8003d (patch) | |
tree | cedea2151cdc673bddfe541cf4956c5fd6b431ab /lib/wx/c_src/wxe_impl.cpp | |
parent | c922ea6f82bb3d95d05d1f212b91e13f97e972b8 (diff) | |
parent | fd055cf43486358a413a1fa22f30f0aa711d25e1 (diff) | |
download | otp-75bfbe5a61fa1a1dd204fdb21dc4bf7df8e8003d.tar.gz otp-75bfbe5a61fa1a1dd204fdb21dc4bf7df8e8003d.tar.bz2 otp-75bfbe5a61fa1a1dd204fdb21dc4bf7df8e8003d.zip |
Merge branch 'dgud/wx/refactor-and-bugfixes/OTP-11586'
* dgud/wx/refactor-and-bugfixes/OTP-11586:
wx: Delay all deletes if recursed in event loop
wx: wx could hang if wxe_server died (or had died) when inside a callback
wx: Delay memory cleanup until safe
wx: Refactor C++ code
wx: Add init_dialog event
Update icons
wx: Update icons to plain old white ones
Diffstat (limited to 'lib/wx/c_src/wxe_impl.cpp')
-rw-r--r-- | lib/wx/c_src/wxe_impl.cpp | 461 |
1 files changed, 99 insertions, 362 deletions
diff --git a/lib/wx/c_src/wxe_impl.cpp b/lib/wx/c_src/wxe_impl.cpp index cc9bcc9957..5964ccfd00 100644 --- a/lib/wx/c_src/wxe_impl.cpp +++ b/lib/wx/c_src/wxe_impl.cpp @@ -23,10 +23,6 @@ #include <wx/wx.h> -#if defined(_WIN32) -#include <wx/msw/private.h> // for wxSetInstance -#endif - // Avoid including these in dcbuffer below #include "wx/dcmemory.h" #include "wx/dcclient.h" @@ -34,12 +30,12 @@ // Ok ugly but needed for wxBufferedDC crash workaround #define private public #include <wx/dcbuffer.h> - #undef private #include "wxe_impl.h" #include "wxe_events.h" #include "wxe_return.h" +#include "wxe_gl.h" IMPLEMENT_APP_NO_MAIN(WxeApp) @@ -47,120 +43,21 @@ DECLARE_APP(WxeApp) DEFINE_EVENT_TYPE(wxeEVT_META_COMMAND) -#define WXE_NOT_INITIATED 0 -#define WXE_INITIATED 1 -#define WXE_EXITED 2 -#define WXE_ERROR -1 - #define WXE_NORMAL 0 #define WXE_CALLBACK 1 #define WXE_STORED 2 -ErlDrvTid wxe_thread; - -ErlDrvMutex *wxe_status_m; -ErlDrvCond *wxe_status_c; - -ErlDrvMutex * wxe_batch_locker_m; -ErlDrvCond * wxe_batch_locker_c; - -static int wxe_status = WXE_NOT_INITIATED; +// Globals initiated in wxe_init.cpp +extern ErlDrvMutex *wxe_status_m; +extern ErlDrvCond *wxe_status_c; +extern ErlDrvMutex * wxe_batch_locker_m; +extern ErlDrvCond * wxe_batch_locker_c; +extern ErlDrvTermData init_caller; +extern int wxe_status; wxList * wxe_batch = NULL; wxList * wxe_batch_cb_saved = NULL; - -ErlDrvTermData wxe_batch_caller = 0; -ErlDrvTermData init_caller = 0; - -// extern opengl -void gl_dispatch(int op, char *bp, ErlDrvTermData caller, WXEBinRef *bins[]); - - -// Until fixed in emulator -#ifndef _WIN32 -extern "C" { -extern void erts_thread_disable_fpe(void); -} -#endif - -#if defined(__APPLE__) && defined(__MACH__) && !defined(__DARWIN__) -#define __DARWIN__ 1 -#endif - -#ifdef __DARWIN__ -extern "C" { - int erl_drv_stolen_main_thread_join(ErlDrvTid tid, void **respp); - int erl_drv_steal_main_thread(char *name, - ErlDrvTid *dtid, - void* (*func)(void*), - void* arg, - ErlDrvThreadOpts *opts); -} -#endif - -void *wxe_main_loop(void * ); - -/* ************************************************************ - * START AND STOP of driver thread - * ************************************************************/ - -int load_native_gui() -{ - return 1; -} - -int start_native_gui(wxe_data *sd) -{ - int res; - wxe_status_m = erl_drv_mutex_create((char *) "wxe_status_m"); - wxe_status_c = erl_drv_cond_create((char *)"wxe_status_c"); - - wxe_batch_locker_m = erl_drv_mutex_create((char *)"wxe_batch_locker_m"); - wxe_batch_locker_c = erl_drv_cond_create((char *)"wxe_batch_locker_c"); - init_caller = driver_connected(sd->port_handle); - -#ifdef __DARWIN__ - res = erl_drv_steal_main_thread((char *)"wxwidgets", - &wxe_thread,wxe_main_loop,(void *) sd->pdl,NULL); -#else - res = erl_drv_thread_create((char *)"wxwidgets", - &wxe_thread,wxe_main_loop,(void *) sd->pdl,NULL); -#endif - if(res == 0) { - erl_drv_mutex_lock(wxe_status_m); - for(;wxe_status == WXE_NOT_INITIATED;) { - erl_drv_cond_wait(wxe_status_c, wxe_status_m); - } - erl_drv_mutex_unlock(wxe_status_m); - return wxe_status; - } else { - wxString msg; - msg.Printf(wxT("Erlang failed to create wxe-thread %d\r\n"), res); - send_msg("error", &msg); - return -1; - } -} - -void stop_native_gui(wxe_data *sd) -{ - if(wxe_status == WXE_INITIATED) { - meta_command(WXE_SHUTDOWN, sd); - } -#ifdef __DARWIN__ - erl_drv_stolen_main_thread_join(wxe_thread, NULL); -#else - erl_drv_thread_join(wxe_thread, NULL); -#endif - erl_drv_mutex_destroy(wxe_status_m); - erl_drv_cond_destroy(wxe_status_c); - erl_drv_mutex_destroy(wxe_batch_locker_m); - erl_drv_cond_destroy(wxe_batch_locker_c); -} - -void unload_native_gui() -{ - -} +int wxe_batch_caller = 0; // inside batch if larger than 0 /* ************************************************************ * Commands from erlang @@ -169,7 +66,8 @@ void unload_native_gui() void push_command(int op,char * buf,int len, wxe_data *sd) { - // fprintf(stderr, "Op %d %d\r\n", op, (int) driver_caller(sd->port_handle)),fflush(stderr); + /* fprintf(stderr, "Op %d %d [%ld] %d\r\n", op, (int) driver_caller(sd->port_handle), + wxe_batch->size(), wxe_batch_caller),fflush(stderr); */ wxeCommand *Cmd = new wxeCommand(op, buf, len, sd); erl_drv_mutex_lock(wxe_batch_locker_m); wxe_batch->Append(Cmd); @@ -206,60 +104,19 @@ void meta_command(int what, wxe_data *sd) { } } -/* ************************************************************ - * wxWidgets Thread - * ************************************************************/ - -void *wxe_main_loop(void *vpdl) -{ - int result; - int argc = 1; - char * temp = (char *) "Erlang"; - char * argv[] = {temp,NULL}; - ErlDrvPDL pdl = (ErlDrvPDL) vpdl; - - driver_pdl_inc_refc(pdl); - - // Disable floating point execption if they are on. - // This should be done in emulator but it's not in yet. -#ifndef _WIN32 - erts_thread_disable_fpe(); -#else - // Setup that wxWidgets should look for cursors and icons in - // this dll and not in werl.exe (which is the default) - HMODULE WXEHandle = GetModuleHandle(_T("wxe_driver")); - wxSetInstance((HINSTANCE) WXEHandle); -#endif - - wxe_ps_init(); - result = wxEntry(argc, argv); - // fprintf(stderr, "WXWidgets quits main loop %d \r\n", result); - if(result >= 0 && wxe_status == WXE_INITIATED) { - /* We are done try to make a clean exit */ - wxe_status = WXE_EXITED; - driver_pdl_dec_refc(pdl); -#ifndef __DARWIN__ - erl_drv_thread_exit(NULL); -#endif - return NULL; - } else { - erl_drv_mutex_lock(wxe_status_m); - wxe_status = WXE_ERROR; - erl_drv_cond_signal(wxe_status_c); - erl_drv_mutex_unlock(wxe_status_m); - driver_pdl_dec_refc(pdl); - return NULL; - } -} - -void WxeApp::dummy_close(wxEvent& Ev) { - // fprintf(stderr, "Dummy Close invoked\r\n"); - // wxMac really wants a top level window which command-q quits if there are no - // windows open, and this will kill the erlang, override default handling +void send_msg(const char * type, wxString * msg) { + wxeReturn rt = wxeReturn(WXE_DRV_PORT, init_caller); + rt.addAtom((char *) "wxe_driver"); + rt.addAtom((char *) type); + rt.add(msg); + rt.addTupleCount(3); + rt.send(); } +/* ************************************************************ + * Init WxeApp the application emulator + * ************************************************************/ -// Init wx-widgets thread bool WxeApp::OnInit() { @@ -267,6 +124,9 @@ bool WxeApp::OnInit() wxe_batch = new wxList; wxe_batch_cb_saved = new wxList; cb_buff = NULL; + recurse_level = 0; + delayed_cleanup = new wxList; + delayed_delete = new wxList; wxe_ps_init2(); // wxIdleEvent::SetMode(wxIDLE_PROCESS_SPECIFIED); // Hmm printpreview doesn't work in 2.9 with this @@ -304,13 +164,16 @@ void WxeApp::shutdown(wxeMetaCommand& Ecmd) { ExitMainLoop(); } -void send_msg(const char * type, wxString * msg) { - wxeReturn rt = wxeReturn(WXE_DRV_PORT, init_caller); - rt.addAtom((char *) "wxe_driver"); - rt.addAtom((char *) type); - rt.add(msg); - rt.addTupleCount(3); - rt.send(); +void WxeApp::dummy_close(wxEvent& Ev) { + // fprintf(stderr, "Dummy Close invoked\r\n"); + // wxMac really wants a top level window which command-q quits if there are no + // windows open, and this will kill the erlang, override default handling +} + +// Called by wx thread +void WxeApp::idle(wxIdleEvent& event) { + event.Skip(true); + dispatch_cmds(); } /* ************************************************************ @@ -327,29 +190,51 @@ void handle_event_callback(ErlDrvPort port, ErlDrvTermData process) { WxeApp * app = (WxeApp *) wxTheApp; ErlDrvMonitor monitor; - driver_monitor_process(port, process, &monitor); - // Should we be able to handle commands when recursing? probably - erl_drv_mutex_lock(wxe_batch_locker_m); - //fprintf(stderr, "\r\nCB EV Start %lu \r\n", process);fflush(stderr); - app->dispatch_cb(wxe_batch, wxe_batch_cb_saved, process); - //fprintf(stderr, "CB EV done %lu \r\n", process);fflush(stderr); - wxe_batch_caller = 0; - erl_drv_mutex_unlock(wxe_batch_locker_m); - driver_demonitor_process(port, &monitor); -} - -// Called by wx thread -void WxeApp::idle(wxIdleEvent& event) { - event.Skip(true); - dispatch_cmds(); + // Is thread safe if pdl have been incremented + if(driver_monitor_process(port, process, &monitor) == 0) { + // Should we be able to handle commands when recursing? probably + erl_drv_mutex_lock(wxe_batch_locker_m); + //fprintf(stderr, "\r\nCB EV Start %lu \r\n", process);fflush(stderr); + app->recurse_level++; + app->dispatch_cb(wxe_batch, wxe_batch_cb_saved, process); + app->recurse_level--; + //fprintf(stderr, "CB EV done %lu \r\n", process);fflush(stderr); + wxe_batch_caller = 0; + erl_drv_mutex_unlock(wxe_batch_locker_m); + driver_demonitor_process(port, &monitor); + } } -void WxeApp::dispatch_cmds() { +void WxeApp::dispatch_cmds() +{ erl_drv_mutex_lock(wxe_batch_locker_m); + recurse_level++; int level = dispatch(wxe_batch_cb_saved, 0, WXE_STORED); dispatch(wxe_batch, level, WXE_NORMAL); + recurse_level--; wxe_batch_caller = 0; erl_drv_mutex_unlock(wxe_batch_locker_m); + // Cleanup old memenv's and deleted objects + if(recurse_level == 0) { + if(delayed_delete->size() > 0) + for( wxList::compatibility_iterator node = delayed_delete->GetFirst(); + node; + node = delayed_delete->GetFirst()) { + wxeCommand *event = (wxeCommand *)node->GetData(); + delayed_delete->Erase(node); + wxe_dispatch(*event); + event->Delete(); + } + if(delayed_cleanup->size() > 0) + for( wxList::compatibility_iterator node = delayed_cleanup->GetFirst(); + node; + node = delayed_cleanup->GetFirst()) { + wxeMetaCommand *event = (wxeMetaCommand *)node->GetData(); + delayed_cleanup->Erase(node); + destroyMemEnv(*event); + delete event; + } + } } // Should have erl_drv_mutex_lock(wxe_batch_locker_m); @@ -401,7 +286,7 @@ int WxeApp::dispatch(wxList * batch, int blevel, int list_type) erl_drv_mutex_lock(wxe_batch_locker_m); break; } - delete event; + event->Delete(); } } else { if((list_type == WXE_STORED) || (blevel <= 0 && list_type == WXE_NORMAL)) { @@ -433,6 +318,7 @@ void WxeApp::dispatch_cb(wxList * batch, wxList * temp, ErlDrvTermData process) // fprintf(stderr, " Ev %d %lu\r\n", event->op, event->caller); if(event->caller == process || // Callbacks from CB process only event->op == WXE_CB_START || // Event callback start change process + event->op == WXE_CB_DIED || // Event callback process died // Allow connect_cb during CB i.e. msg from wxe_server. (memenv && event->caller == memenv->owner)) { @@ -445,7 +331,8 @@ void WxeApp::dispatch_cb(wxList * batch, wxList * temp, ErlDrvTermData process) if(event->len > 0) { cb_buff = (char *) driver_alloc(event->len); memcpy(cb_buff, event->buffer, event->len); - } + } // continue + case WXE_CB_DIED: callback_returned = 1; return; case WXE_CB_START: @@ -480,7 +367,7 @@ void WxeApp::dispatch_cb(wxList * batch, wxList * temp, ErlDrvTermData process) return; break; } - delete event; + event->Delete(); } else { // fprintf(stderr, " save %d \r\n", event->op); temp->Append(event); @@ -517,7 +404,8 @@ void WxeApp::newMemEnv(wxeMetaCommand& Ecmd) { erl_drv_send_term(WXE_DRV_PORT,Ecmd.caller,rt,2); } -void WxeApp::destroyMemEnv(wxeMetaCommand& Ecmd) { +void WxeApp::destroyMemEnv(wxeMetaCommand& Ecmd) +{ // Clear incoming cmd queue first // dispatch_cmds(); wxWindow *parent = NULL; @@ -536,26 +424,33 @@ void WxeApp::destroyMemEnv(wxeMetaCommand& Ecmd) { ptrMap::iterator it = ptr2ref.find(ptr); if(it != ptr2ref.end()) { wxeRefData *refd = it->second; - if(refd->alloc_in_erl) { - if(refd->type == 2) { - wxDialog *win = (wxDialog *) ptr; - if(win->IsModal()) { - win->EndModal(-1); - } - parent = win->GetParent(); - if(parent) { - ptrMap::iterator parentRef = ptr2ref.find(parent); - if(parentRef == ptr2ref.end()) { - // The parent is already dead delete the parent ref - win->SetParent(NULL); - } + if(refd->alloc_in_erl && refd->type == 2) { + wxDialog *win = (wxDialog *) ptr; + if(win->IsModal()) { + win->EndModal(-1); + } + parent = win->GetParent(); + if(parent) { + ptrMap::iterator parentRef = ptr2ref.find(parent); + if(parentRef == ptr2ref.end()) { + // The parent is already dead delete the parent ref + win->SetParent(NULL); } + } + if(recurse_level > 0) { + // Delay delete until we are out of dispatch* + delayed_cleanup->Append(Ecmd.Clone()); + } else { delete win; } } } } } + + if(recurse_level > 0) + return; + // First pass, delete all top parents/windows of all linked objects // fprintf(stderr, "close port %x\r\n", Ecmd.port);fflush(stderr); @@ -780,161 +675,3 @@ void WxeApp::registerPid(char * bp, ErlDrvTermData pid, wxeMemEnv * memenv) { }; throw wxe_badarg(index); } - - -/* ************************************************************ - * Misc utility classes - * ************************************************************/ - -/* **************************************************************************** - * Memory handling - * ****************************************************************************/ - -wxeMemEnv::wxeMemEnv() { - ref2ptr = (void **) driver_alloc(128*sizeof(void *)); - ref2ptr[0] = NULL; - next = 1; - max = 128; -} - -wxeMemEnv::~wxeMemEnv() { - driver_free(ref2ptr); -} - -/* **************************************************************************** - * Erlang Commands (don't need to be derived of wxEvent anymore should - * be re-written to own class struct) - * ****************************************************************************/ - -wxeCommand::wxeCommand(int fc,char * cbuf,int buflen, wxe_data *sd) - : wxObject() -{ - WXEBinRef *temp, *start, *prev; - int n = 0; - caller = driver_caller(sd->port_handle); - port = sd->port; - op = fc; - len = buflen; - bin[0] = NULL; - bin[1] = NULL; - bin[2] = NULL; - - if(cbuf) { - buffer = (char *) driver_alloc(len); - memcpy((void *) buffer, (void *) cbuf, len);; - - temp = sd->bin; - - prev = NULL; - start = temp; - - while(temp) { - if(caller == temp->from) { - bin[n++] = temp; - if(prev) { - prev->next = temp->next; - } else { - start = temp->next; - } - temp = temp->next; - } else { - prev = temp; - temp = temp->next; - } - } - sd->bin = start; - } else { // No-op only PING currently - buffer = NULL; - } -} - -wxeCommand::~wxeCommand() { - int n = 0; - if(buffer) { - while(bin[n]) { - if(bin[n]->bin) - driver_free_binary(bin[n]->bin); - driver_free(bin[n++]); - } - driver_free(buffer); - } -} - -/* **************************************************************************** - * TreeItemData - * ****************************************************************************/ - -wxETreeItemData::wxETreeItemData(int sz, char * data) { - size = sz; - bin = (char *) driver_alloc(sz); - memcpy(bin, data, sz); -} - -wxETreeItemData::~wxETreeItemData() -{ - driver_free(bin); -} - -/* **************************************************************************** - * CallbackData * - * ****************************************************************************/ - -wxeCallbackData::wxeCallbackData(ErlDrvTermData caller, int req, char *req_type, - int funcb, int skip_ev, wxeErlTerm * userData, - wxeEvtListener *handler_cb) - : wxObject() -{ - listener = caller; - obj = req; - fun_id = funcb; - strcpy(class_name, req_type); - skip = skip_ev; - user_data = userData; - handler = handler_cb; -} - -wxeCallbackData::~wxeCallbackData() { - // fprintf(stderr, "CBD Deleteing %p %s\r\n", this, class_name); fflush(stderr); - if(user_data) { - delete user_data; - } - ptrMap::iterator it; - it = ((WxeApp *)wxTheApp)->ptr2ref.find(handler); - if(it != ((WxeApp *)wxTheApp)->ptr2ref.end()) { - wxeRefData *refd = it->second; - wxeReturn rt = wxeReturn(WXE_DRV_PORT, refd->memenv->owner, false); - rt.addAtom("wx_delete_cb"); - rt.addInt(fun_id); - rt.addRef(refd->ref, "wxeEvtListener"); - rt.addRef(obj, class_name); - rt.addTupleCount(4); - rt.send(); - } -} - -/* **************************************************************************** - * wxListCtrlCompare wrapper - * ****************************************************************************/ - -int wxCALLBACK wxEListCtrlCompare(long item1, long item2, long callbackInfoPtr) -{ - callbackInfo * cb = (callbackInfo *)callbackInfoPtr; - wxeMemEnv * memenv = ((WxeApp *) wxTheApp)->getMemEnv(cb->port); - wxeReturn rt = wxeReturn(WXE_DRV_PORT, memenv->owner, false); - rt.addInt(cb->callbackID); - rt.addInt(item1); - rt.addInt(item2); - rt.endList(2); - rt.addAtom("_wx_invoke_cb_"); - rt.addTupleCount(3); - rt.send(); - handle_event_callback(WXE_DRV_PORT_HANDLE, memenv->owner); - - if(((WxeApp *) wxTheApp)->cb_buff) { - int res = * (int*) ((WxeApp *) wxTheApp)->cb_buff; - driver_free(((WxeApp *) wxTheApp)->cb_buff); - ((WxeApp *) wxTheApp)->cb_buff = NULL; - return res; - } - return 0; -} |