├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── COPYING
├── Makefile
├── README.md
├── i3blocks.1.md
├── i3blocks.conf
├── include
├── bar.h
├── block.h
├── click.h
├── ini.h
├── io.h
├── json.h
├── log.h
└── sched.h
├── scripts
├── bandwidth
├── battery
├── cpu_usage
├── disk
├── iface
├── load_average
├── memory
├── volume
└── wifi
└── src
├── .gitignore
├── bar.c
├── block.c
├── click.c
├── ini.c
├── io.c
├── json.c
├── main.c
└── sched.c
/.gitignore:
--------------------------------------------------------------------------------
1 | i3blocks
2 | i3blocks.1
3 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: c
2 |
3 | env:
4 | - TARGET=all
5 | - TARGET=debug
6 |
7 | compiler:
8 | - gcc
9 |
10 | script:
11 | - make $TARGET
12 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # CHANGELOG
2 |
3 | ## master
4 |
5 | ## 1.4
6 |
7 | * Several scripts improvements and additions.
8 | * Add support for the "markup" property (GH #94).
9 | * Merge contrib scripts into the defaults (GH #36).
10 | * Add support for persistent blocks (GH #85).
11 | * Add support for JSON output format (GH #85).
12 |
13 | ## 1.3
14 |
15 | * Fix click checking (GH #34).
16 | * Use an alarm (if needed), which is more accurate.
17 | * Use real-time signals (SIGRTMIN+1 to SIGRTMAX) for blocks, deprecate
18 | SIGUSR1 and SIGUSR2.
19 | * Implement asynchronous block updates (GH #23).
20 | * Now check for config file ~/.config/i3blocks/config (or
21 | $XDG_CONFIG_HOME/i3blocks/config if set) before ~/.i3blocks.conf (GH #32),
22 | and similar with $XDG_CONFIG_DIRS.
23 | * Add a cpu usage script (GH #11).
24 |
25 | ## 1.2
26 |
27 | * Always define env variables related to clicks. Thus, set them to an empty
28 | string when no click happened (GH #9).
29 |
30 | ## 1.1
31 |
32 | * Change return code for urgency to 33 (GH #8).
33 | * Do not setup stdin if it refers to a tty (GH #7).
34 |
35 | ## 1.0
36 |
37 | Initial release
38 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | RELEASE_VERSION = 1.4
2 |
3 | ifndef PREFIX
4 | PREFIX=/usr/local
5 | endif
6 | ifndef SYSCONFDIR
7 | ifeq ($(PREFIX),/usr)
8 | SYSCONFDIR=/etc
9 | else
10 | SYSCONFDIR=$(PREFIX)/etc
11 | endif
12 | endif
13 | ifndef DATAROOTDIR
14 | DATAROOTDIR=$(PREFIX)/share
15 | endif
16 | ifndef LIBEXECDIR
17 | LIBEXECDIR=$(PREFIX)/libexec
18 | endif
19 | ifndef VERSION
20 | VERSION = $(shell git describe --tags --always 2> /dev/null)
21 | ifeq ($(strip $(VERSION)),)
22 | VERSION = $(RELEASE_VERSION)
23 | endif
24 | endif
25 |
26 | PROGRAM = i3blocks
27 |
28 | CPPFLAGS += -DSYSCONFDIR=\"$(SYSCONFDIR)\"
29 | CPPFLAGS += -DVERSION=\"${VERSION}\"
30 | CFLAGS += -std=gnu99 -Iinclude -Wall -Werror=format-security
31 |
32 | OBJS := $(sort $(wildcard src/*.c))
33 | OBJS := $(OBJS:.c=.o)
34 |
35 | %.o: %.c %.h
36 | $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
37 | @echo " CC $<"
38 |
39 | all: $(PROGRAM)
40 |
41 | debug: CPPFLAGS += -DDEBUG
42 | debug: CFLAGS += -g
43 | debug: $(PROGRAM)
44 |
45 | $(PROGRAM): ${OBJS}
46 | $(CC) $(LDFLAGS) -o $@ $^
47 | @echo " LD $@"
48 |
49 | man: $(PROGRAM).1
50 |
51 | $(PROGRAM).1: $(PROGRAM).1.md
52 | pandoc --to man --standalone --output $@ $<
53 |
54 | clean:
55 | rm -f src/*.o $(PROGRAM)
56 |
57 | install: all
58 | install -m 755 -d $(DESTDIR)$(PREFIX)/bin
59 | install -m 755 -d $(DESTDIR)$(SYSCONFDIR)
60 | install -m 755 -d $(DESTDIR)$(LIBEXECDIR)/$(PROGRAM)
61 | install -m 755 $(PROGRAM) $(DESTDIR)$(PREFIX)/bin/$(PROGRAM)
62 | sed 's,$$SCRIPT_DIR,$(LIBEXECDIR)/$(PROGRAM),' $(PROGRAM).conf > $(DESTDIR)$(SYSCONFDIR)/$(PROGRAM).conf
63 | chmod 644 $(DESTDIR)$(SYSCONFDIR)/$(PROGRAM).conf
64 | install -m 755 scripts/* $(DESTDIR)$(LIBEXECDIR)/$(PROGRAM)/
65 |
66 | install-man: man
67 | install -m 755 -d $(DESTDIR)$(DATAROOTDIR)/man/man1
68 | install -m 644 $(PROGRAM).1 $(DESTDIR)$(DATAROOTDIR)/man/man1
69 |
70 | uninstall:
71 | rm -f $(DESTDIR)$(PREFIX)/bin/$(PROGRAM)
72 | rm -f $(DESTDIR)$(SYSCONFDIR)/$(PROGRAM).conf
73 | rm -rf $(DESTDIR)$(LIBEXECDIR)/$(PROGRAM)
74 |
75 | uninstall-man:
76 | rm -f $(DESTDIR)$(DATAROOTDIR)/man/man1/$(PROGRAM).1
77 |
78 | dist: clean man
79 | ( git ls-files * ; ls $(PROGRAM).1 ) | \
80 | tar -czT - --transform 's,^,$(PROGRAM)-$(VERSION)/,' \
81 | -f $(PROGRAM)-$(VERSION).tar.gz
82 |
83 | distclean: clean
84 | rm -f $(PROGRAM).1 $(PROGRAM)-*.tar.gz
85 |
86 | .PHONY: all clean install uninstall install-man uninstall-man
87 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # i3blocks-gaps
2 |
3 | This repository is a fork of [i3blocks](https://github.com/vivien/i3blocks) made to be compatible with [i3-gaps](https://github.com/Airblader/i3), itself a fork of i3wm.
4 |
5 | ***
6 |
7 | i3blocks is a highly flexible **status line** for the [i3](http://i3wm.org)
8 | window manager. It handles *clicks*, *signals* and *language-agnostic* user
9 | *scripts*.
10 |
11 | The content of each *block* (e.g. time, battery status, network state, ...) is
12 | the output of a *command* provided by the user. Blocks are updated on *click*,
13 | at a given *interval* of time or on a given *signal*, also specified by the
14 | user.
15 |
16 | It aims to respect the
17 | [i3bar protocol](http://i3wm.org/docs/i3bar-protocol.html), providing
18 | customization such as text alignment, urgency, color, and more.
19 |
20 | - - -
21 |
22 | Here is an example of status line, showing the time updated every 5 seconds,
23 | the volume updated only when i3blocks receives a SIGRTMIN+1, and click events.
24 |
25 | ```` ini
26 | [volume]
27 | label=Volume:
28 | command=amixer get Master | grep -E -o '[0-9]{1,3}?%' | head -1
29 | interval=once
30 | signal=1
31 | # use 'pkill -RTMIN+1 i3blocks' after changing the volume
32 |
33 | [time]
34 | command=date '+%D %T'
35 | interval=5
36 |
37 | [clickme]
38 | full_text=Click me!
39 | command=echo button=$BLOCK_BUTTON x=$BLOCK_X y=$BLOCK_Y
40 | min_width=button=1 x=1366 y=768
41 | align=left
42 | ````
43 |
44 | You can use your own scripts, or the
45 | [ones](https://github.com/Airblader/i3blocks-gaps/tree/master/scripts) provided with
46 | i3blocks. Many [community-contributed scripts](https://github.com/vivien/i3blocks-contrib) are also available. Feel free to contribute and improve them!
47 |
48 | The default config will look like this:
49 |
50 | 
51 |
52 | The scripts provided by default may use external tools:
53 |
54 | * `mpstat` (often provided by the *sysstat* package) used by `cpu_usage`.
55 | * `acpi` (often provided by a package of the same name) used by `battery`.
56 |
57 | The user contributed scripts may also use external tools:
58 |
59 | * `playerctl` (available [here](https://github.com/acrisci/playerctl)) used by `mediaplayer`.
60 | * `sensors` (often provided by the *lm-sensors* package) used by `temperature`.
61 |
62 | ## Documentation
63 |
64 | For more information about how it works, please refer to the
65 | [**manpage**](http://vivien.github.io/i3blocks).
66 |
67 | You can also take a look at the
68 | [i3bar protocol](http://i3wm.org/docs/i3bar-protocol.html) to see what
69 | possibilities it offers you.
70 |
71 | Take a look at the [wiki](https://github.com/vivien/i3blocks/wiki) for examples
72 | of blocks and screenshots. If you want to share your ideas and status line,
73 | feel free to edit it!
74 |
75 | ## Installation
76 |
77 | Install i3blocks-gaps from source:
78 |
79 | $ git clone https://github.com/Airblader/i3blocks-gaps i3blocks
80 | $ cd i3blocks
81 | $ make clean debug # or make clean all
82 | # make install
83 |
84 | Note: the generation of the manpage requires the `pandoc` utility, packaged in
85 | common distributions as `pandoc`.
86 |
87 | ### Usage
88 |
89 | * Set your `status_command` in a bar block of your i3 config file:
90 |
91 | bar {
92 | status_command i3blocks
93 | }
94 |
95 | * For customization, copy the default i3blocks.conf into ~/.i3blocks.conf
96 | (e.g. `cp /etc/i3blocks.conf ~/.i3blocks.conf`)
97 | * Restart i3 (e.g. `i3-msg restart`)
98 |
99 | ## Copying
100 |
101 | i3blocks is Copyright (C) 2014 Vivien Didelot
102 | See the file COPYING for information of licensing and distribution.
103 |
--------------------------------------------------------------------------------
/i3blocks.1.md:
--------------------------------------------------------------------------------
1 | % I3BLOCKS(1)
2 |
3 | # NAME
4 | i3blocks - A flexible scheduler for your i3bar blocks
5 |
6 | # SYNOPSIS
7 |
8 | i3blocks [*options*]
9 |
10 | # DESCRIPTION
11 |
12 | **i3blocks** allows one to easily describe blocks in a simple format, and
13 | generate a status line for i3bar(1). It handles clicks, signals and time
14 | interval for user scripts.
15 |
16 | #OPTIONS
17 | -c
18 | : Specifies an alternate configuration file path. By default, i3blocks looks
19 | for configuration files in the following order (note that /etc may be
20 | prefixed with /usr/local depending on the compilation flags):
21 |
22 | 1. ~/.config/i3blocks/config (or $XDG_CONFIG_HOME/i3blocks/config if set)
23 | 2. ~/.i3blocks.conf
24 | 3. /etc/xdg/i3blocks/config (or $XDG_CONFIG_DIRS/i3blocks/config if set)
25 | 4. /etc/i3blocks.conf
26 |
27 | -v
28 | : Log level. This option is cumulative. By default, error messages are
29 | displayed on stderr. Passed once, a failure during an update is shown
30 | within the block. Passed twice enables the debug messages on stderr.
31 |
32 | -V
33 | : Print the version and exit.
34 |
35 | -h
36 | : Print the help message and exit.
37 |
38 | # CONFIGURATION
39 |
40 | The configuration file is an ini file. Each section describes a new block.
41 | A line beginning with a `#` sign is a comment, and empty lines are ignored.
42 | A property is a `key=value` pair per line, with no space around the equal sign.
43 | Properties declared outside a block (i.e. at the beginning of the file)
44 | describe global settings.
45 |
46 | Here is an example config file:
47 |
48 | # This is a comment
49 | interval=5
50 | color=#00FF00
51 |
52 | [weather]
53 | command=~/bin/weather.pl
54 | interval=1800
55 |
56 | [time]
57 | command=date +%T
58 |
59 | To use i3blocks as your status line, define it in a *bar* block of your
60 | `~/i3/config` file:
61 |
62 | bar {
63 | status_command i3blocks
64 | }
65 |
66 | # BLOCK
67 |
68 | The properties used to describe a block are the keys specified in the i3bar
69 | protocol http://i3wm.org/docs/i3bar-protocol.html , plus additional properties
70 | used by **i3blocks** to describe when and how to update a block.
71 | All the supported properties are described below.
72 |
73 | The following keys are standard, see
74 | http://i3wm.org/docs/i3bar-protocol.html
75 | for details.
76 |
77 | - `full_text`
78 |
79 | - `short_text`
80 |
81 | - `color`
82 |
83 | - `background`
84 |
85 | - `border`
86 |
87 | - `min_width`
88 |
89 | - `align`
90 |
91 | - `name`
92 |
93 | - `instance`
94 |
95 | - `urgent`
96 |
97 | - `separator`
98 |
99 | - `separator_block_width`
100 |
101 | - `markup`
102 |
103 | The following keys are specific to **i3blocks**.
104 |
105 | `command`
106 | : The command executed by a shell, used to update the block. The expected
107 | behavior is described below, in the **COMMAND** section.
108 |
109 | `interval`
110 | : If it is a positive integer, then the block is spawned on startup and the
111 | value is used as a time interval in seconds to schedule future updates.
112 | If unspecified or 0, the block won't be executed on startup (which is
113 | useful to simulate buttons).
114 |
115 | If "*once*" (or -1), the block will be executed only on startup (note that a
116 | click or signal will still trigger an update).
117 |
118 | If "*repeat*" (or -2), the block will be spawned on startup, and as soon as
119 | it terminates (useful to repeat blocking commands). Use with caution!
120 |
121 | If "*persist*" (or -3), the block will be executed only on startup, and
122 | updated as soon as it outputs a line. Thus limited to single line updates.
123 |
124 | `signal`
125 | : The signal number used to update the block. All the real-time (think
126 | prioritized and queueable) signals are available to the user. The number is
127 | valid between 1 and N, where SIGRTMIN+N = SIGRTMAX. (Note: there are 31
128 | real-time signals in Linux.) For instance, `signal=10` means that this
129 | block will be updated when **i3blocks** receives SIGRTMIN+10.
130 |
131 | `label`
132 | : An optional label to preprend to the `full_text` after an update.
133 |
134 | `format`
135 | : This property specifies the format of the output text. The default format
136 | is plain text, as described in the **COMMAND** section.
137 | If "json" (or 1) is used, the block output is parsed as JSON.
138 |
139 | # COMMAND
140 |
141 | The value of the `command` key will be passed and executed as is by a shell.
142 |
143 | The standard output of the command line is used to update the block content.
144 | Each non-empty line of the output will overwrite the corresponding property:
145 |
146 | 1. full_text
147 | 2. short_text
148 | 3. color
149 |
150 | For example, this script sets the `full_text` in blue but no `short_text`:
151 |
152 | echo "Here's my label"
153 | echo
154 | echo \#0000FF
155 |
156 | If the command line returns 0 or 33, the block is updated. Otherwise, it is
157 | considered a failure and the first line (if any) is still displayed. Note that
158 | stderr is ignored. A return code of 33 will set the `urgent` flag to true.
159 |
160 | For example, this script prints the battery percentage and sets the urgent flag
161 | if it is below 10%:
162 |
163 | BAT=`acpi -b | grep -E -o '[0-9][0-9]?%'`
164 | echo "BAT: $BAT"
165 | test ${BAT%?} -le 10 && exit 33 || exit 0
166 |
167 | When forking a block command, **i3blocks** will set the environment with some
168 | `BLOCK_*` variables. The following variables are always provided, with
169 | eventually an empty string as the value.
170 |
171 | `BLOCK_NAME`
172 | : The name of the block (usually the section name).
173 |
174 | `BLOCK_INSTANCE`
175 | : An optional argument to the script.
176 |
177 | `BLOCK_BUTTON`
178 | : Mouse button (1, 2 or 3) if the block was clicked.
179 |
180 | `BLOCK_X` and `BLOCK_Y`
181 | : Coordinates where the click occurred, if the block was clicked.
182 |
183 | Here is an example using the environment:
184 |
185 | [block]
186 | command=echo name=$BLOCK_NAME instance=$BLOCK_INSTANCE
187 | interval=1
188 |
189 | [clickme]
190 | full_text=Click me!
191 | command=echo button=$BLOCK_BUTTON x=$BLOCK_X y=$BLOCK_Y
192 | min_width=button=1 x=1366 y=768
193 | align=left
194 |
195 | Note that **i3blocks** provides a set of optional scripts for convenience, such
196 | as network status, battery check, cpu load, volume, etc.
197 |
198 | # EXAMPLES
199 |
200 | As an example, here is a close configuration to i3status(1) default settings:
201 |
202 | **TODO**
203 |
204 | interval=5
205 | signal=10
206 |
207 | [ipv6]
208 |
209 | [free]
210 |
211 | [dhcp]
212 |
213 | [vpn]
214 |
215 | [wifi]
216 |
217 | [ethernet]
218 | min_width=E: 255.255.255.255 (1000 Mbit/s)
219 |
220 | [battery]
221 |
222 | [cpu]
223 |
224 | [datetime]
225 |
226 | The following block shows the usage of `signal` with some i3(1) bindings
227 | which adjust the volume, before issuing a `pkill -RTMIN+1 i3blocks`:
228 |
229 | [volume]
230 | command=echo -n 'Volume: '; amixer get Master | grep -E -o '[0-9][0-9]?%'
231 | interval=once
232 | signal=1
233 | # no interval, only check on SIGRTMIN+1
234 |
235 | Here is an example of a very minimalist config, assuming you have a bunch of
236 | scripts under `~/bin/blocks/` with the same name as the blocks:
237 |
238 | command=~/bin/blocks/$BLOCK_NAME
239 | interval=1
240 |
241 | [free]
242 | [wifi]
243 | [ethernet]
244 | [battery]
245 | [cpu]
246 | [datetime]
247 |
248 | # SEE ALSO
249 |
250 | The development of i3blocks takes place on [Github](https://github.com/vivien/i3blocks).
251 |
252 | The [wiki](https://github.com/vivien/i3blocks/wiki) is a good
253 | source of examples for blocks and screenshots.
254 |
255 | `i3` (1), `i3bar` (1), `i3status` (1)
256 |
257 | # Reporting Bugs
258 |
259 | Please report bugs on the [issue tracker](https://github.com/vivien/i3blocks/issues).
260 |
261 | # Known Bugs
262 |
263 | None.
264 |
265 | # AUTHOR
266 |
267 | Written by Vivien Didelot .
268 |
269 | # COPYRIGHT
270 |
271 | Copyright (C) 2014 Vivien Didelot
272 |
273 | License GPLv3+: GNU GPL version 3 or later .
274 |
275 | This is free software: you are free to change and redistribute it. There is NO
276 | WARRANTY, to the extent permitted by law.
277 |
--------------------------------------------------------------------------------
/i3blocks.conf:
--------------------------------------------------------------------------------
1 | # i3blocks config file
2 | #
3 | # Please see man i3blocks for a complete reference!
4 | # The man page is also hosted at http://vivien.github.io/i3blocks
5 | #
6 | # List of valid properties:
7 | #
8 | # align
9 | # color
10 | # background
11 | # border
12 | # command
13 | # full_text
14 | # instance
15 | # interval
16 | # label
17 | # min_width
18 | # name
19 | # separator
20 | # separator_block_width
21 | # short_text
22 | # signal
23 | # urgent
24 |
25 | # Global properties
26 | #
27 | # The top properties below are applied to every block, but can be overridden.
28 | # Each block command defaults to the script name to avoid boilerplate.
29 | command=$SCRIPT_DIR/$BLOCK_NAME
30 | separator_block_width=15
31 | markup=none
32 |
33 | # Volume indicator
34 | #
35 | # The first parameter sets the step (and units to display)
36 | # The second parameter overrides the mixer selection
37 | # See the script for details.
38 | [volume]
39 | label=VOL
40 | #label=♪
41 | instance=Master
42 | #instance=PCM
43 | interval=once
44 | signal=10
45 |
46 | # Memory usage
47 | #
48 | # The type defaults to "mem" if the instance is not specified.
49 | [memory]
50 | label=MEM
51 | separator=false
52 | interval=30
53 |
54 | [memory]
55 | label=SWAP
56 | instance=swap
57 | separator=false
58 | interval=30
59 |
60 | # Disk usage
61 | #
62 | # The directory defaults to $HOME if the instance is not specified.
63 | # The script may be called with a optional argument to set the alert
64 | # (defaults to 10 for 10%).
65 | [disk]
66 | label=HOME
67 | #instance=/mnt/data
68 | interval=30
69 |
70 | # Network interface monitoring
71 | #
72 | # If the instance is not specified, use the interface used for default route.
73 | # The address can be forced to IPv4 or IPv6 with -4 or -6 switches.
74 | [iface]
75 | #instance=wlan0
76 | color=#00FF00
77 | interval=10
78 | separator=false
79 |
80 | [wifi]
81 | #instance=wlp3s0
82 | interval=10
83 | separator=false
84 |
85 | [bandwidth]
86 | #instance=eth0
87 | interval=5
88 |
89 | # CPU usage
90 | #
91 | # The script may be called with -w and -c switches to specify thresholds,
92 | # see the script for details.
93 | [cpu_usage]
94 | label=CPU
95 | interval=10
96 | min_width=CPU: 100.00%
97 | #separator=false
98 |
99 | #[load_average]
100 | #interval=10
101 |
102 | # Battery indicator
103 | #
104 | # The battery instance defaults to 0.
105 | [battery]
106 | label=BAT
107 | #label=⚡
108 | #instance=1
109 | interval=30
110 |
111 | # Date Time
112 | #
113 | [time]
114 | command=date '+%Y-%m-%d %H:%M:%S'
115 | interval=5
116 |
--------------------------------------------------------------------------------
/include/bar.h:
--------------------------------------------------------------------------------
1 | /*
2 | * bar.h - definition of a status line handling functions
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #ifndef _BAR_H
20 | #define _BAR_H
21 |
22 | struct bar {
23 | struct block *blocks;
24 | unsigned int num;
25 | };
26 |
27 | void bar_poll_timed(struct bar *);
28 | void bar_poll_clicked(struct bar *);
29 | void bar_poll_outdated(struct bar *);
30 | void bar_poll_signaled(struct bar *, int);
31 | void bar_poll_exited(struct bar *);
32 | void bar_poll_readable(struct bar *, int);
33 |
34 | #endif /* _BAR_H */
35 |
--------------------------------------------------------------------------------
/include/block.h:
--------------------------------------------------------------------------------
1 | /*
2 | * block.h - definition of a block
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #ifndef _BLOCK_H
20 | #define _BLOCK_H
21 |
22 | #include
23 |
24 | #include "click.h"
25 | #include "log.h"
26 |
27 | #define PROP_I3BAR 1 /* See http://i3wm.org/docs/i3bar-protocol.html */
28 | #define PROP_GAPS 1 /* See https://github.com/Airblader/i3/blob/gaps/docs/i3bar-protocol */
29 | #define PROP_STRING 2
30 | #define PROP_NUMBER 4
31 | #define PROP_BOOLEAN 8
32 |
33 | #define INTER_ONCE -1
34 | #define INTER_REPEAT -2
35 | #define INTER_PERSIST -3
36 |
37 | #define FORMAT_PLAIN 0
38 | #define FORMAT_JSON 1
39 |
40 | /* Block command exit codes */
41 | #define EXIT_URGENT '!' /* 33 */
42 | #define EXIT_ERR_INTERNAL 66
43 |
44 | #define PROPERTIES(_) \
45 | _(full_text, 1024, PROP_I3BAR | PROP_STRING) \
46 | _(short_text, 512, PROP_I3BAR | PROP_STRING) \
47 | _(color, 10, PROP_I3BAR | PROP_STRING) \
48 | _(background, 10, PROP_I3BAR | PROP_STRING) \
49 | _(border, 10, PROP_I3BAR | PROP_STRING) \
50 | _(min_width, 1024, PROP_I3BAR | PROP_STRING | PROP_NUMBER) \
51 | _(align, 8, PROP_I3BAR | PROP_STRING) \
52 | _(name, 32, PROP_I3BAR | PROP_STRING) \
53 | _(instance, 256, PROP_I3BAR | PROP_STRING) \
54 | _(urgent, 8, PROP_I3BAR | PROP_BOOLEAN) \
55 | _(separator, 8, PROP_I3BAR | PROP_BOOLEAN) \
56 | _(separator_block_width, 8, PROP_I3BAR | PROP_NUMBER) \
57 | _(markup, 8, PROP_I3BAR | PROP_STRING) \
58 | _(command, 1024, PROP_STRING) \
59 | _(interval, 8, PROP_STRING | PROP_NUMBER) \
60 | _(signal, 8, PROP_NUMBER) \
61 | _(label, 32, PROP_STRING) \
62 | _(format, 8, PROP_STRING | PROP_NUMBER) \
63 | _(border_top, 8, PROP_GAPS | PROP_NUMBER) \
64 | _(border_bottom, 8, PROP_GAPS | PROP_NUMBER) \
65 | _(border_left, 8, PROP_GAPS | PROP_NUMBER) \
66 | _(border_right, 8, PROP_GAPS | PROP_NUMBER) \
67 |
68 | struct properties {
69 | #define DEFINE(_name, _size, _flags) char _name[_size];
70 | PROPERTIES(DEFINE);
71 | #undef DEFINE
72 | };
73 |
74 | struct block {
75 | struct properties default_props;
76 | struct properties updated_props;
77 |
78 | /* Shortcuts */
79 | int interval;
80 | unsigned signal;
81 | unsigned format;
82 |
83 | /* Runtime info */
84 | unsigned long timestamp;
85 | pid_t pid;
86 | int out, err;
87 | };
88 |
89 | /* Shortcuts to config */
90 | #define NAME(_block) (_block->default_props.name)
91 | #define INSTANCE(_block) (_block->default_props.instance)
92 | #define COMMAND(_block) (_block->default_props.command)
93 | #define LABEL(_block) (_block->default_props.label)
94 | #define INTERVAL(_block) (_block->default_props.interval)
95 |
96 | /* Shortcuts to update */
97 | #define FULL_TEXT(_block) (_block->updated_props.full_text)
98 |
99 | #define bdebug(block, msg, ...) \
100 | debug("[%s] " msg, NAME(block), ##__VA_ARGS__)
101 |
102 | #define berror(block, msg, ...) \
103 | error("[%s] " msg, NAME(block), ##__VA_ARGS__)
104 |
105 | #define berrorx(block, msg, ...) \
106 | errorx("[%s] " msg, NAME(block), ##__VA_ARGS__)
107 |
108 | void block_setup(struct block *);
109 | void block_spawn(struct block *, struct click *);
110 | void block_reap(struct block *);
111 | void block_update(struct block *);
112 |
113 | #endif /* _BLOCK_H */
114 |
--------------------------------------------------------------------------------
/include/click.h:
--------------------------------------------------------------------------------
1 | /*
2 | * click.h - definition of click parsing functions
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #ifndef _CLICK_H
20 | #define _CLICK_H
21 |
22 | struct click {
23 | char *name;
24 | char *instance;
25 | char *button;
26 | char *x;
27 | char *y;
28 | };
29 |
30 | void click_parse(char *, struct click *);
31 |
32 | #endif /* _CLICK_H */
33 |
--------------------------------------------------------------------------------
/include/ini.h:
--------------------------------------------------------------------------------
1 | /*
2 | * ini.h - INI parsing header
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #ifndef _INI_H
20 | #define _INI_H
21 |
22 | #include "bar.h"
23 |
24 | struct bar *ini_load(const char *);
25 |
26 | #endif /* _INI_H */
27 |
--------------------------------------------------------------------------------
/include/io.h:
--------------------------------------------------------------------------------
1 | /*
2 | * io.h - header for non-blocking I/O operations
3 | * Copyright (C) 2015 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #ifndef _IO_H
20 | #define _IO_H
21 |
22 | int io_signal(int, int);
23 | int io_readline(int, char *, size_t);
24 |
25 | #endif /* _IO_H */
26 |
--------------------------------------------------------------------------------
/include/json.h:
--------------------------------------------------------------------------------
1 | /*
2 | * json.h - JSON manipulation header
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #ifndef _JSON_H
20 | #define _JSON_H
21 |
22 | #include "bar.h"
23 | #include "block.h"
24 |
25 | void json_parse(const char *json, const char *name, int *start, int *len);
26 | void json_print_bar(struct bar *);
27 |
28 | #endif /* _JSON_H */
29 |
30 |
--------------------------------------------------------------------------------
/include/log.h:
--------------------------------------------------------------------------------
1 | /*
2 | * log.h - debug and error printing macros
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #ifndef _LOG_H
20 | #define _LOG_H
21 |
22 | #include
23 | #include
24 | #include
25 |
26 | extern unsigned log_level;
27 |
28 | enum log_level {
29 | LOG_NORMAL,
30 | LOG_WARN,
31 | LOG_DEBUG,
32 | };
33 |
34 | #define debug(msg, ...) \
35 | if (log_level >= LOG_DEBUG) { \
36 | fprintf(stderr, "DEBUG %s:%d: " msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
37 | }
38 |
39 | #define error(msg, ...) \
40 | fprintf(stderr, "ERROR %s:%d: " msg "\n", __func__, __LINE__, ##__VA_ARGS__)
41 |
42 | #define errorx(msg, ...) \
43 | error(msg ": %s", ##__VA_ARGS__, strerror(errno))
44 |
45 | #endif /* _LOG_H */
46 |
--------------------------------------------------------------------------------
/include/sched.h:
--------------------------------------------------------------------------------
1 | /*
2 | * sched.h - scheduling header
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #ifndef _SCHED_H
20 | #define _SCHED_H
21 |
22 | int sched_init(struct bar *);
23 | void sched_start(struct bar *);
24 |
25 | #endif /* _SCHED_H */
26 |
--------------------------------------------------------------------------------
/scripts/bandwidth:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # Copyright (C) 2012 Stefan Breunig
3 | # Copyright (C) 2014 kaueraal
4 | # Copyright (C) 2015 Thiago Perrotta
5 |
6 | # This program 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 | # This program 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 this program. If not, see .
18 |
19 | # Get custom IN and OUT labels if provided by command line arguments
20 | while [[ $# -gt 1 ]]; do
21 | key="$1"
22 | case "$key" in
23 | -i|--inlabel)
24 | INLABEL="$2"
25 | shift;;
26 | -o|--outlabel)
27 | OUTLABEL="$2"
28 | shift;;
29 | esac
30 | shift
31 | done
32 |
33 | [[ -z $INLABEL ]] && INLABEL="IN "
34 | [[ -z $OUTLABEL ]] && OUTLABEL="OUT "
35 |
36 | # Use the provided interface, otherwise the device used for the default route.
37 | if [[ -n $BLOCK_INSTANCE ]]; then
38 | INTERFACE=$BLOCK_INSTANCE
39 | else
40 | INTERFACE=$(ip route | awk '/^default/ { print $5 ; exit }')
41 | fi
42 |
43 | # Issue #36 compliant.
44 | if ! [ -e "/sys/class/net/${INTERFACE}/operstate" ] || ! [ "`cat /sys/class/net/${INTERFACE}/operstate`" = "up" ]
45 | then
46 | echo "$INTERFACE down"
47 | echo "$INTERFACE down"
48 | echo "#FF0000"
49 | exit 0
50 | fi
51 |
52 | # path to store the old results in
53 | path="/dev/shm/$(basename $0)-${INTERFACE}"
54 |
55 | # grabbing data for each adapter.
56 | read rx < "/sys/class/net/${INTERFACE}/statistics/rx_bytes"
57 | read tx < "/sys/class/net/${INTERFACE}/statistics/tx_bytes"
58 |
59 | # get time
60 | time=$(date +%s)
61 |
62 | # write current data if file does not exist. Do not exit, this will cause
63 | # problems if this file is sourced instead of executed as another process.
64 | if ! [[ -f "${path}" ]]; then
65 | echo "${time} ${rx} ${tx}" > "${path}"
66 | chmod 0666 "${path}"
67 | fi
68 |
69 | # read previous state and update data storage
70 | read old < "${path}"
71 | echo "${time} ${rx} ${tx}" > "${path}"
72 |
73 | # parse old data and calc time passed
74 | old=(${old//;/ })
75 | time_diff=$(( $time - ${old[0]} ))
76 |
77 | # sanity check: has a positive amount of time passed
78 | [[ "${time_diff}" -gt 0 ]] || exit
79 |
80 | # calc bytes transferred, and their rate in byte/s
81 | rx_diff=$(( $rx - ${old[1]} ))
82 | tx_diff=$(( $tx - ${old[2]} ))
83 | rx_rate=$(( $rx_diff / $time_diff ))
84 | tx_rate=$(( $tx_diff / $time_diff ))
85 |
86 | # shift by 10 bytes to get KiB/s. If the value is larger than
87 | # 1024^2 = 1048576, then display MiB/s instead
88 |
89 | # incoming
90 | echo -n "$INLABEL"
91 | rx_kib=$(( $rx_rate >> 10 ))
92 | if hash bc 2>/dev/null && [[ "$rx_rate" -gt 1048576 ]]; then
93 | printf '%sM' "`echo "scale=1; $rx_kib / 1024" | bc`"
94 | else
95 | echo -n "${rx_kib}K"
96 | fi
97 |
98 | echo -n " "
99 |
100 | # outgoing
101 | echo -n "$OUTLABEL"
102 | tx_kib=$(( $tx_rate >> 10 ))
103 | if hash bc 2>/dev/null && [[ "$tx_rate" -gt 1048576 ]]; then
104 | printf '%sM' "`echo "scale=1; $tx_kib / 1024" | bc`"
105 | else
106 | echo -n "${tx_kib}K"
107 | fi
108 |
--------------------------------------------------------------------------------
/scripts/battery:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 | #
3 | # Copyright 2014 Pierre Mavro
4 | # Copyright 2014 Vivien Didelot
5 | #
6 | # Licensed under the terms of the GNU GPL v3, or any later version.
7 | #
8 | # This script is meant to use with i3blocks. It parses the output of the "acpi"
9 | # command (often provided by a package of the same name) to read the status of
10 | # the battery, and eventually its remaining time (to full charge or discharge).
11 | #
12 | # The color will gradually change for a percentage below 85%, and the urgency
13 | # (exit code 33) is set if there is less that 5% remaining.
14 |
15 | use strict;
16 | use warnings;
17 | use utf8;
18 |
19 | my $acpi;
20 | my $status;
21 | my $percent;
22 | my $ac_adapt;
23 | my $full_text;
24 | my $short_text;
25 | my $bat_number = $ENV{BLOCK_INSTANCE} || 0;
26 |
27 | # read the first line of the "acpi" command output
28 | open (ACPI, "acpi -b | grep 'Battery $bat_number' |") or die;
29 | $acpi = ;
30 | close(ACPI);
31 |
32 | # fail on unexpected output
33 | if ($acpi !~ /: (\w+), (\d+)%/) {
34 | die "$acpi\n";
35 | }
36 |
37 | $status = $1;
38 | $percent = $2;
39 | $full_text = "$percent%";
40 |
41 | if ($status eq 'Discharging') {
42 | $full_text .= ' DIS';
43 | } elsif ($status eq 'Charging') {
44 | $full_text .= ' CHR';
45 | } elsif ($status eq 'Unknown') {
46 | open (AC_ADAPTER, "acpi -a |") or die;
47 | $ac_adapt = ;
48 | close(AC_ADAPTER);
49 |
50 | if ($ac_adapt =~ /: ([\w-]+)/) {
51 | $ac_adapt = $1;
52 |
53 | if ($ac_adapt eq 'on-line') {
54 | $full_text .= ' CHR';
55 | } elsif ($ac_adapt eq 'off-line') {
56 | $full_text .= ' DIS';
57 | }
58 | }
59 | }
60 |
61 | $short_text = $full_text;
62 |
63 | if ($acpi =~ /(\d\d:\d\d):/) {
64 | $full_text .= " ($1)";
65 | }
66 |
67 | # print text
68 | print "$full_text\n";
69 | print "$short_text\n";
70 |
71 | # consider color and urgent flag only on discharge
72 | if ($status eq 'Discharging') {
73 |
74 | if ($percent < 20) {
75 | print "#FF0000\n";
76 | } elsif ($percent < 40) {
77 | print "#FFAE00\n";
78 | } elsif ($percent < 60) {
79 | print "#FFF600\n";
80 | } elsif ($percent < 85) {
81 | print "#A8FF00\n";
82 | }
83 |
84 | if ($percent < 5) {
85 | exit(33);
86 | }
87 | }
88 |
89 | exit(0);
90 |
--------------------------------------------------------------------------------
/scripts/cpu_usage:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl
2 | #
3 | # Copyright 2014 Pierre Mavro
4 | # Copyright 2014 Vivien Didelot
5 | # Copyright 2014 Andreas Guldstrand
6 | #
7 | # Licensed under the terms of the GNU GPL v3, or any later version.
8 |
9 | use strict;
10 | use warnings;
11 | use utf8;
12 | use Getopt::Long;
13 |
14 | # default values
15 | my $t_warn = 50;
16 | my $t_crit = 80;
17 | my $cpu_usage = -1;
18 | my $decimals = 2;
19 |
20 | sub help {
21 | print "Usage: cpu_usage [-w ] [-c ] [-d ]\n";
22 | print "-w : warning threshold to become yellow\n";
23 | print "-c : critical threshold to become red\n";
24 | print "-d : Use decimals for percentage (default is $decimals) \n";
25 | exit 0;
26 | }
27 |
28 | GetOptions("help|h" => \&help,
29 | "w=i" => \$t_warn,
30 | "c=i" => \$t_crit,
31 | "d=i" => \$decimals,
32 | );
33 |
34 | # Get CPU usage
35 | $ENV{LC_ALL}="en_US"; # if mpstat is not run under en_US locale, things may break, so make sure it is
36 | open (MPSTAT, 'mpstat 1 1 |') or die;
37 | while () {
38 | if (/^.*\s+(\d+\.\d+)\s+$/) {
39 | $cpu_usage = 100 - $1; # 100% - %idle
40 | last;
41 | }
42 | }
43 | close(MPSTAT);
44 |
45 | $cpu_usage eq -1 and die 'Can\'t find CPU information';
46 |
47 | # Print short_text, full_text
48 | printf "%.${decimals}f%%\n", $cpu_usage;
49 | printf "%.${decimals}f%%\n", $cpu_usage;
50 |
51 | # Print color, if needed
52 | if ($cpu_usage >= $t_crit) {
53 | print "#FF0000\n";
54 | exit 33;
55 | } elsif ($cpu_usage >= $t_warn) {
56 | print "#FFFC00\n";
57 | }
58 |
59 | exit 0;
60 |
--------------------------------------------------------------------------------
/scripts/disk:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # Copyright (C) 2014 Julien Bonjean
3 |
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 |
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 |
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | DIR="${BLOCK_INSTANCE:-$HOME}"
18 | ALERT_LOW="${1:-10}" # color will turn red under this value (default: 10%)
19 |
20 | df -h -P -l "$DIR" | awk -v alert_low=$ALERT_LOW '
21 | /\/.*/ {
22 | # full text
23 | print $4
24 |
25 | # short text
26 | print $4
27 |
28 | use=$5
29 |
30 | # no need to continue parsing
31 | exit 0
32 | }
33 |
34 | END {
35 | gsub(/%$/,"",use)
36 | if (100 - use < alert_low) {
37 | # color
38 | print "#FF0000"
39 | }
40 | }
41 | '
42 |
--------------------------------------------------------------------------------
/scripts/iface:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # Copyright (C) 2014 Julien Bonjean
3 | # Copyright (C) 2014 Alexander Keller
4 |
5 | # This program 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 | # This program 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 this program. If not, see .
17 |
18 | #------------------------------------------------------------------------
19 |
20 | # Use the provided interface, otherwise the device used for the default route.
21 | if [[ -n $BLOCK_INSTANCE ]]; then
22 | IF=$BLOCK_INSTANCE
23 | else
24 | IF=$(ip route | awk '/^default/ { print $5 ; exit }')
25 | fi
26 |
27 | #------------------------------------------------------------------------
28 |
29 | # As per #36 -- It is transparent: e.g. if the machine has no battery or wireless
30 | # connection (think desktop), the corresponding block should not be displayed.
31 | [[ ! -d /sys/class/net/${IF} ]] && exit
32 |
33 | #------------------------------------------------------------------------
34 |
35 | if [[ "$(cat /sys/class/net/$IF/operstate)" = 'down' ]]; then
36 | echo down # full text
37 | echo down # short text
38 | echo \#FF0000 # color
39 | exit
40 | fi
41 |
42 | case $1 in
43 | -4)
44 | AF=inet ;;
45 | -6)
46 | AF=inet6 ;;
47 | *)
48 | AF=inet6? ;;
49 | esac
50 |
51 | # if no interface is found, use the first device with a global scope
52 | IPADDR=$(ip addr show $IF | perl -n -e "/$AF ([^\/]+).* scope global/ && print \$1 and exit")
53 |
54 | case $BLOCK_BUTTON in
55 | 3) echo -n "$IPADDR" | xclip -q -se c ;;
56 | esac
57 |
58 | #------------------------------------------------------------------------
59 |
60 | echo "$IPADDR" # full text
61 | echo "$IPADDR" # short text
62 |
--------------------------------------------------------------------------------
/scripts/load_average:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # Copyright (C) 2014 Julien Bonjean
3 |
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 |
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 |
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | load="$(cut -d ' ' -f1 /proc/loadavg)"
18 | cpus="$(nproc)"
19 |
20 | # full text
21 | echo "$load"
22 |
23 | # short text
24 | echo "$load"
25 |
26 | # color if load is too high
27 | awk -v cpus=$cpus -v cpuload=$load '
28 | BEGIN {
29 | if (cpus <= cpuload) {
30 | print "#FF0000";
31 | exit 33;
32 | }
33 | }
34 | '
35 |
--------------------------------------------------------------------------------
/scripts/memory:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # Copyright (C) 2014 Julien Bonjean
3 |
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 |
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 |
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | TYPE="${BLOCK_INSTANCE:-mem}"
18 |
19 | awk -v type=$TYPE '
20 | /^MemTotal:/ {
21 | mem_total=$2
22 | }
23 | /^MemFree:/ {
24 | mem_free=$2
25 | }
26 | /^Buffers:/ {
27 | mem_free+=$2
28 | }
29 | /^Cached:/ {
30 | mem_free+=$2
31 | }
32 | /^SwapTotal:/ {
33 | swap_total=$2
34 | }
35 | /^SwapFree:/ {
36 | swap_free=$2
37 | }
38 | END {
39 | if (type == "swap") {
40 | free=swap_free/1024/1024
41 | used=(swap_total-swap_free)/1024/1024
42 | total=swap_total/1024/1024
43 | } else {
44 | free=mem_free/1024/1024
45 | used=(mem_total-mem_free)/1024/1024
46 | total=mem_total/1024/1024
47 | }
48 | pct=used/total*100
49 |
50 | # full text
51 | printf("%.1fG/%.1fG (%.f%%)\n", used, total, pct)
52 |
53 | # short text
54 | printf("%.f%%\n", pct)
55 |
56 | # color
57 | if (pct > 90) {
58 | print("#FF0000\n")
59 | } else if (pct > 80) {
60 | print("#FFAE00\n")
61 | } else if (pct > 70) {
62 | print("#FFF600\n")
63 | }
64 | }
65 | ' /proc/meminfo
66 |
--------------------------------------------------------------------------------
/scripts/volume:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # Copyright (C) 2014 Julien Bonjean
3 | # Copyright (C) 2014 Alexander Keller
4 |
5 | # This program 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 | # This program 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 this program. If not, see .
17 |
18 | #------------------------------------------------------------------------
19 |
20 | # The second parameter overrides the mixer selection
21 | # For PulseAudio users, use "pulse"
22 | # For Jack/Jack2 users, use "jackplug"
23 | # For ALSA users, you may use "default" for your primary card
24 | # or you may use hw:# where # is the number of the card desired
25 | MIXER="default"
26 | [ -n "$(lsmod | grep pulse)" ] && MIXER="pulse"
27 | [ -n "$(lsmod | grep jack)" ] && MIXER="jackplug"
28 | MIXER="${2:-$MIXER}"
29 |
30 | # The instance option sets the control to report and configure
31 | # This defaults to the first control of your selected mixer
32 | # For a list of the available, use `amixer -D $Your_Mixer scontrols`
33 | SCONTROL="${BLOCK_INSTANCE:-$(amixer -D $MIXER scontrols |
34 | sed -n "s/Simple mixer control '\([A-Za-z ]*\)',0/\1/p" |
35 | head -n1
36 | )}"
37 |
38 | # The first parameter sets the step to change the volume by (and units to display)
39 | # This may be in in % or dB (eg. 5% or 3dB)
40 | STEP="${1:-5%}"
41 |
42 | #------------------------------------------------------------------------
43 |
44 | capability() { # Return "Capture" if the device is a capture device
45 | amixer -D $MIXER get $SCONTROL |
46 | sed -n "s/ Capabilities:.*cvolume.*/Capture/p"
47 | }
48 |
49 | volume() {
50 | amixer -D $MIXER get $SCONTROL $(capability)
51 | }
52 |
53 | format() {
54 | perl_filter='if (/.*\[(\d+%)\] (\[(-?\d+.\d+dB)\] )?\[(on|off)\]/)'
55 | perl_filter+='{CORE::say $4 eq "off" ? "MUTE" : "'
56 | # If dB was selected, print that instead
57 | perl_filter+=$([[ $STEP = *dB ]] && echo '$3' || echo '$1')
58 | perl_filter+='"; exit}'
59 | perl -ne "$perl_filter"
60 | }
61 |
62 | #------------------------------------------------------------------------
63 |
64 | case $BLOCK_BUTTON in
65 | 3) amixer -q -D $MIXER sset $SCONTROL $(capability) toggle ;; # right click, mute/unmute
66 | 4) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}+ unmute ;; # scroll up, increase
67 | 5) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}- unmute ;; # scroll down, decrease
68 | esac
69 |
70 | volume | format
71 |
--------------------------------------------------------------------------------
/scripts/wifi:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # Copyright (C) 2014 Alexander Keller
3 |
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 |
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU General Public License for more details.
13 |
14 | # You should have received a copy of the GNU General Public License
15 | # along with this program. If not, see .
16 |
17 | #------------------------------------------------------------------------
18 |
19 | INTERFACE="${BLOCK_INSTANCE:-wlan0}"
20 |
21 | #------------------------------------------------------------------------
22 |
23 | # As per #36 -- It is transparent: e.g. if the machine has no battery or wireless
24 | # connection (think desktop), the corresponding block should not be displayed.
25 | [[ ! -d /sys/class/net/${INTERFACE}/wireless ]] ||
26 | [[ "$(cat /sys/class/net/$INTERFACE/operstate)" = 'down' ]] && exit
27 |
28 | #------------------------------------------------------------------------
29 |
30 | QUALITY=$(grep $INTERFACE /proc/net/wireless | awk '{ print int($3 * 100 / 70) }')
31 |
32 | #------------------------------------------------------------------------
33 |
34 | echo $QUALITY% # full text
35 | echo $QUALITY% # short text
36 |
37 | # color
38 | if [[ $QUALITY -ge 80 ]]; then
39 | echo "#00FF00"
40 | elif [[ $QUALITY -ge 60 ]]; then
41 | echo "#FFF600"
42 | elif [[ $QUALITY -ge 40 ]]; then
43 | echo "#FFAE00"
44 | else
45 | echo "#FF0000"
46 | fi
47 |
--------------------------------------------------------------------------------
/src/.gitignore:
--------------------------------------------------------------------------------
1 | *.o
2 |
--------------------------------------------------------------------------------
/src/bar.c:
--------------------------------------------------------------------------------
1 | /*
2 | * bar.c - status line handling functions
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | #include "bar.h"
28 | #include "block.h"
29 | #include "click.h"
30 | #include "io.h"
31 | #include "log.h"
32 |
33 | void
34 | bar_poll_timed(struct bar *bar)
35 | {
36 | for (int i = 0; i < bar->num; ++i) {
37 | struct block *block = bar->blocks + i;
38 |
39 | /* spawn unless it is only meant for click or signal */
40 | if (block->interval != 0)
41 | block_spawn(block, NULL);
42 | }
43 | }
44 |
45 | void
46 | bar_poll_clicked(struct bar *bar)
47 | {
48 | char json[1024] = { 0 };
49 |
50 | while (io_readline(STDIN_FILENO, json, sizeof(json)) > 0) {
51 | struct click click;
52 |
53 | /* find the corresponding block */
54 | click_parse(json, &click);
55 | if (!*click.name && !*click.instance)
56 | continue;
57 |
58 | for (int i = 0; i < bar->num; ++i) {
59 | struct block *block = bar->blocks + i;
60 |
61 | if (strcmp(NAME(block), click.name) == 0 && strcmp(INSTANCE(block), click.instance) == 0) {
62 | bdebug(block, "clicked");
63 | block_spawn(block, &click);
64 | break; /* Unlikely to click several blocks */
65 | }
66 | }
67 | }
68 | }
69 |
70 | void
71 | bar_poll_outdated(struct bar *bar)
72 | {
73 | for (int i = 0; i < bar->num; ++i) {
74 | struct block *block = bar->blocks + i;
75 |
76 | if (block->interval > 0) {
77 | const unsigned long now = time(NULL);
78 | const unsigned long next_update = block->timestamp + block->interval;
79 |
80 | if (((long) (next_update - now)) <= 0) {
81 | bdebug(block, "outdated");
82 | block_spawn(block, NULL);
83 | }
84 | }
85 | }
86 | }
87 |
88 | void
89 | bar_poll_signaled(struct bar *bar, int sig)
90 | {
91 | for (int i = 0; i < bar->num; ++i) {
92 | struct block *block = bar->blocks + i;
93 |
94 | if (block->signal == sig) {
95 | bdebug(block, "signaled");
96 | block_spawn(block, NULL);
97 | }
98 | }
99 | }
100 |
101 | void
102 | bar_poll_exited(struct bar *bar)
103 | {
104 | for (;;) {
105 | siginfo_t infop = { 0 };
106 |
107 | /* Non-blocking check for dead child(ren) */
108 | if (waitid(P_ALL, 0, &infop, WEXITED | WNOHANG | WNOWAIT) == -1)
109 | if (errno != ECHILD)
110 | errorx("waitid");
111 |
112 | /* Error or no (dead yet) child(ren) */
113 | if (infop.si_pid == 0)
114 | break;
115 |
116 | /* Find the dead process */
117 | for (int i = 0; i < bar->num; ++i) {
118 | struct block *block = bar->blocks + i;
119 |
120 | if (block->pid == infop.si_pid) {
121 | bdebug(block, "exited");
122 | block_reap(block);
123 | if (block->interval == INTER_REPEAT) {
124 | if (block->timestamp == time(NULL))
125 | berror(block, "loop too fast");
126 | block_spawn(block, NULL);
127 | } else if (block->interval == INTER_PERSIST) {
128 | bdebug(block, "unexpected exit?");
129 | }
130 | break;
131 | }
132 | }
133 | }
134 | }
135 |
136 | void
137 | bar_poll_readable(struct bar *bar, const int fd)
138 | {
139 | for (int i = 0; i < bar->num; ++i) {
140 | struct block *block = bar->blocks + i;
141 |
142 | if (block->out == fd) {
143 | bdebug(block, "readable");
144 | block_update(block);
145 | break;
146 | }
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/src/block.c:
--------------------------------------------------------------------------------
1 | /*
2 | * block.c - update of a single status line block
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 |
29 | #include "block.h"
30 | #include "click.h"
31 | #include "io.h"
32 | #include "json.h"
33 | #include "log.h"
34 |
35 | static void
36 | child_setenv(struct block *block, const char *name, const char *value)
37 | {
38 | if (setenv(name, value, 1) == -1) {
39 | berrorx(block, "setenv(%s=%s)", name, value);
40 | _exit(EXIT_ERR_INTERNAL);
41 | }
42 | }
43 |
44 | static void
45 | child_setup_env(struct block *block, struct click *click)
46 | {
47 | child_setenv(block, "BLOCK_NAME", NAME(block));
48 | child_setenv(block, "BLOCK_INSTANCE", INSTANCE(block));
49 | child_setenv(block, "BLOCK_INTERVAL", INTERVAL(block));
50 | child_setenv(block, "BLOCK_BUTTON", click ? click->button : "");
51 | child_setenv(block, "BLOCK_X", click ? click->x : "");
52 | child_setenv(block, "BLOCK_Y", click ? click->y : "");
53 | }
54 |
55 | static void
56 | child_reset_signals(struct block *block)
57 | {
58 | sigset_t set;
59 |
60 | if (sigfillset(&set) == -1) {
61 | berrorx(block, "sigfillset");
62 | _exit(EXIT_ERR_INTERNAL);
63 | }
64 |
65 | /* It should be safe to assume that all signals are unblocked by default */
66 | if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
67 | berrorx(block, "sigprocmask");
68 | _exit(EXIT_ERR_INTERNAL);
69 | }
70 | }
71 |
72 | static void
73 | child_redirect_write(struct block *block, int pipe[2], int fd)
74 | {
75 | if (close(pipe[0]) == -1) {
76 | berrorx(block, "close pipe read end");
77 | _exit(EXIT_ERR_INTERNAL);
78 | }
79 |
80 | /* Defensive check */
81 | if (pipe[1] == fd)
82 | return;
83 |
84 | if (dup2(pipe[1], fd) == -1) {
85 | berrorx(block, "dup pipe write end");
86 | _exit(EXIT_ERR_INTERNAL);
87 | }
88 |
89 | if (close(pipe[1]) == -1) {
90 | berrorx(block, "close pipe write end");
91 | _exit(EXIT_ERR_INTERNAL);
92 | }
93 | }
94 |
95 | static void
96 | child_exec(struct block *block)
97 | {
98 | static const char * const shell = "/bin/sh";
99 |
100 | execl(shell, shell, "-c", COMMAND(block), (char *) NULL);
101 | /* Unlikely to reach this point */
102 | berrorx(block, "exec(%s -c %s)", shell, COMMAND(block));
103 | _exit(EXIT_ERR_INTERNAL);
104 | }
105 |
106 | static void
107 | block_dump_stderr(struct block *block)
108 | {
109 | char buf[2048] = { 0 };
110 |
111 | /* Note read(2) returns 0 for end-of-pipe */
112 | if (read(block->err, buf, sizeof(buf) - 1) == -1) {
113 | berrorx(block, "read stderr");
114 | return;
115 | }
116 |
117 | if (*buf)
118 | bdebug(block, "stderr:\n{\n%s\n}", buf);
119 | }
120 |
121 | static void
122 | linecpy(char **lines, char *dest, size_t size)
123 | {
124 | char *newline = strchr(*lines, '\n');
125 |
126 | /* split if there's a newline */
127 | if (newline)
128 | *newline = '\0';
129 |
130 | strncpy(dest, *lines, size);
131 | *lines += strlen(dest);
132 |
133 | /* increment if next char is non-null */
134 | if (*(*lines + 1))
135 | *lines += 1;
136 | }
137 |
138 | static void
139 | mark_as_failed(struct block *block, const char *reason)
140 | {
141 | if (log_level < LOG_WARN)
142 | return;
143 |
144 | struct properties *props = &block->updated_props;
145 |
146 | memset(props, 0, sizeof(struct properties));
147 | strcpy(props->name, NAME(block));
148 | strcpy(props->instance, INSTANCE(block));
149 | snprintf(props->full_text, sizeof(props->full_text), "[%s] %s", NAME(block), reason);
150 | snprintf(props->short_text, sizeof(props->short_text), "[%s] ERROR", NAME(block));
151 | strcpy(props->color, "#FF0000");
152 | strcpy(props->urgent, "true");
153 | }
154 |
155 | static void
156 | block_update_plain_text(struct block *block, char *buf)
157 | {
158 | struct properties *props = &block->updated_props;
159 | char *lines = buf;
160 |
161 | linecpy(&lines, props->full_text, sizeof(props->full_text) - 1);
162 | if (block->interval == INTER_PERSIST)
163 | return;
164 |
165 | linecpy(&lines, props->short_text, sizeof(props->short_text) - 1);
166 | if (*lines)
167 | linecpy(&lines, props->color, sizeof(props->color) - 1);
168 | }
169 |
170 | static void
171 | block_update_json(struct block *block, char *buf)
172 | {
173 | struct properties *props = &block->updated_props;
174 | int start, length, size;
175 |
176 | #define PARSE(_name, _size, _flags) \
177 | if ((_flags) & PROP_I3BAR) { \
178 | json_parse(buf, #_name, &start, &length); \
179 | if (start > 0) { \
180 | size = _size - 1 < length ? _size - 1 : length; \
181 | strncpy(props->_name, buf + start, size); \
182 | props->_name[size] = '\0'; \
183 | } \
184 | }
185 |
186 | PROPERTIES(PARSE);
187 |
188 | #undef PARSE
189 | }
190 |
191 | void
192 | block_update(struct block *block)
193 | {
194 | struct properties *props = &block->updated_props;
195 | char buf[2048] = { 0 };
196 | int nr;
197 |
198 | /* Read a single line for persistent block, everything otherwise */
199 | if (block->interval == INTER_PERSIST) {
200 | nr = io_readline(block->out, buf, sizeof(buf));
201 | if (nr < 0) {
202 | berror(block, "failed to read a line");
203 | return mark_as_failed(block, "failed to read");
204 | } else if (nr == 0) {
205 | berror(block, "pipe closed");
206 | return mark_as_failed(block, "pipe closed");
207 | }
208 | } else {
209 | /* Note: read(2) returns 0 for end-of-pipe */
210 | if (read(block->out, buf, sizeof(buf) - 1) == -1) {
211 | berrorx(block, "read stdout");
212 | return mark_as_failed(block, strerror(errno));
213 | }
214 | }
215 |
216 | /* Reset the defaults and merge the output */
217 | memcpy(props, &block->default_props, sizeof(struct properties));
218 |
219 | if (block->format == FORMAT_JSON)
220 | block_update_json(block, buf);
221 | else
222 | block_update_plain_text(block, buf);
223 |
224 | if (*FULL_TEXT(block) && *LABEL(block)) {
225 | static const size_t size = sizeof(props->full_text);
226 | char concat[size];
227 | snprintf(concat, size, "%s %s", LABEL(block), FULL_TEXT(block));
228 | strcpy(props->full_text, concat);
229 | }
230 |
231 | bdebug(block, "updated successfully");
232 | }
233 |
234 | void
235 | block_spawn(struct block *block, struct click *click)
236 | {
237 | const unsigned long now = time(NULL);
238 | int out[2], err[2];
239 |
240 | if (!*COMMAND(block)) {
241 | bdebug(block, "no command, skipping");
242 | return;
243 | }
244 |
245 | if (block->pid > 0) {
246 | bdebug(block, "process already spawned");
247 | return;
248 | }
249 |
250 | if (pipe(out) == -1 || pipe(err) == -1) {
251 | berrorx(block, "pipe");
252 | return mark_as_failed(block, strerror(errno));
253 | }
254 |
255 | if (block->interval == INTER_PERSIST) {
256 | if (io_signal(out[0], SIGRTMIN))
257 | return mark_as_failed(block, "event I/O impossible");
258 | }
259 |
260 | block->pid = fork();
261 | if (block->pid == -1) {
262 | berrorx(block, "fork");
263 | return mark_as_failed(block, strerror(errno));
264 | }
265 |
266 | /* Child? */
267 | if (block->pid == 0) {
268 | /* Error messages are merged into the parent's stderr... */
269 | child_setup_env(block, click);
270 | child_reset_signals(block);
271 | child_redirect_write(block, out, STDOUT_FILENO);
272 | child_redirect_write(block, err, STDERR_FILENO);
273 | /* ... until here */
274 | child_exec(block);
275 | }
276 |
277 | /*
278 | * Note: for non-persistent blocks, no need to set the pipe read end as
279 | * non-blocking, since it is meant to be read once the child has exited
280 | * (and thus the write end is closed and read is available).
281 | */
282 |
283 | /* Parent */
284 | if (close(out[1]) == -1)
285 | berrorx(block, "close stdout");
286 | if (close(err[1]) == -1)
287 | berrorx(block, "close stderr");
288 |
289 | block->out = out[0];
290 | block->err = err[0];
291 |
292 | if (!click)
293 | block->timestamp = now;
294 |
295 | bdebug(block, "forked child %d at %ld", block->pid, now);
296 | }
297 |
298 | void
299 | block_reap(struct block *block)
300 | {
301 | int status, code;
302 |
303 | if (block->pid <= 0) {
304 | bdebug(block, "not spawned yet");
305 | return;
306 | }
307 |
308 | if (waitpid(block->pid, &status, 0) == -1) {
309 | berrorx(block, "waitpid(%d)", block->pid);
310 | mark_as_failed(block, strerror(errno));
311 | goto close;
312 | }
313 |
314 | code = WEXITSTATUS(status);
315 | bdebug(block, "process %d exited with %d", block->pid, code);
316 |
317 | /* Process successfully reaped, reset the block PID */
318 | block->pid = 0;
319 |
320 | block_dump_stderr(block);
321 |
322 | if (code != 0 && code != EXIT_URGENT) {
323 | char reason[32];
324 |
325 | if (code == EXIT_ERR_INTERNAL)
326 | sprintf(reason, "internal error");
327 | else
328 | sprintf(reason, "bad exit code %d", code);
329 |
330 | berror(block, "%s", reason);
331 | mark_as_failed(block, reason);
332 | goto close;
333 | }
334 |
335 | /* Do not update unless it was meant to terminate */
336 | if (block->interval == INTER_PERSIST)
337 | goto close;
338 |
339 | block_update(block);
340 |
341 | /* Exit code takes precedence over the output */
342 | if (code == EXIT_URGENT)
343 | strcpy(block->updated_props.urgent, "true");
344 | close:
345 | if (close(block->out) == -1)
346 | berrorx(block, "close stdout");
347 | if (close(block->err) == -1)
348 | berrorx(block, "close stderr");
349 |
350 | /* Invalidate descriptors to avoid misdetection after reassignment */
351 | block->out = block->err = -1;
352 | }
353 |
354 | void block_setup(struct block *block)
355 | {
356 | struct properties *defaults = &block->default_props;
357 | struct properties *updated = &block->updated_props;
358 |
359 | /* Convenient shortcuts */
360 | if (strcmp(defaults->interval, "once") == 0)
361 | block->interval = INTER_ONCE;
362 | else if (strcmp(defaults->interval, "repeat") == 0)
363 | block->interval = INTER_REPEAT;
364 | else if (strcmp(defaults->interval, "persist") == 0)
365 | block->interval = INTER_PERSIST;
366 | else
367 | block->interval = atoi(defaults->interval);
368 |
369 | if (strcmp(defaults->format, "json") == 0)
370 | block->format = FORMAT_JSON;
371 | else
372 | block->format = FORMAT_PLAIN;
373 |
374 | block->signal = atoi(defaults->signal);
375 |
376 | /* First update (for static blocks and loading labels) */
377 | memcpy(updated, defaults, sizeof(struct properties));
378 |
379 | #define PLACEHOLDERS(_name, _size, _flags) " %s: \"%s\"\n"
380 | #define ARGS(_name, _size, _flags) #_name, updated->_name,
381 |
382 | debug("\n{\n" PROPERTIES(PLACEHOLDERS) "%s", PROPERTIES(ARGS) "}");
383 |
384 | #undef ARGS
385 | #undef PLACEHOLDERS
386 | }
387 |
--------------------------------------------------------------------------------
/src/click.c:
--------------------------------------------------------------------------------
1 | /*
2 | * click.c - read and parse of a click
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #include "click.h"
20 | #include "json.h"
21 | #include "log.h"
22 |
23 | /*
24 | * Parse a click, previous read from stdin.
25 | *
26 | * A click looks like this ("name" and "instance" can be absent):
27 | *
28 | * ',{"name":"foo","instance":"bar","button":1,"x":1186,"y":13}\n'
29 | *
30 | * Note that this function is non-idempotent. We need to parse from right to
31 | * left. It's ok since the JSON layout is known and fixed.
32 | */
33 | void
34 | click_parse(char *json, struct click *click)
35 | {
36 | int nst, nlen;
37 | int ist, ilen;
38 | int bst, blen;
39 | int xst, xlen;
40 | int yst, ylen;
41 |
42 | json_parse(json, "y", &yst, &ylen);
43 | json_parse(json, "x", &xst, &xlen);
44 | json_parse(json, "button", &bst, &blen);
45 | json_parse(json, "instance", &ist, &ilen);
46 | json_parse(json, "name", &nst, &nlen);
47 |
48 | click->name = json + nst;
49 | *(click->name + nlen) = '\0';
50 |
51 | click->instance = json + ist;
52 | *(click->instance + ilen) = '\0';
53 |
54 | click->button = json + bst;
55 | *(click->button + blen) = '\0';
56 |
57 | click->y = json + yst;
58 | *(click->y + ylen) = '\0';
59 |
60 | click->x = json + xst;
61 | *(click->x + xlen) = '\0';
62 |
63 | debug("parsed click: name=%s instance=%s button=%s x=%s y=%s",
64 | click->name, click->instance,
65 | click->button, click->x, click->y);
66 | }
67 |
--------------------------------------------------------------------------------
/src/ini.c:
--------------------------------------------------------------------------------
1 | /*
2 | * ini.c - parsing of the INI configuration file
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 |
26 | #include "bar.h"
27 | #include "block.h"
28 | #include "log.h"
29 |
30 | #ifndef SYSCONFDIR
31 | #define SYSCONFDIR "/etc"
32 | #endif
33 |
34 | static struct block *
35 | add_block(struct bar *bar)
36 | {
37 | struct block *block = NULL;
38 | void *reloc;
39 |
40 | reloc = realloc(bar->blocks, sizeof(struct block) * (bar->num + 1));
41 | if (reloc) {
42 | bar->blocks = reloc;
43 | block = bar->blocks + bar->num;
44 | bar->num++;
45 | }
46 |
47 | return block;
48 | }
49 |
50 | static int
51 | parse_section(const char *line, char *name, unsigned int size)
52 | {
53 | char *closing = strchr(line, ']');
54 | const int len = strlen(line);
55 |
56 | /* stop if the last char is not a closing bracket */
57 | if (!closing || line + len - 1 != closing) {
58 | error("malformated section \"%s\"", line);
59 | return 1;
60 | }
61 |
62 | if (size - 1 < len - 2) {
63 | error("section name too long \"%s\"", line);
64 | return 1;
65 | }
66 |
67 | memcpy(name, line + 1, len - 2);
68 | name[len - 2] = '\0';
69 | return 0;
70 | }
71 |
72 | static int
73 | parse_property(const char *line, struct properties *props, bool strict)
74 | {
75 | char *equal = strchr(line, '=');
76 | const char *key, *value;
77 |
78 | if (!equal) {
79 | error("malformated property, should be a key=value pair");
80 | return 1;
81 | }
82 |
83 | /* split key and value */
84 | *equal = '\0';
85 | key = line;
86 | value = equal + 1;
87 |
88 | #define PARSE(_name, _size, _flags) \
89 | if ((!strict || (_flags) & PROP_I3BAR) && strcmp(key, #_name) == 0) { \
90 | strncpy(props->_name, value, _size - 1); \
91 | return 0; \
92 | }
93 |
94 | PROPERTIES(PARSE);
95 |
96 | #undef PARSE
97 |
98 | error("unknown key: \"%s\"", key);
99 | return 1;
100 | }
101 |
102 | static struct bar *
103 | parse_bar(FILE *fp)
104 | {
105 | char line[2048];
106 | struct block *block = NULL;
107 | struct block global = {};
108 | struct bar *bar;
109 |
110 | bar = calloc(1, sizeof(struct bar));
111 | if (!bar)
112 | return NULL;
113 |
114 | while (fgets(line, sizeof(line), fp) != NULL) {
115 | int len = strlen(line);
116 |
117 | if (line[len - 1] != '\n') {
118 | error("line \"%s\" is not terminated by a newline", line);
119 | goto free;
120 | }
121 | line[len - 1] = '\0';
122 |
123 | switch (*line) {
124 | /* Comment or empty line? */
125 | case '#':
126 | case '\0':
127 | break;
128 |
129 | /* Section? */
130 | case '[':
131 | /* Finalize previous block */
132 | if (block)
133 | block_setup(block);
134 |
135 | block = add_block(bar);
136 | if (!block)
137 | goto free;
138 |
139 | /* Init the block with default settings (if any) */
140 | memcpy(block, &global, sizeof(struct block));
141 |
142 | if (parse_section(line, block->default_props.name, sizeof(block->default_props.name)))
143 | goto free;
144 |
145 | bdebug(block, "new block");
146 | break;
147 |
148 | /* Property? */
149 | case 'a' ... 'z':
150 | if (!block) {
151 | debug("parsing global properties");
152 | block = &global;
153 | }
154 |
155 | if (parse_property(line, &block->default_props, false))
156 | goto free;
157 |
158 | break;
159 |
160 | /* Syntax error */
161 | default:
162 | error("malformated line: %s", line);
163 | goto free;
164 | }
165 | }
166 |
167 | /* Finalize the last block */
168 | if (block)
169 | block_setup(block);
170 |
171 | return bar;
172 |
173 | free:
174 | free(bar->blocks);
175 | free(bar);
176 | return NULL;
177 | }
178 |
179 | static struct bar *
180 | try_parse(const char *path, bool *found)
181 | {
182 | struct bar *bar = NULL;
183 | bool noent = false;
184 | FILE *fp = fopen(path, "r");
185 |
186 | debug("try file %s", path);
187 |
188 | if (!fp) {
189 | if (errno == ENOENT && found)
190 | noent = true;
191 | else
192 | errorx("fopen");
193 | } else {
194 | bar = parse_bar(fp);
195 |
196 | if (fclose(fp))
197 | errorx("fclose");
198 | }
199 |
200 | if (found)
201 | *found = !noent;
202 |
203 | return bar;
204 | }
205 |
206 | struct bar *
207 | ini_load(const char *inifile)
208 | {
209 | const char * const home = getenv("HOME");
210 | const char * const xdg_home = getenv("XDG_CONFIG_HOME");
211 | const char * const xdg_dirs = getenv("XDG_CONFIG_DIRS");
212 | char buf[PATH_MAX];
213 | struct bar *bar;
214 | bool found;
215 |
216 | /* command line config file? */
217 | if (inifile)
218 | return try_parse(inifile, NULL);
219 |
220 | /* user config file? */
221 | if (home) {
222 | if (xdg_home)
223 | snprintf(buf, PATH_MAX, "%s/i3blocks/config", xdg_home);
224 | else
225 | snprintf(buf, PATH_MAX, "%s/.config/i3blocks/config", home);
226 | bar = try_parse(buf, &found);
227 | if (found)
228 | return bar;
229 |
230 | snprintf(buf, PATH_MAX, "%s/.i3blocks.conf", home);
231 | bar = try_parse(buf, &found);
232 | if (found)
233 | return bar;
234 | }
235 |
236 | /* system config file? */
237 | if (xdg_dirs)
238 | snprintf(buf, PATH_MAX, "%s/i3blocks/config", xdg_dirs);
239 | else
240 | snprintf(buf, PATH_MAX, "%s/xdg/i3blocks/config", SYSCONFDIR);
241 | bar = try_parse(buf, &found);
242 | if (found)
243 | return bar;
244 |
245 | snprintf(buf, PATH_MAX, "%s/i3blocks.conf", SYSCONFDIR);
246 | return try_parse(buf, NULL);
247 | }
248 |
--------------------------------------------------------------------------------
/src/io.c:
--------------------------------------------------------------------------------
1 | /*
2 | * io.c - non-blocking I/O operations
3 | * Copyright (C) 2015 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #define _GNU_SOURCE /* for F_SETSIG */
20 |
21 | #include
22 | #include
23 |
24 | #include "log.h"
25 |
26 | int
27 | io_signal(int fd, int sig)
28 | {
29 | int flags;
30 |
31 | /* Assign the signal for this file descriptor */
32 | if (fcntl(fd, F_SETSIG, sig) == -1) {
33 | errorx("failed to set signal %d on fd %d", sig, fd);
34 | return 1;
35 | }
36 |
37 | /* Set owner process that is to receive "I/O possible" signal */
38 | if (fcntl(fd, F_SETOWN, getpid()) == -1) {
39 | errorx("failed to set process as owner of fd %d", fd);
40 | return 1;
41 | }
42 |
43 | flags = fcntl(fd, F_GETFL);
44 | if (flags == -1) {
45 | errorx("failed to get flags of fd %d", fd);
46 | return 1;
47 | }
48 |
49 | /* Enable "I/O possible" signaling and make I/O nonblocking */
50 | if (fcntl(fd, F_SETFL, flags | O_ASYNC | O_NONBLOCK) == -1) {
51 | errorx("failed to enable I/O signaling for fd %d", fd);
52 | return 1;
53 | }
54 |
55 | return 0;
56 | }
57 |
58 | static ssize_t
59 | read_nonblock(int fd, char *buf, size_t size)
60 | {
61 | ssize_t nr;
62 |
63 | nr = read(fd, buf, size);
64 | if (nr == -1) {
65 | if (errno == EAGAIN) {
66 | /* no more reading */
67 | return 0;
68 | }
69 |
70 | errorx("read from %d", fd);
71 | return -1;
72 | }
73 |
74 | /* Note read(2) returns 0 for end-of-pipe */
75 | return nr;
76 | }
77 |
78 | int
79 | io_readline(int fd, char *buffer, size_t size)
80 | {
81 | int nr = 0;
82 | char c;
83 |
84 | while (nr < size && read_nonblock(fd, &c, 1) > 0) {
85 | buffer[nr++] = c;
86 | if (c == '\n')
87 | break;
88 | }
89 |
90 | return nr;
91 | }
92 |
--------------------------------------------------------------------------------
/src/json.c:
--------------------------------------------------------------------------------
1 | /*
2 | * json.c - basic JSON parsing and printing
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 |
26 | #include "bar.h"
27 | #include "block.h"
28 |
29 | static inline bool
30 | is_number(const char *str)
31 | {
32 | char *end;
33 |
34 | strtoul(str, &end, 10);
35 |
36 | /* not a valid number if end is not a null character */
37 | return !(*str == 0 || *end != 0);
38 | }
39 |
40 | static inline void
41 | escape(const char *str)
42 | {
43 | fprintf(stdout, "\"");
44 |
45 | while (*str) {
46 | switch (*str) {
47 | case '"':
48 | case '\\':
49 | fprintf(stdout, "\\%c", *str);
50 | break;
51 | default:
52 | fprintf(stdout, "%c", *str);
53 | }
54 |
55 | str++;
56 | }
57 |
58 | fprintf(stdout, "\"");
59 | }
60 |
61 | static void
62 | print_prop(const char *key, const char *value, int flags)
63 | {
64 | /* Only print i3bar-specific properties */
65 | if (!(flags & PROP_I3BAR))
66 | return;
67 |
68 | if (!*value)
69 | return;
70 |
71 | fprintf(stdout, ",\"%s\":", key);
72 |
73 | /* Print as-is (except strings which must be escaped) */
74 | if (flags & PROP_STRING && flags & PROP_NUMBER && is_number(value))
75 | fprintf(stdout, "%s", value);
76 | else if (flags & PROP_STRING)
77 | escape(value);
78 | else
79 | fprintf(stdout, "%s", value);
80 | }
81 |
82 | static void
83 | print_block(struct block *block)
84 | {
85 | #define PRINT(_name, _size, _flags) \
86 | print_prop(#_name, block->updated_props._name, _flags); \
87 |
88 | fprintf(stdout, ",{\"\":\"\"");
89 | PROPERTIES(PRINT);
90 | fprintf(stdout, "}");
91 |
92 | #undef PRINT
93 | }
94 |
95 | /*
96 | * Parse the input for the key and store the start of its value
97 | * into and its size into .
98 | *
99 | * set to 0 means that the key was not present.
100 | */
101 | void
102 | json_parse(const char *json, const char *name, int *start, int *len)
103 | {
104 | const size_t keylen = strlen(name) + 2;
105 | char key[keylen + 1];
106 | snprintf(key, sizeof(key), "\"%s\"", name);
107 |
108 | *start = *len = 0;
109 |
110 | char *here = strstr(json, key);
111 | if (here) {
112 | here += keylen + 1;
113 |
114 | /* skip pre-value whitespace */
115 | while (isspace(*here))
116 | here++;
117 |
118 | if (*here == '"') {
119 | /* string */
120 | here++;
121 | *start = here - json;
122 | while (*here && *here != '"')
123 | *len += 1, here++;
124 |
125 | /* invalidate on incomplete string */
126 | if (*here != '"')
127 | *start = 0;
128 | } else {
129 | /* number */
130 | *start = here - json;
131 | while (isdigit(*here++))
132 | *len += 1;
133 | }
134 | }
135 | }
136 |
137 | void
138 | json_print_bar(struct bar *bar)
139 | {
140 | fprintf(stdout, ",[{\"full_text\":\"\"}");
141 |
142 | for (int i = 0; i < bar->num; ++i) {
143 | struct block *block = bar->blocks + i;
144 |
145 | /* full_text is the only mandatory key, skip if empty */
146 | if (!*FULL_TEXT(block)) {
147 | bdebug(block, "no text to display, skipping");
148 | continue;
149 | }
150 |
151 | print_block(block);
152 | }
153 |
154 | fprintf(stdout, "]\n");
155 | fflush(stdout);
156 | }
157 |
--------------------------------------------------------------------------------
/src/main.c:
--------------------------------------------------------------------------------
1 | /*
2 | * i3blocks.c - main entry point, load the config and start the scheduler
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #include
20 | #include
21 |
22 | #include "ini.h"
23 | #include "log.h"
24 | #include "sched.h"
25 |
26 | #ifndef VERSION
27 | #define VERSION "unknown"
28 | #endif
29 |
30 | unsigned log_level = LOG_NORMAL;
31 |
32 | static void
33 | start(void)
34 | {
35 | fprintf(stdout, "{\"version\":1,\"click_events\":true}\n[[]\n");
36 | fflush(stdout);
37 | }
38 |
39 | int
40 | main(int argc, char *argv[])
41 | {
42 | char *inifile = NULL;
43 | struct bar *bar;
44 | int c;
45 |
46 | while (c = getopt(argc, argv, "c:vhV"), c != -1) {
47 | switch (c) {
48 | case 'c':
49 | inifile = optarg;
50 | break;
51 | case 'v':
52 | log_level++;
53 | break;
54 | case 'h':
55 | printf("Usage: %s [-c ] [-h] [-V]\n", argv[0]);
56 | return 0;
57 | case 'V':
58 | printf("i3blocks " VERSION " © 2014 Vivien Didelot and contributors\n");
59 | return 0;
60 | default:
61 | error("Try '%s -h' for more information.", argv[0]);
62 | return 1;
63 | }
64 | }
65 |
66 | debug("log level %u", log_level);
67 |
68 | bar = ini_load(inifile);
69 | if (!bar) {
70 | error("Try '%s -h' for more information.", argv[0]);
71 | return 1;
72 | }
73 |
74 | if (sched_init(bar))
75 | return 1;
76 |
77 | start();
78 | sched_start(bar);
79 |
80 | //stop();
81 | return 0;
82 | }
83 |
--------------------------------------------------------------------------------
/src/sched.c:
--------------------------------------------------------------------------------
1 | /*
2 | * sched.c - scheduling of block updates (timeout, signal or click)
3 | * Copyright (C) 2014 Vivien Didelot
4 | *
5 | * This program 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 | * This program 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 this program. If not, see .
17 | */
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 |
28 | #include "bar.h"
29 | #include "block.h"
30 | #include "io.h"
31 | #include "json.h"
32 | #include "log.h"
33 |
34 | static sigset_t set;
35 |
36 | static int
37 | gcd(int a, int b)
38 | {
39 | while (b != 0)
40 | a %= b, a ^= b, b ^= a, a ^= b;
41 |
42 | return a;
43 | }
44 |
45 | static unsigned int
46 | longest_sleep(struct bar *bar)
47 | {
48 | unsigned int time = 0;
49 |
50 | if (bar->num > 0 && bar->blocks->interval > 0)
51 | time = bar->blocks->interval; /* first block's interval */
52 |
53 | if (bar->num < 2)
54 | return time;
55 |
56 | /* The maximum sleep time is actually the GCD between all block intervals */
57 | for (int i = 1; i < bar->num; ++i)
58 | if ((bar->blocks + i)->interval > 0)
59 | time = gcd(time, (bar->blocks + i)->interval);
60 |
61 | return time;
62 | }
63 |
64 | static int
65 | setup_timer(struct bar *bar)
66 | {
67 | const unsigned sleeptime = longest_sleep(bar);
68 |
69 | if (!sleeptime) {
70 | debug("no timer needed");
71 | return 0;
72 | }
73 |
74 | struct itimerval itv = {
75 | .it_value.tv_sec = sleeptime,
76 | .it_interval.tv_sec = sleeptime,
77 | };
78 |
79 | if (setitimer(ITIMER_REAL, &itv, NULL) == -1) {
80 | errorx("setitimer");
81 | return 1;
82 | }
83 |
84 | debug("starting timer with interval of %d seconds", sleeptime);
85 | return 0;
86 | }
87 |
88 | static int
89 | setup_signals(void)
90 | {
91 | if (sigemptyset(&set) == -1) {
92 | errorx("sigemptyset");
93 | return 1;
94 | }
95 |
96 | #define ADD_SIG(_sig) \
97 | if (sigaddset(&set, _sig) == -1) { errorx("sigaddset(%d)", _sig); return 1; }
98 |
99 | /* Control signals */
100 | ADD_SIG(SIGTERM);
101 | ADD_SIG(SIGINT);
102 |
103 | /* Timer signal */
104 | ADD_SIG(SIGALRM);
105 |
106 | /* Block updates (forks) */
107 | ADD_SIG(SIGCHLD);
108 |
109 | /* Deprecated signals */
110 | ADD_SIG(SIGUSR1);
111 | ADD_SIG(SIGUSR2);
112 |
113 | /* Click signal */
114 | ADD_SIG(SIGIO);
115 |
116 | /* I/O Possible signal for persistent blocks */
117 | ADD_SIG(SIGRTMIN);
118 |
119 | /* Real-time signals for blocks */
120 | for (int sig = SIGRTMIN + 1; sig <= SIGRTMAX; ++sig) {
121 | debug("provide signal %d (%s)", sig, strsignal(sig));
122 | ADD_SIG(sig);
123 | }
124 |
125 | #undef ADD_SIG
126 |
127 | /* Block signals for which we are interested in waiting */
128 | if (sigprocmask(SIG_SETMASK, &set, NULL) == -1) {
129 | errorx("sigprocmask");
130 | return 1;
131 | }
132 |
133 | return 0;
134 | }
135 |
136 | int
137 | sched_init(struct bar *bar)
138 | {
139 | if (setup_signals())
140 | return 1;
141 |
142 | if (setup_timer(bar))
143 | return 1;
144 |
145 | /* Setup event I/O for stdin (clicks) */
146 | if (!isatty(STDIN_FILENO))
147 | if (io_signal(STDIN_FILENO, SIGIO))
148 | return 1;
149 |
150 | return 0;
151 | }
152 |
153 | void
154 | sched_start(struct bar *bar)
155 | {
156 | siginfo_t siginfo;
157 | int sig;
158 |
159 | /*
160 | * Initial display (for static blocks and loading labels),
161 | * and first forks (for commands with an interval).
162 | */
163 | json_print_bar(bar);
164 | bar_poll_timed(bar);
165 |
166 | while (1) {
167 | sig = sigwaitinfo(&set, &siginfo);
168 | if (sig == -1) {
169 | /* Hiding the bar may interrupt this system call */
170 | if (errno == EINTR)
171 | continue;
172 |
173 | errorx("sigwaitinfo");
174 | break;
175 | }
176 |
177 | debug("received signal %d (%s)", sig, strsignal(sig));
178 |
179 | if (sig == SIGTERM || sig == SIGINT)
180 | break;
181 |
182 | /* Interval tick? */
183 | if (sig == SIGALRM) {
184 | bar_poll_outdated(bar);
185 |
186 | /* Child(ren) dead? */
187 | } else if (sig == SIGCHLD) {
188 | bar_poll_exited(bar);
189 | json_print_bar(bar);
190 |
191 | /* Block clicked? */
192 | } else if (sig == SIGIO) {
193 | bar_poll_clicked(bar);
194 |
195 | /* Persistent block ready to be read? */
196 | } else if (sig == SIGRTMIN) {
197 | bar_poll_readable(bar, siginfo.si_fd);
198 | json_print_bar(bar);
199 |
200 | /* Blocks signaled? */
201 | } else if (sig > SIGRTMIN && sig <= SIGRTMAX) {
202 | bar_poll_signaled(bar, sig - SIGRTMIN);
203 |
204 | /* Deprecated signals? */
205 | } else if (sig == SIGUSR1 || sig == SIGUSR2) {
206 | error("SIGUSR{1,2} are deprecated, ignoring.");
207 |
208 | } else debug("unhandled signal %d", sig);
209 | }
210 |
211 | /*
212 | * Unblock signals (so subsequent syscall can be interrupted)
213 | * and wait for child processes termination.
214 | */
215 | if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1)
216 | errorx("sigprocmask");
217 | while (waitpid(-1, NULL, 0) > 0)
218 | continue;
219 |
220 | debug("quit scheduling");
221 | }
222 |
--------------------------------------------------------------------------------