FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/desktop/DesktopWin32ApplicationLaunch.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
desktop
/
DesktopWin32ApplicationLaunch.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
238 lines (198 loc) · 6.46 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
desktop
/
DesktopWin32ApplicationLaunch.cpp
Copy path
File metadata and controls
238 lines (198 loc) · 6.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* DesktopWin32ApplicationLaunch.cpp
*
* Copyright (C) 2009-17 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
"
DesktopApplicationLaunch.hpp
"
#
include
<
windows.h
>
#
include
<
QWidget
>
#
include
<
core/system/Process.hpp
>
#
include
<
core/system/Environment.hpp
>
#
include
<
core/r_util/RUserData.hpp
>
#
include
"
DesktopOptions.hpp
"
/*
This class is implemented using a message-only Win32 window with
a well-known window title. The message-only window can respond
to a "get main window handle" message, which will return the HWND
of the application's main window. It also responds to WM_COPYDATA
with OPENFILE as dwData, which causes openFileRequest(QString) to
be signaled with the copied data.
It also uses a lockfile (%TEMP%\rstudio.lock) to try to ensure two
copies of the app don't run concurrently even if something goes
wrong with the message-only window.
*/
namespace
rstudio
{
namespace
desktop
{
namespace
{
const
ULONG_PTR
OPENFILE
=
1
;
const
char
*
WINDOW_TITLE
=
"
RStudio_LaunchWindow_6dd82276-ccc3-4324-839e-4e6bcd5145bd
"
;
UINT
wmGetMainWindowHandle
()
{
static
UINT
msg =
0
;
if
(!msg)
msg = ::
RegisterWindowMessage
(
"
3dd567f8-7c07-4d9d-934b-dcb922cd737e
"
);
return
msg;
}
void
activate
(
HWND
hWnd)
{
HWND
hwndPopup = ::
GetLastActivePopup
(hWnd);
if
(::
IsWindow
(hwndPopup))
hWnd = hwndPopup;
::SetForegroundWindow
(hWnd);
if
(::
IsIconic
(hWnd))
::ShowWindow
(hWnd,
SW_RESTORE
);
}
}
//
anonymous namespace
ApplicationLaunch::ApplicationLaunch
() :
QWidget
(
nullptr
),
pMainWindow_
(
nullptr
)
{
setAttribute
(Qt::WA_NativeWindow);
setWindowTitle
(
QString::fromUtf8
(
WINDOW_TITLE
));
::SetParent
((
HWND
)winId(), HWND_MESSAGE);
}
void
ApplicationLaunch::init
(QString,
int
& argc,
char
* argv[],
boost::scoped_ptr<QApplication>* ppApp,
boost::scoped_ptr<ApplicationLaunch>* ppAppLaunch)
{
ppApp->
reset
(
new
QApplication
(argc, argv));
ppAppLaunch->
reset
(
new
ApplicationLaunch
());
}
void
ApplicationLaunch::setActivationWindow
(QWidget* pWindow)
{
pMainWindow_ = pWindow;
}
void
ApplicationLaunch::activateWindow
()
{
if
(pMainWindow_)
activate
((
HWND
) pMainWindow_->
winId
());
else
activate
((
HWND
)
winId
());
}
QString
ApplicationLaunch::startupOpenFileRequest
()
const
{
return
QString
();
}
namespace
{
bool
acquireLock
()
{
//
The file is implicitly released/deleted when the process exits
QString lockFilePath =
QDir::temp
().
absoluteFilePath
(
QString::fromUtf8
(
"
rstudio.lock
"
));
HANDLE
hFile = ::
CreateFileW
(lockFilePath.
toStdWString
().
c_str
(),
GENERIC_WRITE
,
0
,
//
exclusive access
nullptr
,
OPEN_ALWAYS
,
FILE_ATTRIBUTE_NORMAL
|
FILE_FLAG_DELETE_ON_CLOSE
,
nullptr
);
if
(hFile ==
INVALID_HANDLE_VALUE
)
{
if
(::
GetLastError
() ==
ERROR_SHARING_VIOLATION
)
return
false
;
}
return
true
;
}
}
//
anonymous namespace
void
ApplicationLaunch::attemptToRegisterPeer
()
{
acquireLock
();
}
bool
ApplicationLaunch::sendMessage
(QString filename)
{
if
(
acquireLock
())
return
false
;
HWND
hwndAppLaunch =
nullptr
;
do
{
hwndAppLaunch = ::
FindWindowEx
(
HWND_MESSAGE
, hwndAppLaunch,
nullptr
,
WINDOW_TITLE
);
}
while
(hwndAppLaunch == (
HWND
)
winId
());
//
Ignore ourselves
if
(::
IsWindow
(hwndAppLaunch))
{
HWND
hwnd =
reinterpret_cast
<
HWND
>(::
SendMessage
(hwndAppLaunch,
wmGetMainWindowHandle
(),
0
,
0
));
if
(::
IsWindow
(hwnd))
{
activate
(hwnd);
if
(!filename.
isEmpty
())
{
QByteArray data = filename.
toUtf8
();
COPYDATASTRUCT
copydata;
copydata.
dwData
=
OPENFILE
;
copydata.
lpData
= data.
data
();
copydata.
cbData
= data.
size
();
HWND
sender = (
HWND
)
winId
();
::SendMessage
(hwndAppLaunch,
WM_COPYDATA
,
reinterpret_cast
<
WPARAM
>(sender),
reinterpret_cast<LPARAM>(©data));
}
}
}
return
true
;
}
bool
ApplicationLaunch::nativeEvent
(
const
QByteArray & eventType,
void
* msg,
long
* result)
{
MSG
* message =
reinterpret_cast
<
MSG
*>(msg);
if
(message->
message
==
WM_COPYDATA
)
{
COPYDATASTRUCT
* cds =
reinterpret_cast
<
COPYDATASTRUCT
*>(message->
lParam
);
if
(cds->
dwData
==
OPENFILE
)
{
QString fileName =
QString::fromUtf8
(
reinterpret_cast
<
char
*>(cds->
lpData
),
cds->
cbData
);
openFileRequest
(fileName);
*result =
1
;
return
true
;
}
}
else
if
(message->
message
==
wmGetMainWindowHandle
())
{
if
(pMainWindow_)
*result =
reinterpret_cast
<
LRESULT
>((
HWND
)(pMainWindow_->
winId
()));
else
*result =
0
;
return
true
;
}
return
QWidget::nativeEvent
(eventType, message, result);
}
void
ApplicationLaunch::launchRStudio
(
const
std::vector<std::string>& args,
const
std::string& initialDir)
{
core::system::ProcessOptions options;
options.
breakawayFromJob
=
true
;
options.
detachProcess
=
true
;
//
supply initial dir to child process if specified
core::system::Options childEnv;
core::system::environment
(&childEnv);
if
(!initialDir.
empty
())
{
core::system::setenv
(&childEnv,
kRStudioInitialWorkingDir
, initialDir);
options.
environment
= childEnv;
}
core::Error error =
core::system::runProgram
(
desktop::options
().
executablePath
().
getAbsolutePath
(),
args,
"
"
,
options,
nullptr
);
if
(error)
LOG_ERROR
(error);
}
}
//
namespace desktop
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL