robotgo/screen/goScreen.h

113 lines
2.6 KiB
C
Raw Permalink Normal View History

2016-11-10 06:34:13 +00:00
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
2016-12-10 18:41:08 +00:00
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
2016-11-10 06:34:13 +00:00
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2016-10-06 10:41:37 +00:00
#include "../base/types.h"
#include "../base/rgb.h"
2016-11-13 16:01:21 +00:00
#include "screengrab_c.h"
#include "screen_c.h"
2016-11-17 08:00:00 +00:00
// #include "../MMBitmap_c.h"
2016-10-06 10:41:37 +00:00
void padHex(MMRGBHex color, char* hex){
2018-02-11 11:03:35 +00:00
// Length needs to be 7 because snprintf includes a terminating null.
// Use %06x to pad hex value with leading 0s.
2016-10-06 10:41:37 +00:00
snprintf(hex, 7, "%06x", color);
}
char* pad_hex(MMRGBHex color){
char hex[7];
padHex(color, hex);
// destroyMMBitmap(bitmap);
char* str = (char*)calloc(100, sizeof(char*));
2019-03-19 15:05:32 +00:00
if (str) { strcpy(str, hex); }
return str;
}
static uint8_t rgb[3];
2017-12-25 09:17:50 +00:00
uint8_t* color_hex_to_rgb(uint32_t h){
rgb[0] = RED_FROM_HEX(h);
rgb[1] = GREEN_FROM_HEX(h);
rgb[2] = BLUE_FROM_HEX(h);
return rgb;
}
uint32_t color_rgb_to_hex(uint8_t r, uint8_t g, uint8_t b){
return RGB_TO_HEX(r, g, b);
}
MMRGBHex get_px_color(int32_t x, int32_t y){
2017-12-23 15:38:35 +00:00
MMBitmapRef bitmap;
MMRGBHex color;
if (!pointVisibleOnMainDisplay(MMPointInt32Make(x, y))){
2017-12-23 15:38:35 +00:00
return color;
}
bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, 1, 1));
2017-12-23 15:38:35 +00:00
// bitmap = MMRectMake(x, y, 1, 1);
color = MMRGBHexAtPoint(bitmap, 0, 0);
destroyMMBitmap(bitmap);
2017-12-23 15:38:35 +00:00
return color;
}
2016-10-06 10:41:37 +00:00
char* get_pixel_color(int32_t x, int32_t y){
MMRGBHex color = get_px_color(x, y);
2016-10-06 10:41:37 +00:00
char* s = pad_hex(color);
2016-10-06 10:41:37 +00:00
return s;
}
MMSizeInt32 get_screen_size(){
2018-05-06 10:04:31 +00:00
// Get display size.
MMSizeInt32 displaySize = getMainDisplaySize();
2016-10-06 10:41:37 +00:00
return displaySize;
}
2017-12-29 12:18:28 +00:00
char* set_XDisplay_name(char* name){
2017-01-02 15:10:31 +00:00
#if defined(USE_X11)
2018-06-11 13:47:55 +00:00
setXDisplay(name);
return "success";
2017-01-02 15:10:31 +00:00
#else
2018-09-14 21:42:03 +00:00
return "SetXDisplayName is only supported on Linux";
2017-01-02 15:10:31 +00:00
#endif
}
2017-12-29 12:18:28 +00:00
char* get_XDisplay_name(){
2016-10-06 10:41:37 +00:00
#if defined(USE_X11)
2018-06-11 13:47:55 +00:00
const char* display = getXDisplay();
char* sd = (char*)calloc(100, sizeof(char*));
2016-10-06 10:41:37 +00:00
2019-07-20 12:32:43 +00:00
if (sd) { strcpy(sd, display); }
2018-06-11 13:47:55 +00:00
return sd;
2016-10-06 10:41:37 +00:00
#else
2018-09-14 21:42:03 +00:00
return "GetXDisplayName is only supported on Linux";
2016-10-06 10:41:37 +00:00
#endif
}
2017-12-29 12:18:28 +00:00
// capture_screen capture screen
MMBitmapRef capture_screen(int32_t x, int32_t y, int32_t w, int32_t h){
2016-10-06 10:41:37 +00:00
// if (){
// x = 0;
// y = 0;
// // Get screen size.
2016-10-06 10:41:37 +00:00
// MMSize displaySize = getMainDisplaySize();
// w = displaySize.width;
// h = displaySize.height;
// }
MMBitmapRef bitmap = copyMMBitmapFromDisplayInRect(MMRectInt32Make(x, y, w, h));
2016-10-06 10:41:37 +00:00
// printf("%s\n", bitmap);
return bitmap;
}