├── .gitignore
├── .vscode
├── launch.json
└── tasks.json
├── LICENSE
├── README.md
├── clocker.conf
├── common
├── defs
│ ├── basic_defs.h
│ ├── color_defs.h
│ ├── gen.h
│ ├── inc.h
│ ├── oprs.h
│ ├── opt.h
│ ├── program_defs.h
│ ├── result.h
│ └── type_defs.h
└── domain
│ ├── domain.h
│ └── inc.h
├── deploy
├── clocker
│ ├── clocker
│ └── clocker.c
└── updater
│ ├── updater
│ └── updater.c
├── framework
├── prims
│ ├── assert.h
│ ├── debug.h
│ └── inc.h
└── system
│ ├── fs
│ ├── inc.h
│ └── wrapper.c
│ ├── memory
│ ├── boxed.c
│ └── inc.h
│ └── thread
│ ├── guarded.c
│ ├── inc.h
│ └── thread.c
├── includes
├── cmd.h
├── data_manager.h
├── listener.h
├── sig_handler.h
├── thread_arg.h
├── time_handler.h
├── timer.h
└── updater_checker.h
├── makefile
└── source
├── cmd.c
├── data_manager.c
├── listener.c
├── sig_handler.c
├── time_handler.c
├── timer.c
└── update_checker.c
/.gitignore:
--------------------------------------------------------------------------------
1 | .vscode
2 | final
3 | build
4 | # Prerequisites
5 | *.d
6 |
7 | # Object files
8 | *.o
9 | *.ko
10 | *.obj
11 | *.elf
12 |
13 | # Linker output
14 | *.ilk
15 | *.map
16 | *.exp
17 |
18 | # Precompiled Headers
19 | *.gch
20 | *.pch
21 |
22 | # Libraries
23 | *.lib
24 | *.a
25 | *.la
26 | *.lo
27 |
28 | # Shared objects (inc. Windows DLLs)
29 | *.dll
30 | *.so
31 | *.so.*
32 | *.dylib
33 |
34 | # Executables
35 | *.exe
36 | *.out
37 | *.app
38 | *.i*86
39 | *.x86_64
40 | *.hex
41 |
42 | # Debug files
43 | *.dSYM/
44 | *.su
45 | *.idb
46 | *.pdb
47 |
48 | # Kernel Module Compile Results
49 | *.mod*
50 | *.cmd
51 | .tmp_versions/
52 | modules.order
53 | Module.symvers
54 | Mkfile.old
55 | dkms.conf
56 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "name": "gcc-11 - Build and debug active file",
9 | "type": "cppdbg",
10 | "request": "launch",
11 | "program": "${fileDirname}/${fileBasenameNoExtension}",
12 | "args": [
13 | "--update"
14 | ],
15 | "stopAtEntry": false,
16 | "cwd": "${fileDirname}",
17 | "environment": [],
18 | "externalConsole": false,
19 | "MIMode": "gdb",
20 | "setupCommands": [
21 | {
22 | "description": "Enable pretty-printing for gdb",
23 | "text": "-enable-pretty-printing",
24 | "ignoreFailures": true
25 | }
26 | ],
27 | "preLaunchTask": "C/C++: gcc-11 build active file",
28 | "miDebuggerPath": "/usr/bin/gdb"
29 | }
30 | ]
31 | }
--------------------------------------------------------------------------------
/.vscode/tasks.json:
--------------------------------------------------------------------------------
1 | {
2 | "tasks": [
3 | {
4 | "type": "cppbuild",
5 | "label": "C/C++: gcc-11 build active file",
6 | "command": "/usr/bin/gcc-11",
7 | "args": [
8 | "-fdiagnostics-color=always",
9 | "-I${workspaceFolder}",
10 | "-DDEBUG",
11 | "-g",
12 | // "${workspaceFolder}/framework/data_structure/tree.c",
13 | // "${workspaceFolder}/framework/data_structure/btree.c",
14 | // "${workspaceFolder}/framework/data/serde.c",
15 | "${workspaceFolder}/framework/system/thread/guarded.c",
16 | "${workspaceFolder}/framework/system/thread/thread.c",
17 | "${workspaceFolder}/framework/system/fs/wrapper.c",
18 | "${workspaceFolder}/framework/system/memory/boxed.c",
19 | "${workspaceFolder}/source/listener.c",
20 | "${workspaceFolder}/source/timer.c",
21 | "${workspaceFolder}/source/time_handler.c",
22 | "${workspaceFolder}/source/update_checker.c",
23 | "${workspaceFolder}/source/sig_handler.c",
24 | "${workspaceFolder}/source/cmd.c",
25 | "${workspaceFolder}/source/data_manager.c",
26 |
27 | "${file}",
28 | "-o",
29 | "${fileDirname}/${fileBasenameNoExtension}"
30 | ],
31 | "options": {
32 | "cwd": "${fileDirname}"
33 | },
34 | "problemMatcher": [
35 | "$gcc"
36 | ],
37 | "group": {
38 | "kind": "build",
39 | "isDefault": true
40 | },
41 | "detail": "Task generated by Debugger."
42 | }
43 | ],
44 | "version": "2.0.0"
45 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
19 |
20 | 
21 | [](https://www.codefactor.io/repository/github/rdwnsjjd/clocker)
22 |
23 | 
24 |
25 |
26 | ### Clocker
27 | #### A useful time calculator!
28 | This app is created by me for my own usage. but you also if have any need or was useful, can use it.
29 |
30 | This is a useful time calculator. this app calculates your on-work time and gives a simple report of your time spent.
31 | this could be used for everyone who wants to know how much time spends on his work, parents for controlling their child PC time usage, and employees who want to report true time of their work and others.
32 |
33 | ***but stop and NOTE: this is a pre-release, unstable, and uncompleted version of this app!***
34 | but I try to complete it because of my self-usage!
35 |
36 | #### How it works?
37 | It simply tracks your mouse and keyboard usage on your system by reading `/dev/input/by_path` input files.
38 | When they become idle for 2 min, the app considers your time as "waste time" and as you come back it will continue normally.
39 |
40 | #### How to use it?
41 | You can directly use the `/build/clocker` binary file by copying it to the `/usr/bin` system directory.
42 | or make source easily by running `make && make install` in the root directory of the project (recommended).
43 | it will copy `clocker` and `clocker-updater` to `/usr/bin`.
44 | for run clocker run:
45 | ```
46 | $ sudo clocker
47 | ```
48 |
49 | for updating application run:
50 | ```
51 | $ sudo clocker --update
52 | ```
53 |
54 |
55 | #### Modes
56 | You can use several modes as your need. there are three modes: `busy`, `paused` and `default`.
57 |
58 | You are starting in default mode and it is the normal behavior of the app. Clocker will calculate your useful time when you're using your system, and otherwise, going to idle.
59 |
60 | When you're choosing `busy` mode, probably you are doing your work, but not just with your system. in other words, you are not using your system while you want to your time be considered useful time.
61 |
62 | Unlike that, when you use your system, but not in your main job, you can `pause` the Clocker and do your stuff, and then back and `Resume` it.
63 |
64 | #### What will be added
65 | This app created because of need, So whenever the need arises, a new feature will be added. for now I want to add two more features,
66 | - [x] data saver
67 | - [ ] data exporter
68 |
69 | if you find this app useful and feel maybe some new features is needed, please create an issue, I welcome any idea or suggestion!
70 |
71 |
72 | also If you face any problem, unknown behavior or some other bugs, please open an issue or email me.
73 | rdwnsjjd@gmail.com
74 |
--------------------------------------------------------------------------------
/clocker.conf:
--------------------------------------------------------------------------------
1 | 0.2-beta.0
--------------------------------------------------------------------------------
/common/defs/basic_defs.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Owl.
4 | //
5 | // Owl is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Owl is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Owl. If not, see .
17 |
18 | #ifndef _INC_DEFS_BASIC_DEFS_H_
19 | #define _INC_DEFS_BASIC_DEFS_H_
20 |
21 |
22 | #ifndef __unix__
23 | #error "sorry! this project is just for unix-like machines use!"
24 | #endif
25 |
26 | #define loop while(1)
27 | #define STATIC static
28 | #define INLINE static inline
29 |
30 | #define IN
31 | #define OUT
32 |
33 | #define MUT
34 | #define PUB
35 | #define _UNSAFE
36 | #define UNSAFE(a, ...) a ##__VA_ARGS__
37 |
38 | #define NO_RET __attribute__((noreturn))
39 |
40 |
41 | #define DEFAULT_STACK_SIZE 64
42 | #define DEFAULT_TREE_CHILD 64
43 |
44 |
45 | #endif // _INC_DEFS_BASIC_DEFS_H_
46 |
--------------------------------------------------------------------------------
/common/defs/color_defs.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Owl.
4 | //
5 | // Owl is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Owl is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Owl. If not, see .
17 |
18 | #ifndef _INC_DEFS_COLOR_DEFS_H_
19 | #define _INC_DEFS_COLOR_DEFS_H_
20 |
21 | #define COLOR_WHITE "\x1b[0m"
22 | #define COLOR_BOLD "\x1b[1m"
23 | #define COLOR_TRANSPARENT "\x1b[2m"
24 | #define COLOR_ITALIC "\x1b[3m"
25 | #define COLOR_UNDERLINE "\x1b[4m"
26 | #define COLOR_BLIND "\x1b[5m"
27 |
28 | #define COLOR_IN_RED "\x1b[41m"
29 | #define COLOR_IN_GREEN "\x1b[41m"
30 | #define COLOR_IN_YELLOW "\x1b[43m"
31 | #define COLOR_IN_BLUE "\x1b[44m"
32 |
33 | #define COLOR_BLACK "\x1b[30m"
34 | #define COLOR_RED "\x1b[31m"
35 | #define COLOR_GREEN "\x1b[32m"
36 | #define COLOR_YELLOW "\x1b[33m"
37 | #define COLOR_BLUE "\x1b[34m"
38 | #define COLOR_MAGENTA "\x1b[35m"
39 | #define COLOR_CYAN "\x1b[36m"
40 | #define COLOR_RESET "\x1b[0m"
41 |
42 | #define COLOR_B_RED "\x1b[91m"
43 | #define COLOR_B_GREEN "\x1b[92m"
44 | #define COLOR_B_YELLOW "\x1b[93m"
45 | #define COLOR_B_BLUE "\x1b[94m"
46 | #define COLOR_B_CYAN "\x1b[96m"
47 |
48 | #define ERR_TXT(txt_) COLOR_RED txt_ COLOR_RESET
49 | #define WRN_TXT(txt_) COLOR_YELLOW txt_ COLOR_RESET
50 | #define OK_TXT(txt_) COLOR_GREEN txt_ COLOR_RESET
51 | #define INF_TXT(txt_) COLOR_B_CYAN txt_ COLOR_RESET
52 |
53 | #define DNG_TXT(txt_) COLOR_B_RED txt_ COLOR_RESET
54 | #define ATT_TXT(txt_) COLOR_B_YELLOW txt_ COLOR_RESET
55 |
56 | #define TRANSPARENT_TXT(txt_) COLOR_TRANSPARENT txt_ COLOR_RESET
57 | #define BOLD_TXT(txt_) COLOR_BOLD txt_ COLOR_RESET
58 | #define UNDER_TXT(txt_) COLOR_UNDERLINE txt_ COLOR_RESET
59 | #define ITALIC_TXT(txt_) COLOR_ITALIC txt_ COLOR_RESET
60 |
61 | #endif // _INC_DEFS_COLOR_DEFS_H_
62 |
--------------------------------------------------------------------------------
/common/defs/gen.h:
--------------------------------------------------------------------------------
1 | #ifndef _INC_DEFS_GEN_H_
2 | #define _INC_DEFS_GEN_H_
3 |
4 | #include "type_defs.h"
5 | #include "framework/prims/debug.h"
6 |
7 | typedef union {
8 | char_t i8;
9 | u8_t u8;
10 | i16_t i16;
11 | u16_t u16;
12 | i32_t i32;
13 | u32_t u32;
14 | i64_t i64;
15 | u64_t u64;
16 | f32_t f32;
17 | f64_t d64;
18 | i128_t d128;
19 | size_t size;
20 | ssize_t ssize;
21 | handle_t ptr;
22 | str_t str;
23 | bytes_t pi8;
24 | i32_t* pi32;
25 | f32_t* pf32;
26 | f64_t* pd64;
27 | }
28 | gen_t;
29 |
30 | #define G_NONE (gen_t) 0
31 | #define _(data_) (gen_t) data_
32 |
33 | #endif // _INC_DEFS_GEN_H_
34 |
--------------------------------------------------------------------------------
/common/defs/inc.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Owl.
4 | //
5 | // Owl is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Owl is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Owl. If not, see .
17 |
18 | #ifndef _COMMON_DEFS_INC_H_
19 | #define _COMMON_DEFS_INC_H_
20 |
21 | #include "basic_defs.h"
22 | #include "type_defs.h"
23 |
24 | // #ifdef OPR_DEFS
25 | #include "oprs.h"
26 | // #endif
27 |
28 | // #ifdef GEN_TYPE
29 | #include "gen.h"
30 | // #endif
31 |
32 | // #ifdef RES_DEFS
33 | #include "result.h"
34 | // #endif
35 |
36 | // #ifdef OPT_TYPE
37 | #include "opt.h"
38 | // #endif
39 |
40 | #endif // _COMMON_DEFS_INC_H_
41 |
--------------------------------------------------------------------------------
/common/defs/oprs.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Owl.
4 | //
5 | // Owl is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Owl is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Owl. If not, see .
17 |
18 | #ifndef _INC_DEFS_OPRS_H_
19 | #define _INC_DEFS_OPRS_H_
20 |
21 |
22 | #define AND &&
23 | #define OR ||
24 | #define NOT !
25 | #define IS_EQ ==
26 | #define IS_NOT_EQ !=
27 |
28 | #define IS_BIGGER_OR_EQ >=
29 | #define IS_SMALLER_OR_EQ <=
30 | #define IS_BIGGER_THAN >
31 | #define IS_SMALLER_THAN <
32 |
33 | #define B_AND &
34 | #define B_AND_EQ &=
35 | #define B_OR |
36 | #define B_OR_EQ |=
37 | #define XOR ^
38 | #define XOR_EQ ^=
39 | #define CMPL ~
40 |
41 |
42 | #endif // _INC_DEFS_OPRS_H_
43 |
--------------------------------------------------------------------------------
/common/defs/opt.h:
--------------------------------------------------------------------------------
1 | #ifndef _INC_DEFS_OPT_H_
2 | #define _INC_DEFS_OPT_H_
3 |
4 | #include "type_defs.h"
5 | #include "gen.h"
6 | // #include "res.h"
7 |
8 | typedef struct {
9 | gen_t some;
10 | bool_t is_none;
11 | }
12 | opt_t;
13 |
14 | #define opt_is_some(opt_) (!(opt_.is_none))
15 | #define opt_is_none(opt_) (opt_.is_none)
16 | #define some(data_) (opt_t) {.some = (gen_t) data_, .is_none = B_False}
17 | #define None (opt_t) {.some = (gen_t) 0, .is_none = B_True}
18 | #define get_some(opt_) (opt_).some
19 |
20 | // #define match_opt(opt_, if_matches_, else_) if (opt_is_some(opt_)) {gen_t data = opt_.some; if_matches_} else {else_}
21 |
22 | #endif // _INC_DEFS_OPT_H_
--------------------------------------------------------------------------------
/common/defs/program_defs.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2021 root
2 | //
3 | // This file is part of Clocker.
4 | //
5 | // Clocker is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Clocker is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Clocker. If not, see .
17 |
18 | #ifndef _COMMON_DEFS_PROGRAM_DEFS_H_
19 | #define _COMMON_DEFS_PROGRAM_DEFS_H_
20 |
21 | #include "common/defs/inc.h"
22 |
23 | #define INIT_VERSION "0.5-beta.0"
24 |
25 | #define CLOCKER_BANNER COLOR_TRANSPARENT"\n"\
26 | " █████████ ████ █████ (beta)\n" \
27 | " ███░░░░░███░░███ ░░███\n" \
28 | " ███ ░░░ ░███ ██████ ██████ ░███ █████ ██████ ████████\n" \
29 | "░███"" ░███ ███░░███ ███░░███ ░███░░███ ███░░███░░███░░███\n" \
30 | "░███"" ░███ ░███ ░███░███ ░░░ ░██████░ ░███████ ░███ ░░░\n" \
31 | "░░███ ███ ░███ ░███ ░███░███ ███ ░███░░███ ░███░░░ ░███\n" \
32 | " ░░█████████ █████░░██████ ░░██████ ████ █████░░██████ █████\n" \
33 | " ░░░░░░░░░ ░░░░░ ░░░░░░ ░░░░░░ ░░░░ ░░░░░ ░░░░░░ ░░░░░" \
34 | // COLOR_TRANSPARENT" (In memory of the Palestinian people...)\n"COLOR_RESET
35 |
36 |
37 | #define ERR_TXT(txt_) COLOR_RED txt_ COLOR_RESET
38 | #define WRN_TXT(txt_) COLOR_YELLOW txt_ COLOR_RESET
39 | #define OK_TXT(txt_) COLOR_GREEN txt_ COLOR_RESET
40 | #define INF_TXT(txt_) COLOR_B_CYAN txt_ COLOR_RESET
41 |
42 | #define DNG_TXT(txt_) COLOR_B_RED txt_ COLOR_RESET
43 | #define ATT_TXT(txt_) COLOR_B_YELLOW txt_ COLOR_RESET
44 |
45 | #define TRANSPARENT_TXT(txt_) COLOR_TRANSPARENT txt_ COLOR_RESET
46 | #define BOLD_TXT(txt_) COLOR_BOLD txt_ COLOR_RESET
47 | #define ITALIC_TXT(txt_) COLOR_ITALIC txt_ COLOR_RESET
48 |
49 | #endif // _COMMON_DEFS_PROGRAM_DEFS_H_
50 |
--------------------------------------------------------------------------------
/common/defs/result.h:
--------------------------------------------------------------------------------
1 | #ifndef __TYPES_INC_RESULT_H
2 | #define __TYPES_INC_RESULT_H
3 |
4 | #include
5 | // #include
6 |
7 | #include "type_defs.h"
8 | #include "gen.h"
9 |
10 |
11 | typedef size_t err_num_t;
12 |
13 | typedef enum {
14 | ek_gen = 0,
15 | ek_unexpected_eof,
16 | ek_unexpected_error,
17 | ek_perm,
18 | ek_not_found,
19 | ek_access,
20 | ek_busy,
21 | ek_exist,
22 | ek_notdir,
23 | ek_inval,
24 | ek_stck,
25 | ek_openf,
26 | ek_closef,
27 | ek_createf,
28 | ek_connectf,
29 | ek_bindf,
30 | ek_insrtf,
31 | ek_appndf,
32 | ek_pushf,
33 | ek_popf,
34 | ek_allcf,
35 | ek_not_impl,
36 | }
37 | err_kind_t;
38 |
39 |
40 | typedef struct {
41 | err_kind_t e_kind;
42 | err_num_t e_num;
43 | }
44 | err_t;
45 |
46 |
47 | typedef struct {
48 | gen_t ok;
49 | err_t err;
50 | bool_t is_ok;
51 | }
52 | result_t;
53 |
54 |
55 | #define GEN_ERR_CODE 0
56 | #define is_ok(res_) (res_.is_ok)
57 | #define res_get_data(res_) ((res_.is_ok) ? (res_.ok) : (G_NONE))
58 | #define res_get_error(res_) (res_.err)
59 | #define ok(data_) (result_t) {.ok = (gen_t)data_, .err = {0}, .is_ok = B_True}
60 | #define err(err_kind_) (result_t) {.ok = (gen_t)0 , .err = {.e_kind = err_kind_, .e_num = GEN_ERR_CODE}, .is_ok = B_False}
61 |
62 | #define unwrap(data_, res_) if(is_ok(res_)) data_ = res_.ok; else {debug_err("The program paniced because of (%s)!", e_print(res_.err)); exit(-1);}
63 |
64 | #define match_res(res_, if_matches_, else_) (res_is_ok(res_)) ? {gen_t data = res_.ok; if_matches_} : {else_}
65 |
66 |
67 | static inline str_t e_print(err_t err) {
68 | switch (err.e_kind) {
69 | case ek_gen:
70 | return "General Error";
71 | break;
72 | case ek_unexpected_eof:
73 | return "Unexpected End Of File Error";
74 | break;
75 | case ek_perm:
76 | return "Permision Error";
77 | break;
78 | case ek_unexpected_error:
79 | return "Unexpected Error";
80 | break;
81 | case ek_access:
82 | return "Access Error";
83 | break;
84 | case ek_busy:
85 | return "Resource Is Busy Error";
86 | break;
87 | case ek_exist:
88 | return "Already Exists Error";
89 | break;
90 | case ek_notdir:
91 | return "Not A Directory Error";
92 | break;
93 | case ek_inval:
94 | return "Invalid Data Error";
95 | break;
96 | case ek_openf:
97 | return "Opening Failure";
98 | break;
99 | case ek_closef:
100 | return "Closing Failure";
101 | break;
102 | case ek_createf:
103 | return "Creation Failure";
104 | break;
105 | case ek_connectf:
106 | return "Connection Failure";
107 | break;
108 | case ek_bindf:
109 | return "Binding Failure";
110 | break;
111 | case ek_insrtf:
112 | return "Insertion Failure";
113 | break;
114 | case ek_appndf:
115 | return "Appending Failure";
116 | break;
117 | case ek_pushf:
118 | return "Pushing Failure";
119 | break;
120 | case ek_popf:
121 | return "Poping Failure";
122 | break;
123 | case ek_stck:
124 | return "Stack Error";
125 | break;
126 | case ek_allcf:
127 | return "Allocation Failure";
128 | break;
129 | case ek_not_impl:
130 | return "Not Implemented";
131 | break;
132 |
133 | default:
134 | return "Unknown Error";
135 | break;
136 | }
137 | }
138 |
139 |
140 | #endif // __TYPES_INC_RESULT_H
--------------------------------------------------------------------------------
/common/defs/type_defs.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Owl.
4 | //
5 | // Owl is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Owl is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Owl. If not, see .
17 |
18 | #ifndef _INC_DEFS_TYPE_DEFS_H_
19 | #define _INC_DEFS_TYPE_DEFS_H_
20 |
21 |
22 | #include
23 |
24 |
25 | typedef unsigned char u8_t;
26 | typedef unsigned short int u16_t;
27 | typedef unsigned int u32_t;
28 | typedef unsigned long int u64_t;
29 |
30 | // typedef char int8_t;
31 | typedef short int i16_t;
32 | typedef int i32_t;
33 | typedef long int i64_t;
34 | typedef __int128_t i128_t;
35 |
36 | typedef float f32_t;
37 | typedef double f64_t;
38 |
39 | typedef enum {B_False, B_True} bool_t;
40 |
41 | typedef char char_t;
42 | typedef char byte_t;
43 | typedef char* str_t;
44 | typedef char* bytes_t;
45 |
46 | typedef void void_t;
47 | typedef void* handle_t;
48 | typedef void* pointer_t;
49 |
50 | typedef long int ssize_t;
51 | typedef unsigned long int size_t;
52 | // typedef unsigned long int id_t;
53 | typedef unsigned long int index_t;
54 |
55 | // #define INVALID_INDEX (id_t)0
56 | #define INVALID_INDEX (index_t) 0
57 | #define INVALID_HNDL (handle_t)0
58 |
59 | #define INF_U8 (u8_t) -1
60 | #define INF_U16 (u16_t) -1
61 | #define INF_U32 (u32_t) -1
62 | #define INF_U64 (u64_t) -1
63 |
64 | #endif // _INC_TYPE_DEFS_H_
65 |
--------------------------------------------------------------------------------
/common/domain/domain.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Owl.
4 | //
5 | // Owl is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Owl is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Owl. If not, see .
17 |
18 | #ifndef _COMMON_DOMAIN_DOMAIN_H_
19 | #define _COMMON_DOMAIN_DOMAIN_H_
20 |
21 |
22 | typedef enum {
23 | d_common_defs,
24 | d_framework_prims,
25 | d_framework_storage,
26 | d_framework_system_io,
27 | d_framework_system_thread,
28 | d_platform_core,
29 | }
30 | domain_t;
31 |
32 |
33 | #endif // _COMMON_DOMAIN_DOMAIN_H_
34 |
--------------------------------------------------------------------------------
/common/domain/inc.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2022 Redwan
3 | *
4 | * This file is part of Owl.
5 | *
6 | * Owl is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Owl is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Owl. If not, see .
18 | */
19 |
20 | #ifndef _COMMON_DOMAIN_INC_H_
21 | #define _COMMON_DOMAIN_INC_H_
22 |
23 | #include "domain.h"
24 |
25 |
26 | #endif // _COMMON_DOMAIN_INC_H_
27 |
--------------------------------------------------------------------------------
/deploy/clocker/clocker:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rdwnsjjd/Clocker/4b797244eef7cf28d2ab1ae30fbaded50d36cf8d/deploy/clocker/clocker
--------------------------------------------------------------------------------
/deploy/clocker/clocker.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 |
25 | #include "common/defs/program_defs.h"
26 | #include "common/defs/basic_defs.h"
27 | #include "framework/system/thread/inc.h"
28 | #include "includes/thread_arg.h"
29 | #include "includes/time_handler.h"
30 | #include "includes/updater_checker.h"
31 | #include "includes/sig_handler.h"
32 | #include "includes/data_manager.h"
33 | #include "includes/cmd.h"
34 |
35 |
36 | void_t clocker_update_checking(updater_t* checker) {
37 |
38 | // check for update
39 | update_status_t update_check_res = update_checker_check(checker);
40 | soft_assert_wrn(
41 | update_check_res != US_Error,
42 | "Checking for new version failed!"
43 | );
44 |
45 | // if update is available, prepare it
46 | if (update_check_res == US_Updatable) {
47 |
48 | // first, ask user if he/she wants to update app?
49 | printf("Version %s is available!\nyou have version %s!\nDo you want to install it? [Y/n]",
50 | update_checker_get_new_tag(checker),
51 | update_checker_get_version(checker)
52 | );
53 |
54 | // getting user response to above question
55 | char_t result = fgetc(stdin);
56 | if (result == '\n' || result == 'Y' || result == 'y') {
57 |
58 | // if YES, do update
59 | str_t arg[3] = {"/usr/bin/clocker-updater", "--from-clocker", INVALID_HNDL};
60 | execvp("/usr/bin/clocker-updater", arg);
61 | perror("");
62 | }
63 | else {
64 | // if NO, abort updating process
65 | printf("Aborted!\n\n");
66 | }
67 | }
68 |
69 | // if update is not available, tell it to the user
70 | else if (update_check_res == US_NoUpdate) {
71 | printf("All is up to date!\n");
72 | }
73 | }
74 |
75 |
76 | result_t clear_cache() {
77 | printf(INF_TXT("\n Cleaning cash ..."));
78 | soft_assert_ret_res(
79 | remove("/root/.config/clocker/lock") == 0,
80 | "Removing lock file failed: (%s)!",
81 | strerror(errno)
82 | );
83 | // soft_assert_ret_res(
84 | // remove("/root/.config/clocker/data") == 0,
85 | // "Removing data file failed: (%s)!",
86 | // strerror(errno)
87 | // );
88 | printf(INF_TXT("\n Done!\n\n"));
89 | }
90 |
91 |
92 | i32_t main(
93 | i32_t argc,
94 | str_t argv[]
95 | ) {
96 |
97 | printf(BOLD_TXT("\n\n IMPORTANT: ")
98 | "The Clocker updater had some important changes.\n"\
99 | " Unfortunately, the updater cannot update itself\n"\
100 | " So if you want to have the last version of updater,\n"\
101 | " please get it of clocker offical repository: " UNDER_TXT(ITALIC_TXT("https://github.com/rdwnsjjd/Clocker\n\n"))
102 | );
103 |
104 | // no_save flag determine if user wants to save its data on system for later use or not
105 | bool_t no_save = argv[1] && strcmp(argv[1], "--no-save") == 0 ? B_True : B_False;
106 |
107 | // if `--clean` flag used, clear the cash and lock file
108 | if (argv[1] && strcmp(argv[1], "--clean") == 0) {
109 | soft_assert_res_ret_int(clear_cache(), "Failed to clear cache!");
110 | }
111 |
112 | // create new update checker object and check if it sucessfully created
113 | updater_t checker = update_checker_new();
114 | soft_assert_wrn(boxed_unbox(&checker.inner) != 0, "Creating new update object failed!");
115 |
116 | // if `--update` flag is used, check for update and do it if availabe
117 | if (argv[1] && strcmp(argv[1], "--update") == 0) {
118 | clocker_update_checking(&checker);
119 | }
120 |
121 | // start data manager and check if it fully started
122 | data_manager_t manager = data_manager_start(no_save);
123 | soft_assert_ret_int(boxed_unbox(&manager.inner) != INVALID_HNDL, "Failed to starting data manager!");
124 |
125 | // handle `SIGINT` and `SIGSEGV` signals
126 | signal(SIGINT, signal_handler);
127 | signal(SIGSEGV, signal_handler);
128 |
129 | // print app banner and some helpful messages
130 | printf("%s\n\n", CLOCKER_BANNER);
131 | printf("Type "ITALIC_TXT("`help`")" to list supported commands\n");
132 |
133 | // creating new thread argument for sending to `time_handle` thread
134 | thread_arg_t thread_arg = thread_arg_new(TC_None, TM_Default, update_checker_get_version(&checker), manager);
135 | guarded_t thread_arg_guarded = guarded_new(_((handle_t) &thread_arg));
136 |
137 | // spawing time handling thread
138 | thread_t timer_thread = thread_new();
139 | thread_spawn(&timer_thread, &time_handle, &thread_arg_guarded);
140 |
141 | // spawing command handling thread
142 | thread_t cmd_thread = thread_new();
143 | thread_spawn(&cmd_thread, &cmd_run, &thread_arg_guarded);
144 |
145 | // waiting for time thread to join
146 | thread_wait(timer_thread);
147 |
148 | // destroy another thread
149 | thread_destroy(cmd_thread);
150 |
151 | // destroy update checker after `timer_thread` joined
152 | update_checker_destroy(checker);
153 | }
154 |
--------------------------------------------------------------------------------
/deploy/updater/updater:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rdwnsjjd/Clocker/4b797244eef7cf28d2ab1ae30fbaded50d36cf8d/deploy/updater/updater
--------------------------------------------------------------------------------
/deploy/updater/updater.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | #include "common/defs/inc.h"
28 | #include "includes/updater_checker.h"
29 |
30 | typedef struct passwd passwd_t;
31 | typedef struct stat stat_t;
32 | typedef struct dirent dirnet_t;
33 |
34 | typedef struct {
35 | byte_t version[32];
36 | byte_t new_version[32];
37 | }
38 | updater_inner_t;
39 |
40 |
41 | str_t updater_get_conf_file(str_t buff) {
42 |
43 | passwd_t* home_dir = getpwuid(getuid());
44 | sprintf(buff, "%s/%s", home_dir->pw_dir, ".config/clocker/clocker.conf");
45 | }
46 |
47 |
48 | bool_t updater_do_update(updater_t* updater) {
49 |
50 | updater_inner_t* _updater = (updater_inner_t*) boxed_unbox(&updater->inner);
51 |
52 | printf("Updating app...\n");
53 |
54 | char_t cmd_buff[2048] = {0};
55 | sprintf(
56 | cmd_buff, "wget %s/v%s -P /root/.config/clocker/.new --quiet",
57 | "https://api.github.com/repos/rdwnsjjd/Clocker/zipball/refs/tags",
58 | _updater->new_version
59 | );
60 |
61 | soft_assert_goto_return(
62 | system(cmd_buff) == 0,
63 | "Cannot get available version of clocker!"
64 | );
65 |
66 | memset(cmd_buff, 0, 2048);
67 | sprintf(
68 | cmd_buff, "unzip -q /root/.config/clocker/.new/v%s -d /root/.config/clocker/.new/source",
69 | _updater->new_version
70 | );
71 |
72 | soft_assert_goto_return(
73 | system(cmd_buff) == 0,
74 | "Cannot extract source file!"
75 | );
76 |
77 | char_t path_buff[2048] = {0};
78 | sprintf(
79 | path_buff, "/root/.config/clocker/.new/v%s",
80 | _updater->new_version
81 | );
82 |
83 | soft_assert_goto_return(
84 | remove(path_buff) == 0,
85 | "Cannot remove source zip file!"
86 | );
87 |
88 | dirnet_t* dir_net = {0};
89 | DIR* dir = opendir ("/root/.config/clocker/.new/source");
90 | soft_assert_goto_return(
91 | dir != INVALID_HNDL,
92 | "Opening source directory failed! (%s)",
93 | strerror(errno)
94 | );
95 |
96 | while (dir_net = readdir(dir)) {
97 |
98 | if (*(dir_net->d_name) != '.') {
99 |
100 | memset(path_buff, 0, 2048);
101 | getcwd(path_buff, 2048);
102 | strcat(path_buff, "/");
103 |
104 | memset(cmd_buff, 0, 2048);
105 | sprintf(
106 | cmd_buff, "make --file=/root/.config/clocker/.new/source/%s/makefile all && clear",
107 | dir_net->d_name
108 | );
109 |
110 | soft_assert_goto_return(
111 | system(cmd_buff) == 0,
112 | "Coping binary failed!"
113 | );
114 |
115 | memset(cmd_buff, 0, 2048);
116 | sprintf(
117 | cmd_buff,
118 | "cp /root/.config/clocker/.new/source/%s/build/clocker /usr/bin/clocker",
119 | dir_net->d_name
120 | );
121 |
122 | soft_assert_goto_return(
123 | system(cmd_buff) == 0,
124 | "Coping binary failed!"
125 | );
126 |
127 | memset(path_buff, 0, 2048);
128 | updater_get_conf_file(path_buff);
129 |
130 | FILE* data_file_ptr = fopen(path_buff, "w");
131 | soft_assert_goto_return(
132 | data_file_ptr != INVALID_HNDL,
133 | "Creating main app data file failed! (%s)",
134 | strerror(errno)
135 | );
136 |
137 | fputs(_updater->new_version, data_file_ptr);
138 | memcpy(_updater->version, _updater->new_version, strlen(_updater->new_version));
139 |
140 | soft_assert_goto_return(
141 | fclose(data_file_ptr) == 0,
142 | "Closing main app data failed! (%s)",
143 | strerror(errno)
144 | );
145 |
146 | break;
147 | }
148 | }
149 |
150 | RETURN:
151 | soft_assert_ret_id(
152 | closedir(dir) == 0,
153 | "Closing directory failed! (%s)",
154 | strerror(errno)
155 | );
156 |
157 | soft_assert_ret_id(
158 | system("rm -rf /root/.config/clocker/.new") == 0,
159 | "Deleting directory failed!"
160 | );
161 |
162 | printf("Done!\n");
163 | return 1;
164 | }
165 |
166 |
167 | void_t main(
168 | i32_t argc,
169 | str_t args[]
170 | ) {
171 |
172 | if (!args[1] || (strcmp(args[1], "--from-clocker") != 0)) {
173 | printf(COLOR_IN_YELLOW"WARNING"COLOR_RESET WRN_TXT(": Running updater directly not recommended.\n"));
174 | printf("Please use: "TRANSPARENT_TXT("clocker --update\n"));
175 | return -1;
176 | }
177 |
178 | str_t argv[2] = {"/usr/bin/clocker", INVALID_HNDL};
179 |
180 | updater_t checker = update_checker_new();
181 | soft_assert_wrn(
182 | boxed_unbox(&checker.inner) != 0,
183 | "Creating new update object failed!"
184 | );
185 |
186 | update_status_t update_check_res = update_checker_check(&checker);
187 | soft_assert_wrn(
188 | update_check_res != US_Error,
189 | "Checking for new version failed!"
190 | );
191 |
192 | updater_do_update(&checker);
193 | update_checker_destroy(checker);
194 | execvp("/usr/bin/clocker", argv);
195 | return;
196 | }
197 |
--------------------------------------------------------------------------------
/framework/prims/assert.h:
--------------------------------------------------------------------------------
1 | #ifndef _PRIMS_INC_ASSERT_H_
2 | #define _PRIMS_INC_ASSERT_H_
3 |
4 | #include
5 |
6 | #define OPR_DEFS
7 | #define OPT_DEFS
8 | #define RES_DEFS
9 | #include "common/defs/inc.h"
10 |
11 |
12 | #define assert(exp1_) (exp1_) ? B_True : B_False
13 | #define assert_eq(exp1_, exp2_) (exp1_ IS_EQ exp2_) ? B_True : B_False
14 | #define assert_ne(exp1_, exp2_) (exp1_ IS_EQ exp2_) ? B_False; : B_True
15 | #define assert_be(exp1_, exp2_) (exp1_ IS_BIGGER_OR_EQ exp2_) ? B_True; : B_False
16 | #define assert_le(exp1_, exp2_) (exp1_ IS_SMALLER_OR_EQ exp2_) ? B_True; : B_False
17 | #define assert_ez(exp1_) assert_eq(exp1_, 0)
18 | #define assert_nez(exp1_) assert_ne(exp1_, 0)
19 | #define assert_bez(exp1_) assert_be(exp1_, 0)
20 | #define assert_lez(exp1_) assert_le(exp1_, 0)
21 |
22 | #define soft_assert(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return B_False; } } while(0)
23 | #define soft_assert_ret(exp_, ret_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return ret_; } } while(0)
24 | #define soft_assert_ret_int(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return -1; } } while(0)
25 | #define soft_assert_ret_ptr(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return INVALID_HNDL; } } while(0)
26 | #define soft_assert_ret_id(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return INVALID_INDEX; } } while(0)
27 | #define soft_assert_ret_float(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return (float_t)-1; } } while(0)
28 | #define soft_assert_ret_void(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return ; } } while(0)
29 | #define soft_assert_goto_return(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); goto RETURN; } } while(0)
30 |
31 | #define soft_assert_wrn(exp_, msg_, ...) do { if (!(exp_)) { debug_wrn(msg_, ## __VA_ARGS__); } } while(0)
32 | #define soft_assert_inf(exp_, msg_, ...) do { if (!(exp_)) { debug_inf(msg_, ## __VA_ARGS__); } } while(0)
33 | #define soft_assert_log(exp_, msg_, ...) do { if (!(exp_)) { debug_log(msg_, ## __VA_ARGS__); } } while(0)
34 |
35 | #define soft_assert_break(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); break;} } while(0)
36 | #define soft_assert_continue(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); continue;} } while(0)
37 | #define soft_assert_break_wrn(exp_, msg_, ...) if (!(exp_)) { debug_wrn(msg_, ## __VA_ARGS__); break;} while(0)
38 | #define soft_assert_continue_wrn(exp_, msg_, ...) do { if (!(exp_)) { debug_wrn(msg_, ## __VA_ARGS__); continue;} } while(0)
39 | #define soft_assert_break_inf(exp_, msg_, ...) do { if (!(exp_)) { debug_inf(msg_, ## __VA_ARGS__); break;} } while(0)
40 | #define soft_assert_continue_inf(exp_, msg_, ...) do { if (!(exp_)) { debug_inf(msg_, ## __VA_ARGS__); continue;} } while(0)
41 | #define soft_assert_break_log(exp_, msg_, ...) do { if (!(exp_)) { debug_log(msg_, ## __VA_ARGS__); break;} } while(0)
42 | #define soft_assert_continue_log(exp_, msg_, ...) do { if (!(exp_)) { debug_log(msg_, ## __VA_ARGS__); continue;} } while(0)
43 |
44 |
45 |
46 | #define soft_assert_res(res_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); return B_False; } } while(0)
47 | #define soft_assert_res_ret_int(res_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); return -1; } } while(0)
48 | #define soft_assert_res_ret_ptr(res_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); return INVALID_HNDL; } } while(0)
49 | #define soft_assert_res_ret_id(res_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); return INVALID_INDEX; } } while(0)
50 | #define soft_assert_res_ret_float(res_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); return (float_t)-1; } } while(0)
51 | #define soft_assert_res_ret_void(res_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); return ; } } while(0)
52 | #define soft_assert_res_ret_res(res_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); return (result_t)err(ek_gen); } } while(0)
53 | #define soft_assert_res_ret_gen(res_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); return G_NONE;} } while(0)
54 |
55 | #define soft_assert_res_wrn(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_wrn(msg_, ## __VA_ARGS__); } } while(0)
56 | #define soft_assert_res_inf(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_inf(msg_, ## __VA_ARGS__); } } while(0)
57 | #define soft_assert_res_log(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_log(msg_, ## __VA_ARGS__); } } while(0)
58 |
59 | #define soft_assert_res_break(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); break;} } while(0)
60 | #define soft_assert_res_continue(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_err(msg_, ## __VA_ARGS__); continue;} } while(0)
61 | #define soft_assert_res_break_wrn(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_wrn(msg_, ## __VA_ARGS__); break;} } while(0)
62 | #define soft_assert_res_continue_wrn(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_wrn(msg_, ## __VA_ARGS__); continue;} } while(0)
63 | #define soft_assert_res_break_inf(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_inf(msg_, ## __VA_ARGS__); break;} } while(0)
64 | #define soft_assert_res_continue_inf(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_inf(msg_, ## __VA_ARGS__); continue;} } while(0)
65 | #define soft_assert_res_break_log(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_log(msg_, ## __VA_ARGS__); break;} } while(0)
66 | #define soft_assert_res_continue_log(exp_, msg_, ...) do { if (!is_ok(res_)) { debug_log(msg_, ## __VA_ARGS__); continue;} } while(0)
67 | #define soft_assert_ret_res(expr_, msg_, ...) do { if (!(expr_)) { debug_err(msg_, ## __VA_ARGS__); return err(ek_gen); } } while(0)
68 |
69 |
70 |
71 | #define soft_assert_opt(opt_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); return B_False; } } while(0)
72 | #define soft_assert_opt_ret_int(opt_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); return -1; } } while(0)
73 | #define soft_assert_opt_ret_ptr(opt_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); return INVALID_HNDL; } } while(0)
74 | #define soft_assert_opt_ret_id(opt_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); return INVALID_INDEX; } } while(0)
75 | #define soft_assert_opt_ret_float(opt_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); return (float_t)-1; } } while(0)
76 | #define soft_assert_opt_ret_void(opt_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); return ; } } while(0)
77 | #define soft_assert_opt_ret_res(opt_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); return (result_t)err(ek_gen); } } while(0)
78 | #define soft_assert_ret_opt(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return None; } } while(0)
79 |
80 | #define soft_assert_opt_wrn(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_wrn(msg_, ## __VA_ARGS__); } } while(0)
81 | #define soft_assert_opt_inf(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_inf(msg_, ## __VA_ARGS__); } } while(0)
82 | #define soft_assert_opt_log(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_log(msg_, ## __VA_ARGS__); } } while(0)
83 |
84 | #define soft_assert_opt_break(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); break;} } while(0)
85 | #define soft_assert_opt_continue(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_err(msg_, ## __VA_ARGS__); continue;} } while(0)
86 | #define soft_assert_opt_break_wrn(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_wrn(msg_, ## __VA_ARGS__); break;} } while(0)
87 | #define soft_assert_opt_continue_wrn(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_wrn(msg_, ## __VA_ARGS__); continue;} } while(0)
88 | #define soft_assert_opt_break_inf(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_inf(msg_, ## __VA_ARGS__); break;} } while(0)
89 | #define soft_assert_opt_continue_inf(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_inf(msg_, ## __VA_ARGS__); continue;} } while(0)
90 | #define soft_assert_opt_break_log(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_log(msg_, ## __VA_ARGS__); break;} } while(0)
91 | #define soft_assert_opt_continue_log(exp_, msg_, ...) do { if (opt_is_none(opt_)) { debug_log(msg_, ## __VA_ARGS__); continue;} } while(0)
92 |
93 |
94 |
95 | #define soft_assert_ret_gen(exp_, msg_, ...) do { if (!(exp_)) { debug_err(msg_, ## __VA_ARGS__); return G_NONE; } } while(0)
96 |
97 | #endif // __PRIMS_INC_ASSERT_H
--------------------------------------------------------------------------------
/framework/prims/debug.h:
--------------------------------------------------------------------------------
1 | #ifndef _PRIMS_INC_DEBUG_H_
2 | #define _PRIMS_INC_DEBUG_H_
3 |
4 | #include
5 | #include
6 |
7 | #include "common/defs/color_defs.h"
8 |
9 | #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
10 |
11 | #define ERR_LBL "ERR"
12 | #define WRN_LBL "WRN"
13 | #define INF_LBL "INF"
14 | #define LOG_LBL "LOG"
15 |
16 | #ifdef DEBUG
17 | #define _debug_msg(label_in_color_, label_color_, label_, msg_, ...) do { printf( "[" BOLD_TXT(label_color_" %s ") "] ", label_); \
18 | printf( label_in_color_ msg_, ##__VA_ARGS__);\
19 | printf(" " UNDER_TXT( ITALIC_TXT( TRANSPARENT_TXT ( "at: %s %s, %s, fn: %s, ln: %d"))) , __DATE__, __TIME__, __FILENAME__, __func__, __LINE__ ); \
20 | printf("\n");\
21 | } while(0)
22 | #else
23 | #define _debug_msg(label_, msg_, ...)
24 | #endif
25 |
26 | // ------------------------------------------------------------------------------------------------------------------------------------------
27 |
28 | #define debug_err(msg_, ...) _debug_msg(COLOR_RED, COLOR_IN_RED, ERR_LBL, msg_, ##__VA_ARGS__)
29 | #define debug_wrn(msg_, ...) _debug_msg(COLOR_YELLOW, COLOR_IN_YELLOW, WRN_LBL, msg_, ##__VA_ARGS__)
30 | #define debug_inf(msg_, ...) _debug_msg(COLOR_BLUE, COLOR_IN_BLUE, INF_LBL, msg_, ##__VA_ARGS__)
31 | #define debug_log(msg_, ...) _debug_msg(COLOR_BOLD, COLOR_BOLD, LOG_LBL, msg_, ##__VA_ARGS__)
32 |
33 | #endif // __PRIMS_INC_DEBUG_H
--------------------------------------------------------------------------------
/framework/prims/inc.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2022 Redwan
3 | *
4 | * This file is part of Owl.
5 | *
6 | * Owl is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Owl is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Owl. If not, see .
18 | */
19 |
20 | #ifndef _FRAMEWORK_PRIMS_MOD_H_
21 | #define _FRAMEWORK_PRIMS_MOD_H_
22 |
23 | #include "assert.h"
24 | #include "debug.h"
25 |
26 |
27 | #endif // _FRAMEWORK_PRIMS_MOD_H_
28 |
--------------------------------------------------------------------------------
/framework/system/fs/inc.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Owl.
4 | //
5 | // Owl is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Owl is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Owl. If not, see .
17 |
18 | #ifndef _FRAMEWORK_SYSTEM_FS_MOD_H_
19 | #define _FRAMEWORK_SYSTEM_FS_MOD_H_
20 |
21 | #include "framework/prims/inc.h"
22 | #include "common/defs/inc.h"
23 |
24 | #define FRAMEWORK_SYSTEM_FS
25 |
26 | typedef struct {
27 | FILE* inner;
28 | }
29 | wfile_t;
30 |
31 |
32 | result_t w_file_open(
33 | wfile_t* OUT wfile,
34 | str_t IN path,
35 | str_t IN mode
36 | );
37 |
38 |
39 | result_t w_file_write(
40 | wfile_t* IN wfile,
41 | bytes_t IN buffer,
42 | size_t IN size
43 | );
44 |
45 |
46 | result_t w_file_size(
47 | wfile_t* IN wfile
48 | );
49 |
50 |
51 | result_t w_file_read(
52 | wfile_t* IN wfile,
53 | bytes_t OUT buffer,
54 | size_t IN size
55 | );
56 |
57 |
58 | result_t w_file_close(
59 | wfile_t* IN wfile
60 | );
61 |
62 |
63 | result_t w_file_serialize(
64 | wfile_t* IN wfile,
65 | bytes_t OUT buffer,
66 | size_t IN capacity
67 | );
68 |
69 |
70 | #endif // _FRAMEWORK_SYSTEM_FS_MOD_H_
71 |
--------------------------------------------------------------------------------
/framework/system/fs/wrapper.c:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Owl.
4 | //
5 | // Owl is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Owl is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Owl. If not, see .
17 |
18 | #include
19 |
20 | #include "inc.h"
21 |
22 |
23 | result_t w_file_open(
24 | wfile_t* OUT wfile,
25 | str_t IN path,
26 | str_t IN mode
27 | ) {
28 | soft_assert_ret_res(
29 | path IS_NOT_EQ INVALID_HNDL,
30 | "Invalid argument (path is null)!"
31 | );
32 |
33 | soft_assert_ret_res(
34 | mode IS_NOT_EQ INVALID_HNDL,
35 | "Invalid argument (mode is null)!"
36 | );
37 |
38 | wfile->inner = fopen(path, mode);
39 | soft_assert_ret(
40 | wfile->inner IS_NOT_EQ INVALID_HNDL,
41 | err(ek_openf),
42 | "Opening file failed (os error: %s)!",
43 | strerror(errno)
44 | );
45 |
46 | return ok(_((handle_t) wfile));
47 | }
48 |
49 |
50 | result_t w_file_write(
51 | wfile_t* IN wfile,
52 | bytes_t IN buffer,
53 | size_t IN size
54 | ) {
55 | soft_assert_ret_res(
56 | buffer IS_NOT_EQ INVALID_HNDL,
57 | "Invalid argument (buffer is null)!"
58 | );
59 |
60 | soft_assert_wrn(
61 | size IS_NOT_EQ 0,
62 | "The given buffer size is 0!"
63 | );
64 |
65 | for (index_t idx = 0; idx < size; idx++) {
66 | soft_assert_ret(
67 | fputc(buffer[idx], wfile->inner) IS_NOT_EQ EOF,
68 | err(ek_unexpected_eof),
69 | "Putting byte into file failed (os error: %s)!",
70 | strerror(errno)
71 | );
72 | }
73 |
74 | return ok(_(size));
75 | }
76 |
77 |
78 | result_t w_file_size(
79 | wfile_t* IN wfile
80 | ) {
81 | size_t tmp = ftell(wfile->inner);
82 |
83 | fseek(wfile->inner, 0L, SEEK_END);
84 | size_t size = ftell(wfile->inner);
85 | fseek(wfile->inner, tmp, SEEK_SET);
86 |
87 | return ok(_(size));
88 | }
89 |
90 |
91 | result_t w_file_read(
92 | wfile_t* IN wfile,
93 | bytes_t OUT buffer,
94 | size_t IN size
95 | ) {
96 | soft_assert_ret_res(
97 | buffer IS_NOT_EQ INVALID_HNDL,
98 | "Invalid argument (buffer is null)!"
99 | );
100 |
101 | soft_assert_wrn(
102 | size IS_NOT_EQ 0,
103 | "The given buffer size is 0!"
104 | );
105 |
106 | result_t file_size = w_file_size(wfile);
107 | soft_assert_res_ret_res(
108 | file_size,
109 | "Getting file size failed!"
110 | );
111 |
112 | soft_assert_ret(
113 | size IS_SMALLER_OR_EQ res_get_data(file_size).size,
114 | err(ek_unexpected_eof),
115 | "The given buffer read size exceeds the file size (buffer size: %ld while file size: %ld)!",
116 | size, res_get_data(file_size).size
117 | );
118 |
119 | for (index_t idx = 0; idx < size; idx++) {
120 | buffer[idx] = fgetc(wfile->inner);
121 | }
122 |
123 | return ok(_(size));
124 | }
125 |
126 |
127 | result_t w_file_close(
128 | wfile_t* IN wfile
129 | ) {
130 |
131 | soft_assert_ret(
132 | fclose(wfile->inner) IS_NOT_EQ EOF,
133 | err(ek_openf),
134 | "Closing file failed (os error: %s)!",
135 | strerror(errno)
136 | );
137 |
138 | return ok(G_NONE);
139 | }
140 |
141 |
142 | result_t _file_serialize(
143 | FILE* IN file,
144 | bytes_t OUT buffer,
145 | size_t IN capacity
146 | ) {
147 | // TODO
148 |
149 | return err(ek_not_impl);
150 | }
151 |
152 |
153 | result_t w_file_serialize(
154 | wfile_t* IN wfile,
155 | bytes_t OUT buffer,
156 | size_t IN capacity
157 | ) {
158 | // TODO
159 |
160 | return err(ek_not_impl);
161 | }
--------------------------------------------------------------------------------
/framework/system/memory/boxed.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2022 Redwan
3 | *
4 | * This file is part of clocker-safe.
5 | *
6 | * clocker-safe is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * clocker-safe is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with clocker-safe. If not, see .
18 | */
19 |
20 | #include
21 | #include "inc.h"
22 |
23 | boxed_t boxed_new(size_t size) {
24 | handle_t inner = malloc(size);
25 | soft_assert_ret(inner != INVALID_HNDL, (boxed_t) {.inner = INVALID_HNDL}, "Allocating new boxed pointer failed!");
26 | return (boxed_t) {.inner = inner};
27 | }
28 |
29 | boxed_t boxed_zeroed_new(size_t n_memb, size_t size) {
30 | handle_t inner = calloc(n_memb, size);
31 | soft_assert_ret(inner != INVALID_HNDL, (boxed_t) {.inner = INVALID_HNDL}, "Allocating new boxed pointer failed!");
32 | return (boxed_t) {.inner = inner};
33 | }
34 |
35 | boxed_t boxed_resize(boxed_t boxed, size_t size) {
36 | debug_err("`boxed_resize` is not implemented yet!");
37 | return (boxed_t) {.inner = INVALID_HNDL};
38 | }
39 |
40 | handle_t boxed_unbox(boxed_t* boxed) {
41 | return boxed->inner;
42 | }
43 |
44 | void_t boxed_destroy(boxed_t boxed) {
45 | free(boxed.inner);
46 | }
--------------------------------------------------------------------------------
/framework/system/memory/inc.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2022 Redwan
3 | *
4 | * This file is part of clocker-safe.
5 | *
6 | * clocker-safe is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * clocker-safe is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with clocker-safe. If not, see .
18 | */
19 |
20 | #ifndef _FRAMEWORK_SYSTEM_MEMORY_INC_H_
21 | #define _FRAMEWORK_SYSTEM_MEMORY_INC_H_
22 |
23 | #include "framework/prims/inc.h"
24 | #include "common/defs/inc.h"
25 |
26 | typedef struct {
27 | handle_t inner;
28 | }
29 | boxed_t;
30 |
31 |
32 | boxed_t boxed_new(size_t size);
33 |
34 | boxed_t boxed_zeroed_new(size_t n_memb, size_t size);
35 |
36 | boxed_t boxed_resize(boxed_t boxed, size_t size);
37 |
38 | handle_t boxed_unbox(boxed_t* boxed);
39 |
40 | void_t boxed_destroy(boxed_t boxed);
41 |
42 | #endif // _FRAMEWORK_SYSTEM_MEMORY_INC_H_
43 |
--------------------------------------------------------------------------------
/framework/system/thread/guarded.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2022 Redwan
3 | *
4 | * This file is part of Owl.
5 | *
6 | * Owl is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Owl is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Owl. If not, see .
18 | */
19 |
20 | #include "inc.h"
21 |
22 |
23 | guarded_t guarded_new(
24 | gen_t IN data
25 | ) {
26 | guarded_t guard = {0};
27 | guard.data = data;
28 |
29 | pthread_mutex_init(&(guard.mutex), INVALID_HNDL);
30 | return guard;
31 | }
32 |
33 |
34 | void_t guarded_change(
35 | guarded_t* IN guard,
36 | gen_t IN data
37 | ) {
38 | guard->data = data;
39 | }
40 |
41 |
42 | result_t guarded_lock(
43 | guarded_t* IN guard
44 | ) {
45 | pthread_mutex_lock(&(guard->mutex));
46 | return ok((handle_t) &(guard->data));
47 | }
48 |
49 |
50 | result_t guarded_unlock(
51 | guarded_t* IN guard
52 | ) {
53 | pthread_mutex_unlock(&(guard->mutex));
54 | return ok(guard->data);
55 | }
56 |
57 |
58 | result_t guarded_serialize(
59 | guarded_t* IN gaurd,
60 | bytes_t OUT buffer,
61 | size_t IN capacity
62 | ) {
63 | soft_assert_wrn(capacity IS_NOT_EQ 0, "The given buffer capacity is zero!");
64 |
65 | // TODO
66 |
67 | return err(ek_not_impl);
68 | }
69 |
70 | // #define GUARD(data_) guarded_new()
--------------------------------------------------------------------------------
/framework/system/thread/inc.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2022 Redwan
3 | *
4 | * This file is part of Owl.
5 | *
6 | * Owl is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Owl is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Owl. If not, see .
18 | */
19 |
20 | #ifndef _FRAMEWORK_SYSTEM_THREAD_INC_H_
21 | #define _FRAMEWORK_SYSTEM_THREAD_INC_H_
22 |
23 | #include
24 |
25 | #include "framework/prims/inc.h"
26 | #include "common/defs/inc.h"
27 |
28 |
29 | typedef struct {
30 | gen_t data;
31 | pthread_mutex_t mutex;
32 | }
33 | guarded_t;
34 |
35 |
36 | typedef struct {
37 | pthread_t hndle;
38 | pthread_attr_t attr;
39 | }
40 | thread_t;
41 |
42 |
43 | thread_t thread_new();
44 |
45 | result_t thread_spawn(
46 | thread_t* OUT thread,
47 | handle_t IN (*thread_handle) (guarded_t*),
48 | guarded_t* IN thread_argument
49 | );
50 |
51 | result_t thread_wait(
52 | thread_t OUT thread
53 | );
54 |
55 | result_t thread_destroy(
56 | thread_t OUT thread
57 | );
58 |
59 | guarded_t guarded_new(
60 | gen_t IN data
61 | );
62 |
63 | void_t guarded_change(
64 | guarded_t* IN guard,
65 | gen_t IN data
66 | );
67 |
68 | result_t guarded_lock(
69 | guarded_t* IN guard
70 | );
71 |
72 |
73 | result_t guarded_unlock(
74 | guarded_t* IN guard
75 | );
76 |
77 |
78 | #endif // _FRAMEWORK_SYSTEM_THREAD_INC_H_
79 |
--------------------------------------------------------------------------------
/framework/system/thread/thread.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2022 Redwan
3 | *
4 | * This file is part of Owl.
5 | *
6 | * Owl is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Owl is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Owl. If not, see .
18 | */
19 |
20 | #include
21 |
22 | #include "inc.h"
23 |
24 |
25 | thread_t thread_new() {
26 | return (thread_t) {
27 | .attr = 0,
28 | .hndle = 0
29 | };
30 | }
31 |
32 |
33 | result_t thread_spawn(
34 | thread_t* OUT thread,
35 | handle_t IN (*thread_handle) (guarded_t*),
36 | guarded_t* IN thread_argument
37 | ) {
38 | soft_assert_ret_res(
39 | thread IS_NOT_EQ INVALID_HNDL,
40 | "Invalid argument (thread object is null)!"
41 | );
42 |
43 | handle_t (*pthread_handle) (handle_t) = (handle_t(*) (handle_t)) (*thread_handle);
44 |
45 | soft_assert_ret_res(
46 | thread_handle IS_NOT_EQ INVALID_HNDL,
47 | "Invalid argument (thread object is null)!"
48 | );
49 |
50 | soft_assert_ret(
51 | pthread_create(&(thread->hndle), INVALID_HNDL, pthread_handle, thread_argument) IS_EQ 0,
52 | err(ek_createf),
53 | "Creating new thread failed (os error: %s)!",
54 | strerror(errno)
55 | );
56 |
57 | return ok(G_NONE);
58 | }
59 |
60 |
61 | result_t thread_wait(
62 | thread_t OUT thread
63 | ) {
64 | pthread_join(thread.hndle, INVALID_HNDL);
65 | }
66 |
67 |
68 | result_t thread_destroy(
69 | thread_t OUT thread
70 | ) {
71 | pthread_detach(thread.hndle);
72 | }
--------------------------------------------------------------------------------
/includes/cmd.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2021 root
2 | //
3 | // This file is part of Clocker.
4 | //
5 | // Clocker is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Clocker is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Clocker. If not, see .
17 |
18 | #ifndef _INCLUDES_CMD_H_
19 | #define _INCLUDES_CMD_H_
20 |
21 | #include "common/defs/inc.h"
22 | #include "framework/system/thread/inc.h"
23 |
24 | handle_t cmd_run(guarded_t* state);
25 |
26 | #endif // _INCLUDES_CMD_H_
27 |
--------------------------------------------------------------------------------
/includes/data_manager.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2022 Redwan
2 | //
3 | // This file is part of Clocker.
4 | //
5 | // Clocker is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Clocker is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Clocker. If not, see .
17 |
18 | #ifndef _INCLUDES_DATA_MANAGER_H_
19 | #define _INCLUDES_DATA_MANAGER_H_
20 |
21 | #include "common/defs/inc.h"
22 | #include "framework/prims/inc.h"
23 | #include "framework/system/memory/inc.h"
24 | #include "includes/timer.h"
25 |
26 | typedef struct {
27 | boxed_t inner;
28 | }
29 | data_manager_t;
30 |
31 | bool_t data_manager_save_data(
32 | data_manager_t* manager,
33 | clocker_timer_t* timer,
34 | bool_t final
35 | );
36 |
37 | data_manager_t data_manager_start(bool_t no_save);
38 |
39 | void_t data_manager_stop(data_manager_t manager, clocker_timer_t* timer);
40 |
41 | clocker_timer_t* data_manager_get_timer(data_manager_t* manager);
42 |
43 | bool_t data_manager_allow_saving(data_manager_t* manager);
44 |
45 | #endif // _INCLUDES_DATA_MANAGER_H_
46 |
--------------------------------------------------------------------------------
/includes/listener.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2021 rdwn
2 | //
3 | // This file is part of Clocker.
4 | //
5 | // Clocker is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Clocker is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Clocker. If not, see .
17 |
18 | #ifndef HEADERS_LISTENER_H
19 | #define HEADERS_LISTENER_H
20 |
21 | #include "common/defs/inc.h"
22 | #include "framework/system/memory/inc.h"
23 |
24 | typedef struct {
25 | boxed_t inner;
26 | }
27 | listener_t;
28 |
29 |
30 | listener_t listener_new();
31 |
32 | void_t listener_listen(listener_t* listener);
33 |
34 | i64_t listener_fired(listener_t* listener);
35 |
36 | i64_t listener_destroy(listener_t listener);
37 |
38 | #define listener_event_fired(_listener) listener_fired(_listener)
39 |
40 | #endif // HEADERS_LISTENER_H
--------------------------------------------------------------------------------
/includes/sig_handler.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2021 rdwn
2 | //
3 | // This file is part of Clocker.
4 | //
5 | // Clocker is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Clocker is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Clocker. If not, see .
17 |
18 | #ifndef _INCLUDES_SIG_HANDLER_H_
19 | #define _INCLUDES_SIG_HANDLER_H_
20 |
21 | #include
22 | #include "common/defs/inc.h"
23 |
24 | typedef sig_t Sig;
25 |
26 | void_t signal_handler(i32_t signal);
27 |
28 | #endif // _INCLUDES_SIG_HANDLER_H_
29 |
--------------------------------------------------------------------------------
/includes/thread_arg.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2021 rdwn
2 | //
3 | // This file is part of Clocker.
4 | //
5 | // Clocker is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Clocker is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Clocker. If not, see .
17 |
18 | #ifndef _INCLUDES_THREAD_ARG_H_
19 | #define _INCLUDES_THREAD_ARG_H_
20 |
21 | #include "common/defs/inc.h"
22 | #include "framework/system/thread/inc.h"
23 | #include "includes/data_manager.h"
24 |
25 |
26 | typedef enum {
27 | TM_Default,
28 | TM_Busy,
29 | TM_Paused
30 | }
31 | time_mode_t;
32 |
33 | typedef enum {
34 | TC_None,
35 | TC_End,
36 | TC_Report,
37 | TC_Pause,
38 | TC_Resume,
39 | TC_Save
40 | }
41 | time_command_t;
42 |
43 | typedef struct {
44 | data_manager_t manager;
45 | str_t version;
46 | time_command_t command;
47 | time_mode_t mode;
48 | }
49 | thread_arg_t;
50 |
51 |
52 | INLINE thread_arg_t thread_arg_new(
53 | time_command_t command,
54 | time_mode_t mode,
55 | str_t version,
56 | data_manager_t manager
57 | ) {
58 | return (thread_arg_t) {
59 | .command = command,
60 | .mode = mode,
61 | .version = version,
62 | .manager = manager
63 | };
64 | }
65 |
66 |
67 | INLINE str_t time_mode_from(time_mode_t mode) {
68 | switch (mode) {
69 | case TM_Default:
70 | return "default";
71 |
72 | case TM_Busy:
73 | return "busy";
74 |
75 | case TM_Paused:
76 | return "paused";
77 |
78 | default:
79 | return "";
80 | }
81 | }
82 |
83 | #endif // _INCLUDES_THREAD_ARG_H_
84 |
--------------------------------------------------------------------------------
/includes/time_handler.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2021 rdwn
2 | //
3 | // This file is part of Clocker.
4 | //
5 | // Clocker is free software: you can redistribute it and/or modify
6 | // it under the terms of the GNU General Public License as published by
7 | // the Free Software Foundation, either version 3 of the License, or
8 | // (at your option) any later version.
9 | //
10 | // Clocker is distributed in the hope that it will be useful,
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | // GNU General Public License for more details.
14 | //
15 | // You should have received a copy of the GNU General Public License
16 | // along with Clocker. If not, see .
17 |
18 | #ifndef _INCLUDES_TIME_HANDLER_H_
19 | #define _INCLUDES_TIME_HANDLER_H_
20 |
21 | #include "common/defs/inc.h"
22 | #include "framework/system/thread/inc.h"
23 |
24 | handle_t time_handle(guarded_t* arg);
25 |
26 | #endif // _INCLUDES_TIME_HANDLER_H_
27 |
28 |
--------------------------------------------------------------------------------
/includes/timer.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #ifndef HEADERS_TIMER_H
21 | #define HEADERS_TIMER_H
22 |
23 | #include "common/defs/inc.h"
24 | #include "framework/prims/inc.h"
25 | #include "framework/system/memory/inc.h"
26 |
27 | typedef struct {
28 | boxed_t inner;
29 | }
30 | clocker_timer_t;
31 |
32 | clocker_timer_t timer_new();
33 |
34 | void_t timer_init(
35 | clocker_timer_t* timer,
36 | u64_t time,
37 | u64_t spent
38 | );
39 |
40 | void_t timer_start(clocker_timer_t* timer);
41 |
42 | void_t timer_pause(clocker_timer_t* timer);
43 |
44 | void_t timer_resume(clocker_timer_t* timer);
45 |
46 | void_t timer_stop(clocker_timer_t* timer);
47 |
48 | void_t timer_reset(clocker_timer_t* timer);
49 |
50 | void_t timer_reduce(clocker_timer_t* timer, u64_t seconds);
51 |
52 | u64_t timer_get_time(clocker_timer_t* timer);
53 |
54 | u64_t timer_time_spend(clocker_timer_t* timer);
55 |
56 | void_t timer_destroy(clocker_timer_t timer);
57 |
58 | #endif // HEADERS_TIMER_H
--------------------------------------------------------------------------------
/includes/updater_checker.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #ifndef _INCLUDES_UPDATER_CHECKER_H_
21 | #define _INCLUDES_UPDATER_CHECKER_H_
22 |
23 | #include "common/defs/inc.h"
24 | #include "framework/prims/inc.h"
25 | #include "framework/system/memory/inc.h"
26 |
27 | typedef enum {
28 | US_Error,
29 | US_NoUpdate,
30 | US_Updatable
31 | }
32 | update_status_t;
33 |
34 | typedef struct {
35 | boxed_t inner;
36 | }
37 | updater_t;
38 |
39 |
40 | updater_t update_checker_new();
41 |
42 | update_status_t update_checker_check(updater_t* updater);
43 |
44 | str_t update_checker_get_version(updater_t* updater);
45 |
46 | str_t update_checker_get_new_tag(updater_t* updater);
47 |
48 | void_t update_checker_destroy(updater_t updater);
49 |
50 | #endif // _INCLUDES_UPDATER_CHECKER_H_
51 |
--------------------------------------------------------------------------------
/makefile:
--------------------------------------------------------------------------------
1 | # Copyright (C) 2021 rdwn
2 | #
3 | # This file is part of Clocker.
4 | #
5 | # Clocker is free software: you can redistribute it and/or modify
6 | # it under the terms of the GNU General Public License as published by
7 | # the Free Software Foundation, either version 3 of the License, or
8 | # (at your option) any later version.
9 | #
10 | # Clocker is distributed in the hope that it will be useful,
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | # GNU General Public License for more details.
14 | #
15 | # You should have received a copy of the GNU General Public License
16 | # along with Clocker. If not, see .
17 |
18 | current = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
19 |
20 | component := ${current}/source/listener.c
21 | component += ${current}/source/time_handler.c
22 | component += ${current}/source/timer.c
23 | component += ${current}/source/update_checker.c
24 | component += ${current}/source/sig_handler.c
25 | component += ${current}/source/cmd.c
26 | component += ${current}/source/data_manager.c
27 | component += ${current}/framework/system/thread/guarded.c
28 | component += ${current}/framework/system/thread/thread.c
29 | component += ${current}/framework/system/fs/wrapper.c
30 | component += ${current}/framework/system/memory/boxed.c
31 |
32 | updaterComponent := ${current}/source/update_checker.c
33 | updaterComponent += ${current}/framework/system/memory/boxed.c
34 |
35 | header := ${current}
36 |
37 | clocker := ${current}/deploy/clocker/clocker.c
38 | updater := ${current}/deploy/updater/updater.c
39 |
40 | buildClocker := ${current}/build/clocker
41 | buildUpdater := ${current}/build/clocker-updater
42 |
43 | all:
44 | @mkdir -p ${current}/build/
45 | @gcc -I${header} -fcompare-debug-second -w -g ${component} ${clocker} -o ${buildClocker} -lpthread
46 | @gcc -I${header} -fcompare-debug-second -w -g ${updater} ${updaterComponent} -o ${buildUpdater} -lpthread
47 |
48 | install:
49 | @mkdir -p /root/.config/clocker
50 | @cp ${buildClocker} /usr/bin
51 | @cp ${buildUpdater} /usr/bin
52 |
53 | run:
54 | @sudo ${buildClocker}
55 |
56 | clean:
57 | @rm -rf ${current}/build/
58 |
59 | uninstall:
60 | @rm -rf /root/.config/clocker
61 | @rm /usr/bin/clocker
62 | @rm /usr/bin/clocker-updater
63 |
--------------------------------------------------------------------------------
/source/cmd.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 root
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include
21 |
22 | #include "common/defs/inc.h"
23 | #include "includes/cmd.h"
24 | #include "includes/thread_arg.h"
25 |
26 |
27 | handle_t cmd_run(guarded_t* arg) {
28 |
29 | // TODO: thread safe
30 |
31 | thread_arg_t* cmd_arg = arg->data.ptr;
32 | time_command_t* command = &cmd_arg->command;
33 | time_mode_t* mode = &cmd_arg->mode;
34 | str_t version = cmd_arg->version;
35 |
36 | str_t mode_str = "default";
37 | loop {
38 | mode_str = time_mode_from((time_mode_t) *mode);
39 | printf(TRANSPARENT_TXT("%s")" > ", mode_str);
40 |
41 | char_t input[1024] = {0};
42 | fgets(input, 1024, stdin);
43 | if (*input == '\n') {
44 | continue;
45 | }
46 |
47 |
48 | if ((input[0] == 'e' && input[1] == 'n' && input[2] == 'd') || (input[0] == 'E' && input[1] == '\n')) {
49 | *command = TC_End;
50 | break;
51 | }
52 |
53 | else if (strcmp(input, "busy\n") == 0 || (input[0] == 'b' && input[1] == '\n')) {
54 |
55 | printf(OK_TXT(" Changed to busy mode\n"));
56 | *mode = TM_Busy;
57 | }
58 |
59 | else if (strcmp(input, "default\n") == 0 || (input[0] == 'd' && input[1] == '\n')) {
60 |
61 | printf(OK_TXT(" Changed to default mode\n"));
62 | *mode = TM_Default;
63 | }
64 |
65 | else if (strcmp(input, "report\n") == 0 || (input[0] == 'r' && input[1] == '\n')) {
66 | *command = TC_Report;
67 | sleep(1);
68 | }
69 |
70 | else if (strcmp(input, "pause\n") == 0 || (input[0] == 'P' && input[1] == '\n')) {
71 |
72 | *command = TC_Pause;
73 | *mode = TM_Paused;
74 | }
75 |
76 | else if (strcmp(input, "resume\n") == 0 || (input[0] == 'R' && input[1] == '\n')) {
77 |
78 | if (*mode != TM_Paused) {
79 | printf(ERR_TXT(" Not paused mode!\n"));
80 | }
81 |
82 |
83 | *command = TC_Resume;
84 | *mode = TM_Default;
85 | }
86 |
87 | else if (strcmp(input, "save\n") == 0 || (input[0] == 'S' && input[1] == '\n')) {
88 | printf(ERR_TXT(" Not for use yet!\n"));
89 | // guarded_change(command, _(TC_Save));
90 | }
91 |
92 | else if (strcmp(input, "clear\n") == 0 || (input[0] == 'C' && input[1] == '\n')) {
93 | printf("\e[1;1H\e[2J");
94 | }
95 |
96 | else if (strcmp(input, "version\n") == 0 || (input[0] == 'v' && input[1] == '\n')) {
97 | printf("\n Clocker (%s)\n"\
98 | TRANSPARENT_TXT(" Copyright (C) 2021 Redwan\n"\
99 | " This is free software; see the source for copying conditions.\n"\
100 | " There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"
101 | ),
102 | version
103 | );
104 | }
105 |
106 | else if (strcmp(input, "help\n") == 0 || (input[0] == 'h' && input[1] == '\n')) {
107 | printf("\n Clocker supports these commands:\n" \
108 | TRANSPARENT_TXT(BOLD_TXT(" b, busy "))\
109 | TRANSPARENT_TXT(ITALIC_TXT( ":I have no waste time, even when idle!\n"))\
110 | TRANSPARENT_TXT(BOLD_TXT(" d, default "))\
111 | TRANSPARENT_TXT(ITALIC_TXT(":Oh, I have some waste time, please consider it!\n"))\
112 | TRANSPARENT_TXT(BOLD_TXT(" r, report "))\
113 | TRANSPARENT_TXT(ITALIC_TXT(":Get me my time to here\n"))\
114 | TRANSPARENT_TXT(BOLD_TXT(" P, pause "))\
115 | TRANSPARENT_TXT(ITALIC_TXT(":Consider all times from now as waste \n"))\
116 | TRANSPARENT_TXT(BOLD_TXT(" R, resume "))\
117 | TRANSPARENT_TXT(ITALIC_TXT(":Resume paused mode\n"))\
118 | TRANSPARENT_TXT(BOLD_TXT(" C, clear "))\
119 | TRANSPARENT_TXT(ITALIC_TXT(":Set console offset to the top!\n"))
120 | TRANSPARENT_TXT(BOLD_TXT(" v, version "))\
121 | TRANSPARENT_TXT(ITALIC_TXT(":Get app version \n"))\
122 | TRANSPARENT_TXT(BOLD_TXT(" E, end "))\
123 | TRANSPARENT_TXT(ITALIC_TXT(":End the program and get final report\n\n"))
124 | );
125 | }
126 |
127 | else {
128 | printf(WRN_TXT(" Invalid command!\n"));
129 | }
130 | }
131 |
132 | // guarded_unlock(arg);
133 |
134 | }
--------------------------------------------------------------------------------
/source/data_manager.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2022 Redwan
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 |
28 |
29 | #include "includes/data_manager.h"
30 | #include "common/defs/inc.h"
31 | #include "framework/system/memory/inc.h"
32 |
33 | typedef struct passwd passwd_t;
34 | typedef struct stat stat_t;
35 | typedef struct dirent dirnet_t;
36 | typedef struct timeval time_eval_t;
37 |
38 | typedef enum {
39 | DFS_Nothing,
40 | DFS_Saved,
41 | DFS_NotSaved
42 | }
43 | data_file_status_t;
44 |
45 | typedef struct {
46 | data_file_status_t state;
47 | bool_t is_locked;
48 | }
49 | data_file_lock_t;
50 |
51 | typedef struct {
52 | u64_t total;
53 | u64_t final;
54 | u64_t now;
55 | }
56 | data_file_data_t;
57 |
58 | // this struct contains three pointers. but as they're array and static-defined (on stack)
59 | // we don't need to be worried about them. we consider them as usual variables
60 | typedef struct {
61 | str_t home_dir;
62 | char_t main_dir[1024];
63 | char_t data_file[2048];
64 | char_t lock_file[4096];
65 | stat_t stat_var;
66 | }
67 | paths_t;
68 |
69 | // our main data manager struct
70 | // if we use pointer in the struct we ourself are responsible to free it.
71 | // in other word pointer in the structs considers as `UNSAFE` item. so we are not
72 | // allowed to use it and if use it, it means we are aware of what we're doing.
73 | // so we prefer to avoid using pointer in the structs and if we have to do it,
74 | // consider freeing or destructing it in our main struct destructor.
75 | typedef struct {
76 | FILE* data_file_handle;
77 | FILE* lock_file_handle;
78 | bool_t saving;
79 | bool_t is_locked;
80 | paths_t paths;
81 | clocker_timer_t timer;
82 | }
83 | data_manager_inner_t;
84 |
85 |
86 | void_t paths_get(data_manager_inner_t MUT* manager) {
87 | manager->paths.home_dir = getpwuid(getuid())->pw_dir;
88 | sprintf(manager->paths.main_dir, "%s/%s", manager->paths.home_dir, ".config/clocker");
89 | sprintf(manager->paths.data_file, "%s/%s", manager->paths.main_dir, "data");
90 | sprintf(manager->paths.lock_file, "%s/%s", manager->paths.main_dir, "lock");
91 | }
92 |
93 |
94 | bool_t data_manager_dir_init(data_manager_inner_t* manager) {
95 | soft_assert_ret_id(manager->paths.main_dir, "Invalid argument (main_dir is null)!");
96 | soft_assert_ret_id(manager->paths.data_file, "Invalid argument (main_dir is null)!");
97 | soft_assert_ret_id(manager->paths.home_dir, "Invalid argument (main_dir is null)!");
98 | soft_assert_ret_id(manager->paths.lock_file, "Invalid argument (main_dir is null)!");
99 |
100 | if (!(stat(manager->paths.main_dir, &manager->paths.stat_var) == 0 &&
101 | S_ISDIR(manager->paths.stat_var.st_mode))
102 | ) {
103 | soft_assert_ret_id(
104 | mkdir(manager->paths.main_dir, 0700) == 0,
105 | "Creating main app directory failed: (%s)!",
106 | strerror(errno)
107 | );
108 | }
109 |
110 | return B_True;
111 | }
112 |
113 |
114 | bool_t data_manager_file_init(data_manager_inner_t* manager) {
115 |
116 | manager->data_file_handle = fopen(manager->paths.data_file, "r");
117 | if (manager->data_file_handle == INVALID_HNDL) {
118 |
119 | manager->data_file_handle = fopen(manager->paths.data_file, "w");
120 | soft_assert_ret_id(
121 | manager->data_file_handle != INVALID_HNDL,
122 | "Creating main app data file failed: (%s)!",
123 | strerror(errno)
124 | );
125 | }
126 |
127 | soft_assert_ret_id(
128 | fclose(manager->data_file_handle) == 0,
129 | "Failed to close data file: (%s)!",
130 | strerror(errno)
131 | );
132 |
133 | return B_True;
134 | }
135 |
136 |
137 | bool_t data_manager_lock_init(data_manager_inner_t* manager) {
138 |
139 | manager->lock_file_handle = fopen(manager->paths.lock_file, "r");
140 | if (manager->lock_file_handle == INVALID_HNDL) {
141 |
142 | manager->lock_file_handle = fopen(manager->paths.lock_file, "w");
143 | soft_assert_ret_id(
144 | manager->lock_file_handle != INVALID_HNDL,
145 | "Creating main app lock file failed: (%s)!",
146 | strerror(errno)
147 | );
148 | }
149 |
150 | soft_assert_ret_id(
151 | fclose(manager->lock_file_handle) == 0,
152 | "Failed to close lock file: (%s)!",
153 | strerror(errno)
154 | );
155 |
156 | return B_True;
157 | }
158 |
159 |
160 | void_t data_manager_get_lock(
161 | data_manager_inner_t* manager,
162 | data_file_lock_t* lock
163 | ) {
164 |
165 | manager->lock_file_handle = fopen(manager->paths.lock_file, "r");
166 | str_t a = strerror(errno);
167 | soft_assert_ret_id(
168 | manager->lock_file_handle != INVALID_HNDL,
169 | "Opening main app lock file failed: (%s)!",
170 | strerror(errno)
171 | );
172 |
173 | fgets((bytes_t)lock, sizeof(data_file_lock_t), manager->lock_file_handle);
174 |
175 | soft_assert_ret_id(
176 | fclose(manager->lock_file_handle) == 0,
177 | "Failed to close lock file: (%s)!",
178 | strerror(errno)
179 | );
180 |
181 | return;
182 | }
183 |
184 |
185 | bool_t data_manager_lock(data_manager_inner_t* manager) {
186 |
187 | bool_t first_time = B_True;
188 | data_file_lock_t lock = {0};
189 | data_manager_get_lock(manager, &lock);
190 |
191 | loop {
192 |
193 | if (lock.is_locked) {
194 |
195 | if (first_time) {
196 | printf(DNG_TXT("Blocked! waiting...")"\n");
197 | printf(TRANSPARENT_TXT("NOTE: Another Clocker app is running on your system!\n"\
198 | TRANSPARENT_TXT("If you want to make this one running, run `clocker --no-save`\n")\
199 | ));
200 | first_time = B_False;
201 | }
202 |
203 | data_manager_get_lock(manager, &lock);
204 | sleep(5);
205 | }
206 |
207 | else {
208 |
209 | manager->lock_file_handle = fopen(manager->paths.lock_file, "w");
210 | soft_assert_ret_id(
211 | manager->lock_file_handle != INVALID_HNDL,
212 | "Failed to open lock file: (%s)!",
213 | strerror(errno)
214 | );
215 |
216 | lock.state = (lock.state == DFS_Nothing) ? DFS_NotSaved : lock.state;
217 | lock.is_locked = B_True;
218 |
219 | for (index_t idx = 0; idx < sizeof(data_file_lock_t); idx++) {
220 | fputc(((bytes_t)&lock)[idx], manager->lock_file_handle);
221 | }
222 |
223 | break;
224 | }
225 | }
226 |
227 | soft_assert_ret_id(
228 | fclose(manager->lock_file_handle) == 0,
229 | "Failed to close lock file: (%s)!",
230 | strerror(errno)
231 | );
232 |
233 | return B_True;
234 | }
235 |
236 |
237 | bool_t data_manager_unlock(data_manager_inner_t* manager) {
238 |
239 | manager->lock_file_handle = fopen(manager->paths.lock_file, "w");
240 | soft_assert_ret_id(
241 | manager->lock_file_handle != INVALID_HNDL,
242 | "Failed to open main app lock file: (%s)!",
243 | strerror(errno)
244 | );
245 |
246 | data_file_lock_t lock = {
247 | .state = DFS_Saved,
248 | .is_locked = B_False
249 | };
250 |
251 | for (index_t idx = 0; idx < sizeof(data_file_lock_t); idx++) {
252 | fputc(((bytes_t)&lock)[idx], manager->lock_file_handle);
253 | }
254 |
255 | soft_assert_ret_id(
256 | fclose(manager->lock_file_handle) == 0,
257 | "Failed to close lock file: (%s)!",
258 | strerror(errno)
259 | );
260 |
261 | return B_True;
262 | }
263 |
264 |
265 | bool_t data_manager_read_data(data_manager_inner_t* manager) {
266 |
267 | manager->data_file_handle = fopen(manager->paths.data_file, "r");
268 | soft_assert_ret_id(
269 | manager->data_file_handle != INVALID_HNDL,
270 | "Opening main app data file failed: (%s)!",
271 | strerror(errno)
272 | );
273 |
274 | data_file_data_t data = {0};
275 | fgets((bytes_t)& data, sizeof(data_file_data_t), manager->data_file_handle);
276 |
277 | manager->timer = timer_new();
278 | timer_init(&manager->timer, data.total, data.final);
279 | soft_assert_ret_id(boxed_unbox(&manager->timer.inner) != INVALID_HNDL, "Initializing new timer failed!");
280 |
281 | soft_assert_ret_id(
282 | fclose(manager->data_file_handle) == 0,
283 | "Failed to close lock file: (%s)!",
284 | strerror(errno)
285 | );
286 |
287 | return B_True;
288 | }
289 |
290 |
291 | PUB
292 | bool_t data_manager_save_data(
293 | data_manager_t* manager,
294 | clocker_timer_t* timer,
295 | bool_t final
296 | ) {
297 |
298 | time_eval_t time = {0};
299 | data_manager_inner_t* _manager = (data_manager_inner_t*) boxed_unbox(&manager->inner);
300 |
301 | _manager->data_file_handle = fopen(_manager->paths.data_file, "w");
302 | soft_assert_ret_id(
303 | _manager->data_file_handle != INVALID_HNDL,
304 | "Opening main app data file failed: (%s)!",
305 | strerror(errno)
306 | );
307 |
308 | gettimeofday(&time, INVALID_HNDL);
309 |
310 | data_file_data_t data = {
311 | .now = time.tv_sec,
312 | .total = timer_get_time(timer),
313 | .final = timer_time_spend(timer)
314 | };
315 |
316 | for (index_t idx = 0; idx < sizeof(data_file_data_t); idx++) {
317 | fputc(((bytes_t)&data)[idx], _manager->data_file_handle);
318 | }
319 |
320 |
321 | soft_assert_ret_id(
322 | fclose(_manager->data_file_handle) == 0,
323 | "Failed to close lock file: (%s)!",
324 | strerror(errno)
325 | );
326 |
327 | return B_True;
328 | }
329 |
330 |
331 | bool_t data_manager_clear_data(data_manager_inner_t* manager) {
332 | manager->timer = timer_new();
333 | soft_assert_ret_id(boxed_unbox(&manager->timer.inner) != INVALID_HNDL, "Creating new timer failed!");
334 |
335 | return B_True;
336 | }
337 |
338 |
339 | PUB
340 | data_manager_t data_manager_start(bool_t no_save) {
341 |
342 | boxed_t boxed = boxed_new(sizeof(data_manager_inner_t));
343 | soft_assert_ret(boxed_unbox(&boxed) != INVALID_HNDL, (data_manager_t) {.inner = 0}, "Creating data_manager boxed pointer failed!");
344 |
345 | data_manager_inner_t* new = (data_manager_inner_t*) boxed_unbox(&boxed);
346 |
347 | paths_get(new);
348 | new->saving = !no_save;
349 |
350 | if (new->saving) {
351 | soft_assert_ret(
352 | data_manager_lock_init(new),
353 | (data_manager_t) {.inner = 0},
354 | "Initializing app lock file failed!"
355 | );
356 |
357 | soft_assert_ret(
358 | data_manager_lock(new),
359 | (data_manager_t) {.inner = 0},
360 | "Initializing app lock file failed!"
361 | );
362 | }
363 |
364 | soft_assert_ret(
365 | data_manager_dir_init(new),
366 | (data_manager_t) {.inner = 0},
367 | "Initializing app main directory failed!"
368 | );
369 |
370 | soft_assert_ret(
371 | data_manager_file_init(new),
372 | (data_manager_t) {.inner = 0},
373 | "Initializing app data file failed!"
374 | );
375 |
376 | data_file_lock_t lock = {0};
377 | data_manager_get_lock(new, &lock);
378 |
379 | if (lock.state != DFS_Nothing && new->saving) {
380 | printf(INF_TXT("There are some data from last time Clocker was run.\n"\
381 | "Do you want to resotre and continue that?[Y/n]")
382 | );
383 |
384 | char_t result = fgetc(stdin);
385 | if (result == '\n' || result == 'Y' || result == 'y') {
386 | soft_assert_ret(
387 | data_manager_read_data(new),
388 | (data_manager_t) {.inner = 0},
389 | "Reading data failed!"
390 | );
391 |
392 | return (data_manager_t) (data_manager_t) {.inner = boxed};
393 | }
394 | }
395 |
396 | soft_assert_ret(
397 | data_manager_clear_data(new),
398 | (data_manager_t) {.inner = 0},
399 | "Clearing data failed!"
400 | );
401 |
402 | return (data_manager_t) (data_manager_t) {.inner = boxed};
403 | }
404 |
405 |
406 | PUB
407 | void_t data_manager_stop(data_manager_t manager, clocker_timer_t* timer) {
408 |
409 | data_manager_inner_t* _manager = (data_manager_inner_t*) boxed_unbox(&manager.inner);
410 |
411 | if (_manager->saving) {
412 |
413 | soft_assert_ret_void(
414 | data_manager_save_data(&manager, timer, B_True),
415 | "Initializing app data file failed!"
416 | );
417 |
418 | soft_assert_ret_void(
419 | data_manager_unlock(_manager),
420 | "Initializing app lock file failed!"
421 | );
422 | }
423 |
424 | timer_destroy(_manager->timer);
425 | boxed_destroy(manager.inner);
426 | }
427 |
428 |
429 | PUB
430 | clocker_timer_t* data_manager_get_timer(data_manager_t* manager) {
431 | data_manager_inner_t* _manager = (data_manager_inner_t*) boxed_unbox(&manager->inner);
432 | return &_manager->timer;
433 | }
434 |
435 |
436 | PUB
437 | bool_t data_manager_allow_saving(data_manager_t* manager) {
438 | data_manager_inner_t* _manager = (data_manager_inner_t*) boxed_unbox(&manager->inner);
439 | return _manager->saving;
440 | }
--------------------------------------------------------------------------------
/source/listener.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | #include
28 | #include
29 |
30 | #include
31 | #include
32 |
33 | #include "framework/prims/inc.h"
34 | #include "includes/listener.h"
35 |
36 | typedef struct passwd passwd_t;
37 | typedef struct stat stat_t;
38 | typedef struct dirent dirnet_t;
39 | typedef struct pollfd pollfd_t;
40 |
41 | typedef pthread_t Thread;
42 |
43 |
44 | typedef struct {
45 | i64_t input_file;
46 | bool_t fired;
47 | }
48 | event_listener_t;
49 |
50 |
51 | typedef struct {
52 | event_listener_t mouse;
53 | event_listener_t keyboard;
54 | bool_t flag;
55 | nfds_t nfds;
56 | pollfd_t fds[2];
57 | }
58 | listener_inner_t;
59 |
60 |
61 | listener_t listener_new() {
62 | boxed_t boxed = boxed_new(sizeof(listener_inner_t));
63 | soft_assert_ret(boxed_unbox(&boxed) != INVALID_HNDL, (listener_t) {.inner = 0}, "Allocating new listener failed!");
64 |
65 | listener_inner_t* new = (listener_inner_t*) boxed_unbox(&boxed);
66 |
67 | dirnet_t* dir_net = {0};
68 | DIR* dir = opendir ("/dev/input/by-path");
69 | soft_assert_ret(
70 | dir != INVALID_HNDL,
71 | (listener_t) {.inner = 0},
72 | "Opening /dev/input/by-path/ failed! (%s)",
73 | strerror(errno)
74 | );
75 |
76 | //! FIXME: it's not working on some laptop systems
77 | while (dir_net = readdir(dir)) {
78 |
79 | if (*(dir_net->d_name) != '.') {
80 |
81 | char_t buff[2048] = {0};
82 | sprintf(buff, "/dev/input/by-path/%s", dir_net->d_name);
83 |
84 | if (strstr(dir_net->d_name, "event-mouse") != INVALID_HNDL) {
85 | new->mouse.input_file = open(buff, O_RDONLY);
86 | soft_assert_ret(
87 | new->mouse.input_file != -1,
88 | (listener_t) {.inner = 0},
89 | "Opening mouse input file failed: %s",
90 | strerror(errno)
91 | );
92 | }
93 |
94 | if (strstr(dir_net->d_name, "kbd") != INVALID_HNDL) {
95 | new->keyboard.input_file = open(buff, O_RDONLY);
96 | soft_assert_ret(
97 | new->keyboard.input_file != -1,
98 | (listener_t) {.inner = 0},
99 | "Opening keyboard input file failed: %s",
100 | strerror(errno)
101 | );
102 | }
103 | }
104 | }
105 |
106 | soft_assert_ret(
107 | closedir(dir) == 0,
108 | (listener_t) {.inner = 0},
109 | "Closing directory failed! (%s)",
110 | strerror(errno)
111 | );
112 |
113 | new->keyboard.fired = B_True;
114 | new->mouse.fired = B_True;
115 | new->flag = B_False;
116 |
117 | new->fds[0].fd = new->keyboard.input_file;
118 | new->fds[0].events = POLLIN;
119 |
120 | new->fds[1].fd = new->mouse.input_file;
121 | new->fds[1].events = POLLIN;
122 |
123 | new->nfds = 2;
124 |
125 | return (listener_t) {.inner = boxed};
126 | }
127 |
128 |
129 | void_t listener_listen(listener_t* listener) {
130 |
131 | Thread mouse_thrd = 0;
132 | Thread keyboard_thrd = 0;
133 |
134 | listener_inner_t* _listener = (listener_inner_t*) boxed_unbox(&listener->inner);
135 | size_t counter = 0;
136 |
137 | loop {
138 | int poll_num = poll(_listener->fds, _listener->nfds, 1000);
139 | if (poll_num == 0) {
140 | char_t dot_cont[32] = {0};
141 | if (counter >= 120) {
142 | sprintf(dot_cont, "\r"TRANSPARENT_TXT("idle (%lds)")" > ", counter - 120);
143 | write(STDOUT_FILENO, " ", 32);
144 | write(STDOUT_FILENO, dot_cont, 32);
145 | }
146 | counter++;
147 | continue;
148 | }
149 |
150 | char_t buff[1024] = {0};
151 | if (_listener->fds[0].revents & POLLIN) {
152 | read(_listener->fds[0].fd, buff, 1024);
153 | }
154 | else {
155 | read(_listener->fds[1].fd, buff, 1024);
156 | }
157 |
158 | // printf(">%ld: \n", counter);
159 | return;
160 | }
161 | }
162 |
163 |
164 | i64_t listener_fired(listener_t* listener) {
165 | listener_inner_t* _listener = (listener_inner_t*) boxed_unbox(&listener->inner);
166 | return (_listener->keyboard.fired || _listener->mouse.fired);
167 | }
168 |
169 |
170 | i64_t listener_destroy(listener_t listener) {
171 | boxed_destroy(listener.inner);
172 | }
--------------------------------------------------------------------------------
/source/sig_handler.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include
21 | #include "includes/sig_handler.h"
22 |
23 |
24 | void_t signal_handler(i32_t signal) {
25 |
26 | switch (signal) {
27 |
28 | case SIGINT:
29 | break;
30 |
31 | case SIGSEGV:
32 | printf(ERR_TXT("\n\nSignal 11 (SEGV) was caught by Clocker,\n") \
33 | "Please send bug reports to \n");
34 | _exit(-1);
35 | default:
36 | break;
37 | }
38 | }
--------------------------------------------------------------------------------
/source/time_handler.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include
21 |
22 | #include "includes/time_handler.h"
23 | #include "framework/system/thread/inc.h"
24 | #include "includes/timer.h"
25 | #include "includes/listener.h"
26 | #include "includes/data_manager.h"
27 | #include "includes/thread_arg.h"
28 |
29 |
30 | void_t time_print(clocker_timer_t* main_timer) {
31 |
32 | u64_t total_time = timer_get_time(main_timer);
33 | u64_t total_final_time = timer_time_spend(main_timer);
34 | u64_t total_waste_time = total_time - total_final_time;
35 |
36 | printf(
37 | " The "BOLD_TXT("total")" time is " TRANSPARENT_TXT("=>") INF_TXT(" %ld:%ld:%ld\n"),
38 | total_time / 3600,
39 | (total_time % 3600) / 60,
40 | (total_time % 60)
41 | );
42 |
43 | printf(
44 | " The "BOLD_TXT("waste")" time is " TRANSPARENT_TXT("=>") DNG_TXT(" %ld:%ld:%ld\n"),
45 | total_waste_time / 3600,
46 | (total_waste_time % 3600) / 60,
47 | (total_waste_time % 60)
48 | );
49 |
50 | printf(
51 | " The "BOLD_TXT("final")" time is " TRANSPARENT_TXT("=>") OK_TXT(" %ld:%ld:%ld\n"),
52 | total_final_time / 3600,
53 | (total_final_time % 3600) / 60,
54 | (total_final_time % 60)
55 | );
56 | }
57 |
58 |
59 | handle_t time_handle(guarded_t* arg) {
60 |
61 | // TODO: thread safe
62 |
63 | thread_arg_t* cmd_arg = arg->data.ptr;
64 |
65 | data_manager_t manager = cmd_arg->manager;
66 | time_command_t* command = &cmd_arg->command;
67 | time_mode_t* mode = &cmd_arg->mode;
68 | str_t version = cmd_arg->version;
69 |
70 | // starting temperory timer for calculating waste time
71 | clocker_timer_t tmp_timer = timer_new();
72 |
73 | // starting main timer for calculating whole time
74 | clocker_timer_t* main_timer = data_manager_get_timer(&manager);
75 |
76 | // start main timer
77 | timer_start(main_timer);
78 |
79 | // creating new mouse-keyboard event listener
80 | listener_t listener = listener_new();
81 | if (boxed_unbox(&listener.inner) == INVALID_HNDL) {
82 | debug_err("Creating new listener failed!");
83 | _exit(-1);
84 | }
85 |
86 | bool_t is_paused = B_False;
87 | u64_t saver_count = 0;
88 | do {
89 | // calculating time if is in paused mode
90 | if (is_paused) {
91 | timer_reduce(main_timer, 1);
92 | }
93 |
94 | // calculating time if is in normal mode
95 | else {
96 | // temperory timer start
97 | timer_start(&tmp_timer);
98 |
99 | // listen to keyboard-mouse events
100 | listener_listen(&listener);
101 |
102 | // temperory timer pause
103 | timer_pause(&tmp_timer);
104 |
105 | // calculating waste time
106 | u64_t waste_time = timer_time_spend(&tmp_timer);
107 | if (waste_time > 120 &&
108 | (*mode) != TM_Busy &&
109 | !is_paused
110 | ) {
111 | // if waste time was more than 2 min, reducing main time
112 | timer_reduce(main_timer, waste_time);
113 | }
114 |
115 | // reset temperory timer
116 | timer_reset(&tmp_timer);
117 | }
118 |
119 | // get report
120 | if (*command == TC_Report) {
121 | // first pause main timer
122 | timer_pause(main_timer);
123 |
124 | // print time
125 | time_print(main_timer);
126 |
127 | // reset command
128 | *command = TC_None;
129 |
130 | // resume main timer
131 | timer_resume(main_timer);
132 | }
133 |
134 | // pause time start
135 | if (*command == TC_Pause) {
136 |
137 | // reset command
138 | *command = TC_None;
139 | is_paused = B_True;
140 | }
141 |
142 | // pause time end
143 | if (*command == TC_Resume) {
144 |
145 | // reset command
146 | *command = TC_None;
147 | is_paused = B_False;
148 | }
149 |
150 | if ((saver_count % 5 == 0 &&
151 | *command != TC_Save &&
152 | data_manager_allow_saving(&manager))
153 | ) {
154 | // first pause main timer
155 | timer_pause(main_timer);
156 |
157 | // save time
158 | data_manager_save_data(&manager, main_timer, B_False);
159 |
160 | // reset command
161 | *command = TC_None;
162 |
163 | // resume main timer
164 | timer_resume(main_timer);
165 | }
166 |
167 | // wait for 1 sec
168 | sleep(1);
169 | saver_count++;
170 |
171 | // while listener is fired or program is not terminated, continue
172 | } while (*command != TC_End);
173 |
174 | // outside of the loop, the main time is over!
175 | timer_stop(main_timer);
176 | time_print(main_timer);
177 |
178 | // guarded_unlock(arg);
179 | data_manager_stop(manager, main_timer);
180 | timer_destroy(tmp_timer);
181 | listener_destroy(listener);
182 |
183 | return INVALID_HNDL;
184 | }
185 |
--------------------------------------------------------------------------------
/source/timer.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include "includes/timer.h"
21 |
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | typedef struct timeval time_eval_t;
28 |
29 | typedef struct {
30 | time_eval_t begin;
31 | time_eval_t end;
32 | i64_t spend;
33 | i64_t total_offset;
34 | i64_t spend_offset;
35 | }
36 | _clocker_timer_t;
37 |
38 |
39 | clocker_timer_t timer_new() {
40 | boxed_t boxed = boxed_new(sizeof(_clocker_timer_t));
41 | soft_assert_ret(boxed_unbox(&boxed) != INVALID_HNDL, (clocker_timer_t) {.inner = INVALID_HNDL}, "Creating new timer pointer boxed failed!");
42 |
43 | _clocker_timer_t* new_timer = (_clocker_timer_t*) boxed_unbox(&boxed);
44 |
45 | new_timer->begin = (time_eval_t) {0};
46 | new_timer->end = (time_eval_t) {0};
47 | new_timer->spend = 0;
48 |
49 | new_timer->total_offset = 0;
50 | new_timer->spend_offset = 0;
51 |
52 | return (clocker_timer_t) {
53 | .inner = boxed
54 | };
55 | }
56 |
57 |
58 | void_t timer_init(clocker_timer_t* timer, u64_t time, u64_t spent) {
59 | _clocker_timer_t* _timer = (_clocker_timer_t*) boxed_unbox(&timer->inner);
60 | _timer->total_offset = time;
61 | _timer->spend_offset = spent;
62 | }
63 |
64 |
65 | void_t timer_start(clocker_timer_t* timer) {
66 | _clocker_timer_t* _timer = (_clocker_timer_t*) boxed_unbox(&timer->inner);
67 |
68 | if (_timer->begin.tv_sec == 0) {
69 | gettimeofday(&(_timer->begin), INVALID_HNDL);
70 | _timer->begin.tv_sec -= _timer->total_offset;
71 | }
72 |
73 | return;
74 | }
75 |
76 |
77 | void_t timer_pause(clocker_timer_t* timer) {
78 | _clocker_timer_t* _timer = (_clocker_timer_t*) boxed_unbox(&timer->inner);
79 | i32_t res = gettimeofday(&(_timer->end), INVALID_HNDL);
80 | _timer->spend += (_timer->end.tv_sec - _timer->begin.tv_sec);
81 | }
82 |
83 |
84 | void_t timer_resume(clocker_timer_t* timer) {
85 | _clocker_timer_t* _timer = (_clocker_timer_t*) boxed_unbox(&timer->inner);
86 | _timer->spend -= (_timer->end.tv_sec - _timer->begin.tv_sec);
87 | }
88 |
89 |
90 | void_t timer_stop(clocker_timer_t* timer) {
91 | timer_pause(timer);
92 | }
93 |
94 |
95 | void_t timer_reset(clocker_timer_t* timer) {
96 | _clocker_timer_t* _timer = (_clocker_timer_t*) boxed_unbox(&timer->inner);
97 | // i32_t res = gettimeofday(&(_timer->end), INVALID_HNDL);
98 |
99 | _timer->begin = (time_eval_t) {0};
100 | _timer->end = (time_eval_t) {0};
101 |
102 | _timer->spend_offset = 0;
103 | _timer->total_offset = 0;
104 |
105 | _timer->spend = 0;
106 | }
107 |
108 |
109 | void_t timer_reduce(clocker_timer_t* timer, u64_t seconds) {
110 | _clocker_timer_t* _timer = (_clocker_timer_t*) boxed_unbox(&timer->inner);
111 | _timer->spend -= seconds;
112 | }
113 |
114 |
115 | u64_t timer_get_time(clocker_timer_t* timer) {
116 | _clocker_timer_t* _timer = (_clocker_timer_t*) boxed_unbox(&timer->inner);
117 | return (_timer->end.tv_sec - _timer->begin.tv_sec);
118 | }
119 |
120 |
121 | u64_t timer_time_spend(clocker_timer_t* timer) {
122 | _clocker_timer_t* _timer = (_clocker_timer_t*) boxed_unbox(&timer->inner);
123 | return (u64_t) _timer->spend + _timer->spend_offset - _timer->total_offset;
124 | }
125 |
126 |
127 | void_t timer_destroy(clocker_timer_t timer) {
128 | boxed_destroy(timer.inner);
129 | }
--------------------------------------------------------------------------------
/source/update_checker.c:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2021 rdwn
3 | *
4 | * This file is part of Clocker.
5 | *
6 | * Clocker is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * Clocker is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with Clocker. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | typedef struct passwd passwd_t;
28 | typedef struct stat stat_t;
29 | typedef struct dirent dirnet_t;
30 |
31 | #include "common/defs/program_defs.h"
32 | #include "includes/updater_checker.h"
33 |
34 | typedef struct {
35 | byte_t version[32];
36 | byte_t new_version[32];
37 | }
38 | updater_inner_t;
39 |
40 |
41 | updater_t update_checker_new() {
42 | boxed_t boxed = boxed_new(sizeof(updater_inner_t));
43 | soft_assert_ret(boxed_unbox(&boxed) != INVALID_HNDL, (updater_t) {.inner = 0}, "Allocating new updater failed!");
44 |
45 | updater_inner_t* new = (updater_inner_t*) boxed_unbox(&boxed);
46 |
47 | stat_t stat_var = {0};
48 | passwd_t* home_dir = getpwuid(getuid());
49 | char_t config_file[4096] = {0};
50 |
51 | sprintf(config_file, "%s/%s", home_dir->pw_dir, ".config/clocker/clocker.conf");
52 |
53 | size_t config_file_size = 0;
54 | FILE* config_file_ptr = fopen(config_file, "r");
55 |
56 | if (config_file_ptr == INVALID_HNDL) {
57 |
58 | config_file_ptr = fopen(config_file, "w");
59 | soft_assert_ret(
60 | config_file_ptr != INVALID_HNDL,
61 | (updater_t) {.inner = 0},
62 | "Creating main app config file failed! (%s)",
63 | strerror(errno)
64 | );
65 |
66 | fputs(INIT_VERSION, config_file_ptr);
67 | config_file_size = strlen(INIT_VERSION);
68 |
69 | memcpy(new->version, INIT_VERSION, config_file_size);
70 | }
71 |
72 | else {
73 | // getting data file size
74 | fseek(config_file_ptr, 0L, SEEK_END);
75 | config_file_size = ftell(config_file_ptr);
76 | fseek(config_file_ptr, 0L, SEEK_SET);
77 |
78 | fscanf(config_file_ptr, "%s", new->version);
79 | }
80 |
81 | soft_assert_ret(
82 | fclose(config_file_ptr) == 0,
83 | (updater_t) {.inner = 0},
84 | "Closing main app config failed! (%s)",
85 | strerror(errno)
86 | );
87 |
88 | return (updater_t) {.inner = boxed};
89 | }
90 |
91 |
92 | update_status_t update_checker_check(updater_t* updater) {
93 |
94 | updater_inner_t* _updater = (updater_inner_t*) boxed_unbox(&updater->inner);
95 |
96 | soft_assert_ret_id(
97 | system("wget https://api.github.com/repos/rdwnsjjd/clocker/tags -P /root/.config/clocker/.tags --quiet") == 0,
98 | "Cannot get available version of clocker!"
99 | );
100 |
101 | FILE* tags_file_ptr = fopen("/root/.config/clocker/.tags/tags", "r");
102 | soft_assert_ret_id(tags_file_ptr != INVALID_HNDL, "Tag file is missed!");
103 |
104 | // getting tags file size
105 | fseek(tags_file_ptr, 0L, SEEK_END);
106 | size_t tags_file_size = ftell(tags_file_ptr);
107 | fseek(tags_file_ptr, 0L, SEEK_SET);
108 |
109 | boxed_t boxed = boxed_new(sizeof(byte_t) * tags_file_size);
110 | soft_assert_ret_id(boxed_unbox(&boxed) != INVALID_HNDL, "Creating new boxed pointer failed!");
111 | str_t available_version = (str_t) boxed_unbox(&boxed);
112 |
113 | for (index_t idx = 0; idx < tags_file_size; idx++) {
114 | available_version[idx] = fgetc(tags_file_ptr);
115 | }
116 |
117 | str_t cur = available_version;
118 | bool_t target_expected = B_False;
119 | index_t last_index = 0;
120 |
121 | for (index_t idx = 0; *cur != EOF; cur++, idx++) {
122 |
123 | if (*cur == ' ' || *cur == '\n') {
124 | continue;
125 | }
126 |
127 | if (*cur == 'n' && *(cur + 1) == 'a' && *(cur + 2) == 'm' && *(cur + 3) == 'e') {
128 | cur += 9;
129 | target_expected = B_True;
130 | idx += 9;
131 | last_index = idx;
132 | }
133 |
134 | if (*cur == '\"' && target_expected) {
135 |
136 | size_t len = idx - last_index;
137 |
138 | boxed_t string_boxed = boxed_zeroed_new(1, len);
139 | soft_assert_ret_id(boxed_unbox(&string_boxed) != INVALID_HNDL, "Creating new boxed pointer for string failed!");
140 | str_t string = (str_t) boxed_unbox(&string_boxed);
141 |
142 | memcpy(string, available_version + last_index, len);
143 | if (strncmp(_updater->version, string, 32) == 0) {
144 |
145 | soft_assert_ret_id(
146 | fclose(tags_file_ptr) == 0,
147 | "Closing tags file failed! (%s)",
148 | strerror(errno)
149 | );
150 |
151 | soft_assert_ret_id(
152 | remove("/root/.config/clocker/.tags/tags") == 0,
153 | "Deleting tags file failed (%s)",
154 | strerror(errno)
155 | );
156 |
157 | boxed_destroy(boxed);
158 | boxed_destroy(string_boxed);
159 | return US_NoUpdate;
160 | }
161 |
162 | else {
163 | memcpy(_updater->new_version, string, len);
164 |
165 | soft_assert_ret_id(
166 | fclose(tags_file_ptr) == 0,
167 | "Closing tags file failed! (%s)",
168 | strerror(errno)
169 | );
170 |
171 | soft_assert_ret_id(
172 | remove("/root/.config/clocker/.tags/tags") == 0,
173 | "Deleting tags file failed (%s)",
174 | strerror(errno)
175 | );
176 |
177 | boxed_destroy(boxed);
178 | boxed_destroy(string_boxed);
179 | return US_Updatable;
180 | }
181 |
182 | boxed_destroy(string_boxed);
183 | }
184 | }
185 | boxed_destroy(boxed);
186 | }
187 |
188 |
189 | str_t update_checker_get_version(updater_t* updater) {
190 | updater_inner_t* _updater = (updater_inner_t*) boxed_unbox(&updater->inner);
191 | return _updater->version;
192 | }
193 |
194 |
195 | // str_t update_checker_get_version(updater_t* updater) {
196 | // updater_inner_t* _updater = (updater_inner_t*) boxed_unbox(&updater->inner);
197 | // return _updater->version;
198 | // }
199 |
200 |
201 | str_t update_checker_get_new_tag(updater_t* updater) {
202 | updater_inner_t* _updater = (updater_inner_t*) boxed_unbox(&updater->inner);
203 | return _updater->new_version;
204 | }
205 |
206 |
207 | void_t update_checker_destroy(updater_t updater) {
208 | boxed_destroy(updater.inner);
209 | }
--------------------------------------------------------------------------------