From 964d6ebc0686838b942f644baf6f827fa2d4b460 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Thu, 1 Nov 2018 21:12:34 -0700 Subject: [PATCH] pcbnew: Set clipboard locale to avoid Klipper crash Klipper seems to poll at odd times. When we copy to wxTheClipboard in one locale and Klipper extracts data in a different locale, it throws an XError. We don't need the extra LOCALE_IO calls as Format() uses its own LOCALE_IO setting. Fixes: lp:1800648 * https://bugs.launchpad.net/kicad/+bug/1800648 --- pcbnew/kicad_clipboard.cpp | 74 ++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/pcbnew/kicad_clipboard.cpp b/pcbnew/kicad_clipboard.cpp index 9b8c2f0611..113b607e46 100644 --- a/pcbnew/kicad_clipboard.cpp +++ b/pcbnew/kicad_clipboard.cpp @@ -61,13 +61,18 @@ void CLIPBOARD_IO::SetBoard( BOARD* aBoard ) void CLIPBOARD_IO::SaveSelection( const SELECTION& aSelected ) { - LOCALE_IO toggle; // toggles on, then off, the C locale. VECTOR2I refPoint( 0, 0 ); // dont even start if the selection is empty if( aSelected.Empty() ) return; + auto clipboard = wxTheClipboard; + wxClipboardLocker clipboardLock( clipboard ); + + if( !clipboardLock || !clipboard->IsOpened() ) + return; + if( aSelected.HasReferencePoint() ) refPoint = aSelected.GetReferencePoint(); @@ -175,16 +180,14 @@ void CLIPBOARD_IO::SaveSelection( const SELECTION& aSelected ) Format( clone.get(), 1 ); } - } m_formatter.Print( 0, "\n)" ); } - if( wxTheClipboard->Open() ) - { - wxTheClipboard->SetData( new wxTextDataObject( - wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) ); - wxTheClipboard->Close(); - } + + clipboard->SetData( new wxTextDataObject( + wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) ); + + clipboard->Flush(); } @@ -193,16 +196,18 @@ BOARD_ITEM* CLIPBOARD_IO::Parse() BOARD_ITEM* item; wxString result; - if( wxTheClipboard->Open() ) - { - if( wxTheClipboard->IsSupported( wxDF_TEXT ) ) - { - wxTextDataObject data; - wxTheClipboard->GetData( data ); - result = data.GetText(); - } + auto clipboard = wxTheClipboard; + wxClipboardLocker clipboardLock( clipboard ); - wxTheClipboard->Close(); + if( !clipboardLock ) + return nullptr; + + + if( clipboard->IsSupported( wxDF_TEXT ) ) + { + wxTextDataObject data; + clipboard->GetData( data ); + result = data.GetText(); } try @@ -221,7 +226,11 @@ BOARD_ITEM* CLIPBOARD_IO::Parse() void CLIPBOARD_IO::Save( const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties ) { - LOCALE_IO toggle; // toggles on, then off, the C locale. + auto clipboard = wxTheClipboard; + wxClipboardLocker clipboardLock( clipboard ); + + if( !clipboardLock ) + return; init( aProperties ); @@ -241,13 +250,9 @@ void CLIPBOARD_IO::Save( const wxString& aFileName, BOARD* aBoard, m_out->Print( 0, ")\n" ); - if( wxTheClipboard->Open() ) - { - wxTheClipboard->SetData( new wxTextDataObject( - wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) ); - wxTheClipboard->Close(); - } - + clipboard->SetData( new wxTextDataObject( + wxString( m_formatter.GetString().c_str(), wxConvUTF8 ) ) ); + clipboard->Flush(); } @@ -256,17 +261,18 @@ BOARD* CLIPBOARD_IO::Load( const wxString& aFileName, { std::string result; - if( wxTheClipboard->Open() ) + auto clipboard = wxTheClipboard; + wxClipboardLocker clipboardLock( clipboard ); + + if( !clipboardLock ) + return nullptr; + + if( clipboard->IsSupported( wxDF_TEXT ) ) { - if( wxTheClipboard->IsSupported( wxDF_TEXT ) ) - { - wxTextDataObject data; - wxTheClipboard->GetData( data ); + wxTextDataObject data; + clipboard->GetData( data ); - result = data.GetText().mb_str(); - } - - wxTheClipboard->Close(); + result = data.GetText().mb_str(); } STRING_LINE_READER reader(result, wxT( "clipboard" ) );