From e32c939aa443ea6c386f0af8dd516d1a1f44cd72 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 26 Feb 2014 19:35:13 +0100 Subject: struct + doc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/__main__.py | 24 ++++++++++++++++-------- src/x.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/__main__.py b/src/__main__.py index f28d7f4..d1b5d66 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -22,17 +22,30 @@ import Xlib.display, Xlib.Xatom, Xlib.ext.randr, Xlib.X from x import * +OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 24, True +FONT = '-misc-fixed-*-*-*-*-10-*-*-*-*-*-*-*' +BACKGROUND, FOREGROUND = (0, 0, 0), (192, 192, 192) + + open_x() -width, height, left, top, panel_height, at_top = get_monitors()[0][:3] + [24, 1 * 12, True] +width, height, left, top, panel_height, at_top = get_monitors()[OUTPUT][:3] + [YPOS, HEIGHT, TOP] display = get_display() window = create_panel(width, height, left, top, panel_height, at_top) gc = window.create_gc() +cmap = window.get_attributes().colormap window.map() display.flush() +background = cmap.alloc_color(*[x * 257 for x in BACKGROUND]).pixel +foreground = cmap.alloc_color(*[x * 257 for x in FOREGROUND]).pixel +font = display.open_font(FONT) +font_q = font.query() +font_height = font_q.font_ascent + font_q.font_descent +text_width = lambda text : font.query_text_extents(text).overall_width + while True: try: e = display.next_event() @@ -40,15 +53,10 @@ while True: break except KeyboardInterrupt: break - cmap = window.get_attributes().colormap - gc.change(foreground = cmap.alloc_color(0x0000, 0x0000, 0x0000).pixel) + gc.change(foreground = background) window.fill_rectangle(gc, 0, 0, width, panel_height) - gc.change(foreground = cmap.alloc_color(0xC0C0, 0xC0C0, 0xC0C0).pixel) - font = display.open_font('-misc-fixed-*-*-*-*-10-*-*-*-*-*-*-*') + gc.change(foreground = foreground) gc.change(font = font) - print(font.query().font_ascent) - print(font.query().font_descent) - print(font.query_text_extents("Test").overall_width) text_ = '°°° TEST °°° ꚺ░∈𝕐 '.encode('utf-16')[2:] text = [] for i in range(len(text_)): diff --git a/src/x.py b/src/x.py index f82a943..94437f5 100644 --- a/src/x.py +++ b/src/x.py @@ -22,11 +22,27 @@ import Xlib.display, Xlib.Xatom, Xlib.ext.randr, Xlib.X display = None +''' +The connection the X display +''' + screen = None +''' +The screen the panel is placed in +''' + screen_i = None +''' +The index of the screen `screen` +''' def open_x(screen_no = None): + ''' + Open connection the X and select screen + + @param screen_no:int? The index of the screen to use, `None` for default + ''' global display, screen, screen_i display = Xlib.display.Display() screen_i = screen_no if screen_no is not None else display.get_default_screen() @@ -34,6 +50,13 @@ def open_x(screen_no = None): def get_monitors(): + ''' + Returns the pixel size and position of the monitors in the screen + + @return :list<(width:int, height:int, left:int, top:int)> + The width, height, left position and top position of each + monitor, starting with the primary output + ''' global screen_i p = subprocess.Popen(['xrandr'], stdout = subprocess.PIPE) p = p.communicate()[0].decode('utf-8', 'replace') @@ -60,18 +83,35 @@ def get_monitors(): def get_display(): + ''' + Returns the X display + ''' global display return display def get_screen(): + ''' + Returns the X screen + ''' global screen return screen -def create_panel(width, height, left, top, panel_height, at_top): +def create_panel(width, height, left, ypos, panel_height, at_top): + ''' + Create a docked panel, not mapped yet + + @param width:int The width of the output + @param height:int The height of the output + @param left:int The left position of the output + @param ypos:int The position of the panel in relation the either the top or bottom edge of the output + @param panel_height:int The height of the panel + @param at_top:bool Whether the panel is to be docked to the top of the output, otherwise to the bottom + @return The window + ''' global display, screen - ypos = top if at_top else (height - top - panel_height) + 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, event_mask = ( @@ -89,7 +129,7 @@ def create_panel(width, height, left, top, panel_height, at_top): _CARD = display.intern_atom("CARDINAL") _PSTRUT = display.intern_atom("_NET_WM_STRUT_PARTIAL") - window.change_property(_PSTRUT, _CARD, 32, (top_ if at_top else bottom_)(left, top, width, panel_height)) + window.change_property(_PSTRUT, _CARD, 32, (top_ if at_top else bottom_)(left, ypos, width, panel_height)) _ATOM = display.intern_atom("ATOM") _TYPE = display.intern_atom("_NET_WM_WINDOW_TYPE") @@ -100,6 +140,9 @@ def create_panel(width, height, left, top, panel_height, at_top): def close_x(): + ''' + Closes the connection to X, but flushes it first + ''' global display display.flush() display.close() -- cgit v1.2.3-70-g09d2