mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-05-31 06:13:55 +00:00
Merge pull request #443 from go-vgo/bitmap-pr
Refactor the Alert() code and remove some code
This commit is contained in:
commit
921059926e
@ -1,46 +0,0 @@
|
||||
#ifndef _PORTABLE_SNPRINTF_H_
|
||||
#define _PORTABLE_SNPRINTF_H_
|
||||
|
||||
#define PORTABLE_SNPRINTF_VERSION_MAJOR 2
|
||||
#define PORTABLE_SNPRINTF_VERSION_MINOR 2
|
||||
|
||||
#include "os.h"
|
||||
#if defined(IS_MACOSX)
|
||||
#define HAVE_SNPRINTF
|
||||
#else
|
||||
#define HAVE_SNPRINTF
|
||||
#define PREFER_PORTABLE_SNPRINTF
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SNPRINTF
|
||||
#include <stdio.h>
|
||||
#else
|
||||
extern int snprintf(char *, size_t, const char *, /*args*/ ...);
|
||||
extern int vsnprintf(char *, size_t, const char *, va_list);
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_SNPRINTF) && defined(PREFER_PORTABLE_SNPRINTF)
|
||||
extern int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...);
|
||||
extern int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap);
|
||||
#define snprintf portable_snprintf
|
||||
#define vsnprintf portable_vsnprintf
|
||||
#endif
|
||||
|
||||
extern int asprintf (char **ptr, const char *fmt, /*args*/ ...);
|
||||
extern int vasprintf (char **ptr, const char *fmt, va_list ap);
|
||||
extern int asnprintf (char **ptr, size_t str_m, const char *fmt, /*args*/ ...);
|
||||
extern int vasnprintf(char **ptr, size_t str_m, const char *fmt, va_list ap);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
1019
base/snprintf_c.h
1019
base/snprintf_c.h
File diff suppressed because it is too large
Load Diff
@ -9,6 +9,14 @@
|
||||
|
||||
/* Some generic, cross-platform types. */
|
||||
|
||||
#ifdef RobotGo_64
|
||||
typedef int64_t intptr;
|
||||
typedef uint64_t uintptr;
|
||||
#else
|
||||
typedef int32_t intptr;
|
||||
typedef uint32_t uintptr; // Unsigned pointer integer
|
||||
#endif
|
||||
|
||||
struct _MMPoint {
|
||||
size_t x;
|
||||
size_t y;
|
||||
|
@ -19,12 +19,11 @@ import (
|
||||
|
||||
func alert() {
|
||||
// show Alert Window
|
||||
abool := robotgo.ShowAlert("hello", "robotgo")
|
||||
abool := robotgo.Alert("hello", "robotgo")
|
||||
if abool {
|
||||
fmt.Println("ok@@@", "ok")
|
||||
}
|
||||
robotgo.ShowAlert("hello", "robotgo", "Ok", "Cancel")
|
||||
|
||||
robotgo.Alert("hello", "robotgo", "Ok", "Cancel")
|
||||
}
|
||||
|
||||
func get() {
|
||||
|
@ -13,32 +13,9 @@
|
||||
|
||||
// Global delays.
|
||||
int mouseDelay = 0;
|
||||
// int keyboardDelay = 10;
|
||||
|
||||
|
||||
// int CheckMouseButton(const char * const b,
|
||||
// MMMouseButton * const button){
|
||||
// if (!button) return -1;
|
||||
|
||||
// if (strcmp(b, "left") == 0) {
|
||||
// *button = LEFT_BUTTON;
|
||||
// }
|
||||
// else if (strcmp(b, "right") == 0) {
|
||||
// *button = RIGHT_BUTTON;
|
||||
// }
|
||||
// else if (strcmp(b, "middle") == 0) {
|
||||
// *button = CENTER_BUTTON;
|
||||
// } else {
|
||||
// return -2;
|
||||
// }
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
int move_mouse(int32_t x, int32_t y){
|
||||
MMPointInt32 point;
|
||||
// int x = 103;
|
||||
// int y = 104;
|
||||
point = MMPointInt32Make(x, y);
|
||||
moveMouse(point);
|
||||
|
||||
@ -47,16 +24,11 @@ int move_mouse(int32_t x, int32_t y){
|
||||
|
||||
|
||||
int drag_mouse(int32_t x, int32_t y, MMMouseButton button){
|
||||
// const size_t x = 10;
|
||||
// const size_t y = 20;
|
||||
// MMMouseButton button = LEFT_BUTTON;
|
||||
|
||||
MMPointInt32 point;
|
||||
point = MMPointInt32Make(x, y);
|
||||
dragMouse(point, button);
|
||||
microsleep(mouseDelay);
|
||||
|
||||
// printf("%s\n", "gyp-----");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -73,15 +45,10 @@ bool move_mouse_smooth(int32_t x, int32_t y, double lowSpeed,
|
||||
|
||||
MMPointInt32 get_mouse_pos(){
|
||||
MMPointInt32 pos = getMousePos();
|
||||
|
||||
// Return object with .x and .y.
|
||||
// printf("%zu\n%zu\n", pos.x, pos.y );
|
||||
return pos;
|
||||
}
|
||||
|
||||
int mouse_click(MMMouseButton button, bool doubleC){
|
||||
// MMMouseButton button = LEFT_BUTTON;
|
||||
// bool doubleC = false;
|
||||
if (!doubleC) {
|
||||
clickMouse(button);
|
||||
} else {
|
||||
@ -94,7 +61,6 @@ int mouse_click(MMMouseButton button, bool doubleC){
|
||||
}
|
||||
|
||||
int mouse_toggle(char* d, MMMouseButton button){
|
||||
// MMMouseButton button = LEFT_BUTTON;
|
||||
bool down = false;
|
||||
if (strcmp(d, "down") == 0) {
|
||||
down = true;
|
||||
@ -111,9 +77,7 @@ int mouse_toggle(char* d, MMMouseButton button){
|
||||
}
|
||||
|
||||
int set_mouse_delay(size_t val){
|
||||
// int val = 10;
|
||||
mouseDelay = val;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -125,7 +89,6 @@ int scroll(int x, int y, int msDelay){
|
||||
}
|
||||
|
||||
int scroll_mouse(size_t scrollMagnitude, char *s){
|
||||
// int scrollMagnitude = 20;
|
||||
MMMouseWheelDirection scrollDirection;
|
||||
|
||||
if (strcmp(s, "up") == 0) {
|
||||
@ -133,7 +96,6 @@ int scroll_mouse(size_t scrollMagnitude, char *s){
|
||||
} else if (strcmp(s, "down") == 0) {
|
||||
scrollDirection = DIRECTION_DOWN;
|
||||
} else {
|
||||
// return "Invalid scroll direction specified.";
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
18
robotgo.go
18
robotgo.go
@ -1214,23 +1214,13 @@ ____ __ ____ __ .__ __. _______ ______ ____ __ ____
|
||||
|
||||
*/
|
||||
|
||||
// ShowAlert show a alert window
|
||||
// Displays alert with the attributes.
|
||||
// If cancel button is not given, only the default button is displayed
|
||||
//
|
||||
// Examples:
|
||||
// robotgo.ShowAlert("hi", "window", "ok", "cancel")
|
||||
func ShowAlert(title, msg string, args ...string) bool {
|
||||
func alertArgs(args ...string) (string, string) {
|
||||
var (
|
||||
// title string
|
||||
// msg string
|
||||
defaultBtn = "Ok"
|
||||
cancelBtn = "Cancel"
|
||||
)
|
||||
|
||||
if len(args) > 0 {
|
||||
// title = args[0]
|
||||
// msg = args[1]
|
||||
defaultBtn = args[0]
|
||||
}
|
||||
|
||||
@ -1238,6 +1228,12 @@ func ShowAlert(title, msg string, args ...string) bool {
|
||||
cancelBtn = args[1]
|
||||
}
|
||||
|
||||
return defaultBtn, cancelBtn
|
||||
}
|
||||
|
||||
func showAlert(title, msg string, args ...string) bool {
|
||||
defaultBtn, cancelBtn := alertArgs(args...)
|
||||
|
||||
cTitle := C.CString(title)
|
||||
cMsg := C.CString(msg)
|
||||
defaultButton := C.CString(defaultBtn)
|
||||
|
@ -55,3 +55,13 @@ func ActivePID(pid int32, args ...int) error {
|
||||
func DisplaysNum() int {
|
||||
return getNumDisplays()
|
||||
}
|
||||
|
||||
// Alert show a alert window
|
||||
// Displays alert with the attributes.
|
||||
// If cancel button is not given, only the default button is displayed
|
||||
//
|
||||
// Examples:
|
||||
// robotgo.Alert("hi", "window", "ok", "cancel")
|
||||
func Alert(title, msg string, args ...string) bool {
|
||||
return showAlert(title, msg, args...)
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ package robotgo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/robotn/xgb"
|
||||
@ -172,3 +173,25 @@ func DisplaysNum() int {
|
||||
|
||||
return int(reply.Number)
|
||||
}
|
||||
|
||||
// Alert show a alert window
|
||||
// Displays alert with the attributes.
|
||||
// If cancel button is not given, only the default button is displayed
|
||||
//
|
||||
// Examples:
|
||||
// robotgo.Alert("hi", "window", "ok", "cancel")
|
||||
func Alert(title, msg string, args ...string) bool {
|
||||
defaultBtn, cancelBtn := alertArgs(args...)
|
||||
c := `xmessage -center ` + msg +
|
||||
` -title ` + title + ` -buttons ` + defaultBtn + ":0," + cancelBtn + ":1" + ` -default Ok`
|
||||
out, err := Run(c)
|
||||
if err != nil {
|
||||
fmt.Println("Alert: ", err, ". ", string(out))
|
||||
return false
|
||||
}
|
||||
|
||||
if string(out) == "1" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -95,7 +95,7 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRectInt32 rect, int32_t display_id)
|
||||
if (display_id == -1) {
|
||||
screen = GetDC(NULL); /* Get entire screen */
|
||||
} else {
|
||||
screen = GetDC((HWND) display_id);
|
||||
screen = GetDC((HWND) (uintptr) display_id);
|
||||
}
|
||||
if (screen == NULL) { return NULL; }
|
||||
|
||||
|
@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef ALERT_H
|
||||
#define ALERT_H
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include "ms_stdbool.h"
|
||||
#else
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
/* Displays alert with given attributes, and blocks execution until the user
|
||||
* responds. Returns 0 if defaultButton was pressed, 1 if cancelButton was
|
||||
* pressed, or -1 if an error occurred. */
|
||||
int showAlert(const char *title, const char *msg,
|
||||
const char *defaultButton, const char *cancelButton);
|
||||
|
||||
#endif /* ALERT_H */
|
133
window/alert_c.h
133
window/alert_c.h
@ -1,35 +1,10 @@
|
||||
#include "alert.h"
|
||||
// #include "os.h"
|
||||
#include <assert.h>
|
||||
|
||||
#if defined(IS_MACOSX)
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#elif defined(USE_X11)
|
||||
#include <stdio.h> /* For fputs() */
|
||||
#include <stdlib.h> /* For exit() */
|
||||
#include <sys/wait.h> /* For wait() */
|
||||
#include <unistd.h> /* For fork() */
|
||||
#include <sys/types.h> /* For pid_t */
|
||||
#include "../base/snprintf.h" /* For asprintf() */
|
||||
#endif
|
||||
|
||||
#if defined(USE_X11)
|
||||
enum {
|
||||
TASK_SUCCESS = 0,
|
||||
FORK_FAILED = -1,
|
||||
EXEC_FAILED = -2
|
||||
};
|
||||
|
||||
/*
|
||||
* Unfortunately, X has no standard method of displaying alerts, so instead we
|
||||
* have to rely on the shell command "xmessage" (or nicer-looking equivalents).
|
||||
*
|
||||
* The return value and arguments are the same as those from to runTask()
|
||||
* (see below).
|
||||
*/
|
||||
static int xmessage(char *argv[], int *exit_status);
|
||||
|
||||
#elif defined(IS_MACOSX)
|
||||
#if defined(IS_MACOSX)
|
||||
#define CFStringCreateWithUTF8String(string) \
|
||||
((string) == NULL ? NULL : CFStringCreateWithCString(NULL, \
|
||||
string, \
|
||||
@ -56,41 +31,7 @@ int showAlert(const char *title, const char *msg,
|
||||
if (err != 0) { return -1; }
|
||||
return (responseFlags == kCFUserNotificationDefaultResponse) ? 0 : 1;
|
||||
#elif defined(USE_X11)
|
||||
/* Note that args[0] is set by the xmessage() function. */
|
||||
const char *args[10] = {NULL, msg, "-title", title, "-center"};
|
||||
int response, ret;
|
||||
char *buttonList = NULL; /* To be free()'d. */
|
||||
|
||||
if (defaultButton == NULL) defaultButton = "OK";
|
||||
|
||||
// snprintf.h
|
||||
if (cancelButton == NULL) {
|
||||
asprintf(&buttonList, "%s:2", defaultButton);
|
||||
} else {
|
||||
asprintf(&buttonList, "%s:2,%s:3", defaultButton, cancelButton);
|
||||
}
|
||||
|
||||
if (buttonList == NULL) { return -1; /* asprintf() failed. */ }
|
||||
args[5] = "-buttons";
|
||||
args[6] = buttonList;
|
||||
args[7] = "-default";
|
||||
args[8] = defaultButton;
|
||||
args[9] = NULL;
|
||||
|
||||
ret = xmessage((char **)args, &response);
|
||||
if (buttonList != NULL) {
|
||||
free(buttonList);
|
||||
buttonList = NULL;
|
||||
}
|
||||
|
||||
if (ret != TASK_SUCCESS) {
|
||||
if (ret == EXEC_FAILED) {
|
||||
fputs("xmessage or equivalent not found.\n", stderr);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (response == 2) ? 0 : 1;
|
||||
return 0;
|
||||
#else
|
||||
/* TODO: Display custom buttons instead of the pre-defined "OK"
|
||||
* and "Cancel". */
|
||||
@ -100,73 +41,3 @@ int showAlert(const char *title, const char *msg,
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(USE_X11)
|
||||
|
||||
/*
|
||||
* Attempts to run the given task synchronously with the given arguments.
|
||||
*
|
||||
* If |exit_status| is non-NULL and the task ran successfully, |exit_status| is
|
||||
* set to the exit code of the task on return.
|
||||
*
|
||||
* Returns -1 if process could not be forked, -2 if the task could not be run,
|
||||
* or 0 if the task was ran successfully.
|
||||
*/
|
||||
static int runTask(const char *taskname, char * const argv[], int *exit_status);
|
||||
|
||||
static int xmessage(char *argv[], int *exit_status) {
|
||||
// static const char * const MSG_PROGS[] = {"gmessage", "gxmessage",
|
||||
// "kmessage", "xmessage"};
|
||||
static const char * const MSG_PROGS[] = {"xmessage"};
|
||||
static int PREV_MSG_INDEX = -1;
|
||||
#define MSG_PROGS_LEN (sizeof(MSG_PROGS) / sizeof(MSG_PROGS[0]))
|
||||
|
||||
char *prog = NULL;
|
||||
int ret;
|
||||
|
||||
/* Save some fork()'ing and attempt to use last program if possible. */
|
||||
if (PREV_MSG_INDEX >= 0) {
|
||||
assert(PREV_MSG_INDEX < MSG_PROGS_LEN);
|
||||
|
||||
prog = argv[0] = (char *)MSG_PROGS[PREV_MSG_INDEX];
|
||||
ret = runTask(prog, argv, exit_status);
|
||||
} else {
|
||||
/* Otherwise, try running each xmessage alternative until one works or
|
||||
* we run out of options. */
|
||||
size_t i;
|
||||
for (i = 0; i < MSG_PROGS_LEN; ++i) {
|
||||
prog = argv[0] = (char *)MSG_PROGS[i];
|
||||
ret = runTask(prog, argv, exit_status);
|
||||
if (ret != EXEC_FAILED) break;
|
||||
}
|
||||
|
||||
if (ret == TASK_SUCCESS) PREV_MSG_INDEX = i;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int runTask(const char *taskname, char * const argv[], int *exit_status) {
|
||||
pid_t pid = fork();
|
||||
int status;
|
||||
|
||||
switch (pid) {
|
||||
case -1: /* Failed to fork */
|
||||
perror("fork");
|
||||
return FORK_FAILED; /* Failed to fork. */
|
||||
case 0: /* Child process */
|
||||
if (strcmp(argv[0], "xmessage") == 0){
|
||||
execvp(taskname, argv);
|
||||
perror("execvp failed");
|
||||
}
|
||||
exit(42); /* Failed to run task. */
|
||||
default: /* Parent process */
|
||||
wait(&status); /* Block execution until finished. */
|
||||
|
||||
if (!WIFEXITED(status) || (status = WEXITSTATUS(status)) == 42) {
|
||||
return EXEC_FAILED; /* Task failed to run. */
|
||||
}
|
||||
if (exit_status != NULL) *exit_status = status;
|
||||
return TASK_SUCCESS; /* Success! */
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -15,14 +15,6 @@
|
||||
#include <X11/Xatom.h>
|
||||
#endif
|
||||
|
||||
#ifdef RobotGo_64
|
||||
typedef int64_t intptr;
|
||||
typedef uint64_t uintptr;
|
||||
#else
|
||||
typedef int32_t intptr;
|
||||
typedef uint32_t uintptr; // Unsigned pointer integer
|
||||
#endif
|
||||
|
||||
struct _MData{
|
||||
#if defined(IS_MACOSX)
|
||||
CGWindowID CgID; // Handle to a CGWindowID
|
||||
|
Loading…
Reference in New Issue
Block a user