|
| 1 | +// Simple screen grabber that draws the upper left corner of the screen to the display |
| 2 | +// |
| 3 | +// Copyright (c) 2019, X41 <[email protected]> |
| 4 | +// |
| 5 | +// Permission to use, copy, modify, and/or distribute this software for any |
| 6 | +// purpose with or without fee is hereby granted, provided that the above |
| 7 | +// copyright notice and this permission notice appear in all copies. |
| 8 | +// |
| 9 | +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 | +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 | +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 | +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 | +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 | +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 | +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | + |
| 17 | +#include <types.h> |
| 18 | +#include <matrix.h> |
| 19 | +#include <timers.h> |
| 20 | +#include <random.h> |
| 21 | +#include <stddef.h> |
| 22 | +#include <X11/Xlib.h> |
| 23 | +#include <X11/X.h> |
| 24 | + |
| 25 | +#define FPS 30 |
| 26 | +#define FRAMETIME (T_SECOND / FPS) |
| 27 | +#define FRAMES (TIME_SHORT * FPS) |
| 28 | + |
| 29 | +static int modno; |
| 30 | +static int frame; |
| 31 | +static ulong nexttick; |
| 32 | + |
| 33 | +int width; |
| 34 | +int height; |
| 35 | + |
| 36 | +Display *display; |
| 37 | +Window root; |
| 38 | +XImage *image; |
| 39 | + |
| 40 | +unsigned long red_mask; |
| 41 | +unsigned long green_mask; |
| 42 | +unsigned long blue_mask; |
| 43 | +unsigned long pixel; |
| 44 | +unsigned char blue; |
| 45 | +unsigned char green; |
| 46 | +unsigned char red; |
| 47 | + |
| 48 | +int init(int moduleno, char* argstr) { |
| 49 | + if (matrix_getx() < 3) |
| 50 | + return 1; |
| 51 | + modno = moduleno; |
| 52 | + |
| 53 | + // grab X11 screen |
| 54 | + display = XOpenDisplay(NULL); |
| 55 | + root = DefaultRootWindow(display); |
| 56 | + XWindowAttributes gwa; |
| 57 | + XGetWindowAttributes(display, root, &gwa); |
| 58 | + |
| 59 | + // use dimensions of matrix |
| 60 | + width = matrix_getx(); |
| 61 | + height = matrix_gety(); |
| 62 | + |
| 63 | + // get first frame and set up color masks |
| 64 | + image = XGetImage(display, root, 0, 0, width, height, AllPlanes, ZPixmap); |
| 65 | + red_mask = image->red_mask; |
| 66 | + green_mask = image->green_mask; |
| 67 | + blue_mask = image->blue_mask; |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +void reset(void) { |
| 72 | + nexttick = udate(); |
| 73 | +} |
| 74 | + |
| 75 | +int draw(int argc, char* argv[]) { |
| 76 | + nexttick = udate() + FRAMETIME; |
| 77 | + |
| 78 | + image = XGetImage(display,root, 0,0 , width,height,AllPlanes, ZPixmap); |
| 79 | + |
| 80 | + for (int x = 0; x < width; x++){ |
| 81 | + for (int y = 0; y < height ; y++){ |
| 82 | + pixel = XGetPixel(image,x,y); |
| 83 | + |
| 84 | + blue = pixel & blue_mask; |
| 85 | + green = (pixel & green_mask) >> 8; |
| 86 | + red = (pixel & red_mask) >> 16; |
| 87 | + |
| 88 | + matrix_set(x, y, RGB(red, green, blue)); |
| 89 | + } |
| 90 | + } |
| 91 | + matrix_render(); |
| 92 | + |
| 93 | + if (frame >= FRAMES) { frame = 0; return 1;} // end drawing |
| 94 | + timer_add(nexttick, modno, 0, NULL); |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | +int deinit(void) { |
| 100 | + return 0; |
| 101 | +} |
0 commit comments