Skip to content

Commit b0a7e05

Browse files
committed
Merge pull request bitcoin#1019 from laanwj/2012_03_uirefactor
Streamline UI ↔ Core interface
2 parents cadae35 + 5cccb13 commit b0a7e05

31 files changed

+323
-375
lines changed

bitcoin-qt.pro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ HEADERS += src/qt/bitcoingui.h \
115115
src/key.h \
116116
src/db.h \
117117
src/script.h \
118-
src/noui.h \
119118
src/init.h \
120119
src/headers.h \
121120
src/irc.h \
@@ -135,7 +134,6 @@ HEADERS += src/qt/bitcoingui.h \
135134
src/qt/guiconstants.h \
136135
src/qt/optionsmodel.h \
137136
src/qt/monitoreddatamapper.h \
138-
src/qtui.h \
139137
src/qt/transactiondesc.h \
140138
src/qt/transactiondescdialog.h \
141139
src/qt/bitcoinamountfield.h \
@@ -155,7 +153,9 @@ HEADERS += src/qt/bitcoingui.h \
155153
src/qt/askpassphrasedialog.h \
156154
src/protocol.h \
157155
src/qt/notificator.h \
158-
src/qt/qtipcserver.h
156+
src/qt/qtipcserver.h \
157+
src/allocators.h \
158+
src/ui_interface.h
159159

160160
SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
161161
src/qt/transactiontablemodel.cpp \

src/allocators.h

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2012 The Bitcoin developers
3+
// Distributed under the MIT/X11 software license, see the accompanying
4+
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
5+
#ifndef BITCOIN_ALLOCATORS_H
6+
#define BITCOIN_ALLOCATORS_H
7+
8+
#include <string>
9+
10+
#ifdef WIN32
11+
#define _WIN32_WINNT 0x0501
12+
#define WIN32_LEAN_AND_MEAN 1
13+
#ifndef NOMINMAX
14+
#define NOMINMAX
15+
#endif
16+
#include <windows.h>
17+
// This is used to attempt to keep keying material out of swap
18+
// Note that VirtualLock does not provide this as a guarantee on Windows,
19+
// but, in practice, memory that has been VirtualLock'd almost never gets written to
20+
// the pagefile except in rare circumstances where memory is extremely low.
21+
#define mlock(p, n) VirtualLock((p), (n));
22+
#define munlock(p, n) VirtualUnlock((p), (n));
23+
#else
24+
#include <sys/mman.h>
25+
#include <limits.h>
26+
/* This comes from limits.h if it's not defined there set a sane default */
27+
#ifndef PAGESIZE
28+
#include <unistd.h>
29+
#define PAGESIZE sysconf(_SC_PAGESIZE)
30+
#endif
31+
#define mlock(a,b) \
32+
mlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
33+
(((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
34+
#define munlock(a,b) \
35+
munlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
36+
(((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
37+
#endif
38+
39+
//
40+
// Allocator that locks its contents from being paged
41+
// out of memory and clears its contents before deletion.
42+
//
43+
template<typename T>
44+
struct secure_allocator : public std::allocator<T>
45+
{
46+
// MSVC8 default copy constructor is broken
47+
typedef std::allocator<T> base;
48+
typedef typename base::size_type size_type;
49+
typedef typename base::difference_type difference_type;
50+
typedef typename base::pointer pointer;
51+
typedef typename base::const_pointer const_pointer;
52+
typedef typename base::reference reference;
53+
typedef typename base::const_reference const_reference;
54+
typedef typename base::value_type value_type;
55+
secure_allocator() throw() {}
56+
secure_allocator(const secure_allocator& a) throw() : base(a) {}
57+
template <typename U>
58+
secure_allocator(const secure_allocator<U>& a) throw() : base(a) {}
59+
~secure_allocator() throw() {}
60+
template<typename _Other> struct rebind
61+
{ typedef secure_allocator<_Other> other; };
62+
63+
T* allocate(std::size_t n, const void *hint = 0)
64+
{
65+
T *p;
66+
p = std::allocator<T>::allocate(n, hint);
67+
if (p != NULL)
68+
mlock(p, sizeof(T) * n);
69+
return p;
70+
}
71+
72+
void deallocate(T* p, std::size_t n)
73+
{
74+
if (p != NULL)
75+
{
76+
memset(p, 0, sizeof(T) * n);
77+
munlock(p, sizeof(T) * n);
78+
}
79+
std::allocator<T>::deallocate(p, n);
80+
}
81+
};
82+
83+
84+
//
85+
// Allocator that clears its contents before deletion.
86+
//
87+
template<typename T>
88+
struct zero_after_free_allocator : public std::allocator<T>
89+
{
90+
// MSVC8 default copy constructor is broken
91+
typedef std::allocator<T> base;
92+
typedef typename base::size_type size_type;
93+
typedef typename base::difference_type difference_type;
94+
typedef typename base::pointer pointer;
95+
typedef typename base::const_pointer const_pointer;
96+
typedef typename base::reference reference;
97+
typedef typename base::const_reference const_reference;
98+
typedef typename base::value_type value_type;
99+
zero_after_free_allocator() throw() {}
100+
zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {}
101+
template <typename U>
102+
zero_after_free_allocator(const zero_after_free_allocator<U>& a) throw() : base(a) {}
103+
~zero_after_free_allocator() throw() {}
104+
template<typename _Other> struct rebind
105+
{ typedef zero_after_free_allocator<_Other> other; };
106+
107+
void deallocate(T* p, std::size_t n)
108+
{
109+
if (p != NULL)
110+
memset(p, 0, sizeof(T) * n);
111+
std::allocator<T>::deallocate(p, n);
112+
}
113+
};
114+
115+
// This is exactly like std::string, but with a custom allocator.
116+
// (secure_allocator<> is defined in serialize.h)
117+
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
118+
119+
#endif

src/bitcoinrpc.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,9 @@ Value stop(const Array& params, bool fHelp)
221221
throw runtime_error(
222222
"stop\n"
223223
"Stop bitcoin server.");
224-
#ifndef QT_GUI
225224
// Shutdown will take long enough that the response should get back
226-
CreateThread(Shutdown, NULL);
225+
QueueShutdown();
227226
return "bitcoin server stopping";
228-
#else
229-
throw runtime_error("NYI: cannot shut down GUI with RPC command");
230-
#endif
231227
}
232228

233229

@@ -1676,11 +1672,6 @@ Value encryptwallet(const Array& params, bool fHelp)
16761672
if (pwalletMain->IsCrypted())
16771673
throw JSONRPCError(-15, "Error: running with an encrypted wallet, but encryptwallet was called.");
16781674

1679-
#ifdef QT_GUI
1680-
// shutting down via RPC while the GUI is running does not work (yet):
1681-
throw runtime_error("Not Yet Implemented: use GUI to encrypt wallet, not RPC command");
1682-
#endif
1683-
16841675
// TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
16851676
// Alternately, find a way to make params[0] mlock()'d to begin with.
16861677
SecureString strWalletPass;
@@ -1698,7 +1689,7 @@ Value encryptwallet(const Array& params, bool fHelp)
16981689
// BDB seems to have a bad habit of writing old data into
16991690
// slack space in .dat files; that is bad if the old data is
17001691
// unencrypted private keys. So:
1701-
CreateThread(Shutdown, NULL);
1692+
QueueShutdown();
17021693
return "wallet encrypted; bitcoin server stopping, restart to run with encrypted wallet";
17031694
}
17041695

@@ -2371,19 +2362,18 @@ void ThreadRPCServer2(void* parg)
23712362
strWhatAmI = strprintf(_("To use the %s option"), "\"-server\"");
23722363
else if (mapArgs.count("-daemon"))
23732364
strWhatAmI = strprintf(_("To use the %s option"), "\"-daemon\"");
2374-
std::string strMessage = _("%s, you must set a rpcpassword in the configuration file:\n %s\n"
2375-
"It is recommended you use the following random password:\n"
2376-
"rpcuser=bitcoinrpc\n"
2377-
"rpcpassword=%s\n"
2378-
"(you do not need to remember this password)\n"
2379-
"If the file does not exist, create it with owner-readable-only file permissions.\n");
2380-
fprintf(stderr, strMessage.c_str(),
2365+
ThreadSafeMessageBox(strprintf(
2366+
_("%s, you must set a rpcpassword in the configuration file:\n %s\n"
2367+
"It is recommended you use the following random password:\n"
2368+
"rpcuser=bitcoinrpc\n"
2369+
"rpcpassword=%s\n"
2370+
"(you do not need to remember this password)\n"
2371+
"If the file does not exist, create it with owner-readable-only file permissions.\n"),
23812372
strWhatAmI.c_str(),
23822373
GetConfigFile().c_str(),
2383-
EncodeBase58(&rand_pwd[0],&rand_pwd[0]+32).c_str());
2384-
#ifndef QT_GUI
2385-
CreateThread(Shutdown, NULL);
2386-
#endif
2374+
EncodeBase58(&rand_pwd[0],&rand_pwd[0]+32).c_str()),
2375+
_("Error"), wxOK | wxMODAL);
2376+
QueueShutdown();
23872377
return;
23882378
}
23892379

src/crypter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#ifndef __CRYPTER_H__
55
#define __CRYPTER_H__
66

7-
#include "util.h" /* for SecureString */
7+
#include "allocators.h" /* for SecureString */
88
#include "key.h"
99

1010
const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;

src/headers.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,5 @@
8787
#include "bignum.h"
8888
#include "base58.h"
8989
#include "main.h"
90-
#ifdef QT_GUI
91-
#include "qtui.h"
92-
#else
93-
#include "noui.h"
94-
#endif
90+
#include "wallet.h"
91+
#include "ui_interface.h"

src/init.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,6 @@
1313
#include <boost/filesystem/convenience.hpp>
1414
#include <boost/interprocess/sync/file_lock.hpp>
1515

16-
#if defined(BITCOIN_NEED_QT_PLUGINS) && !defined(_BITCOIN_QT_PLUGINS_INCLUDED)
17-
#define _BITCOIN_QT_PLUGINS_INCLUDED
18-
#define __INSURE__
19-
#include <QtPlugin>
20-
Q_IMPORT_PLUGIN(qcncodecs)
21-
Q_IMPORT_PLUGIN(qjpcodecs)
22-
Q_IMPORT_PLUGIN(qtwcodecs)
23-
Q_IMPORT_PLUGIN(qkrcodecs)
24-
Q_IMPORT_PLUGIN(qtaccessiblewidgets)
25-
#endif
26-
2716
#ifdef WIN32
2817
#define strncasecmp strnicmp
2918
#endif
@@ -248,7 +237,7 @@ bool AppInit2(int argc, char* argv[])
248237
strUsage.erase(std::remove(strUsage.begin(), strUsage.end(), '\t'), strUsage.end());
249238
#if defined(QT_GUI) && defined(WIN32)
250239
// On windows, show a message box, as there is no stderr
251-
wxMessageBox(strUsage, "Usage");
240+
ThreadSafeMessageBox(strUsage, _("Usage"), wxOK | wxMODAL);
252241
#else
253242
fprintf(stderr, "%s", strUsage.c_str());
254243
#endif
@@ -337,7 +326,7 @@ bool AppInit2(int argc, char* argv[])
337326
static boost::interprocess::file_lock lock(strLockFile.c_str());
338327
if (!lock.try_lock())
339328
{
340-
wxMessageBox(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin is probably already running."), GetDataDir().c_str()), "Bitcoin");
329+
ThreadSafeMessageBox(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin is probably already running."), GetDataDir().c_str()), _("Bitcoin"), wxOK|wxMODAL);
341330
return false;
342331
}
343332

@@ -379,7 +368,7 @@ bool AppInit2(int argc, char* argv[])
379368
{
380369
strErrors << _("Wallet needed to be rewritten: restart Bitcoin to complete") << "\n";
381370
printf("%s", strErrors.str().c_str());
382-
wxMessageBox(strErrors.str(), "Bitcoin", wxOK | wxICON_ERROR);
371+
ThreadSafeMessageBox(strErrors.str(), _("Bitcoin"), wxOK | wxICON_ERROR | wxMODAL);
383372
return false;
384373
}
385374
else
@@ -451,7 +440,7 @@ bool AppInit2(int argc, char* argv[])
451440

452441
if (!strErrors.str().empty())
453442
{
454-
wxMessageBox(strErrors.str(), "Bitcoin", wxOK | wxICON_ERROR);
443+
ThreadSafeMessageBox(strErrors.str(), _("Bitcoin"), wxOK | wxICON_ERROR | wxMODAL);
455444
return false;
456445
}
457446

@@ -507,7 +496,7 @@ bool AppInit2(int argc, char* argv[])
507496
addrProxy = CService(mapArgs["-proxy"], 9050);
508497
if (!addrProxy.IsValid())
509498
{
510-
wxMessageBox(_("Invalid -proxy address"), "Bitcoin");
499+
ThreadSafeMessageBox(_("Invalid -proxy address"), _("Bitcoin"), wxOK | wxMODAL);
511500
return false;
512501
}
513502
}
@@ -538,7 +527,7 @@ bool AppInit2(int argc, char* argv[])
538527
std::string strError;
539528
if (!BindListenPort(strError))
540529
{
541-
wxMessageBox(strError, "Bitcoin");
530+
ThreadSafeMessageBox(strError, _("Bitcoin"), wxOK | wxMODAL);
542531
return false;
543532
}
544533
}
@@ -558,11 +547,11 @@ bool AppInit2(int argc, char* argv[])
558547
{
559548
if (!ParseMoney(mapArgs["-paytxfee"], nTransactionFee))
560549
{
561-
wxMessageBox(_("Invalid amount for -paytxfee=<amount>"), "Bitcoin");
550+
ThreadSafeMessageBox(_("Invalid amount for -paytxfee=<amount>"), _("Bitcoin"), wxOK | wxMODAL);
562551
return false;
563552
}
564553
if (nTransactionFee > 0.25 * COIN)
565-
wxMessageBox(_("Warning: -paytxfee is set very high. This is the transaction fee you will pay if you send a transaction."), "Bitcoin", wxOK | wxICON_EXCLAMATION);
554+
ThreadSafeMessageBox(_("Warning: -paytxfee is set very high. This is the transaction fee you will pay if you send a transaction."), _("Bitcoin"), wxOK | wxICON_EXCLAMATION | wxMODAL);
566555
}
567556

568557
//
@@ -574,7 +563,7 @@ bool AppInit2(int argc, char* argv[])
574563
RandAddSeedPerfmon();
575564

576565
if (!CreateThread(StartNode, NULL))
577-
wxMessageBox(_("Error: CreateThread(StartNode) failed"), "Bitcoin");
566+
ThreadSafeMessageBox(_("Error: CreateThread(StartNode) failed"), _("Bitcoin"), wxOK | wxMODAL);
578567

579568
if (fServer)
580569
CreateThread(ThreadRPCServer, NULL);

src/main.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,8 +1811,8 @@ bool CheckDiskSpace(uint64 nAdditionalBytes)
18111811
string strMessage = _("Warning: Disk space is low ");
18121812
strMiscWarning = strMessage;
18131813
printf("*** %s\n", strMessage.c_str());
1814-
ThreadSafeMessageBox(strMessage, "Bitcoin", wxOK | wxICON_EXCLAMATION);
1815-
CreateThread(Shutdown, NULL);
1814+
ThreadSafeMessageBox(strMessage, "Bitcoin", wxOK | wxICON_EXCLAMATION | wxMODAL);
1815+
QueueShutdown();
18161816
return false;
18171817
}
18181818
return true;
@@ -3450,8 +3450,6 @@ void static BitcoinMiner(CWallet *pwallet)
34503450
dHashesPerSec = 1000.0 * nHashCounter / (GetTimeMillis() - nHPSTimerStart);
34513451
nHPSTimerStart = GetTimeMillis();
34523452
nHashCounter = 0;
3453-
string strStatus = strprintf(" %.0f khash/s", dHashesPerSec/1000.0);
3454-
UIThreadCall(boost::bind(CalledSetStatusBar, strStatus, 0));
34553453
static int64 nLogTime;
34563454
if (GetTime() - nLogTime > 30 * 60)
34573455
{
@@ -3508,7 +3506,6 @@ void static ThreadBitcoinMiner(void* parg)
35083506
vnThreadsRunning[THREAD_MINER]--;
35093507
PrintException(NULL, "ThreadBitcoinMiner()");
35103508
}
3511-
UIThreadCall(boost::bind(CalledSetStatusBar, "", 0));
35123509
nHPSTimerStart = 0;
35133510
if (vnThreadsRunning[THREAD_MINER] == 0)
35143511
dHashesPerSec = 0;

src/makefile.linux-mingw

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ OBJS= \
5959
obj/rpcdump.o \
6060
obj/script.o \
6161
obj/util.o \
62-
obj/wallet.o
62+
obj/wallet.o \
63+
obj/noui.o
6364

6465
all: bitcoind.exe
6566

src/makefile.mingw

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ OBJS= \
5757
obj/rpcdump.o \
5858
obj/script.o \
5959
obj/util.o \
60-
obj/wallet.o
60+
obj/wallet.o \
61+
obj/noui.o
6162

6263

6364
all: bitcoind.exe

src/makefile.osx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ OBJS= \
7474
obj/rpcdump.o \
7575
obj/script.o \
7676
obj/util.o \
77-
obj/wallet.o
77+
obj/wallet.o \
78+
obj/noui.o
7879

7980
ifdef USE_UPNP
8081
DEFS += -DUSE_UPNP=$(USE_UPNP)

0 commit comments

Comments
 (0)