tlibdraw: add visibleclicks mode - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 54bebe6a69875f6f020b00ed8e2c7d944d2ee93b
 (DIR) parent 6f568da64e625a1e517de6147058888515a0ccb7
 (HTM) Author: Russ Cox <rsc@swtch.com>
       Date:   Mon, 17 Sep 2012 12:39:49 -0400
       
       libdraw: add visibleclicks mode
       
       R=rsc
       http://codereview.appspot.com/6501137
       
       Diffstat:
         M src/libdraw/drawclient.c            |       7 ++++++-
         M src/libdraw/event.c                 |       2 ++
         M src/libdraw/init.c                  |      47 ++++++++++++++++++++++++++++++-
       
       3 files changed, 54 insertions(+), 2 deletions(-)
       ---
 (DIR) diff --git a/src/libdraw/drawclient.c b/src/libdraw/drawclient.c
       t@@ -9,6 +9,7 @@
        #include <drawfcall.h>
        #include <mux.h>
        
       +extern Mouse _drawmouse;
        int chattydrawclient = 0;
        
        static int        drawgettag(Mux *mux, void *vmsg);
       t@@ -259,6 +260,7 @@ _displayrdmouse(Display *d, Mouse *m, int *resized)
                tx.type = Trdmouse;
                if(displayrpc(d, &tx, &rx, nil) < 0)
                        return -1;
       +        _drawmouse = rx.mouse;
                *m = rx.mouse;
                *resized = rx.resized;
                return 0;
       t@@ -283,7 +285,10 @@ _displaymoveto(Display *d, Point p)
        
                tx.type = Tmoveto;
                tx.mouse.xy = p;
       -        return displayrpc(d, &tx, &rx, nil);
       +        if(displayrpc(d, &tx, &rx, nil) < 0)
       +                return -1;
       +        _drawmouse.xy = p;
       +        return flushimage(d, 1);
        }
        
        int
 (DIR) diff --git a/src/libdraw/event.c b/src/libdraw/event.c
       t@@ -9,6 +9,7 @@
        
        typedef struct        Slave Slave;
        typedef struct        Ebuf Ebuf;
       +extern Mouse _drawmouse;
        
        struct Slave
        {
       t@@ -331,6 +332,7 @@ extract(int canblock)
                                if(finishrpc(eslave[i].rpc, &w)){
                                        eslave[i].rpc = nil;
                                        eb = newebuf(&eslave[i], sizeof(Mouse));
       +                                _drawmouse = w.mouse;
                                        eb->u.mouse = w.mouse;
                                        if(w.resized)
                                                eresized(1);
 (DIR) diff --git a/src/libdraw/init.c b/src/libdraw/init.c
       t@@ -1,6 +1,7 @@
        #include <u.h>
        #include <libc.h>
        #include <draw.h>
       +#include <mouse.h>
        
        Display        *display;
        Font        *font;
       t@@ -13,6 +14,11 @@ Screen        *_screen;
        int                debuglockdisplay = 1;
        char        *winsize;
        
       +int                visibleclicks = 0;
       +Image        *mousebuttons;
       +Image        *mousesave;
       +Mouse        _drawmouse;
       +
        /*
        static void
        drawshutdown(void)
       t@@ -31,7 +37,7 @@ int
        geninitdraw(char *devdir, void(*error)(Display*, char*), char *fontname, char *label, char *windir, int ref)
        {
                Subfont *df;
       -        char buf[128];
       +        char buf[128], *p;
        
                if(label == nil)
                        label = argv0;
       t@@ -87,6 +93,22 @@ geninitdraw(char *devdir, void(*error)(Display*, char*), char *fontname, char *l
                draw(screen, screen->r, display->white, nil, ZP);
                flushimage(display, 1);
        
       +        p = getenv("visibleclicks");
       +        visibleclicks = p != nil && *p == '1';
       +        if(visibleclicks) {
       +                Font *f;
       +                
       +                f = display->defaultfont;
       +                mousebuttons = allocimage(display, Rect(0,0,64,22), screen->chan, 0, DWhite);
       +                border(mousebuttons, mousebuttons->r, 1, display->black, ZP);
       +                border(mousebuttons, Rect(0, 0, 22, 22), 1, display->black, ZP);
       +                border(mousebuttons, Rect(42, 0, 64, 22), 1, display->black, ZP);
       +                string(mousebuttons, Pt(10-stringwidth(display->defaultfont, "1")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "1");
       +                string(mousebuttons, Pt(21+10-stringwidth(display->defaultfont, "2")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "2");
       +                string(mousebuttons, Pt(42+10-stringwidth(display->defaultfont, "3")/2, 11-f->height/2), display->black, ZP, display->defaultfont, "3");
       +                mousesave = allocimage(display, Rect(0,0,64,22), screen->chan, 0, 0);
       +        }
       +
                /*
                 * I don't see any reason to go away gracefully,
                 * and if some other proc exits holding the display
       t@@ -346,6 +368,29 @@ doflush(Display *d)
        int
        flushimage(Display *d, int visible)
        {
       +        if(visible == 1 && visibleclicks && mousebuttons && _drawmouse.buttons) {
       +                Rectangle r, r1;
       +                int ret;
       +
       +                r = mousebuttons->r;
       +                r = rectaddpt(r, _drawmouse.xy);
       +                r = rectaddpt(r, Pt(-Dx(mousebuttons->r)/2, -Dy(mousebuttons->r)-3));
       +                drawop(mousesave, mousesave->r, screen, nil, r.min, S);
       +                
       +                r1 = rectaddpt(Rect(0, 0, 22, 22), r.min);
       +                if(_drawmouse.buttons & 1)
       +                        drawop(screen, r1, mousebuttons, nil, ZP, S);
       +                r1 = rectaddpt(r1, Pt(21, 0));
       +                if(_drawmouse.buttons & 2)
       +                        drawop(screen, r1, mousebuttons, nil, Pt(21, 0), S);
       +                r1 = rectaddpt(r1, Pt(21, 0));
       +                if(_drawmouse.buttons & 4)
       +                        drawop(screen, r1, mousebuttons, nil, Pt(42, 0), S);
       +                ret = flushimage(d, 2);
       +                drawop(screen, r, mousesave, nil, ZP, S);
       +                return ret;
       +        }
       +        
                if(visible){
                        *d->bufp++ = 'v';        /* five bytes always reserved for this */
                        if(d->_isnewdisplay){