+static BOOL CALLBACK
+w32_monitor_enum (HMONITOR monitor, HDC hdc, RECT *rcMonitor, LPARAM dwData)
+{
+ Lisp_Object *monitor_list = (Lisp_Object *) dwData;
+
+ *monitor_list = Fcons (make_save_ptr (monitor), *monitor_list);
+
+ return TRUE;
+}
+
+static Lisp_Object
+w32_display_monitor_attributes_list (void)
+{
+ Lisp_Object attributes_list = Qnil, primary_monitor_attributes = Qnil;
+ Lisp_Object monitor_list = Qnil, monitor_frames, rest, frame;
+ int i, n_monitors;
+ HMONITOR *monitors;
+ struct gcpro gcpro1, gcpro2, gcpro3;
+
+ if (!(enum_display_monitors_fn && get_monitor_info_fn
+ && monitor_from_window_fn))
+ return Qnil;
+
+ if (!enum_display_monitors_fn (NULL, NULL, w32_monitor_enum,
+ (LPARAM) &monitor_list)
+ || NILP (monitor_list))
+ return Qnil;
+
+ n_monitors = 0;
+ for (rest = monitor_list; CONSP (rest); rest = XCDR (rest))
+ n_monitors++;
+
+ monitors = xmalloc (n_monitors * sizeof (*monitors));
+ for (i = 0; i < n_monitors; i++)
+ {
+ monitors[i] = XSAVE_POINTER (XCAR (monitor_list), 0);
+ monitor_list = XCDR (monitor_list);
+ }
+
+ monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
+ FOR_EACH_FRAME (rest, frame)
+ {
+ struct frame *f = XFRAME (frame);
+
+ if (FRAME_W32_P (f) && !EQ (frame, tip_frame))
+ {
+ HMONITOR monitor =
+ monitor_from_window_fn (FRAME_W32_WINDOW (f),
+ MONITOR_DEFAULT_TO_NEAREST);
+
+ for (i = 0; i < n_monitors; i++)
+ if (monitors[i] == monitor)
+ break;
+
+ if (i < n_monitors)
+ ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
+ }
+ }
+
+ GCPRO3 (attributes_list, primary_monitor_attributes, monitor_frames);
+
+ for (i = 0; i < n_monitors; i++)
+ {
+ Lisp_Object geometry, workarea, name, attributes = Qnil;
+ HDC hdc;
+ int width_mm, height_mm;
+ struct MONITOR_INFO_EX mi;
+
+ mi.cbSize = sizeof (mi);
+ if (!get_monitor_info_fn (monitors[i], (struct MONITOR_INFO *) &mi))
+ continue;
+
+ hdc = CreateDCA ("DISPLAY", mi.szDevice, NULL, NULL);
+ if (hdc == NULL)
+ continue;
+ width_mm = GetDeviceCaps (hdc, HORZSIZE);
+ height_mm = GetDeviceCaps (hdc, VERTSIZE);
+ DeleteDC (hdc);
+
+ attributes = Fcons (Fcons (Qframes, AREF (monitor_frames, i)),
+ attributes);
+
+ name = DECODE_SYSTEM (build_unibyte_string (mi.szDevice));
+
+ attributes = Fcons (Fcons (Qname, name), attributes);
+
+ attributes = Fcons (Fcons (Qmm_size, list2i (width_mm, height_mm)),
+ attributes);
+
+ workarea = list4i (mi.rcWork.left, mi.rcWork.top,
+ mi.rcWork.right - mi.rcWork.left,
+ mi.rcWork.bottom - mi.rcWork.top);
+ attributes = Fcons (Fcons (Qworkarea, workarea), attributes);
+
+ geometry = list4i (mi.rcMonitor.left, mi.rcMonitor.top,
+ mi.rcMonitor.right - mi.rcMonitor.left,
+ mi.rcMonitor.bottom - mi.rcMonitor.top);
+ attributes = Fcons (Fcons (Qgeometry, geometry), attributes);
+
+ if (mi.dwFlags & MONITORINFOF_PRIMARY)
+ primary_monitor_attributes = attributes;
+ else
+ attributes_list = Fcons (attributes, attributes_list);
+ }
+
+ if (!NILP (primary_monitor_attributes))
+ attributes_list = Fcons (primary_monitor_attributes, attributes_list);
+
+ UNGCPRO;
+
+ xfree (monitors);
+
+ return attributes_list;
+}
+
+static Lisp_Object
+w32_display_monitor_attributes_list_fallback (struct w32_display_info *dpyinfo)
+{
+ Lisp_Object geometry, workarea, frames, rest, frame, attributes = Qnil;
+ HDC hdc;
+ double mm_per_pixel;
+ int pixel_width, pixel_height, width_mm, height_mm;
+ RECT workarea_rect;
+
+ /* Fallback: treat (possibly) multiple physical monitors as if they
+ formed a single monitor as a whole. This should provide a
+ consistent result at least on single monitor environments. */
+ attributes = Fcons (Fcons (Qname, build_string ("combined screen")),
+ attributes);
+
+ frames = Qnil;
+ FOR_EACH_FRAME (rest, frame)
+ {
+ struct frame *f = XFRAME (frame);
+
+ if (FRAME_W32_P (f) && !EQ (frame, tip_frame))
+ frames = Fcons (frame, frames);
+ }
+ attributes = Fcons (Fcons (Qframes, frames), attributes);
+
+ pixel_width = x_display_pixel_width (dpyinfo);
+ pixel_height = x_display_pixel_height (dpyinfo);
+
+ hdc = GetDC (NULL);
+ mm_per_pixel = ((double) GetDeviceCaps (hdc, HORZSIZE)
+ / GetDeviceCaps (hdc, HORZRES));
+ width_mm = pixel_width * mm_per_pixel + 0.5;
+ mm_per_pixel = ((double) GetDeviceCaps (hdc, VERTSIZE)
+ / GetDeviceCaps (hdc, VERTRES));
+ height_mm = pixel_height * mm_per_pixel + 0.5;
+ ReleaseDC (NULL, hdc);
+ attributes = Fcons (Fcons (Qmm_size, list2i (width_mm, height_mm)),
+ attributes);
+
+ /* GetSystemMetrics below may return 0 for Windows 95 or NT 4.0, but
+ we don't care. */
+ geometry = list4i (GetSystemMetrics (SM_XVIRTUALSCREEN),
+ GetSystemMetrics (SM_YVIRTUALSCREEN),
+ pixel_width, pixel_height);
+ if (SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea_rect, 0))
+ workarea = list4i (workarea_rect.left, workarea_rect.top,
+ workarea_rect.right - workarea_rect.left,
+ workarea_rect.bottom - workarea_rect.top);
+ else
+ workarea = geometry;
+ attributes = Fcons (Fcons (Qworkarea, workarea), attributes);
+
+ attributes = Fcons (Fcons (Qgeometry, geometry), attributes);
+
+ return list1 (attributes);
+}
+
+DEFUN ("w32-display-monitor-attributes-list", Fw32_display_monitor_attributes_list,
+ Sw32_display_monitor_attributes_list,
+ 0, 1, 0,
+ doc: /* Return a list of physical monitor attributes on the W32 display DISPLAY.
+
+The optional argument DISPLAY specifies which display to ask about.
+DISPLAY should be either a frame or a display name (a string).
+If omitted or nil, that stands for the selected frame's display.
+
+Internal use only, use `display-monitor-attributes-list' instead. */)
+ (Lisp_Object display)
+{
+ struct w32_display_info *dpyinfo = check_x_display_info (display);
+ Lisp_Object attributes_list;
+
+ block_input ();
+ attributes_list = w32_display_monitor_attributes_list ();
+ if (NILP (attributes_list))
+ attributes_list = w32_display_monitor_attributes_list_fallback (dpyinfo);
+ unblock_input ();
+
+ return attributes_list;
+}
+