diff --git a/src/pygwin/_win.py b/src/pygwin/_win.py index 3fa9cda..1454a73 100644 --- a/src/pygwin/_win.py +++ b/src/pygwin/_win.py @@ -4,10 +4,14 @@ from datetime import datetime as _dt from pygwin.image import save as _s from pygwin._pg import pg as _pg import pygwin.image as _img -import win32job as _w32j -import win32api as _w32a -import win32con as _w32c -import win32gui as _w32g +try: + import win32job as _w32j + import win32api as _w32a + import win32con as _w32c + import win32gui as _w32g + nonwin32api = False +except: + nonwin32api = True import requests as _req import tempfile as _tf import threading as _t @@ -70,37 +74,46 @@ class _win(_surface): try:self.tray.stop() except:pass def focus(self): - self.hide() - self.show() - _w32g.BringWindowToTop(self.hwnd) - _w32g.ShowWindow(self.hwnd, _w32c.SW_SHOWNORMAL) - _w32g.SetForegroundWindow(self.hwnd) + if not nonwin32api: + self.hide() + self.show() + _w32g.BringWindowToTop(self.hwnd) + _w32g.ShowWindow(self.hwnd, _w32c.SW_SHOWNORMAL) + _w32g.SetForegroundWindow(self.hwnd) def hide(self): - _w32g.ShowWindow(self.hwnd, _w32c.SW_HIDE) + if not nonwin32api: + _w32g.ShowWindow(self.hwnd, _w32c.SW_HIDE) def show(self): - _w32g.ShowWindow(self.hwnd, _w32c.SW_SHOW) + if not nonwin32api: + _w32g.ShowWindow(self.hwnd, _w32c.SW_SHOW) def move(self, x, y): - rect = _w32g.GetWindowRect(self.hwnd) - _w32g.MoveWindow(self.hwnd, int(x), int(y), - rect[2]-x, rect[3]-y, 0) + if not nonwin32api: + rect = self._getRect() + _w32g.MoveWindow(self.hwnd, int(x), int(y), + rect[2]-x, rect[3]-y, 0) def screenshot(self, path): _s(self._orig, path) return path - def center(self,x=_w32a.GetSystemMetrics(0)/2, - y=_w32a.GetSystemMetrics(1)/2): + def center(self,x=_pg.display.get_desktop_sizes()[0][0]/2, + y=_pg.display.get_desktop_sizes()[0][1]/2): self.move(x-self.size[0]/2,y-self.size[1]/2) + def _getRect(self): + if not nonwin32api: + return _w32g.GetWindowRect(self.hwnd) def denyDrag(self): - self._isallowdrag = True - def loop(self): - while self._isallowdrag: - pos = _m.get_position() - pos = [pos[i]-self.position[i] for i in range(2)] - if pos[0] < _w32g.GetWindowRect(self.hwnd)[2]-137: - if pos[1] < 30: - _m.release('left') - _t.Thread(target=lambda:loop(self),daemon=1).start() + if not nonwin32api: + self._isallowdrag = True + def loop(self): + while self._isallowdrag: + pos = _m.get_position() + pos = [pos[i]-self.position[i] for i in range(2)] + if pos[0] < self._getRect()[2]-137: + if pos[1] < 30: + _m.release('left') + _t.Thread(target=lambda:loop(self),daemon=1).start() def allowDrag(self): - self._isallowdrag = False + if not nonwin32api: + self._isallowdrag = False # def smartDrag(self, x): # self.allowDrag() # self._issmartdrag = x @@ -128,10 +141,11 @@ class _win(_surface): # _t.Thread(target=lambda:loop(self),daemon=1).start() @property def position(self): - rect = _w32g.GetWindowRect(self.hwnd) - x = rect[0] - y = rect[1] - return (x, y) + if not nonwin32api: + rect = self._getRect() + x = rect[0] + y = rect[1] + return (x, y) @property def rawFps(self): if self._withfps: @@ -143,10 +157,12 @@ class _win(_surface): return int(self.rawFps) @property def hwnd(self): - return _pg.display.get_wm_info()['window'] + if not nonwin32api: + return _pg.display.get_wm_info()['window'] @property def visible(self): - return _w32g.IsWindowVisible(self._win) + if not nonwin32api: + return _w32g.IsWindowVisible(self._win) def create(title=None, size=(0,0), icon=None, resizable=False, noframe=False): screen = _pg.display.set_mode(size) @@ -162,23 +178,24 @@ def create(title=None, size=(0,0), icon=None, resizable=False, noframe=False): return _win(icon) def ramLimit(memory_limit): - hjob = _w32j.CreateJobObject(None,job_name) - if breakaway: - info = _w32j.QueryInformationJobObject(hjob,_w32j.JobObjectExtendedLimitInformation) - if breakaway=='silent':info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK) - else:info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_BREAKAWAY_OK) - _w32j.SetInformationJobObject(hjob,_w32j.JobObjectExtendedLimitInformation,info) - hprocess = _w32a.GetCurrentProcess() - try:_w32j.AssignProcessToJobObject(hjob, hprocess);g_hjob=hjob - except _w32j.error as e: - if e.winerror!=winerror.ERROR_ACCESS_DENIED:raise - elif sys.getwindowsversion()>=(6,2):raise - elif _w32j.IsProcessInJob(hprocess,None):raise - warnings.warn('The process is already in a job. Nested jobs are not supported prior to Windows 8.') - info=_w32j.QueryInformationJobObject(g_hjob,_w32j.JobObjectExtendedLimitInformation) - info['ProcessMemoryLimit']=memory_limit - info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_PROCESS_MEMORY) - _w32j.SetInformationJobObject(g_hjob,_w32j.JobObjectExtendedLimitInformation,info) + if not nonwin32api: + hjob = _w32j.CreateJobObject(None,job_name) + if breakaway: + info = _w32j.QueryInformationJobObject(hjob,_w32j.JobObjectExtendedLimitInformation) + if breakaway=='silent':info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK) + else:info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_BREAKAWAY_OK) + _w32j.SetInformationJobObject(hjob,_w32j.JobObjectExtendedLimitInformation,info) + hprocess = _w32a.GetCurrentProcess() + try:_w32j.AssignProcessToJobObject(hjob, hprocess);g_hjob=hjob + except _w32j.error as e: + if e.winerror!=winerror.ERROR_ACCESS_DENIED:raise + elif sys.getwindowsversion()>=(6,2):raise + elif _w32j.IsProcessInJob(hprocess,None):raise + warnings.warn('The process is already in a job. Nested jobs are not supported prior to Windows 8.') + info=_w32j.QueryInformationJobObject(g_hjob,_w32j.JobObjectExtendedLimitInformation) + info['ProcessMemoryLimit']=memory_limit + info['BasicLimitInformation']['LimitFlags']|=(_w32j.JOB_OBJECT_LIMIT_PROCESS_MEMORY) + _w32j.SetInformationJobObject(g_hjob,_w32j.JobObjectExtendedLimitInformation,info) def close(): _pg.quit() diff --git a/src/pygwin/console.py b/src/pygwin/console.py index 1495e70..08f6816 100644 --- a/src/pygwin/console.py +++ b/src/pygwin/console.py @@ -1,67 +1,86 @@ -import win32console as w32con -import win32con as w32c -import win32gui as w32g -import win32api as w32a +from pygwin._pg import pg +try: + import win32console as w32con + import win32con as w32c + import win32gui as w32g + import win32api as w32a + nonwin32api = True +except: + nonwin32api = False import pyautogui as pag class console: def __init__(self): - self._hwnd = w32con.GetConsoleWindow() + if not nonwin32api: + self._hwnd = w32con.GetConsoleWindow() @property def hwnd(self): - return self._hwnd + if not nonwin32api: + return self._hwnd def focus(self): - self.hide() - self.show() - w32g.BringWindowToTop(self.hwnd) - w32g.ShowWindow(self.hwnd, w32c.SW_SHOWNORMAL) - w32g.SetForegroundWindow(self.hwnd) - def unfocus(self): - pass + if not nonwin32api: + self.hide() + self.show() + w32g.BringWindowToTop(self.hwnd) + w32g.ShowWindow(self.hwnd, w32c.SW_SHOWNORMAL) + w32g.SetForegroundWindow(self.hwnd) def hide(self): - w32g.ShowWindow(self.hwnd, w32c.SW_HIDE) + if not nonwin32api: + w32g.ShowWindow(self.hwnd, w32c.SW_HIDE) def show(self): - w32g.ShowWindow(self.hwnd, w32c.SW_SHOW) + if not nonwin32api: + w32g.ShowWindow(self.hwnd, w32c.SW_SHOW) def move(self, x, y): - w32g.SetWindowPos(self.hwnd, x, y, self.size[0], self.size[1]) + if not nonwin32api: + w32g.SetWindowPos(self.hwnd, x, y, self.size[0], self.size[1]) def resize(self, width, height): - w32g.SetWindowPos(self.hwnd, self.position[0], self.position[1], width, height) + if not nonwin32api: + w32g.SetWindowPos(self.hwnd, self.position[0], self.position[1], width, height) def minimize(self): - w32g.ShowWindow(hwnd, w32c.SW_MINIMIZE) - return self.size + if not nonwin32api: + w32g.ShowWindow(hwnd, w32c.SW_MINIMIZE) + return self.size def maximize(self): - w32g.ShowWindow(hwnd, w32c.SW_MAXIMIZE) - return self.size + if not nonwin32api: + w32g.ShowWindow(hwnd, w32c.SW_MAXIMIZE) + return self.size def title(): def fget(self): - return w32con.GetConsoleTitle() + if not nonwin32api: + return w32con.GetConsoleTitle() def fset(self, value): - w32con.SetConsoleTitle(str(value)) + if not nonwin32api: + w32con.SetConsoleTitle(str(value)) def fdel(self): pass return locals() title = property(**title()) - def center(self,x=w32a.GetSystemMetrics(0)/2, - y=w32a.GetSystemMetrics(1)/2): - self.move(x-self.size[0]/2,y-self.size[1]/2) + def center(self,x=_pg.display.get_desktop_sizes()[0][0]/2, + y=_pg.display.get_desktop_sizes()[0][1]/2): + if not nonwin32api: + self.move(x-self.size[0]/2,y-self.size[1]/2) @property def visible(self): - return w32g.IsWindowVisible(self.hwnd) + if not nonwin32api: + return w32g.IsWindowVisible(self.hwnd) @property def position(self): - rect = w32g.GetWindowRect(self.hwnd) - x = rect[0]+7 - y = rect[1] - return (x, y) + if not nonwin32api: + rect = w32g.GetWindowRect(self.hwnd) + x = rect[0]+7 + y = rect[1] + return (x, y) @property def size(self): - rect = w32g.GetWindowRect(self.hwnd) - w = rect[2] - self.position[0]-7 - h = rect[3] - self.position[1]-7 - return (w, h) + if not nonwin32api: + rect = w32g.GetWindowRect(self.hwnd) + w = rect[2] - self.position[0]-7 + h = rect[3] - self.position[1]-7 + return (w, h) def screenshot(self, path): - rect = self.position+self.size - self.focus() - pag.screenshot(path, region=rect) - return path + if not nonwin32api: + rect = self.position+self.size + self.focus() + pag.screenshot(path, region=rect) + return path console = console()