Debian Codegraph @

docs/source-code-overview.rst

raw
1 Source Code Overview
2 ====================
3
4 This page describes application processes and source code.
5
6 Applications, Frameworks and Libraries
7 --------------------------------------
8
9 The application is written in C++17 and uses Qt framework.
10
11 Source code can be build either with CMake.
12
13 Most icons in the application are taken from theme by default (which
14 currently works only on Linux) with fallback to built-in icons provided
15 by `FontAwesome <http://fontawesome.io/>`__.
16
17 The application logo and icons were created in `Inkscape
18 <https://inkscape.org/>`__ (icon source is in `src/images/icon.svg
19 <https://github.com/hluk/CopyQ/blob/master/src/images/icon.svg>`__).
20
21 Application Processes
22 ---------------------
23
24 There are these system processes related to CopyQ:
25
26 - Main GUI application
27 - Clipboard monitor - executes automatic clipboard commands
28 - Menu command filter - enables/hides custom menu items based on "Filter" field
29 in menu commands
30 - Display command - executes display commands as needed
31 - Clipboard and X11 selection owner and synchronization - provides clipboard
32 data; launched as needed
33 - Multiple clients - anything run by user from Action dialog or triggered as
34 menu, automatic or global-shortcut command
35
36 Main GUI Application
37 ~~~~~~~~~~~~~~~~~~~~
38
39 The main GUI application (or server) can be executed by running
40 ``copyq`` binary without attributes (session name can be optionally
41 specified on command line).
42
43 It creates local server allowing communication with clipboard monitor
44 process and other client processes.
45
46 Each user can run multiple main application processes each with unique
47 session name (default name is empty).
48
49 Clipboard Monitor
50 ~~~~~~~~~~~~~~~~~
51
52 Clipboard monitoring happens in separate process because otherwise it
53 would block GUI (in Qt clipboard needs to be accessed in main GUI
54 thread). The process is allowed to crash or loop indefinitely due to
55 bugs on some platforms.
56
57 Setting and retrieving clipboard can still happen in GUI thread (copying
58 and pasting in various GUI widgets) but it's preferred to send and
59 receive clipboard data using monitor process.
60
61 The monitor process is launched as soon as GUI application starts and is
62 restarted whenever it doesn't respond to keep-alive requests.
63
64 Clients and Scripting
65 ~~~~~~~~~~~~~~~~~~~~~
66
67 Scripting language is `Qt
68 Script <https://doc.qt.io/qt-6/qtqml-javascript-functionlist.html>`__ (mostly
69 same syntax and functions as JavaScript).
70
71 API is described in :ref:`scripting-api`.
72
73 A script can be started by passing arguments to ``copyq``.
74 For example: ``copyq "1+1"``
75
76 After script finishes, the server sends back output of last command and
77 exit code (non-zero if script crashes).
78
79 .. code-block:: bash
80
81 copyq eval 'read(0,1,2)' # prints first three items in list
82 copyq eval 'fail()' # exit code will be non-zero
83
84 While script is running, it can send print requests to client.
85
86 .. code-block:: bash
87
88 copyq eval 'print("Hello, "); print("World!\n")'
89
90 Scripts can ask for stdin from client.
91
92 .. code-block:: bash
93
94 copyq eval 'var client_stdin = input()'
95
96 The script run in current directory of client process.
97
98 .. code-block:: bash
99
100 copyq eval 'Dir().absolutePath()'
101 copyq eval 'execute("ls", "-l").stdout'
102
103 Single function call where all arguments are numbers or strings can be
104 executed by passing function name and function arguments on command
105 line. Following commands are equal.
106
107 .. code-block:: bash
108
109 copyq eval 'copy("Hello, World!")'
110 copyq copy "Hello, World!"
111
112 Getting application version or help mustn't require the server to be
113 running.
114
115 .. code-block:: bash
116
117 copyq help
118 copyq version
119
120 Scripts run in separate thread and communicate with main thread by
121 calling methods on an object of ``ScriptableProxy`` class. If called
122 from non-main thread, these methods invoke a slot on an ``QObject`` in
123 main thread and pass it a function object which simply calls the method
124 again.
125
126 .. code-block:: cpp
127
128 bool ScriptableProxy::loadTab(const QString &tabName)
129 {
130 // This section is wrapped in an macro so to remove duplicate code.
131 if (!m_inMainThread) {
132 // Callable object just wraps the lambda so it's possible to send it to a slot.
133 auto callable = createCallable([&]{ return loadTab(tabName); });
134
135 m_inMainThread = true;
136 QMetaObject::invokeMethod(m_wnd, "invoke", Qt::BlockingQueuedConnection, Q_ARG(Callable*, &callable));
137 m_inMainThread = false;
138
139 return callable.result();
140 }
141
142 // Now it's possible to call method on an object in main thread.
143 return m_wnd->loadTab(tabName);
144 }
145
146 Platform-dependent Code
147 -----------------------
148
149 Code for various platforms is stored in
150 `src/platform <https://github.com/hluk/CopyQ/tree/master/src/platform>`__.
151
152 This leverages amount of ``#if``\ s and similar preprocessor directives
153 in common code.
154
155 Each supported platform implements
156 `PlatformNativeInterface <https://github.com/hluk/CopyQ/blob/master/src/platform/platformnativeinterface.h>`__
157 and ``platformNativeInterface()``.
158
159 The implementations can contain:
160
161 - Creating Qt application objects
162 - Clipboard handling (for clipboard monitor)
163 - Focusing window and getting window titles
164 - Getting system paths
165 - Setting "autostart" option
166 - Handling global shortcuts (**note:** this part is in
167 `qxt/ <https://github.com/hluk/CopyQ/tree/master/qxt>`__)
168
169 For unsupported platforms there is `simple
170 implementation <https://github.com/hluk/CopyQ/tree/master/src/platform/dummy>`__
171 to get started.
172
173 Plugins
174 -------
175
176 Plugins are built as dynamic libraries which are loaded from runtime
177 plugin directory (platform-dependent) after application start.
178
179 Code is stored in
180 `plugins <https://github.com/hluk/CopyQ/tree/master/plugins>`__.
181
182 Plugins implement interfaces from
183 `src/item/itemwidget.h <https://github.com/hluk/CopyQ/tree/master/src/item/itemwidget.h>`__.
184
185 To create new plugin just duplicate and rewrite an existing plugin. You
186 can build the plugin with ``make {PLUGIN_NAME}``.
187
188 Continuous Integration (CI)
189 ---------------------------
190
191 The application binaries and packages are built and tested on multiple
192 CI servers.
193
194 - `GitHub Actions <https://github.com/hluk/CopyQ/actions>`__
195 - Builds packages for OS X.
196 - Builds and runs tests for Linux binaries.
197
198 - `GitLab CI <https://gitlab.com/CopyQ/CopyQ/builds>`__
199 - Builds and runs tests for Ubuntu 22.04 binaries.
200 - Screenshots are taken while GUI tests are running. These are
201 available if a test fails.
202
203 - `GitHub Actions <https://github.com/hluk/CopyQ/actions>`__
204 - Builds and runs tests for Windows.
205 - Provides downloads for recent commits.
206
207 - `OBS Linux Packages <https://build.opensuse.org/project/show/home:lukho:copyq>`__
208 - Builds release packages for various Linux distributions.
209
210 - `Beta OBS Linux Packages <https://build.opensuse.org/project/show/home:lukho:copyq-beta>`__
211 - Builds beta and unstable packages for various Linux distributions.
212
213 - `Codecov <https://app.codecov.io/gh/hluk/CopyQ>`__
214 - Contains coverage report from tests run with GitHub Actions.