libcvautomation
2.0
|
00001 """ 00002 libcvautomation_funcs.py 00003 00004 This is a high-level wrapper intended to give you easy access to 00005 libcvautomation, while staying flexible and powerful. 00006 This wrapper was designed to be like the Bash wrapper of the 00007 same name, but allows access to the advanced features of Python - 00008 for example, error handling. 00009 Also, it becomes easy to integrate other systems like dogtail 00010 into the same application test if you want to combine features. 00011 00012 To use: 00013 import libcvautomation_funcs 00014 00015 If there are any questions, comments, concerns, or suggestions, 00016 notify the developer at <bspeice@uncc.edu>""" 00017 00018 ## \file libcvautomation_funcs.py 00019 # \brief Libcvautomation wrapper for python 00020 # \details This source file is designed to give high-level access to libcvautomation using Python. It was modeled after the Bash wrapper, but allows for access to all the extra features of Python.<br>To use: 00021 # \code import libcvautomation_funcs \endcode 00022 # \author Bradlee Speice 00023 00024 ## \package libcvautomation_funcs 00025 # \brief Namespace to contain all of the features needed to use the libcvautomation_funcs wrapper 00026 00027 #------------------------------------------------------------------------------- 00028 # Import the libcvautomation library - this should have already been installed 00029 #------------------------------------------------------------------------------- 00030 import libcvautomation 00031 00032 #------------------------------------------------------------------------------- 00033 # Set up the logging options 00034 #------------------------------------------------------------------------------- 00035 00036 _use_frame_trace=None 00037 00038 try: 00039 import inspect #For getting the function that called us 00040 _use_frame_trace = True 00041 except: 00042 _use_frame_trace = False 00043 00044 import time 00045 00046 def _get_caller(): 00047 if _use_frame_trace: 00048 caller_name = inspect.stack()[2][3] #This requires a bit of explanation - 00049 # The way inspect.stack works, we can actually use 'absolute' 00050 # positioning in the stack. The [2] indicates the frame 00051 # two levels above us, not specifically the second frame. 00052 # The [3] is the name of said function. 00053 return caller_name 00054 else: 00055 return '[frame trace disabled]' 00056 00057 def _get_name(): 00058 if _use_frame_trace: 00059 caller_name = inspect.stack()[1][3] #This requires a bit of explanation - 00060 # The way inspect.stack works, we can actually use 'absolute' 00061 # positioning in the stack. The [1] indicates the frame 00062 # immediately above us, not specifically the first frame. 00063 # The [3] is the name of said function. 00064 return caller_name 00065 else: 00066 return '[frame trace disabled]' 00067 00068 00069 outfile = '/dev/null' 00070 def _log_output( message ): 00071 global outfile #Make sure we use the global outfile, 00072 #that people can change it if need be 00073 00074 #with open(outfile, 'a') as outfile_handle 00075 outfile_handle = open(outfile, 'a') 00076 #Logfile format: 00077 # <hour>:<minute>:<second> <function_name>: <message> 00078 outfile_handle.write(time.strftime('%I:%M:%S ')) 00079 outfile_handle.write(_get_caller() + ' ') 00080 outfile_handle.write(message) 00081 outfile_handle.write('\n') 00082 00083 00084 #------------------------------------------------------------------------------- 00085 # Set up the Xlib handler functions 00086 #------------------------------------------------------------------------------- 00087 00088 X11_display=None #NULL 00089 00090 def _check_display(): 00091 global X11_display 00092 00093 if X11_display is None: 00094 _log_output( 'Trying to call ' + _get_caller() + ' with no open display!' ) 00095 return False 00096 else: 00097 return True 00098 00099 def _get_display(): #Intended to be unsafe (and a bit faster)- 00100 #all wrapper functions should use check_display() explicitly 00101 global X11_display 00102 return X11_display 00103 00104 #------------------------------------------------------------------------------- 00105 # The following two functions are intended to be used on the front-end 00106 #------------------------------------------------------------------------------- 00107 00108 ## \brief Open a display for use in the libcvautomation_funcs 00109 # \details This opens a display that will be used for every other function in libcvautomation_funcs.py. If you need to open a different display, use \ref libcvautomation_funcs::close_display() and then open the new display that you want. 00110 # \param display_name The name of the display to open. The default is to open the first available display, which may not specifically be \c :0. 00111 # \warning You \em must use this function to open a display before any other functions can be used. 00112 # \returns Return \c True if the display was successfully opened, \c False otherwise 00113 def open_display( display_name='' ): 00114 global X11_display 00115 X11_display = libcvautomation.cvaOpenDisplay( display_name ) 00116 if X11_display is None: 00117 return False 00118 else: 00119 _log_output('Opened display with name: ' + display_name ) 00120 return True 00121 00122 ## \brief Close a display currently in use by libcvautomation_funcs 00123 # \details This closes the currently open display. All functions used after this will fail unless you open another display. Use \ref libcvautomation_funcs::open_display() to open the new display. 00124 # \returns None 00125 def close_display(): 00126 global X11_display 00127 if X11_display is None: 00128 _log_output('Trying to close a display that has already been closed.' ) 00129 else: 00130 libcvautomation.cvaCloseDisplay( X11_display ) 00131 X11_display=None 00132 00133 00134 #------------------------------------------------------------------------------- 00135 # Set up the default values for libcv 00136 # This way, we can edit the defaults for all functions very easily 00137 #------------------------------------------------------------------------------- 00138 00139 _search_method_default=0 00140 _tolerance_default=2250000 #Acceptable values are INT_MIN to INT_MAX 00141 _timeout_default=5 00142 _mouse_button_default=1 #Left click 00143 _libcvautomation_error_location = libcvautomation.cvaPoint 00144 _libcvautomation_error_location.x = _libcvautomation_error_location.y = -1 00145 00146 #------------------------------------------------------------------------------- 00147 # Set up the exceptions we will be using later 00148 #------------------------------------------------------------------------------- 00149 class LibcvImageNotFound(Exception): 00150 def __init__(self, value): 00151 self.value = value 00152 def __str__(self): 00153 return repr("Image not found: " + self.value) 00154 00155 class LibcvDisplayNotOpen(Exception): 00156 def __init__(self, value): 00157 self.value = value 00158 def __str__(self): 00159 return repr("Tried to use function " + self.value + " without opening a display.") 00160 00161 #------------------------------------------------------------------------------- 00162 # Begin the actual wrapper functions 00163 #------------------------------------------------------------------------------- 00164 00165 ## \brief Press a mouse button down 00166 # \param mouse_button The number of the mouse button to press 00167 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00168 def mouse_down( mouse_button = _mouse_button_default ): 00169 if _check_display(): 00170 _log_output( 'Mouse button down: ' + mouse_button ) 00171 libcvautomation.xte_mouseDown( _get_display(), mouse_button ) 00172 return True 00173 else: 00174 raise LibcvDisplayNotOpen( _get_name() ) 00175 00176 ## \brief Release a mouse button 00177 # \param mouse_button The number of the mouse button to release 00178 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00179 def mouse_up( mouse_button = _mouse_button_default ): 00180 if _check_display(): 00181 _log_output( 'Mouse button up: ' + mouse_button ) 00182 libcvautomation.xte_mouseUp(_get_display(), mouse_button ) 00183 00184 return True 00185 else: 00186 #Display not open 00187 raise LibcvDisplayNotOpen( _get_name() ) 00188 00189 ## \brief Press and release a mouse button 00190 # \param mouse_button The number of the mouse button to click 00191 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00192 def mouse_click( mouse_button = _mouse_button_default ): 00193 if _check_display(): 00194 _log_output( 'Mouse button click: ' + mouse_button ) 00195 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00196 return True 00197 else: 00198 #Display not open 00199 raise LibcvDisplayNotOpen( _get_name() ) 00200 00201 ## \brief Click a mouse button at an absolute location 00202 # \details Move the mouse to a location given by an x and y coordinate before clicking 00203 # \param x_coord The X-coordinate to move the mouse to 00204 # \param y_coord The Y-coordinate to move the mouse to 00205 # \param mouse_button The mouse button to click after moving 00206 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00207 def mouse_click_xy( x_coord, y_coord, mouse_button = _mouse_button_default ): 00208 if _check_display(): 00209 _log_output( 'Mouse button xy click: x=' + str(x_coord) + ' y=' + str(y_coord) + ' mouse_button=' + mouse_button ) 00210 current_location = libcvautomation.xte_mouseLocation(_get_display() ) 00211 x_increment = x_coord - current_location.x 00212 y_increment = y_coord - current_location.y 00213 00214 libcvautomation.xte_hoverMouseRXY(_get_display(), x_increment, y_increment ) 00215 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00216 00217 return True 00218 else: 00219 #Display not open 00220 raise LibcvDisplayNotOpen( _get_name() ) 00221 00222 ## \brief Click a mouse button at a relative location 00223 # \details Move the mouse horizontally and vertically by an increment, and then click the mouse 00224 # \param x_inc The increment to move the mouse horizontally 00225 # \note A positive increment moves the mouse right, a negative increment moves the mouse left 00226 # \param y_inc The increment to move the mouse vertically 00227 # \note A positive increment moves the mouse down, a negative increment moves the mouse up 00228 # \param mouse_button The mouse button to click after moving 00229 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00230 def mouse_click_rxy( x_inc, y_inc, mouse_button = _mouse_button_default ): 00231 if _check_display(): 00232 _log_output( 'Mouse button rxy click: x=' + str(x_inc) + ' y=' + str(y_inc) + ' mouse_button=' + mouse_button ) 00233 libcvautomation.xte_hoverMouseRXY(_get_display(), x_inc, y_inc ) 00234 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00235 00236 return True 00237 else: 00238 #Display not open 00239 raise LibcvDisplayNotOpen( _get_name() ) 00240 00241 ## \brief Click a mouse button on an image inside the root X11 window 00242 # \details For each image in \c image_names, search for it inside the root X11 window. Return once a match has been found, or the timeout value has been exceeded 00243 # \param image_names List of image names to search for 00244 # \param search_method The search method to use when finding each \c image. See \ref libcvautomation_search_methods for more information on how to use the search methods. 00245 # \param tolerance The tolerance to use when finding each \c image. See \ref libcvautomation_search_methods for more information on how to use tolerance. 00246 # \param timeout Wait for \c timeout seconds before giving up the search for \c image_names in the root X11 window. 00247 # \note \c use_wait must be set to \c True for this parameter to have any effect 00248 # \param mouse_button The mouse button to click after finding an image 00249 # \param use_wait Set to \c True to make this function wait \c timeout seconds before giving up finding an image. 00250 # \note If set to \c False, this function will go through the list of \c image_names once before giving up. 00251 # \returns Return \c False if the X11 display was not opened before execution or there were no images found, and \c True otherwise 00252 def mouse_click_image( image_names, search_method = _search_method_default, 00253 tolerance = _tolerance_default, timeout = _timeout_default, 00254 mouse_button = _mouse_button_default, use_wait = True): 00255 if _check_display(): 00256 _log_output( 'Mouse button click on image: Images=' + image_names + 00257 ' search_method=' + search_method + ' tolerance=' + tolerance + 00258 ' timeout=' + _timeout_default + ' mouse_button=' + 00259 ' use_wait=' + use_wait + ' use_center=' + use_center) 00260 00261 if use_wait: 00262 #Loop through all images 'timeout' times, and click on the first match 00263 loop_check = 0 00264 while loop_check < timeout: 00265 for image in image_names: 00266 if use_center: 00267 image_location = libcvautomation.xte_clickMouseImage_location_center(_get_display(), 00268 image, mouse_button, search_method, tolerance ) 00269 else: 00270 image_location = libcvautomation.xte_clickMouseImage_location(_get_display(), 00271 image, mouse_button, search_method, tolerance ) 00272 00273 if image_location != _libcvautomation_error_location: 00274 #We've found our image, break out of the for loop and while loop 00275 return True 00276 00277 loop_check += 1 00278 else: 00279 #Just cycle through the images once 00280 for image in image_names: 00281 if use_center: 00282 image_location = libcvautomation.xte_clickMouseImage_location_center(_get_display(), 00283 image, mouse_button, search_method, tolerance ) 00284 else: 00285 image_location = libcvautomation.xte_clickMouseImage_location(_get_display(), 00286 image, mouse_button, search_method, tolerance ) 00287 00288 if image_location != _libcvautomation_error_location: 00289 #We've found our image, break out of the for loop 00290 return True 00291 00292 #No image was found 00293 raise LibcvImageNotFound( image_names ) 00294 else: 00295 #Display not open 00296 raise LibcvDisplayNotOpen( _get_name() ) 00297 00298 00299 ## \brief Press and release a mouse button twice 00300 # \param mouse_button The number of the mouse button to click twice 00301 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00302 def mouse_doubleclick( mouse_button = _mouse_button_default ): 00303 if _check_display(): 00304 _log_output( 'Mouse button doubleclick: ' + mouse_button ) 00305 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00306 00307 return True 00308 else: 00309 #Display not open 00310 raise LibcvDisplayNotOpen( _get_name() ) 00311 00312 00313 ## \brief Click a mouse button twice at an absolute location 00314 # \details Move the mouse to a location given by an x and y coordinate before clicking twice 00315 # \param x_coord The X-coordinate to move the mouse to 00316 # \param y_coord The Y-coordinate to move the mouse to 00317 # \param mouse_button The mouse button to click after moving 00318 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00319 def mouse_doubleclick_xy( x_coord, y_coord, mouse_button = _mouse_button_default ): 00320 if _check_display(): 00321 _log_output( 'Mouse button xy doubleclick: x=' + str(x_coord) + ' y=' + str(y_coord) + ' mouse_button=' + mouse_button ) 00322 current_location = libcvautomation.xte_mouseLocation(_get_display() ) 00323 x_increment = x_coord - current_location.x 00324 y_increment = y_coord - current_location.y 00325 00326 libcvautomation.xte_hoverMouseRXY(_get_display(), x_increment, y_increment ) 00327 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00328 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00329 00330 return True 00331 else: 00332 #Display not open 00333 raise LibcvDisplayNotOpen( _get_name() ) 00334 00335 ## \brief Click a mouse button twice at a relative location 00336 # \details Move the mouse horizontally and vertically by an increment, and then click the mouse twice 00337 # \param x_inc The increment to move the mouse horizontally 00338 # \note A positive increment moves the mouse right, a negative increment moves the mouse left 00339 # \param y_inc The increment to move the mouse vertically 00340 # \note A positive increment moves the mouse down, a negative increment moves the mouse up 00341 # \param mouse_button The mouse button to click after moving 00342 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00343 def mouse_doubleclick_rxy( x_inc, y_inc, mouse_button = _mouse_button_default ): 00344 if _check_display(): 00345 _log_output( 'Mouse button relative xy doubleclick: x=' + str(x_inc) + ' y=' + str(y_inc) + ' mouse_button=' + mouse_button ) 00346 libcvautomation.xte_hoverMouseRXY(_get_display(), x_inc, y_inc ) 00347 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00348 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00349 00350 return True 00351 else: 00352 #Display not open 00353 raise LibcvDisplayNotOpen( _get_name() ) 00354 00355 ## \brief Click a mouse button twice on an image inside the root X11 window 00356 # \details For each image in \c image_names, search for it inside the root X11 window. Return once a match has been found, or the timeout value has been exceeded 00357 # \param image_names List of image names to search for 00358 # \param search_method The search method to use when finding each \c image. See \ref libcvautomation_search_methods for more information on how to use the search methods. 00359 # \param tolerance The tolerance to use when finding each \c image. See \ref libcvautomation_search_methods for more information on how to use tolerance. 00360 # \param timeout Wait for \c timeout seconds before giving up the search for \c image_names in the root X11 window. 00361 # \note \c use_wait must be set to \c True for this parameter to have any effect 00362 # \param mouse_button The mouse button to click after finding an image 00363 # \param use_wait Set to \c True to make this function wait \c timeout seconds before giving up finding an image. 00364 # \param use_center Set to \c True to make this function return the center coordinate of the matched image. Setting to \c False will use the top-left corner. 00365 # \note If set to \c False, this function will go through the list of \c image_names once before giving up. 00366 # \returns Return \c False if the X11 display was not opened before execution or no images were found, and \c True otherwise 00367 def mouse_doubleclick_image( image_names, search_method = _search_method_default, 00368 tolerance = _tolerance_default, timeout = _timeout_default, 00369 mouse_button = _mouse_button_default, use_wait = True, 00370 use_center = True ): 00371 if _check_display(): 00372 _log_output( 'Mouse button doubleclick on image: Images=' + image_names + 00373 ' search_method=' + search_method + ' tolerance=' + tolerance + 00374 ' timeout=' + _timeout_default + ' mouse_button=' + 00375 ' use_wait=' + use_wait + ' use_center=' + use_center) 00376 00377 if use_wait: 00378 #Loop through all images 'timeout' times, and click on the first match 00379 loop_check = 0 00380 while loop_check < timeout: 00381 for image in image_names: 00382 if use_center: 00383 image_location = libcvautomation.xte_clickMouseImage_location_center(_get_display(), 00384 image, mouse_button, search_method, tolerance ) 00385 else: 00386 image_location = libcvautomation.xte_clickMouseImage_location(_get_display(), 00387 image, mouse_button, search_method, tolerance ) 00388 00389 if image_location != _libcvautomation_error_location: 00390 #Make sure to click twice once we've found the image 00391 #Technically assumes that the system mouse timeout is less than 00392 #the time it takes to do two boolean compares - I think this is pretty safe 00393 libcvautomation.xte_clickMouse(_get_display(), mouse_button ) 00394 #We've found our image, break out of the for loop and while loop 00395 return True 00396 00397 loop_check += 1 00398 else: 00399 #Just cycle through the images once 00400 for image in image_names: 00401 if use_center: 00402 image_location = libcvautomation.xte_clickMouseImage_location_center(_get_display(), 00403 image, mouse_button, search_method, tolerance ) 00404 else: 00405 image_location = libcvautomation.xte_clickMouseImage_location(_get_display(), 00406 image, mouse_button, search_method, tolerance ) 00407 00408 if image_location != _libcvautomation_error_location: 00409 #We've found our image, break out of the for loop 00410 return True 00411 #No image found 00412 raise LibcvImageNotFound( image_names ) 00413 else: 00414 #Display not open 00415 raise LibcvDisplayNotOpen( _get_name() ) 00416 00417 ## \brief Move the mouse to a given coordinate, and leave it there 00418 # \param x_coord The X-coordinate to move the mouse to 00419 # \param y_coord The Y-coordinate to move the mouse to 00420 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00421 def mouse_hover_xy( x_coord, y_coord ): 00422 if _check_display(): 00423 _log_output( 'Mouse button hover xy: x=' + str(x_coord) + ' y=' + str(y_coord) + ' mouse_button=' + mouse_button ) 00424 current_location = libcvautomation.xte_mouseLocation(_get_display() ) 00425 x_increment = x_coord - current_location.x 00426 y_increment = y_coord - current_location.y 00427 00428 libcvautomation.xte_hoverMouseRXY(_get_display(), x_increment, y_increment ) 00429 00430 return True 00431 else: 00432 #Display not open 00433 raise LibcvDisplayNotOpen( _get_name() ) 00434 00435 ## \brief Move the mouse by a given increment, and leave it there 00436 # \param x_inc The increment to move the mouse horizontally 00437 # \note A positive increment moves the mouse right, a negative increment moves the mouse left 00438 # \param y_inc The increment to move the mouse vertically 00439 # \note A positive increment moves the mouse down, a negative increment moves the mouse up 00440 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00441 def mouse_hover_rxy( x_inc, y_inc ): 00442 if _check_display(): 00443 _log_output( 'Mouse button hover relative xy: x=' + str(x_inc) + ' y=' + str(y_inc) ) 00444 libcvautomation.xte_hoverMouseRXY(_get_display(), x_inc, y_inc ) 00445 00446 return True 00447 else: 00448 #Display not open 00449 raise LibcvDisplayNotOpen( _get_name() ) 00450 00451 ## \brief Move the mouse to an image inside the root X11 window 00452 # \details For each image in \c image_names, search for it inside the root X11 window. Return once a match has been found, or the timeout value has been exceeded 00453 # \param image_names List of image names to search for 00454 # \param search_method The search method to use when finding each \c image. See \ref libcvautomation_search_methods for more information on how to use the search methods. 00455 # \param tolerance The tolerance to use when finding each \c image. See \ref libcvautomation_search_methods for more information on how to use tolerance. 00456 # \param timeout Wait for \c timeout seconds before giving up the search for \c image_names in the root X11 window. 00457 # \note \c use_wait must be set to \c True for this parameter to have any effect 00458 # \param use_wait Set to \c True to make this function wait \c timeout seconds before giving up finding an image. 00459 # \note If set to \c False, this function will go through the list of \c image_names once before giving up. 00460 # \param use_center Set to \c True to make this function return the center coordinate of the matched image. Setting to \c False will use the top-left corner. 00461 # \returns Return \c False if the X11 display was not opened before execution or no images were found, and \c True otherwise 00462 def mouse_hover_image( image_names, search_method = _search_method_default, 00463 tolerance = _tolerance_default, timeout = _timeout_default, 00464 use_wait = True, use_center = True ): 00465 if _check_display(): 00466 _log_output( 'Mouse button click on image: Images=' + image_names + 00467 ' search_method=' + search_method + ' tolerance=' + tolerance + 00468 ' timeout=' + _timeout_default + ' use_wait=' + use_wait + 00469 ' use_center=' + use_center) 00470 00471 if use_wait: 00472 #Loop through all images 'timeout' times, and click on the first match 00473 loop_check = 0 00474 while loop_check < timeout: 00475 for image in image_names: 00476 if use_center: 00477 image_location = libcvautomation.xte_hoverMouseImage_location_center(_get_display(), 00478 image, mouse_button, search_method, tolerance ) 00479 else: 00480 image_location = libcvautomation.xte_hoverMouseImage_location(_get_display(), 00481 image, mouse_button, search_method, tolerance ) 00482 00483 if image_location != _libcvautomation_error_location: 00484 #We've found our image, break out of the for loop and while loop 00485 return True 00486 00487 loop_check += 1 00488 else: 00489 #Just cycle through the images once 00490 for image in image_names: 00491 if use_center: 00492 image_location = libcvautomation.xte_hoverMouseImage_location_center(_get_display(), 00493 image, mouse_button, search_method, tolerance ) 00494 else: 00495 image_location = libcvautomation.xte_hoverMouseImage_location(_get_display(), 00496 image, mouse_button, search_method, tolerance ) 00497 00498 if image_location != _libcvautomation_error_location: 00499 #We've found our image, break out of the for loop 00500 return True 00501 00502 #No images were found 00503 raise LibcvImageNotFound( image_names ) 00504 else: 00505 #Display not open or no image found 00506 raise LibcvDisplayNotOpen( _get_name() ) 00507 00508 ## \brief Jiggle the mouse in place 00509 # \details Move the mouse right and down 1 pixel, and back. Useful for activating menu entries, etc. 00510 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00511 def mouse_jiggle(): 00512 """Jiggle the mouse in place""" 00513 if _check_display(): 00514 _log_output( 'Mouse button jiggle' ) 00515 libcvautomation.xte_mouseJiggle(_get_display() ) 00516 return True 00517 else: 00518 #Display not open 00519 raise LibcvDisplayNotOpen( _get_name() ) 00520 00521 ## \brief Scroll the mouse wheel up 00522 # \details This is the same as pressing mouse button 4 00523 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00524 def mouse_scroll_up(): 00525 if _check_display(): 00526 _log_output( 'Mouse scroll up' ) 00527 libcvautomation.xte_mouseScrollUp(_get_display() ) 00528 return True 00529 else: 00530 #Display not open 00531 raise LibcvDisplayNotOpen( _get_name() ) 00532 00533 ## \brief Scroll the mouse wheel down 00534 # \details This is the same as pressing mouse button 5 00535 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00536 def mouse_scroll_down(): 00537 if _check_display(): 00538 _log_output( 'Mouse scroll down' ) 00539 libcvautomation.xte_mouseScrollDown(_get_display() ) 00540 return True 00541 else: 00542 #Display not open 00543 raise LibcvDisplayNotOpen( _get_name() ) 00544 00545 ## \brief Drag and drop one image to another 00546 # \param drag_image The image to drag from 00547 # \param drag_to The image giving the location to drag to 00548 # \param use_center Drag and drop the image using the center of the image as the location where the mouse will click and drag to 00549 # \returns Return \c False if the X11 display was not opened before execution or an image was not found, and \c True otherwise 00550 def mouse_drag_n_drop( drag_image, drag_to, use_center = True ): 00551 if _check_display(): 00552 _log_output( 'Mouse drag and drop: dragging from=' + drag_image + 00553 ' dragging to=' + drag_to ) 00554 successful_hover = mouse_hover_image( drag_image, use_center=use_center ) 00555 if successful_hover: 00556 mouse_down( mouse_button=1 ) 00557 successful_hover = mouse_hover_image( drag_to ) 00558 mouse_up( mouse_button=1 ) 00559 else: 00560 raise LibcvImageNotFound( drag_image ) 00561 00562 #Return True if both hovers are successful, raise an exception otherwise 00563 if successful_hover: 00564 return True 00565 else: 00566 raise LibcvImageNotFound( drag_to ) 00567 else: 00568 #Display not open 00569 raise LibcvDisplayNotOpen( _get_name() ) 00570 00571 ## \brief Get the current location of the mouse 00572 # \details This function will return the current location of the mouse as two variables - i.e. `x, y = mouse_location()`. This is a Python-specific function. 00573 # \returns Two variables for the \c x and \c y locations 00574 def mouse_location(): 00575 if _check_display(): 00576 current_location = libcvautomation.xte_mouseLocation( _get_display() ) 00577 _log_output( 'Mouse location: x=' + str(current_location.x) + ' y=' + str(current_location.y) ) 00578 return current_location.x, current_location.y 00579 else: 00580 #Display not open 00581 raise LibcvDisplayNotOpen( _get_name() ) 00582 00583 ## \brief Enter a string of text on the keyboard 00584 # \details This function will simulate pressing the keys exactly as they are entered - unlike libcvautomation_funcs::key_down, libcvautomation_funcs::key_up, and libcvautomation_funcs::key_click, this function will display exactly what you entered: A string of \c '!' will produce a \c ! as a keypress. 00585 # \param string The string of text to enter on the keyboard 00586 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00587 def key_string( string ): 00588 if _check_display(): 00589 _log_output( 'Key string enter: string=' + string ) 00590 libcvautomation.xte_clickKeyStr(_get_display(), string ) 00591 return True 00592 else: 00593 #Display not open 00594 raise LibcvDisplayNotOpen( _get_name() ) 00595 00596 ## \brief Press a key down on the keyboard, and leave it down 00597 # \param key_name The name of the key to press down. Unlike libcvautomation_funcs::key_string, this command will press a single key corresponding to the string you give it - for example, \c 'a', \c 'b', or something fancy like \c 'space'. 00598 # \note See \ref xtest_key_strings for a full list of accepted key strings. 00599 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00600 def key_down( key_name ): 00601 if _check_display(): 00602 _log_output( 'Key button down: key_name=' + key_name ) 00603 libcvautomation.xte_keyDown(_get_display(), key_name ) 00604 return True 00605 else: 00606 #Display not open 00607 raise LibcvDisplayNotOpen( _get_name() ) 00608 00609 ## \brief Release a key on the keyboard 00610 # \param key_name The name of the key to release. Unlike libcvautomation_funcs::key_string, this command will press a single key corresponding to the string you give it - for example, \c 'a', \c 'b', or something fancy like \c 'space'. 00611 # \note See \ref xtest_key_strings for a full list of accepted key strings. 00612 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00613 def key_up( key_name ): 00614 if _check_display(): 00615 _log_output( 'Key button up: key_name=' + key_name ) 00616 libcvautomation.xte_keyUp(_get_display(), key_name ) 00617 return True 00618 else: 00619 #Display not open 00620 raise LibcvDisplayNotOpen( _get_name() ) 00621 00622 ## \brief Press and release a key on the keyboard 00623 # \param key_name The name of the key to click. Unlike libcvautomation_funcs::key_string, this command will press a single key corresponding to the string you give it - for example, \c 'a', \c 'b', or something fancy like \c 'space'. 00624 # \note See \ref xtest_key_strings for a full list of accepted key strings. 00625 # \returns Return \c False if the X11 display was not opened before execution, and \c True otherwise 00626 def key_click( key_name ): 00627 if _check_display(): 00628 _log_output( 'Key button click: key_name=' + key_name ) 00629 libcvautomation.xte_keyDown(_get_display(), key_name ) 00630 return True 00631 else: 00632 #Display not open 00633 raise LibcvDisplayNotOpen( _get_name() ) 00634 00635 ## \brief Get the location of an image on the screen 00636 # \param image_names List of image names to search for in the root X11 window 00637 # \param search_method The search method to use when matching images. See \ref libcvautomation_search_methods for more information on using the \c search_method parameter 00638 # \param tolerance The tolerance to use when matching images. See \ref libcvautomation_search_methods for more informatino on using the \c tolerance parameter 00639 # \param use_center Set to \c True to return the center coordinate of an image. Set to \c False to use the top-left corner of an image 00640 # \warning Unlike libcvautomation_funcs::wait_for(), this method will not wait for an image to show up before looking for it. 00641 # \returns A dictionary pairing each image name with a cvaPoint class indicating the location it was found at (a point of (-1, -1) indicates the image was not found), or an empty dictionary if the X11 display was not opened before execution. 00642 def image_location( image_names, search_method = _search_method_default, 00643 tolerance = _tolerance_default, use_center = True ): 00644 if _check_display(): 00645 location_array = {} 00646 _log_output( 'Locate image (image_location): image_names=' + image_names ) 00647 if use_center: 00648 for image in image_names: 00649 image_location = libcvautomation.matchSubImage_X11_center(_get_display(), image, 00650 search_method, tolerance ) 00651 location_array += [image, image_location] 00652 else: 00653 for image in image_names: 00654 image_location = libcvautomation.matchSubImage_X11(_get_display(), image, 00655 search_method, tolerance ) 00656 location_array += [image, image_location] 00657 00658 return location_array 00659 else: 00660 #Display not open 00661 raise LibcvDisplayNotOpen( _get_name() ) 00662 00663 ## \brief Get the location of an image on the screen, waiting for it to show up 00664 # \param image_names List of image names to search for in the root X11 window 00665 # \param search_method The search method to use when matching images. See \ref libcvautomation_search_methods for more information on using the \c search_method parameter 00666 # \param tolerance The tolerance to use when matching images. See \ref libcvautomation_search_methods for more informatino on using the \c tolerance parameter 00667 # \param timeout The time (in seconds) to wait when searching for an image on the root X11 window 00668 # \param use_center Set to \c True to return the center coordinate of an image. Set to \c False to use the top-left corner of an image 00669 # \returns A dictionary pairing each image name with a cvaPoint class indicating the location it was found at (a point of (-1, -1) indicates the image was not found), or an empty dictionary if the X11 display was not opened before execution. 00670 def wait_for( image_names, search_method = _search_method_default, 00671 tolerance = _tolerance_default, timeout = _timeout_default, 00672 use_center = True ): 00673 if _check_display(): 00674 location_array = {} 00675 _log_output( 'Locate image (wait_for): image_names=' + image_names ) 00676 if use_center: 00677 for image in image_names: 00678 image_location = libcvautomation.waitForImage_location(_get_display(), image, 00679 search_method, tolerance, timeout ) 00680 location_array += [image, image_location] 00681 else: 00682 for image in image_names: 00683 image_location = libcvautomation.waitForImage_location_center(_get_display(), image, 00684 search_method, tolerance, timeout ) 00685 location_array += [image, image_location] 00686 00687 return location_array 00688 else: 00689 #Display not open 00690 raise LibcvDisplayNotOpen( _get_name() ) 00691 00692 ## \brief Execute a libcvautomation command based on a string 00693 # \details This is a handler for the xte_commandString() function. 00694 # \note Not all commands need all the parameters specified. See \ref xtest_command_strings for more information on how to use this function. 00695 # \param string The command string to execute 00696 # \param mouse_button The mouse button to click, if applicable 00697 # \param search_method The search method to use, if applicable. See \ref libcvautomation_search_methods for more information on how to use the \c search_method parameter 00698 # \param tolerance The tolerance to use, if applicable. See \ref libcvautomation_search_methods for more information on how to use the \c tolerance parameter 00699 # \param timeout The timeout (in seconds) to use, if applicable 00700 # \returns A cvaPoint class with the following points:<br>(0, 0) or up indicates a success.<br>(-1, -1) indicates either the command was not successful, or that the command was not recognized.<br>(-2, -2) indicates that the command did not need to return anything - For example, commands like \ref mousejiggle don't normally return a value. 00701 def command_string( string, mouse_button = _mouse_button_default, search_method = _search_method_default, 00702 tolerance = _tolerance_default, timeout = _timeout_default): 00703 00704 #The return for this function bears a bit of talking about: 00705 # A return of (0, 0) or up is a success 00706 # A return of (-1, -1) is an error that either the command wasn't successful, or the command wasn't recognized 00707 # A return of (-2, -2) indicates that the command didn't need to return anything - 00708 # This helps differentiate between errors and functions like key_click that 00709 # don't really use a point 00710 00711 if _check_display(): 00712 _log_output( 'Command string: string=' + string + ' search_method=' + str(search_method) + 00713 ' tolerance=' + str(tolerance) + ' timeout=' + str(timeout) ) 00714 result_point = libcvautomation.xte_commandString(_get_display(), string, 00715 mouse_button, search_method, 00716 tolerance, timeout ) 00717 00718 if result_point == _libcvautomation_error_location: 00719 raise LibcvImageNotFound( string ) 00720 else: 00721 return result_point 00722 00723 else: 00724 #Display not open 00725 raise LibcvDisplayNotOpen( _get_name() )