Recent

Author Topic: convert c program  (Read 1261 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1991
    • Lazarus and Free Pascal italian community
convert c program
« on: February 21, 2019, 11:00:23 am »
Hi guys, I have to interact with the mouse and the keyboard of mac os. I found an example written in c to send a mouse click to the operating system. How can I translate it into free pascal.

Code: Pascal  [Select]
  1. // File:
  2. // click.m
  3. //
  4. // Compile with:
  5. // gcc -o click click.m -framework ApplicationServices -framework Foundation
  6. //
  7. // Usage:
  8. // ./click -x pixels -y pixels
  9. // At the given coordinates it will click and release.
  10.  
  11. #import <Foundation/Foundation.h>
  12. #import <ApplicationServices/ApplicationServices.h>
  13.  
  14. int main(int argc, char *argv[]) {
  15. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  16. NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
  17.  
  18. // grabs command line arguments -x and -y
  19. //
  20. int x = [args integerForKey:@"x"];
  21. int y = [args integerForKey:@"y"];
  22.  
  23. // The data structure CGPoint represents a point in a two-dimensional
  24. // coordinate system. Here, X and Y distance from upper left, in pixels.
  25. //
  26. CGPoint pt;
  27. pt.x = x;
  28. pt.y = y;
  29.  
  30. // This is where the magic happens. See CGRemoteOperation.h for details.
  31. //
  32. // CGPostMouseEvent( CGPoint mouseCursorPosition,
  33. // boolean_t updateMouseCursorPosition,
  34. // CGButtonCount buttonCount,
  35. // boolean_t mouseButtonDown, ... )
  36. //
  37. // So, we feed coordinates to CGPostMouseEvent, put the mouse there,
  38. // then click and release.
  39. //
  40. CGPostMouseEvent( pt, 1, 1, 1 );
  41. CGPostMouseEvent( pt, 1, 1, 0 );
  42.  
  43. [pool release];
  44. return 0;
  45. }
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Hansaplast

  • Hero Member
  • *****
  • Posts: 549
  • Tweaking4All.com
    • Tweaking4All
Re: convert c program
« Reply #1 on: February 21, 2019, 11:37:20 am »

I have been doing this by hand ... (which comes with it's own challenges - so I'm open to easier methods)


A few tips;


1) Look at code currently use in Lazarus Pascal units
Quite a few functions etc already have been implemented - you may have to do a search on file content. The forum has some examples as well.


2) Don't forget to link frameworks (you can find the available frameworks here: /System/Library/Frameworks/) eg.;
Code: Pascal  [Select]
  1. {$linkframework foundation}
  2. {$linkframework applicationservices}


3) Take a look at this topic when you want to "import" a .h file.   


4) Some of the items used, are initialized by default (no init needed), some are not (init needed). For example NSFileManager has "defaultManager" initialized.


But all in all it always takes me quite a while to convert, try and learn about the functions I use.

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: convert c program
« Reply #2 on: February 21, 2019, 11:40:36 am »
Have a look at the package http://wiki.freepascal.org/MouseAndKeyInput
It has everything in it to do what you want.

xinyiman

  • Hero Member
  • *****
  • Posts: 1991
    • Lazarus and Free Pascal italian community
Re: convert c program
« Reply #3 on: February 21, 2019, 02:29:29 pm »
Have a look at the package http://wiki.freepascal.org/MouseAndKeyInput
It has everything in it to do what you want.

I know that package, but on mac it does not work natively. He returns this error.

Error: ld: library not found for -lXtst
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

rvk

  • Hero Member
  • *****
  • Posts: 3842
Re: convert c program
« Reply #4 on: February 21, 2019, 03:05:25 pm »
Error: ld: library not found for -lXtst
From http://wiki.freepascal.org/MouseAndKeyInput
Quote
Gtk1/2
  • needs Xtst library

Otherwise you can view carbonmouseinput.pas yourself and see how it's done.


BTW. The sourcecode in CGRemoteOperations.pas says CGPostMouseEvent is deprecated and you should use CGEventCreateMouseEvent instead. (That wasn't in the C code, was it :D)
According to Apples documentation, CGPostMouseEvent is indeed deprecated:
https://developer.apple.com/documentation/coregraphics/1541785-cgpostmouseevent

Quote
This function is not recommended for general use because of undocumented special cases and undesirable side effects. The recommended replacement for this function is CGEventCreateMouseEvent, which allows you to create a mouse event and customize the event before posting it to the event system.

Code: Pascal  [Select]
  1. { This function is obsolete. Use `CGEventCreateMouseEvent' instead.
  2.  
  3.    Synthesize a low-level mouse-button event on the local machine.
  4.  
  5.    The parameter `mouseCursorPosition' specifies the new coordinates of the
  6.    mouse in global display space.
  7.  
  8.    Pass true for `updateMouseCursorPosition' if the on-screen cursor should
  9.    be moved to the location specified in the `mouseCursorPosition'
  10.    parameter; otherwise, pass false.
  11.    
  12.    The parameter `buttonCount' specifies the number of mouse buttons, up to
  13.    a maximum of 32.
  14.  
  15.    Pass true for `mouseButtonDown' to specify that the primary or left mouse
  16.    button is down; otherwise, pass false. The remaining parameters are
  17.    Boolean values that specify whether the remaining mouse buttons are down
  18.    (true) or up (false). The second value, if any, should specify the state
  19.    of the secondary mouse button (right). A third value should specify the
  20.    state of the center button, and the remaining buttons should be in USB
  21.    device order.
  22.  
  23.    Based on the values entered, the appropriate mouse-down, mouse-up,
  24.    mouse-move, or mouse-drag events are generated, by comparing the new
  25.    state with the current state. }
  26.  
  27. function CGPostMouseEvent( mouseCursorPosition: CGPoint; updateMouseCursorPosition: boolean_t; buttonCount: CGButtonCount; mouseButtonDown: boolean_t; ... ): CGError; external name '_CGPostMouseEvent';
  28. (* CG_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_6,
  29.     __IPHONE_NA, __IPHONE_NA) *)
  30.  
« Last Edit: February 21, 2019, 03:14:12 pm by rvk »

xinyiman

  • Hero Member
  • *****
  • Posts: 1991
    • Lazarus and Free Pascal italian community
Re: convert c program
« Reply #5 on: February 21, 2019, 03:18:44 pm »
You can also fill in for cocoa and not for carbon. Apple disagrees with carbon support, so it makes no sense to do things twice. I must be able to use the package you say with cocoa support. Do you know why with cocoa it does not work and with carbon?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 1991
    • Lazarus and Free Pascal italian community
Re: convert c program
« Reply #6 on: February 21, 2019, 03:20:21 pm »
Error: ld: library not found for -lXtst
From http://wiki.freepascal.org/MouseAndKeyInput
Quote
Gtk1/2
  • needs Xtst library

Otherwise you can view carbonmouseinput.pas yourself and see how it's done.


BTW. The sourcecode in CGRemoteOperations.pas says CGPostMouseEvent is deprecated and you should use CGEventCreateMouseEvent instead. (That wasn't in the C code, was it :D)
According to Apples documentation, CGPostMouseEvent is indeed deprecated:
https://developer.apple.com/documentation/coregraphics/1541785-cgpostmouseevent

Quote
This function is not recommended for general use because of undocumented special cases and undesirable side effects. The recommended replacement for this function is CGEventCreateMouseEvent, which allows you to create a mouse event and customize the event before posting it to the event system.

Code: Pascal  [Select]
  1. { This function is obsolete. Use `CGEventCreateMouseEvent' instead.
  2.  
  3.    Synthesize a low-level mouse-button event on the local machine.
  4.  
  5.    The parameter `mouseCursorPosition' specifies the new coordinates of the
  6.    mouse in global display space.
  7.  
  8.    Pass true for `updateMouseCursorPosition' if the on-screen cursor should
  9.    be moved to the location specified in the `mouseCursorPosition'
  10.    parameter; otherwise, pass false.
  11.    
  12.    The parameter `buttonCount' specifies the number of mouse buttons, up to
  13.    a maximum of 32.
  14.  
  15.    Pass true for `mouseButtonDown' to specify that the primary or left mouse
  16.    button is down; otherwise, pass false. The remaining parameters are
  17.    Boolean values that specify whether the remaining mouse buttons are down
  18.    (true) or up (false). The second value, if any, should specify the state
  19.    of the secondary mouse button (right). A third value should specify the
  20.    state of the center button, and the remaining buttons should be in USB
  21.    device order.
  22.  
  23.    Based on the values entered, the appropriate mouse-down, mouse-up,
  24.    mouse-move, or mouse-drag events are generated, by comparing the new
  25.    state with the current state. }
  26.  
  27. function CGPostMouseEvent( mouseCursorPosition: CGPoint; updateMouseCursorPosition: boolean_t; buttonCount: CGButtonCount; mouseButtonDown: boolean_t; ... ): CGError; external name '_CGPostMouseEvent';
  28. (* CG_AVAILABLE_BUT_DEPRECATED(__MAC_10_0, __MAC_10_6,
  29.     __IPHONE_NA, __IPHONE_NA) *)
  30.  

I also discovered that in the last hours :)
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1