a5bd9f6
From 031f4b44a6488a0bd7fe79657480484ac981ce9e Mon Sep 17 00:00:00 2001
a5bd9f6
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
a5bd9f6
Date: Fri, 1 Mar 2013 11:11:36 +0100
a5bd9f6
Subject: [PATCH 174/364] 	Remove nested functions from videoinfo
a5bd9f6
 iterators.
a5bd9f6
a5bd9f6
---
a5bd9f6
 ChangeLog                      |  4 ++++
a5bd9f6
 grub-core/commands/videoinfo.c | 34 ++++++++++++++++++++--------------
a5bd9f6
 grub-core/video/efi_gop.c      | 23 ++++++++++++-----------
a5bd9f6
 grub-core/video/i386/pc/vbe.c  |  4 ++--
a5bd9f6
 include/grub/video.h           |  2 +-
a5bd9f6
 5 files changed, 39 insertions(+), 28 deletions(-)
a5bd9f6
a5bd9f6
diff --git a/ChangeLog b/ChangeLog
a5bd9f6
index 3ca1fed..7f5bcfa 100644
a5bd9f6
--- a/ChangeLog
a5bd9f6
+++ b/ChangeLog
a5bd9f6
@@ -1,5 +1,9 @@
a5bd9f6
 2013-03-01  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
 
a5bd9f6
+	Remove nested functions from videoinfo iterators.
a5bd9f6
+
a5bd9f6
+2013-03-01  Vladimir Serbinenko  <phcoder@gmail.com>
a5bd9f6
+
a5bd9f6
 	* grub-core/loader/i386/pc/linux.c (grub_cmd_linux): Fix compilation
a5bd9f6
 	for 64-bit platforms.
a5bd9f6
 
a5bd9f6
diff --git a/grub-core/commands/videoinfo.c b/grub-core/commands/videoinfo.c
a5bd9f6
index 4a11576..7a75c9d 100644
a5bd9f6
--- a/grub-core/commands/videoinfo.c
a5bd9f6
+++ b/grub-core/commands/videoinfo.c
a5bd9f6
@@ -27,23 +27,28 @@
a5bd9f6
 
a5bd9f6
 GRUB_MOD_LICENSE ("GPLv3+");
a5bd9f6
 
a5bd9f6
-static unsigned height, width, depth; 
a5bd9f6
-static struct grub_video_mode_info *current_mode;
a5bd9f6
+struct hook_ctx
a5bd9f6
+{
a5bd9f6
+  unsigned height, width, depth; 
a5bd9f6
+  struct grub_video_mode_info *current_mode;
a5bd9f6
+};
a5bd9f6
 
a5bd9f6
 static int
a5bd9f6
-hook (const struct grub_video_mode_info *info)
a5bd9f6
+hook (const struct grub_video_mode_info *info, void *hook_arg)
a5bd9f6
 {
a5bd9f6
-  if (height && width && (info->width != width || info->height != height))
a5bd9f6
+  struct hook_ctx *ctx = hook_arg;
a5bd9f6
+
a5bd9f6
+  if (ctx->height && ctx->width && (info->width != ctx->width || info->height != ctx->height))
a5bd9f6
     return 0;
a5bd9f6
 
a5bd9f6
-  if (depth && info->bpp != depth)
a5bd9f6
+  if (ctx->depth && info->bpp != ctx->depth)
a5bd9f6
     return 0;
a5bd9f6
 
a5bd9f6
   if (info->mode_number == GRUB_VIDEO_MODE_NUMBER_INVALID)
a5bd9f6
     grub_printf ("        ");
a5bd9f6
   else
a5bd9f6
     {
a5bd9f6
-      if (current_mode && info->mode_number == current_mode->mode_number)
a5bd9f6
+      if (ctx->current_mode && info->mode_number == ctx->current_mode->mode_number)
a5bd9f6
 	grub_printf ("*");
a5bd9f6
       else
a5bd9f6
 	grub_printf (" ");
a5bd9f6
@@ -126,13 +131,14 @@ grub_cmd_videoinfo (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 {
a5bd9f6
   grub_video_adapter_t adapter;
a5bd9f6
   grub_video_driver_id_t id;
a5bd9f6
+  struct hook_ctx ctx;
a5bd9f6
 
a5bd9f6
-  height = width = depth = 0;
a5bd9f6
+  ctx.height = ctx.width = ctx.depth = 0;
a5bd9f6
   if (argc)
a5bd9f6
     {
a5bd9f6
       char *ptr;
a5bd9f6
       ptr = args[0];
a5bd9f6
-      width = grub_strtoul (ptr, &ptr, 0);
a5bd9f6
+      ctx.width = grub_strtoul (ptr, &ptr, 0);
a5bd9f6
       if (grub_errno)
a5bd9f6
 	return grub_errno;
a5bd9f6
       if (*ptr != 'x')
a5bd9f6
@@ -140,13 +146,13 @@ grub_cmd_videoinfo (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 			   N_("invalid video mode specification `%s'"),
a5bd9f6
 			   args[0]);
a5bd9f6
       ptr++;
a5bd9f6
-      height = grub_strtoul (ptr, &ptr, 0);
a5bd9f6
+      ctx.height = grub_strtoul (ptr, &ptr, 0);
a5bd9f6
       if (grub_errno)
a5bd9f6
 	return grub_errno;
a5bd9f6
       if (*ptr == 'x')
a5bd9f6
 	{
a5bd9f6
 	  ptr++;
a5bd9f6
-	  depth = grub_strtoul (ptr, &ptr, 0);
a5bd9f6
+	  ctx.depth = grub_strtoul (ptr, &ptr, 0);
a5bd9f6
 	  if (grub_errno)
a5bd9f6
 	    return grub_errno;
a5bd9f6
 	}
a5bd9f6
@@ -175,12 +181,12 @@ grub_cmd_videoinfo (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
 	continue;
a5bd9f6
       }
a5bd9f6
 
a5bd9f6
-    current_mode = NULL;
a5bd9f6
+    ctx.current_mode = NULL;
a5bd9f6
 
a5bd9f6
     if (adapter->id == id)
a5bd9f6
       {
a5bd9f6
 	if (grub_video_get_info (&info) == GRUB_ERR_NONE)
a5bd9f6
-	  current_mode = &info;
a5bd9f6
+	  ctx.current_mode = &info;
a5bd9f6
 	else
a5bd9f6
 	  /* Don't worry about errors.  */
a5bd9f6
 	  grub_errno = GRUB_ERR_NONE;
a5bd9f6
@@ -198,14 +204,14 @@ grub_cmd_videoinfo (grub_command_t cmd __attribute__ ((unused)),
a5bd9f6
     if (adapter->print_adapter_specific_info)
a5bd9f6
       adapter->print_adapter_specific_info ();
a5bd9f6
 
a5bd9f6
-    adapter->iterate (hook);
a5bd9f6
+    adapter->iterate (hook, &ctx;;
a5bd9f6
 
a5bd9f6
     if (adapter->get_edid && adapter->get_edid (&edid_info) == GRUB_ERR_NONE)
a5bd9f6
       print_edid (&edid_info);
a5bd9f6
     else
a5bd9f6
       grub_errno = GRUB_ERR_NONE;
a5bd9f6
 
a5bd9f6
-    current_mode = NULL;
a5bd9f6
+    ctx.current_mode = NULL;
a5bd9f6
 
a5bd9f6
     if (adapter->id != id)
a5bd9f6
       {
a5bd9f6
diff --git a/grub-core/video/efi_gop.c b/grub-core/video/efi_gop.c
a5bd9f6
index 37a0015..f73a278 100644
a5bd9f6
--- a/grub-core/video/efi_gop.c
a5bd9f6
+++ b/grub-core/video/efi_gop.c
a5bd9f6
@@ -42,7 +42,7 @@ static int restore_needed;
a5bd9f6
 static grub_efi_handle_t gop_handle;
a5bd9f6
 
a5bd9f6
 static int
a5bd9f6
-grub_video_gop_iterate (int (*hook) (const struct grub_video_mode_info *info));
a5bd9f6
+grub_video_gop_iterate (int (*hook) (const struct grub_video_mode_info *info, void *hook_arg), void *hook_arg);
a5bd9f6
 
a5bd9f6
 static struct
a5bd9f6
 {
a5bd9f6
@@ -52,6 +52,14 @@ static struct
a5bd9f6
   grub_uint8_t *offscreen;
a5bd9f6
 } framebuffer;
a5bd9f6
 
a5bd9f6
+static int
a5bd9f6
+check_protocol_hook (const struct grub_video_mode_info *info __attribute__ ((unused)), void *hook_arg)
a5bd9f6
+{
a5bd9f6
+  int *have_usable_mode = hook_arg;
a5bd9f6
+  *have_usable_mode = 1;
a5bd9f6
+  return 1;
a5bd9f6
+}
a5bd9f6
+
a5bd9f6
 
a5bd9f6
 static int
a5bd9f6
 check_protocol (void)
a5bd9f6
@@ -60,13 +68,6 @@ check_protocol (void)
a5bd9f6
   grub_efi_uintn_t num_handles, i;
a5bd9f6
   int have_usable_mode = 0;
a5bd9f6
 
a5bd9f6
-  auto int hook (const struct grub_video_mode_info *info);
a5bd9f6
-  int hook (const struct grub_video_mode_info *info __attribute__ ((unused)))
a5bd9f6
-  {
a5bd9f6
-    have_usable_mode = 1;
a5bd9f6
-    return 1;
a5bd9f6
-  }
a5bd9f6
-
a5bd9f6
   handles = grub_efi_locate_handle (GRUB_EFI_BY_PROTOCOL,
a5bd9f6
 				    &graphics_output_guid, NULL, &num_handles);
a5bd9f6
   if (!handles || num_handles == 0)
a5bd9f6
@@ -77,7 +78,7 @@ check_protocol (void)
a5bd9f6
       gop_handle = handles[i];
a5bd9f6
       gop = grub_efi_open_protocol (gop_handle, &graphics_output_guid,
a5bd9f6
 				    GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
a5bd9f6
-      grub_video_gop_iterate (hook);
a5bd9f6
+      grub_video_gop_iterate (check_protocol_hook, &have_usable_mode);
a5bd9f6
       if (have_usable_mode)
a5bd9f6
 	{
a5bd9f6
 	  grub_free (handles);
a5bd9f6
@@ -256,7 +257,7 @@ grub_video_gop_fill_mode_info (unsigned mode,
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 static int
a5bd9f6
-grub_video_gop_iterate (int (*hook) (const struct grub_video_mode_info *info))
a5bd9f6
+grub_video_gop_iterate (int (*hook) (const struct grub_video_mode_info *info, void *hook_arg), void *hook_arg)
a5bd9f6
 {
a5bd9f6
   unsigned mode;
a5bd9f6
 
a5bd9f6
@@ -282,7 +283,7 @@ grub_video_gop_iterate (int (*hook) (const struct grub_video_mode_info *info))
a5bd9f6
 	  grub_errno = GRUB_ERR_NONE;
a5bd9f6
 	  continue;
a5bd9f6
 	}
a5bd9f6
-      if (hook (&mode_info))
a5bd9f6
+      if (hook (&mode_info, hook_arg))
a5bd9f6
 	return 1;
a5bd9f6
     }
a5bd9f6
   return 0;
a5bd9f6
diff --git a/grub-core/video/i386/pc/vbe.c b/grub-core/video/i386/pc/vbe.c
a5bd9f6
index 81e5a8e..e8a8c7a 100644
a5bd9f6
--- a/grub-core/video/i386/pc/vbe.c
a5bd9f6
+++ b/grub-core/video/i386/pc/vbe.c
a5bd9f6
@@ -952,7 +952,7 @@ vbe2videoinfo (grub_uint32_t mode,
a5bd9f6
 }
a5bd9f6
 
a5bd9f6
 static int
a5bd9f6
-grub_video_vbe_iterate (int (*hook) (const struct grub_video_mode_info *info))
a5bd9f6
+grub_video_vbe_iterate (int (*hook) (const struct grub_video_mode_info *info, void *hook_arg), void *hook_arg)
a5bd9f6
 {
a5bd9f6
   grub_uint16_t *p;
a5bd9f6
   struct grub_vbe_mode_info_block vbe_mode_info;
a5bd9f6
@@ -969,7 +969,7 @@ grub_video_vbe_iterate (int (*hook) (const struct grub_video_mode_info *info))
a5bd9f6
         }
a5bd9f6
 
a5bd9f6
       vbe2videoinfo (*p, &vbe_mode_info, &mode_info);
a5bd9f6
-      if (hook (&mode_info))
a5bd9f6
+      if (hook (&mode_info, hook_arg))
a5bd9f6
 	return 1;
a5bd9f6
     }
a5bd9f6
   return 0;
a5bd9f6
diff --git a/include/grub/video.h b/include/grub/video.h
a5bd9f6
index 08f7300..9fe4783 100644
a5bd9f6
--- a/include/grub/video.h
a5bd9f6
+++ b/include/grub/video.h
a5bd9f6
@@ -372,7 +372,7 @@ struct grub_video_adapter
a5bd9f6
 
a5bd9f6
   grub_err_t (*get_active_render_target) (struct grub_video_render_target **target);
a5bd9f6
 
a5bd9f6
-  int (*iterate) (int (*hook) (const struct grub_video_mode_info *info));
a5bd9f6
+  int (*iterate) (int (*hook) (const struct grub_video_mode_info *info, void *hook_arg), void *hook_arg);
a5bd9f6
 
a5bd9f6
   grub_err_t (*get_edid) (struct grub_video_edid_info *edid_info);
a5bd9f6
 
a5bd9f6
-- 
a5bd9f6
1.8.1.4
a5bd9f6