Fixed memory leak.
authorArmin Novak <armin.novak@thincast.com>
Mon, 22 Jan 2018 14:45:27 +0000 (15:45 +0100)
committerArmin Novak <armin.novak@thincast.com>
Mon, 22 Jan 2018 15:17:37 +0000 (16:17 +0100)
client/X11/xf_monitor.c

index 9783b70..0ed9e44 100644 (file)
@@ -57,8 +57,8 @@ int xf_list_monitors(xfContext* xfc)
        Display* display;
        int major, minor;
        int i, nmonitors = 0;
-
        display = XOpenDisplay(NULL);
+
        if (!display)
        {
                WLog_ERR(TAG, "failed to open X display");
@@ -66,49 +66,53 @@ int xf_list_monitors(xfContext* xfc)
        }
 
 #if defined(USABLE_XRANDR)
-       if (XRRQueryExtension(xfc->display, &major, &minor) && (XRRQueryVersion(xfc->display, &major, &minor) == True) &&
-               (major * 100 + minor >= 105))
+
+       if (XRRQueryExtension(xfc->display, &major, &minor) &&
+           (XRRQueryVersion(xfc->display, &major, &minor) == True) &&
+           (major * 100 + minor >= 105))
        {
-               XRRMonitorInfo *monitors = XRRGetMonitors(xfc->display, DefaultRootWindow(xfc->display), 1, &nmonitors);
+               XRRMonitorInfo* monitors = XRRGetMonitors(xfc->display, DefaultRootWindow(xfc->display), 1,
+                                          &nmonitors);
 
                for (i = 0; i < nmonitors; i++)
                {
                        printf("      %s [%d] %hdx%hd\t+%hd+%hd\n",
-                                  monitors[i].primary ? "*" : " ", i,
-                                  monitors[i].width, monitors[i].height,
-                                  monitors[i].x, monitors[i].y);
+                              monitors[i].primary ? "*" : " ", i,
+                              monitors[i].width, monitors[i].height,
+                              monitors[i].x, monitors[i].y);
                }
+
                XRRFreeMonitors(monitors);
-       } else
+       }
+       else
 #endif
-
 #ifdef WITH_XINERAMA
-       if (XineramaQueryExtension(display, &major, &minor))
-       {
-               if (XineramaIsActive(display))
+               if (XineramaQueryExtension(display, &major, &minor))
                {
-                       XineramaScreenInfo* screen = XineramaQueryScreens(display, &nmonitors);
-
-                       for (i = 0; i < nmonitors; i++)
+                       if (XineramaIsActive(display))
                        {
-                               printf("      %s [%d] %hdx%hd\t+%hd+%hd\n",
-                                      (i == 0) ? "*" : " ", i,
-                                      screen[i].width, screen[i].height,
-                                      screen[i].x_org, screen[i].y_org);
-                       }
+                               XineramaScreenInfo* screen = XineramaQueryScreens(display, &nmonitors);
 
-                       XFree(screen);
-               }
-       } else
+                               for (i = 0; i < nmonitors; i++)
+                               {
+                                       printf("      %s [%d] %hdx%hd\t+%hd+%hd\n",
+                                              (i == 0) ? "*" : " ", i,
+                                              screen[i].width, screen[i].height,
+                                              screen[i].x_org, screen[i].y_org);
+                               }
 
+                               XFree(screen);
+                       }
+               }
+               else
 #else
        {
                Screen* screen = ScreenOfDisplay(display, DefaultScreen(display));
                printf("      * [0] %dx%d\t+0+0\n", WidthOfScreen(screen), HeightOfScreen(screen));
        }
-#endif
 
-       XCloseDisplay(display);
+#endif
+                       XCloseDisplay(display);
        return 0;
 }
 
@@ -145,10 +149,9 @@ BOOL xf_detect_monitors(xfContext* xfc, UINT32* pMaxWidth, UINT32* pMaxHeight)
        int major, minor;
 #endif
 #if defined(USABLE_XRANDR)
-       XRRMonitorInfo *rrmonitors = NULL;
+       XRRMonitorInforrmonitors = NULL;
        BOOL useXRandr = FALSE;
 #endif
-
        vscreen = &xfc->vscreen;
        *pMaxWidth = settings->DesktopWidth;
        *pMaxHeight = settings->DesktopHeight;
@@ -160,10 +163,13 @@ BOOL xf_detect_monitors(xfContext* xfc, UINT32* pMaxWidth, UINT32* pMaxHeight)
                mouse_x = mouse_y = 0;
 
 #if defined(USABLE_XRANDR)
-       if (XRRQueryExtension(xfc->display, &major, &minor) && (XRRQueryVersion(xfc->display, &major, &minor) == True) &&
-                       (major * 100 + minor >= 105))
+
+       if (XRRQueryExtension(xfc->display, &major, &minor) &&
+           (XRRQueryVersion(xfc->display, &major, &minor) == True) &&
+           (major * 100 + minor >= 105))
        {
-               XRRMonitorInfo *rrmonitors = XRRGetMonitors(xfc->display, DefaultRootWindow(xfc->display), 1, &vscreen->nmonitors);
+               XRRMonitorInfo* rrmonitors = XRRGetMonitors(xfc->display, DefaultRootWindow(xfc->display), 1,
+                                            &vscreen->nmonitors);
 
                if (vscreen->nmonitors > 16)
                        vscreen->nmonitors = 0;
@@ -179,40 +185,42 @@ BOOL xf_detect_monitors(xfContext* xfc, UINT32* pMaxWidth, UINT32* pMaxHeight)
                                vscreen->monitors[i].primary = rrmonitors[i].primary > 0;
                        }
                }
+
+               XRRFreeMonitors(rrmonitors);
                useXRandr = TRUE;
-       } else
+       }
+       else
 #endif
-
 #ifdef WITH_XINERAMA
-       if (XineramaQueryExtension(xfc->display, &major, &minor) && XineramaIsActive(xfc->display))
-       {
-               XineramaScreenInfo* screenInfo = XineramaQueryScreens(xfc->display, &vscreen->nmonitors);
+               if (XineramaQueryExtension(xfc->display, &major, &minor) && XineramaIsActive(xfc->display))
+               {
+                       XineramaScreenInfo* screenInfo = XineramaQueryScreens(xfc->display, &vscreen->nmonitors);
 
-               if (vscreen->nmonitors > 16)
-                       vscreen->nmonitors = 0;
+                       if (vscreen->nmonitors > 16)
+                               vscreen->nmonitors = 0;
 
-               if (vscreen->nmonitors)
-               {
-                       for (i = 0; i < vscreen->nmonitors; i++)
+                       if (vscreen->nmonitors)
                        {
-                               vscreen->monitors[i].area.left = screenInfo[i].x_org;
-                               vscreen->monitors[i].area.top = screenInfo[i].y_org;
-                               vscreen->monitors[i].area.right = screenInfo[i].x_org + screenInfo[i].width - 1;
-                               vscreen->monitors[i].area.bottom = screenInfo[i].y_org + screenInfo[i].height - 1;
-
-                               /* Determine which monitor that the mouse cursor is on */
-                               if ((mouse_x >= vscreen->monitors[i].area.left) &&
-                                       (mouse_x <= vscreen->monitors[i].area.right) &&
-                                       (mouse_y >= vscreen->monitors[i].area.top) &&
-                                       (mouse_y <= vscreen->monitors[i].area.bottom))
-                                       current_monitor = i;
+                               for (i = 0; i < vscreen->nmonitors; i++)
+                               {
+                                       vscreen->monitors[i].area.left = screenInfo[i].x_org;
+                                       vscreen->monitors[i].area.top = screenInfo[i].y_org;
+                                       vscreen->monitors[i].area.right = screenInfo[i].x_org + screenInfo[i].width - 1;
+                                       vscreen->monitors[i].area.bottom = screenInfo[i].y_org + screenInfo[i].height - 1;
+
+                                       /* Determine which monitor that the mouse cursor is on */
+                                       if ((mouse_x >= vscreen->monitors[i].area.left) &&
+                                           (mouse_x <= vscreen->monitors[i].area.right) &&
+                                           (mouse_y >= vscreen->monitors[i].area.top) &&
+                                           (mouse_y <= vscreen->monitors[i].area.bottom))
+                                               current_monitor = i;
+                               }
                        }
+
+                       XFree(screenInfo);
                }
 
-               XFree(screenInfo);
-       }
 #endif
-
        xfc->fullscreenMonitors.top = xfc->fullscreenMonitors.bottom =
                                          xfc->fullscreenMonitors.left = xfc->fullscreenMonitors.right = 0;
 
@@ -297,30 +305,30 @@ BOOL xf_detect_monitors(xfContext* xfc, UINT32* pMaxWidth, UINT32* pMaxHeight)
        /* Create array of all active monitors by taking into account monitors requested on the command-line */
        for (i = 0; i < vscreen->nmonitors; i++)
        {
-               MONITOR_ATTRIBUTES *attrs;
+               MONITOR_ATTRIBUTES* attrs;
+
                if (!xf_is_monitor_id_active(xfc, i))
                        continue;
 
                settings->MonitorDefArray[nmonitors].x = vscreen->monitors[i].area.left;
                settings->MonitorDefArray[nmonitors].y = vscreen->monitors[i].area.top;
                settings->MonitorDefArray[nmonitors].width =
-                       MIN(vscreen->monitors[i].area.right - vscreen->monitors[i].area.left + 1, *pMaxWidth);
+                   MIN(vscreen->monitors[i].area.right - vscreen->monitors[i].area.left + 1, *pMaxWidth);
                settings->MonitorDefArray[nmonitors].height =
-                       MIN(vscreen->monitors[i].area.bottom - vscreen->monitors[i].area.top + 1, *pMaxHeight);
+                   MIN(vscreen->monitors[i].area.bottom - vscreen->monitors[i].area.top + 1, *pMaxHeight);
                settings->MonitorDefArray[nmonitors].orig_screen = i;
-
 #ifdef USABLE_XRANDR
+
                if (useXRandr && rrmonitors)
                {
                        Rotation rot, ret;
-
                        attrs = &settings->MonitorDefArray[nmonitors].attributes;
                        attrs->physicalWidth = rrmonitors[i].mwidth;
                        attrs->physicalHeight = rrmonitors[i].mheight;
-
                        ret = XRRRotations(xfc->display, i, &rot);
                        attrs->orientation = rot;
                }
+
 #endif
 
                if (i == settings->MonitorIds[0])
@@ -477,8 +485,10 @@ BOOL xf_detect_monitors(xfContext* xfc, UINT32* pMaxWidth, UINT32* pMaxHeight)
 
 out:
 #ifdef USABLE_XRANDR
+
        if (rrmonitors)
                XRRFreeMonitors(rrmonitors);
+
 #endif
        return TRUE;
 }