github.com/iDigitalFlame/xmt@v0.5.4/device/screen/screen_windows.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  // Package screen is a helper package that contains generic functions that allow
    21  // for taking ScreenShots of the current display (if supported).
    22  package screen
    23  
    24  import (
    25  	"image"
    26  	"io"
    27  
    28  	"github.com/iDigitalFlame/xmt/device/winapi"
    29  )
    30  
    31  // Capture attempts to take a PNG-encoded screenshot of all the active
    32  // displays on the device into the supplied io.Writer.
    33  //
    34  // This function will return an error getting the screen color info fails or
    35  // encoding the image fails.
    36  //
    37  // This function calculates the dimensions of all the active displays together
    38  // and calls 'CaptureRange' underneath.
    39  //
    40  // TODO(dij): Currently works only on Windows devices.
    41  func Capture(w io.Writer) error {
    42  	c, err := winapi.ActiveDisplays()
    43  	if err != nil {
    44  		return err
    45  	}
    46  	var h, k uint32
    47  	for i := uint32(0); i < c; i++ {
    48  		r, err := winapi.DisplayBounds(i)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		if v := uint32(r.Dy()); v > h {
    53  			h = v
    54  		}
    55  		k += uint32(r.Dx())
    56  	}
    57  	return winapi.ScreenShot(0, 0, k, h, w)
    58  }
    59  
    60  // ActiveDisplays returns the count of current active displays enabled on the
    61  // device.
    62  //
    63  // This function returns an error if any error occurs when retrieving the display
    64  // count.
    65  //
    66  // TODO(dij): Currently works only on Windows devices.
    67  func ActiveDisplays() (uint32, error) {
    68  	return winapi.ActiveDisplays()
    69  }
    70  
    71  // DisplayBounds returns the bounds of the supplied display index.
    72  //
    73  // This function will return the bounds of the first monitor if the index is out
    74  // of bounds of the current display count.
    75  //
    76  // TODO(dij): Currently works only on Windows devices.
    77  func DisplayBounds(i uint32) (image.Rectangle, error) {
    78  	return winapi.DisplayBounds(i)
    79  }
    80  
    81  // CaptureRange attempts to take a PNG-encoded screenshot of the current
    82  // dimensions specified into the supplied io.Writer.
    83  //
    84  // This function will return an error getting the screen color info fails or
    85  // encoding the image fails.
    86  //
    87  // TODO(dij): Currently works only on Windows devices.
    88  func CaptureRange(x, y, width, height uint32, w io.Writer) error {
    89  	return winapi.ScreenShot(x, y, width, height, w)
    90  }