From 6cadf2da966da7ad5f7b613e14b0cb30239f0b82 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 2 Mar 2014 15:29:24 +0100 Subject: m + fix threading weirdness + add clock example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- examples/clock | 33 +++++++++++++++++++++++++++++++++ src/__main__.py | 8 +++++--- src/util.py | 2 ++ src/x.py | 9 ++++++--- 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 examples/clock diff --git a/examples/clock b/examples/clock new file mode 100644 index 0000000..4984213 --- /dev/null +++ b/examples/clock @@ -0,0 +1,33 @@ +# -*- python -*- + +# A simple xpybar configuration example of a clock + +import time +import threading + + +OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True + + +start_ = start +def start(): + start_() + def clock(): + while True: + time.sleep(0.1) + time.sleep(1 - (time.time() % 1)) + if redraw(): + get_display().flush() + async(clock) + + +semaphore = threading.Semaphore() +def redraw(): + if semaphore.acquire(blocking = False): + text = spawn_read('date', '+%Y-(%m)%b-%d %T, %a w%V, %Z') + bar.clear() + bar.draw_coloured_text(0, 10, 0, 2, text) + semaphore.release() + return True + return False + diff --git a/src/__main__.py b/src/__main__.py index f381294..62dc7d0 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -91,6 +91,7 @@ class Bar: @variable font The default font @variable font_metrics The default font's metrics @variable font_height:int The height of the default font + @variable font_width:int The width of an 'X' with the default font @variable palette A 16-array of standard colours ''' @@ -117,6 +118,7 @@ class Bar: self.background = self.create_colour(*background) self.foreground = self.create_colour(*foreground) (self.font, self.font_metrics, self.font_height) = self.create_font(font) + self.font_width = self.text_width('X') self.palette = [0x000000, 0xCD656C, 0x32A679, 0xCCAD47, 0x2495BE, 0xA46EB0, 0x00A09F, 0xD8D8D8] self.palette += [0x555555, 0xEB5E6A, 0x0EC287, 0xF2CA38, 0x00ACE0, 0xC473D1, 0x00C3C7, 0xEEEEEE] self.palette = [((p >> 16) & 255, (p >> 8) & 255, p & 255) for p in self.palette] @@ -155,14 +157,14 @@ class Bar: ''' special = '─│┌┐└┘├┤┬┴┼╱╲╳←↓→↑\0' buf = '' - w = self.text_width('X') - 1 + w = self.font_width - 1 h = self.font_height + descent - 1 y_ = y - self.font_height for c in text + '\0': if c in special: if not buf == '': draw_text(self.window, self.gc, x, y, buf) - x += self.text_width(buf) + x += self.font_width * len(buf) buf = '' if not c == '\0': segs = [] @@ -261,7 +263,7 @@ class Bar: if not buf == '': self.change_colour(bc) h = self.font_height + ascent - w = self.text_width(buf) + w = self.font_width * len(buf) self.window.fill_rectangle(self.gc, x, y - h, w, line_height) self.gc.change(foreground = fc, background = bc) self.draw_text(x, y + ascent, descent, buf) diff --git a/src/util.py b/src/util.py index d819101..4698533 100644 --- a/src/util.py +++ b/src/util.py @@ -21,6 +21,8 @@ import time import threading import subprocess +import Xlib.threaded + def async(target, name = None, group = None): ''' diff --git a/src/x.py b/src/x.py index d1b3995..8b18e61 100644 --- a/src/x.py +++ b/src/x.py @@ -112,11 +112,14 @@ def create_panel(width, height, left, ypos, panel_height, at_top): ''' global display, screen ypos = ypos if at_top else (height - ypos - panel_height) - window = screen.root.create_window(left, ypos, width, panel_height, 0, screen.root_depth, - Xlib.X.InputOutput, Xlib.X.CopyFromParent, + window = screen.root.create_window(left, ypos, width, panel_height, 0, + Xlib.X.CopyFromParent, + Xlib.X.InputOutput, + Xlib.X.CopyFromParent, event_mask = ( Xlib.X.StructureNotifyMask | - Xlib.X.ButtonReleaseMask + Xlib.X.ButtonReleaseMask | + Xlib.X.ExposureMask ), colormap = Xlib.X.CopyFromParent) -- cgit v1.2.3-70-g09d2