Add GdkMonitor

Add a simple object representing a monitor.
This commit is contained in:
Matthias Clasen 2016-03-31 23:10:04 -04:00
parent 8fc11ef2a5
commit 837244809e
5 changed files with 779 additions and 0 deletions

View File

@ -82,6 +82,7 @@ gdk_public_h_sources = \
gdkkeysyms.h \
gdkkeysyms-compat.h \
gdkmain.h \
gdkmonitor.h \
gdkpango.h \
gdkframeclock.h \
gdkpixbuf.h \
@ -151,6 +152,7 @@ gdk_c_sources = \
gdkglobals.c \
gdkkeys.c \
gdkkeyuni.c \
gdkmonitor.c \
gdkoffscreenwindow.c \
gdkframeclock.c \
gdkframeclockidle.c \

View File

@ -46,6 +46,7 @@
#include <gdk/gdkkeys.h>
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkmain.h>
#include <gdk/gdkmonitor.h>
#include <gdk/gdkpango.h>
#include <gdk/gdkpixbuf.h>
#include <gdk/gdkproperty.h>

602
gdk/gdkmonitor.c Normal file
View File

@ -0,0 +1,602 @@
/*
* gdkmonitor.c
*
* Copyright 2016 Red Hat, Inc.
*
* Matthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gdkmonitorprivate.h"
#include "gdkdisplay.h"
#include "gdkenumtypes.h"
/**
* SECTION:gdkmonitor
* @Title: GdkMonitor
* @Short_description: Object representing an output
*
* GdkMonitor objects represent the individual outputs that are
* associated with a #GdkDisplay. GdkDisplay has APIs to enumerate
* monitors with gdk_display_get_monitors() and to find particular
* monitors with gdk_display_get_primary_monitor() or
* gdk_display_get_monitor_at_window().
*
* GdkMonitor was introduced in GTK+ 3.22 and supersedes earlier
* APIs in GdkScreen to obtain monitor-related information.
*/
enum {
PROP_0,
PROP_DISPLAY,
PROP_MANUFACTURER,
PROP_MODEL,
PROP_SCALE_FACTOR,
PROP_GEOMETRY,
PROP_WORKAREA,
PROP_WIDTH_MM,
PROP_HEIGHT_MM,
PROP_REFRESH_RATE,
PROP_SUBPIXEL_LAYOUT,
LAST_PROP
};
static GParamSpec *props[LAST_PROP] = { NULL, };
enum {
INVALIDATE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE (GdkMonitor, gdk_monitor, G_TYPE_OBJECT)
static void
gdk_monitor_init (GdkMonitor *monitor)
{
monitor->scale_factor = 1;
}
static void
gdk_monitor_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GdkMonitor *monitor = GDK_MONITOR (object);
switch (prop_id)
{
case PROP_DISPLAY:
g_value_set_object (value, monitor->display);
break;
case PROP_MANUFACTURER:
g_value_set_string (value, monitor->manufacturer);
break;
case PROP_MODEL:
g_value_set_string (value, monitor->model);
break;
case PROP_SCALE_FACTOR:
g_value_set_int (value, monitor->scale_factor);
break;
case PROP_GEOMETRY:
g_value_set_boxed (value, &monitor->geometry);
break;
case PROP_WORKAREA:
{
GdkRectangle workarea;
gdk_monitor_get_workarea (monitor, &workarea);
g_value_set_boxed (value, &workarea);
}
break;
case PROP_WIDTH_MM:
g_value_set_int (value, monitor->width_mm);
break;
case PROP_HEIGHT_MM:
g_value_set_int (value, monitor->height_mm);
break;
case PROP_REFRESH_RATE:
g_value_set_boolean (value, monitor->refresh_rate);
break;
case PROP_SUBPIXEL_LAYOUT:
g_value_set_enum (value, monitor->subpixel_layout);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gdk_monitor_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GdkMonitor *monitor = GDK_MONITOR (object);
switch (prop_id)
{
case PROP_DISPLAY:
monitor->display = g_value_get_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gdk_monitor_finalize (GObject *object)
{
GdkMonitor *monitor = GDK_MONITOR (object);
g_free (monitor->manufacturer);
g_free (monitor->model);
G_OBJECT_CLASS (gdk_monitor_parent_class)->finalize (object);
}
static void
gdk_monitor_class_init (GdkMonitorClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = gdk_monitor_finalize;
object_class->get_property = gdk_monitor_get_property;
object_class->set_property = gdk_monitor_set_property;
props[PROP_DISPLAY] =
g_param_spec_object ("display",
"Display",
"The display of the monitor",
GDK_TYPE_DISPLAY,
G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY);
props[PROP_MANUFACTURER] =
g_param_spec_string ("manufacturer",
"Manufacturer",
"The manufacturer name",
NULL,
G_PARAM_READABLE);
props[PROP_MODEL] =
g_param_spec_string ("model",
"Model",
"The model name",
NULL,
G_PARAM_READABLE);
props[PROP_SCALE_FACTOR] =
g_param_spec_int ("scale-factor",
"Scale factor",
"The scale factor",
0, G_MAXINT,
1,
G_PARAM_READABLE);
props[PROP_GEOMETRY] =
g_param_spec_boxed ("geometry",
"Geometry",
"The geometry of the monitor",
GDK_TYPE_RECTANGLE,
G_PARAM_READABLE);
props[PROP_WORKAREA] =
g_param_spec_boxed ("workarea",
"Workarea",
"The workarea of the monitor",
GDK_TYPE_RECTANGLE,
G_PARAM_READABLE);
props[PROP_WIDTH_MM] =
g_param_spec_int ("width-mm",
"Physical width",
"The width of the monitor, in millimeters",
0, G_MAXINT,
0,
G_PARAM_READABLE);
props[PROP_HEIGHT_MM] =
g_param_spec_int ("height-mm",
"Physical height",
"The height of the monitor, in millimeters",
0, G_MAXINT,
0,
G_PARAM_READABLE);
props[PROP_REFRESH_RATE] =
g_param_spec_int ("refresh-rate",
"Refresh rate",
"The refresh rate, in millihertz",
0, G_MAXINT,
0,
G_PARAM_READABLE);
props[PROP_SUBPIXEL_LAYOUT] =
g_param_spec_enum ("subpixel-layout",
"Subpixel layout",
"The subpixel layout",
GDK_TYPE_SUBPIXEL_LAYOUT,
GDK_SUBPIXEL_LAYOUT_UNKNOWN,
G_PARAM_READABLE);
g_object_class_install_properties (object_class, LAST_PROP, props);
signals[INVALIDATE] = g_signal_new ("invalidate",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_FIRST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
/**
* gdk_monitor_get_display:
* @monitor: a #GdkMonitor
*
* Gets the display that this monitor belongs to.
*
* Returns: (transfer none): the display
* Since: 3.22
*/
GdkDisplay *
gdk_monitor_get_display (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);
return monitor->display;
}
/**
* gdk_monitor_get_geometry:
* @monitor: a #GdkMonitor
* @geometry: (out): a #GdkRectangle to be filled wiht the monitor geometry
*
* Retrieves the size and position of an individual monitor within the
* display coordinate space. The returned geometry is in application pixels,
* not in device pixels (see gdk_monitor_get_scale_factor()).
*
* Since: 3.22
*/
void
gdk_monitor_get_geometry (GdkMonitor *monitor,
GdkRectangle *geometry)
{
g_return_if_fail (GDK_IS_MONITOR (monitor));
g_return_if_fail (geometry != NULL);
*geometry = monitor->geometry;
}
/**
* gdk_monitor_get_workarea:
* @monitor: a #GdkMonitor
* @workarea: (out): a #GdkRectangle to be filled with
* the monitor workarea
*
* Retrieves the size and position of the work area on a monitor
* within the display coordinate space. The returned geometry is in
* application pixels, not in device pixels (see
* gdk_monitor_get_scale_factor()).
*
* The work area should be considered when positioning menus and
* similar popups, to avoid placing them below panels, docks or other
* desktop components.
*
* Note that not all backends may have a concept of workarea. This
* function will return the monitor geometry if a workarea is not
* available, or does not apply.
*
* Since: 3.22
*/
void
gdk_monitor_get_workarea (GdkMonitor *monitor,
GdkRectangle *workarea)
{
g_return_if_fail (GDK_IS_MONITOR (monitor));
g_return_if_fail (workarea != NULL);
if (GDK_MONITOR_GET_CLASS (monitor)->get_workarea)
GDK_MONITOR_GET_CLASS (monitor)->get_workarea (monitor, workarea);
else
*workarea = monitor->geometry;
}
/**
* gdk_monitor_get_width_mm:
* @monitor: a #GdkMonitor
*
* Gets the width in millimeters of the monitor.
*
* Returns: the physical width of the monitor
*
* Since: 3.22
*/
int
gdk_monitor_get_width_mm (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), 0);
return monitor->width_mm;
}
/**
* gdk_monitor_get_height_mm:
* @monitor: a #GdkMonitor
*
* Gets the height in millimeters of the monitor.
*
* Returns: the physical height of the monitor
* Since: 3.22
*/
int
gdk_monitor_get_height_mm (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), 0);
return monitor->height_mm;
}
/**
* gdk_monitor_get_manufacturer:
* @monitor: a #GdkMonitor
*
* Gets the name of the monitor's manufacturer, if available.
*
* Returns: (transfer none) (nullable): the name of the manufacturer, or %NULL
*/
const char *
gdk_monitor_get_manufacturer (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);
return monitor->manufacturer;
}
/**
* gdk_monitor_get_model:
* @monitor: a #GdkMonitor
*
* Gets the a string identifying the monitor model, if available.
*
* Returns: (transfer none) (nullable): the monitor model, or %NULL
*/
const char *
gdk_monitor_get_model (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), NULL);
return monitor->model;
}
/**
* gdk_monitor_get_scale_factor:
* @monitor: a #GdkMonitor
*
* Gets the internal scale factor that maps from monitor coordinates
* to the actual device pixels. On traditional systems this is 1, but
* on very high density outputs this can be a higher value (often 2).
*
* This can be used if you want to create pixel based data for a
* particular monitor, but most of the time youre drawing to a window
* where it is better to use gdk_window_get_scale_factor() instead.
*
* Returns: the scale factor
* Since: 3.22
*/
int
gdk_monitor_get_scale_factor (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), 0);
return monitor->scale_factor;
}
/**
* gdk_monitor_get_refresh_rate:
* @monitor: a #GdkMonitor
*
* Gets the refresh rate of the monitor, if available.
*
* The value is in milli-Hertz, so a refresh rate of 60Hz
* is returned as 60000.
*
* Returns: the refresh rate in milli-Hertz, or 0
* Since: 3.22
*/
int
gdk_monitor_get_refresh_rate (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), 0);
return monitor->refresh_rate;
}
/**
* gdk_monitor_get_subpixel_layout:
* @monitor: a #GdkMonitor
*
* Gets information about the layout of red, green and blue
* primaries for each pixel in this monitor, if available.
*
* Returns: the subpixel layout
* Since: 3.22
*/
GdkSubpixelLayout
gdk_monitor_get_subpixel_layout (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), GDK_SUBPIXEL_LAYOUT_UNKNOWN);
return monitor->subpixel_layout;
}
/**
* gdk_monitor_is_primary:
* @monitor: a #GdkMonitor
*
* Gets whether this monitor should be considered primary
* (see gdk_display_get_primary_monitor()).
*
* Returns: %TRUE if @monitor is primary
* Since: 3.22
*/
gboolean
gdk_monitor_is_primary (GdkMonitor *monitor)
{
g_return_val_if_fail (GDK_IS_MONITOR (monitor), FALSE);
return monitor == gdk_display_get_primary_monitor (monitor->display);
}
GdkMonitor *
gdk_monitor_new (GdkDisplay *display)
{
return GDK_MONITOR (g_object_new (GDK_TYPE_MONITOR,
"display", display,
NULL));
}
void
gdk_monitor_set_manufacturer (GdkMonitor *monitor,
const char *manufacturer)
{
g_free (monitor->manufacturer);
monitor->manufacturer = g_strdup (manufacturer);
g_object_notify (G_OBJECT (monitor), "manufacturer");
}
void
gdk_monitor_set_model (GdkMonitor *monitor,
const char *model)
{
g_free (monitor->model);
monitor->model = g_strdup (model);
g_object_notify (G_OBJECT (monitor), "model");
}
void
gdk_monitor_set_position (GdkMonitor *monitor,
int x,
int y)
{
g_object_freeze_notify (G_OBJECT (monitor));
if (monitor->geometry.x != x)
{
monitor->geometry.x = x;
g_object_notify (G_OBJECT (monitor), "geometry");
}
if (monitor->geometry.y != y)
{
monitor->geometry.y = y;
g_object_notify (G_OBJECT (monitor), "geometry");
}
g_object_thaw_notify (G_OBJECT (monitor));
}
void
gdk_monitor_set_size (GdkMonitor *monitor,
int width,
int height)
{
g_object_freeze_notify (G_OBJECT (monitor));
if (monitor->geometry.width != width)
{
monitor->geometry.width = width;
g_object_notify (G_OBJECT (monitor), "geometry");
}
if (monitor->geometry.height != height)
{
monitor->geometry.height = height;
g_object_notify (G_OBJECT (monitor), "geometry");
}
g_object_thaw_notify (G_OBJECT (monitor));
}
void
gdk_monitor_set_physical_size (GdkMonitor *monitor,
int width_mm,
int height_mm)
{
g_object_freeze_notify (G_OBJECT (monitor));
if (monitor->width_mm != width_mm)
{
monitor->width_mm = width_mm;
g_object_notify (G_OBJECT (monitor), "width-mm");
}
if (monitor->height_mm != height_mm)
{
monitor->height_mm = height_mm;
g_object_notify (G_OBJECT (monitor), "height-mm");
}
g_object_thaw_notify (G_OBJECT (monitor));
}
void
gdk_monitor_set_scale_factor (GdkMonitor *monitor,
int scale_factor)
{
if (monitor->scale_factor == scale_factor)
return;
monitor->scale_factor = scale_factor;
g_object_notify (G_OBJECT (monitor), "scale-factor");
}
void
gdk_monitor_set_refresh_rate (GdkMonitor *monitor,
int refresh_rate)
{
if (monitor->refresh_rate == refresh_rate)
return;
monitor->refresh_rate = refresh_rate;
g_object_notify (G_OBJECT (monitor), "refresh-rate");
}
void
gdk_monitor_set_subpixel_layout (GdkMonitor *monitor,
GdkSubpixelLayout subpixel_layout)
{
if (monitor->subpixel_layout == subpixel_layout)
return;
monitor->subpixel_layout = subpixel_layout;
g_object_notify (G_OBJECT (monitor), "subpixel-layout");
}
void
gdk_monitor_invalidate (GdkMonitor *monitor)
{
g_signal_emit (monitor, signals[INVALIDATE], 0);
}

95
gdk/gdkmonitor.h Normal file
View File

@ -0,0 +1,95 @@
/*
* gdkmonitor.h
*
* Copyright 2016 Red Hat, Inc.
*
* Matthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GDK_MONITOR_H__
#define __GDK_MONITOR_H__
#if !defined (__GDK_H_INSIDE__) && !defined (GDK_COMPILATION)
#error "Only <gdk/gdk.h> can be included directly."
#endif
#include <gdk/gdkversionmacros.h>
#include <gdk/gdkrectangle.h>
#include <gdk/gdktypes.h>
G_BEGIN_DECLS
#define GDK_TYPE_MONITOR (gdk_monitor_get_type ())
#define GDK_MONITOR(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_MONITOR, GdkMonitor))
#define GDK_IS_MONITOR(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_MONITOR))
typedef struct _GdkMonitor GdkMonitor;
typedef struct _GdkMonitorClass GdkMonitorClass;
/**
* GdkSubpixelLayout:
* @GDK_SUBPIXEL_LAYOUT_UNKNOWN: The layout is not known
* @GDK_SUBPIXEL_LAYOUT_NONE: Not organized in this way
* @GDK_SUBPIXEL_LAYOUT_HORIZONTAL_RGB: The layout is horizontal, the order is RGB
* @GDK_SUBPIXEL_LAYOUT_HORIZONTAL_BGR: The layout is horizontal, the order is BGR
* @GDK_SUBPIXEL_LAYOUT_VERTICAL_RGB: The layout is vertical, the order is RGB
* @GDK_SUBPIXEL_LAYOUT_VERTICAL_BGR: The layout is vertical, the order is BGR
*
* This enumeration describes how the red, green and blue components
* of physical pixels on an output device are laid out.
*
* Since: 3.22
*/
typedef enum {
GDK_SUBPIXEL_LAYOUT_UNKNOWN,
GDK_SUBPIXEL_LAYOUT_NONE,
GDK_SUBPIXEL_LAYOUT_HORIZONTAL_RGB,
GDK_SUBPIXEL_LAYOUT_HORIZONTAL_BGR,
GDK_SUBPIXEL_LAYOUT_VERTICAL_RGB,
GDK_SUBPIXEL_LAYOUT_VERTICAL_BGR
} GdkSubpixelLayout;
GDK_AVAILABLE_IN_3_22
GType gdk_monitor_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_3_22
GdkDisplay * gdk_monitor_get_display (GdkMonitor *monitor);
GDK_AVAILABLE_IN_3_22
void gdk_monitor_get_geometry (GdkMonitor *monitor,
GdkRectangle *geometry);
GDK_AVAILABLE_IN_3_22
void gdk_monitor_get_workarea (GdkMonitor *monitor,
GdkRectangle *geometry);
GDK_AVAILABLE_IN_3_22
int gdk_monitor_get_width_mm (GdkMonitor *monitor);
GDK_AVAILABLE_IN_3_22
int gdk_monitor_get_height_mm (GdkMonitor *monitor);
GDK_AVAILABLE_IN_3_22
const char * gdk_monitor_get_manufacturer (GdkMonitor *monitor);
GDK_AVAILABLE_IN_3_22
const char * gdk_monitor_get_model (GdkMonitor *monitor);
GDK_AVAILABLE_IN_3_22
int gdk_monitor_get_scale_factor (GdkMonitor *monitor);
GDK_AVAILABLE_IN_3_22
int gdk_monitor_get_refresh_rate (GdkMonitor *monitor);
GDK_AVAILABLE_IN_3_22
GdkSubpixelLayout gdk_monitor_get_subpixel_layout (GdkMonitor *monitor);
GDK_AVAILABLE_IN_3_22
gboolean gdk_monitor_is_primary (GdkMonitor *monitor);
G_END_DECLS
#endif /* __GDK_MONITOR_H__ */

79
gdk/gdkmonitorprivate.h Normal file
View File

@ -0,0 +1,79 @@
/*
* gdkmonitorprivate.h
*
* Copyright 2016 Red Hat, Inc.
*
* Matthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GDK_MONITOR_PRIVATE_H__
#define __GDK_MONITOR_PRIVATE_H__
#include "gdkmonitor.h"
G_BEGIN_DECLS
#define GDK_MONITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_MONITOR, GdkMonitorClass))
#define GDK_IS_MONITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_MONITOR))
#define GDK_MONITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_MONITOR, GdkMonitorClass))
struct _GdkMonitor {
GObject parent;
GdkDisplay *display;
char *manufacturer;
char *model;
GdkRectangle geometry;
int width_mm;
int height_mm;
int scale_factor;
int refresh_rate;
GdkSubpixelLayout subpixel_layout;
};
struct _GdkMonitorClass {
GObjectClass parent_class;
void (* get_workarea) (GdkMonitor *monitor,
GdkRectangle *geometry);
};
GdkMonitor * gdk_monitor_new (GdkDisplay *display);
void gdk_monitor_set_manufacturer (GdkMonitor *monitor,
const char *manufacturer);
void gdk_monitor_set_model (GdkMonitor *monitor,
const char *model);
void gdk_monitor_set_position (GdkMonitor *monitor,
int x,
int y);
void gdk_monitor_set_size (GdkMonitor *monitor,
int width,
int height);
void gdk_monitor_set_physical_size (GdkMonitor *monitor,
int width_mm,
int height_mm);
void gdk_monitor_set_scale_factor (GdkMonitor *monitor,
int scale);
void gdk_monitor_set_refresh_rate (GdkMonitor *monitor,
int refresh_rate);
void gdk_monitor_set_subpixel_layout (GdkMonitor *monitor,
GdkSubpixelLayout subpixel);
void gdk_monitor_invalidate (GdkMonitor *monitor);
G_END_DECLS
#endif /* __GDK_MONITOR_PRIVATE_H__ */