📜 ⬆️ ⬇️

Binary Clock Widget for Awesome

How I came to such a life

It all started with the transition to a new job. Although, no ... It all started with Marla from an image with a miracle clock that was accidentally seen somewhere. I was interested in this image, I turned to Wikipedia, and discovered such a wonderful thing as a binary clock ( Binary-coded decimal clocks ), after which I then studied Java in my spare time, so imbued with this concept of displaying time that even wrote a simple application with a binary clock. As time went on, the application was forgotten long ago, the binary clock temporarily fell out of my sight and cozily settled in the attic of memory. But then a series of events shook up this attic, and the thought of binary clocks again went to the processing brain.

The main event was the change of work, and the change of 17-inch monitor to 23-inch that followed with it. After a couple of weeks of work, it became absolutely clear that Gnome, which I have been using for several years, does not allow to fully use all of the available desktop space (1920 * 1080, and it was frightening to think about this resolution on the monitor 10 years ago). Figuring to arrange windows is rather inconvenient, and the specifics of the work of the system administrator in my organization implies the simultaneous opening and parallel use of at least a console, browser, messenger, as well as heaps of applications that change depending on the context of activities such as rdesktop or vncviewer.

Just at that moment, I once again came across the mention of tile desktop managers, and decided to try this method of working space organization. And about a miracle - it turned out to be very convenient. Having tried several WM, I opted for awesome . After a couple of days of use, I was convinced that this particular type of WM is the best for me to work with, and awesome is a worthy representative of this type, and began to explore the possibility of customizing my desktop. Luckily, since the third version of Awesome, the Lua script is used for configuration, which allows you to write widgets directly in the config file. Studying the widgets available on the Awesome website from users, I came across an analog clock widget , and a quick look at the code for this widget showed me a great opportunity to implement a thing I’ve been interested in for a long time - a binary clock, instead of the standard text for awesome text. All that is needed for this is the ability to draw the image with the tools of Awesome itself, so the widget does not depend on external components.

image:draw_rectangle(x,y,w,h)

Of course, I have never used the Lua language before, but the task seemed rather trivial, so I got to work.
')
So let's go

Open the file we need in a text editor:

$vim ~/.config/awesome/rc.lua

We are looking for an existing ad text clock widget

mytextclock = awful.widget.textclock ( { align = "right" } )


And we begin to "create":

binaryclock = { }
binaryclock.widget = widget ( { type = "imagebox" } )
binaryclock.w = 51 --width
binaryclock.h = 24 --height (better to be a multiple of 6)
--dont forget that awesome resizes our wibox's height with clocks to fit
binaryclock.show_sec = true --must we show seconds?
binaryclock.color_active = beautiful.bg_focus - active dot color
binaryclock.color_bg = beautiful.bg_normal --background color
binaryclock.color_inactive = beautiful.fg_focus --inactive dot color
binaryclock.dotsize = math.floor ( binaryclock.h / 6 ) --dot size
binaryclock.step = math.floor ( binaryclock.dotsize / 2 ) --whitespace between dots
binaryclock.widget.image = image.argb32 ( binaryclock.w, binaryclock.h, nil ) --create image
if ( binaryclock.show_sec ) then binaryclock.timeout = 1 else binaryclock.timeout = 20 end


As you can see, all the widget settings are set here, and all the basic size calculations are done right there.
Then I used a ready-made solution for the Lua language, which allows converting numbers in decimal to any other:
binaryclock.DEC_BIN = function ( IN ) --thanx to Lostgallifreyan (http://lua-users.org/lists/lua-l/2004-09/msg00054.html)
local B, K, OUT, I, D = 2 , "01" , "" , 0
while IN > 0 do
I = I + 1
IN, D = math.floor ( IN / B ) , math.mod ( IN, B ) + 1
OUT = string.sub ( K, D, D ) ..OUT
end
return OUT
end


Now let's declare the function that draws the points- “diodes” of the clock, according to the number itself.
binaryclock.paintdot = function ( val, shift, limit )
local binval = binaryclock.DEC_BIN ( val )
local l = string.len ( binval )
local height = 0 --height adjustment
if ( l < limit ) then
for i = 1 , limit - l do binval = "0" .. binval end
end
for i = 0 , limit- 1 do
if ( string.sub ( binval, limit-i, limit-i ) == "1" ) then
binaryclock.widget.image: draw_rectangle ( shift, binaryclock.h - binaryclock.dotsize - height, binaryclock.dotsize, binaryclock.dotsize, true , binaryclock.color_active )
else
binaryclock.widget.image: draw_rectangle ( shift, binaryclock.h - binaryclock.dotsize - height, binaryclock.dotsize, binaryclock.dotsize, true , binaryclock.color_inactive )
end
height = height + binaryclock.dotsize + binaryclock.step
end
end


And finally, the main function that paints our watch:

binaryclock.drawclock = function ( ) --get time and send digits to paintdot ()
binaryclock.widget.image: draw_rectangle ( 0 , 0 , binaryclock.w, binaryclock.h, true , binaryclock.color_bg ) --fill background
local t = os.date ( "* t" )
local hour = t.hour
if ( string.len ( hour ) == 1 ) then
hour = "0" .. t.hour
end
local min = t. min
if ( string.len ( min ) == 1 ) then
min = "0" .. t. min
end
local sec = t.sec
if ( string.len ( sec ) == 1 ) then
sec = "0" .. t.sec
end
local col_count = 6
if ( not binaryclock.show_sec ) then col_count = 4 end
local step = math.floor ( ( binaryclock.w - col_count * binaryclock.dotsize ) / 8 ) --calc horizontal whitespace between cols
binaryclock.paintdot ( 0 + string.sub ( hour, 1 , 1 ) , step, 2 )
binaryclock.paintdot ( 0 + string.sub ( hour, 2 , 2 ) , binaryclock.dotsize + 2 * step, 4 )
binaryclock.paintdot ( 0 + string.sub ( min , 1 , 1 ) , binaryclock.dotsize * 2 + 4 * step, 3 )
binaryclock.paintdot ( 0 + string.sub ( min , 2 , 2 ) , binaryclock.dotsize * 3 + 5 * step, 4 )
if ( binaryclock.show_sec ) then
binaryclock.paintdot ( 0 + string.sub ( sec, 1 , 1 ) , binaryclock.dotsize * 4 + 7 * step, 3 )
binaryclock.paintdot ( 0 + string.sub ( sec, 2 , 2 ) , binaryclock.dotsize * 5 + 8 * step, 4 )
end
binaryclock.widget.image = binaryclock.widget.image
end


Of course, not the most elegant solution, but it works.
We only need to set the update timer:

binarytimer = timer { timeout = binaryclock.timeout } --register timer
binarytimer: add_signal ( "timeout" , function ( )
binaryclock.drawclock ( )
end )
binarytimer: start ( ) --start timer


and place our widget on the panel (vibox):
mywibox [ s ] .widgets = {
--widgets
binaryclock.widget,
--widgets again
layout = awful.widget.layout.horizontal.rightleft
}


Although for greater beauty (and the normal size of the clock on a Full-HD screen), I still had to change the height of the panel:
mywibox [ s ] = awful.wibox ( { position = "top" , screen = s, height = "22" } )


Do not be surprised at the height of the panel at 22px, with the height of the widget at 24. Awesome scales the images in the panel, but for some reason, when the height of the panel is 24px, the Pidgin tray icon behaves very peculiarly, flashes, and is generally capricious.
The work is finished, you can evaluate the result by restarting awesome wonderful combo Mod4 + Ctrl + r.

And what is the result?

You can evaluate the result of my work in the screenshot:

A couple of days of writing code at lunchtime - and the binary clock flashed safely, showing the time in the awesome panel, and I was enriched with knowledge of the Lua language, and decorated my virtual workspace. Awesome, by the way, also showed its suitability for use on my 11-inch laptop, allowing me to almost completely abandon the use of the touchpad / trackpad, and increase the overall responsiveness of the laptop.

PS The widget code will soon be published by me on the Awesome wiki at the following address . Used for writing awesome v3.4.10 (Exploder).

Source: https://habr.com/ru/post/128308/


All Articles