From 30283e3136c22444a2ecb1263a77654e6f1bd48d Mon Sep 17 00:00:00 2001 From: Dan Gudmundsson Date: Thu, 14 Jul 2011 11:36:50 +0200 Subject: Handle overloading callbacks the same as events Previously other process wx calls where allowed to execute in the callback code, but that lead to a deadlock if for example a dialog was created. --- lib/wx/c_src/wxePrintout.cpp | 49 +++++++++++++++++++++++++++++--------------- lib/wx/c_src/wxe_impl.cpp | 43 ++++++++++++++++++-------------------- lib/wx/c_src/wxe_impl.h | 3 +-- 3 files changed, 53 insertions(+), 42 deletions(-) (limited to 'lib/wx/c_src') diff --git a/lib/wx/c_src/wxePrintout.cpp b/lib/wx/c_src/wxePrintout.cpp index 1bcbc5f1a4..90959df379 100644 --- a/lib/wx/c_src/wxePrintout.cpp +++ b/lib/wx/c_src/wxePrintout.cpp @@ -38,7 +38,7 @@ rt.addAtom("_wx_invoke_cb_"); \ rt.addTupleCount(3); \ rt.send(); \ - handle_callback_batch(port); \ + handle_event_callback(port, memenv->owner); \ } #define INVOKE_CALLBACK(port, callback, class_str) \ @@ -68,9 +68,11 @@ bool wxEPrintout::OnBeginDocument(int startPage, int endPage) rt.addInt(startPage); rt.addInt(endPage); INVOKE_CALLBACK_END(port, 2); - if(((WxeApp *) wxTheApp)->cb_len > 0) { - char * bp = ((WxeApp *) wxTheApp)->cb_buff; - return *(int*) bp; + 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 wxPrintout::OnBeginDocument(startPage,endPage); @@ -122,9 +124,11 @@ bool wxEPrintout::HasPage(int page) INVOKE_CALLBACK_INIT(port, hasPage, "wxPrintout"); rt.addInt(page); INVOKE_CALLBACK_END(port, 1); - if(((WxeApp *) wxTheApp)->cb_len > 0) { - char * bp = ((WxeApp *) wxTheApp)->cb_buff; - return *(int*) bp; + 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 wxPrintout::HasPage(page); @@ -135,9 +139,11 @@ bool wxEPrintout::OnPrintPage(int page) INVOKE_CALLBACK_INIT(port, onPrintPage, "wxPrintout"); rt.addInt(page); INVOKE_CALLBACK_END(port, 1); - if(((WxeApp *) wxTheApp)->cb_len > 0) { - char * bp = ((WxeApp *) wxTheApp)->cb_buff; - return *(int*) bp; + 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 FALSE; } @@ -146,12 +152,14 @@ void wxEPrintout::GetPageInfo(int *minPage, int *maxPage, int *pageFrom, int *pa { if(getPageInfo) { INVOKE_CALLBACK(port, getPageInfo, "wxPrintout"); - if(((WxeApp *) wxTheApp)->cb_len > 0) { + if(((WxeApp *) wxTheApp)->cb_buff) { char * bp = ((WxeApp *) wxTheApp)->cb_buff; *minPage = *(int *) bp; bp += 4; *maxPage = *(int *) bp; bp += 4; *pageFrom = *(int *) bp; bp += 4; *pageTo = *(int *) bp; bp += 4; + driver_free(((WxeApp *) wxTheApp)->cb_buff); + ((WxeApp *) wxTheApp)->cb_buff = NULL; } } wxPrintout::GetPageInfo(minPage, maxPage, pageFrom, pageTo); @@ -166,9 +174,11 @@ wxString EwxListCtrl::OnGetItemText(long item, long col) const { rt.addInt(item); rt.addInt(col); INVOKE_CALLBACK_END(port, 2); - if(((WxeApp *) wxTheApp)->cb_len > 0) { + if(((WxeApp *) wxTheApp)->cb_buff) { char * bp = ((WxeApp *) wxTheApp)->cb_buff; wxString str = wxString(bp, wxConvUTF8); + driver_free(((WxeApp *) wxTheApp)->cb_buff); + ((WxeApp *) wxTheApp)->cb_buff = NULL; return str; } } @@ -182,8 +192,11 @@ wxListItemAttr* EwxListCtrl::OnGetItemAttr(long item) const { INVOKE_CALLBACK_END(port, 1); char * bp = ((WxeApp *) wxTheApp)->cb_buff; wxeMemEnv * memenv = ((WxeApp *) wxTheApp)->getMemEnv(port); - if(((WxeApp *) wxTheApp)->cb_len > 0) { - return (wxListItemAttr *) ((WxeApp *) wxTheApp)->getPtr(bp, memenv); + if(bp) { + wxListItemAttr * result = (wxListItemAttr *)((WxeApp *) wxTheApp)->getPtr(bp, memenv); + driver_free(((WxeApp *) wxTheApp)->cb_buff); + ((WxeApp *) wxTheApp)->cb_buff = NULL; + return result; } } return NULL; @@ -199,9 +212,11 @@ int EwxListCtrl::OnGetItemColumnImage(long item, long col) const { rt.addInt(item); rt.addInt(col); INVOKE_CALLBACK_END(port, 2); - if(((WxeApp *) wxTheApp)->cb_len > 0) { - char * bp = ((WxeApp *) wxTheApp)->cb_buff; - return *(int *) bp; + 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 -1; diff --git a/lib/wx/c_src/wxe_impl.cpp b/lib/wx/c_src/wxe_impl.cpp index 897c2b7cca..95755978f1 100644 --- a/lib/wx/c_src/wxe_impl.cpp +++ b/lib/wx/c_src/wxe_impl.cpp @@ -270,6 +270,7 @@ bool WxeApp::OnInit() global_me = new wxeMemEnv(); wxe_batch = new wxList; wxe_batch_cb_saved = new wxList; + cb_buff = NULL; wxIdleEvent::SetMode(wxIDLE_PROCESS_SPECIFIED); @@ -330,24 +331,14 @@ void handle_event_callback(ErlDrvPort port, ErlDrvTermData process) 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 Start ");fflush(stderr); + // fprintf(stderr, "\r\nCB EV Start ");fflush(stderr); app->dispatch_cb(wxe_batch, wxe_batch_cb_saved, process); - //fprintf(stderr, ".. done \r\n");fflush(stderr); + // fprintf(stderr, ".. done \r\n");fflush(stderr); wxe_batch_caller = 0; erl_drv_mutex_unlock(wxe_batch_locker_m); driver_demonitor_process(port, &monitor); } -void handle_callback_batch(ErlDrvPort port) -{ - WxeApp * app = (WxeApp *) wxTheApp; - // Should we be able to handle commands when recursing? probably - erl_drv_mutex_lock(wxe_batch_locker_m); - app->dispatch(wxe_batch, 0, WXE_CALLBACK); - wxe_batch_caller = 0; - erl_drv_mutex_unlock(wxe_batch_locker_m); -} - // Called by wx thread void WxeApp::idle(wxIdleEvent& event) { dispatch_cmds(); @@ -394,8 +385,10 @@ int WxeApp::dispatch(wxList * batch, int blevel, int list_type) case WXE_CB_RETURN: // erl_drv_mutex_unlock(wxe_batch_locker_m); should be called after // whatever cleaning is necessary - memcpy(cb_buff, event->buffer, event->len); - cb_len = event->len; + if(event->len > 0) { + cb_buff = (char *) driver_alloc(event->len); + memcpy(cb_buff, event->buffer, event->len); + } return blevel; default: erl_drv_mutex_unlock(wxe_batch_locker_m); @@ -448,8 +441,10 @@ void WxeApp::dispatch_cb(wxList * batch, wxList * temp, ErlDrvTermData process) case WXE_DEBUG_PING: break; case WXE_CB_RETURN: - memcpy(cb_buff, event->buffer, event->len); - cb_len = event->len; + if(event->len > 0) { + cb_buff = (char *) driver_alloc(event->len); + memcpy(cb_buff, event->buffer, event->len); + } callback_returned = 1; return; case WXE_CB_START: @@ -471,7 +466,7 @@ void WxeApp::dispatch_cb(wxList * batch, wxList * temp, ErlDrvTermData process) } delete event; } else { - // fprintf(stderr, " sav %d \r\n", event->op); + // fprintf(stderr, " save %d \r\n", event->op); temp->Append(event); } } @@ -903,11 +898,13 @@ int wxCALLBACK wxEListCtrlCompare(long item1, long item2, long callbackInfoPtr) rt.addAtom("_wx_invoke_cb_"); rt.addTupleCount(3); rt.send(); - handle_callback_batch(cb->port); + handle_event_callback(cb->port, memenv->owner); - if(((WxeApp *) wxTheApp)->cb_len > 0) { - char * bp = ((WxeApp *) wxTheApp)->cb_buff; - return *(int*) bp; - } else - return 0; + 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; } diff --git a/lib/wx/c_src/wxe_impl.h b/lib/wx/c_src/wxe_impl.h index dd872745be..ee31068d5d 100644 --- a/lib/wx/c_src/wxe_impl.h +++ b/lib/wx/c_src/wxe_impl.h @@ -178,7 +178,7 @@ public: wxeMemEnv * global_me; // Temp container for callbacks - char cb_buff[256]; + char *cb_buff; int cb_len; }; @@ -195,7 +195,6 @@ class wxETreeItemData : public wxTreeItemData bool sendevent(wxEvent * event, ErlDrvPort port); void pre_callback(); -void handle_callback_batch(ErlDrvPort port); // For wxePrintout void handle_event_callback(ErlDrvPort port, ErlDrvTermData process); void activateGL(ErlDrvTermData caller); -- cgit v1.2.3