Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/videomode - little KNF



details:   https://anonhg.NetBSD.org/src/rev/4387465a460c
branches:  trunk
changeset: 763985:4387465a460c
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Apr 09 20:53:39 2011 +0000

description:
- little KNF
- use struct copy instead of memcpy
- snprintf("%s") -> strlcpy
- fix NUL termination

diffstat:

 sys/dev/videomode/edid.c     |  138 ++++++++++++++++++------------------------
 sys/dev/videomode/pickmode.c |   27 +++----
 2 files changed, 70 insertions(+), 95 deletions(-)

diffs (truncated from 346 to 300 lines):

diff -r e6ddfad4caa6 -r 4387465a460c sys/dev/videomode/edid.c
--- a/sys/dev/videomode/edid.c  Sat Apr 09 20:42:12 2011 +0000
+++ b/sys/dev/videomode/edid.c  Sat Apr 09 20:53:39 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: edid.c,v 1.9 2011/04/09 18:18:28 jdc Exp $ */
+/* $NetBSD: edid.c,v 1.10 2011/04/09 20:53:39 christos Exp $ */
 
 /*-
  * Copyright (c) 2006 Itronix Inc.
@@ -32,7 +32,7 @@
  */ 
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: edid.c,v 1.9 2011/04/09 18:18:28 jdc Exp $");
+__KERNEL_RCSID(0, "$NetBSD: edid.c,v 1.10 2011/04/09 20:53:39 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -91,7 +91,7 @@
 
        for (n = 0; n < edid_nvendors; n++)
                if (memcmp(edid_vendors[n].vendor, vendor, 3) == 0)
-                       return (edid_vendors[n].name);
+                       return edid_vendors[n].name;
 #endif
        return NULL;
 }
@@ -103,9 +103,9 @@
        int     n;
 
        for (n = 0; n < edid_nproducts; n++)
-               if ((edid_products[n].product == product) &&
-                   (memcmp(edid_products[n].vendor, vendor, 3) == 0))
-                       return (edid_products[n].name);
+               if (edid_products[n].product == product &&
+                   memcmp(edid_products[n].vendor, vendor, 3) == 0)
+                       return (edid_products[n].name;
 #endif /* EDIDVERBOSE */
        return NULL;
 
@@ -116,11 +116,11 @@
 {
        for (;;) {
                switch (*ptr) {
-               case 0:
+               case '\0':
                        return;
                case '\r':
                case '\n':
-                       *ptr = 0;
+                       *ptr = '\0';
                        return;
                }
                ptr++;
@@ -264,8 +264,7 @@
                    edid->edid_modes[i].hdisplay,
                    edid->edid_modes[i].vdisplay,
                    DIVIDE(DIVIDE(edid->edid_modes[i].dot_clock * 1000,
-                              edid->edid_modes[i].htotal),
-                       edid->edid_modes[i].vtotal));
+                   edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal));
                printf(" (%d %d %d %d %d %d %d",
                    edid->edid_modes[i].dot_clock,
                    edid->edid_modes[i].hsync_start,
@@ -285,8 +284,8 @@
                    edid->edid_preferred_mode->hdisplay,
                    edid->edid_preferred_mode->vdisplay,
                    DIVIDE(DIVIDE(edid->edid_preferred_mode->dot_clock * 1000,
-                              edid->edid_preferred_mode->htotal),
-                       edid->edid_preferred_mode->vtotal));
+                   edid->edid_preferred_mode->htotal),
+                   edid->edid_preferred_mode->vtotal));
 }
 
 static const struct videomode *
@@ -312,9 +311,8 @@
                    mode->vdisplay == edid->edid_modes[i].vdisplay &&
                    refresh == DIVIDE(DIVIDE(
                    edid->edid_modes[i].dot_clock * 1000,
-                   edid->edid_modes[i].htotal),
-                   edid->edid_modes[i].vtotal)) {
-                       return &(edid->edid_modes[i]);
+                   edid->edid_modes[i].htotal), edid->edid_modes[i].vtotal)) {
+                       return &edid->edid_modes[i];
                }
        }
        return NULL;
@@ -351,13 +349,11 @@
        f = EDID_STD_TIMING_VFREQ(data);
 
        /* first try to lookup the mode as a DMT timing */
-       snprintf(name, sizeof (name), "%dx%dx%d", x, y, f);
+       snprintf(name, sizeof(name), "%dx%dx%d", x, y, f);
        if ((lookup = edid_mode_lookup_list(name)) != NULL) {
                *vmp = *lookup;
-       }
-
-       /* failing that, calculate it using gtf */
-       else {
+       } else {
+               /* failing that, calculate it using gtf */
                /*
                 * Hmm. I'm not using alternate GTF timings, which
                 * could, in theory, be present.
@@ -429,68 +425,53 @@
        struct videomode        mode, *exist_mode;
 
        if (EDID_BLOCK_IS_DET_TIMING(data)) {
-               if (edid_det_timing(data, &mode)) {
-                       /* Does this mode already exist? */
-                       exist_mode = edid_search_mode(edid, &mode);
-                       if (exist_mode != NULL) {
-                               memcpy(exist_mode, &mode,
-                                   sizeof(struct videomode));
-                               if (edid->edid_preferred_mode == NULL) {
-                                       edid->edid_preferred_mode =
-                                           exist_mode;
-                               }
-                       } else {
-                               edid->edid_modes[edid->edid_nmodes] = mode;
-                               if (edid->edid_preferred_mode == NULL) {
-                                       edid->edid_preferred_mode =
-                                           &edid->edid_modes[edid->edid_nmodes];
-                               }
-                               edid->edid_nmodes++;    
-                       }
+               if (!edid_det_timing(data, &mode)) {
+                       return;
+               /* Does this mode already exist? */
+               exist_mode = edid_search_mode(edid, &mode);
+               if (exist_mode != NULL) {
+                       *exist_mode = mode;
+                       if (edid->edid_preferred_mode == NULL)
+                               edid->edid_preferred_mode = exist_mode;
+               } else {
+                       edid->edid_modes[edid->edid_nmodes] = mode;
+                       if (edid->edid_preferred_mode == NULL)
+                               edid->edid_preferred_mode =
+                                   &edid->edid_modes[edid->edid_nmodes];
+                       edid->edid_nmodes++;    
                }
                return;
        }
 
        switch (EDID_BLOCK_TYPE(data)) {
        case EDID_DESC_BLOCK_TYPE_SERIAL:
-               memcpy(edid->edid_serial,
-                   data + EDID_DESC_ASCII_DATA_OFFSET,
+               memcpy(edid->edid_serial, data + EDID_DESC_ASCII_DATA_OFFSET,
                    EDID_DESC_ASCII_DATA_LEN);
-               edid->edid_serial[sizeof (edid->edid_serial) - 1] = 0;
+               edid->edid_serial[sizeof(edid->edid_serial) - 1] = 0;
                break;
 
        case EDID_DESC_BLOCK_TYPE_ASCII:
-               memcpy(edid->edid_comment,
-                   data + EDID_DESC_ASCII_DATA_OFFSET,
+               memcpy(edid->edid_comment, data + EDID_DESC_ASCII_DATA_OFFSET,
                    EDID_DESC_ASCII_DATA_LEN);
-               edid->edid_comment[sizeof (edid->edid_comment) - 1] = 0;
+               edid->edid_comment[sizeof(edid->edid_comment) - 1] = 0;
                break;
 
        case EDID_DESC_BLOCK_TYPE_RANGE:
                edid->edid_have_range = 1;
-               edid->edid_range.er_min_vfreq = 
-                   EDID_DESC_RANGE_MIN_VFREQ(data);
-               edid->edid_range.er_max_vfreq = 
-                   EDID_DESC_RANGE_MAX_VFREQ(data);
-               edid->edid_range.er_min_hfreq = 
-                   EDID_DESC_RANGE_MIN_HFREQ(data);
-               edid->edid_range.er_max_hfreq = 
-                   EDID_DESC_RANGE_MAX_HFREQ(data);
-               edid->edid_range.er_max_clock =
-                   EDID_DESC_RANGE_MAX_CLOCK(data);
-               if (EDID_DESC_RANGE_HAVE_GTF2(data)) {
-                       edid->edid_range.er_have_gtf2 = 1;
-                       edid->edid_range.er_gtf2_hfreq =
-                           EDID_DESC_RANGE_GTF2_HFREQ(data);
-                       edid->edid_range.er_gtf2_c =
-                           EDID_DESC_RANGE_GTF2_C(data);
-                       edid->edid_range.er_gtf2_m =
-                           EDID_DESC_RANGE_GTF2_M(data);
-                       edid->edid_range.er_gtf2_j =
-                           EDID_DESC_RANGE_GTF2_J(data);
-                       edid->edid_range.er_gtf2_k =
-                           EDID_DESC_RANGE_GTF2_K(data);
-               }
+               edid->edid_range.er_min_vfreq = EDID_DESC_RANGE_MIN_VFREQ(data);
+               edid->edid_range.er_max_vfreq = EDID_DESC_RANGE_MAX_VFREQ(data);
+               edid->edid_range.er_min_hfreq = EDID_DESC_RANGE_MIN_HFREQ(data);
+               edid->edid_range.er_max_hfreq = EDID_DESC_RANGE_MAX_HFREQ(data);
+               edid->edid_range.er_max_clock = EDID_DESC_RANGE_MAX_CLOCK(data);
+               if (!EDID_DESC_RANGE_HAVE_GTF2(data))
+                       break;
+               edid->edid_range.er_have_gtf2 = 1;
+               edid->edid_range.er_gtf2_hfreq =
+                   EDID_DESC_RANGE_GTF2_HFREQ(data);
+               edid->edid_range.er_gtf2_c = EDID_DESC_RANGE_GTF2_C(data);
+               edid->edid_range.er_gtf2_m = EDID_DESC_RANGE_GTF2_M(data);
+               edid->edid_range.er_gtf2_j = EDID_DESC_RANGE_GTF2_J(data);
+               edid->edid_range.er_gtf2_k = EDID_DESC_RANGE_GTF2_K(data);
                break;
 
        case EDID_DESC_BLOCK_TYPE_NAME:
@@ -549,20 +530,20 @@
            (data[EDID_OFFSET_PRODUCT_ID + 1] << 8);
 
        name = edid_findvendor(edid->edid_vendor);
-       if (name != NULL) {
-               snprintf(edid->edid_vendorname,
-                   sizeof (edid->edid_vendorname), "%s", name);
-       }
-       edid->edid_vendorname[sizeof (edid->edid_vendorname) - 1] = 0;
+       if (name != NULL)
+               strlcpy(edid->edid_vendorname, name,
+                   sizeof(edid->edid_vendorname));
+       else
+               edid->edid_vendorname[0] = '\0';
 
        name = edid_findproduct(edid->edid_vendor, edid->edid_product);
-       if (name != NULL) {
-               snprintf(edid->edid_productname,
-                   sizeof (edid->edid_productname), "%s", name);
-       }
-       edid->edid_productname[sizeof (edid->edid_productname) - 1] = 0;
+       if (name != NULL)
+               strlcpy(edid->edid_productname, name,
+                   sizeof(edid->edid_productname));
+       else
+           edid->edid_productname[0] = '\0';
 
-       snprintf(edid->edid_serial, sizeof (edid->edid_serial), "%08x",
+       snprintf(edid->edid_serial, sizeof(edid->edid_serial), "%08x",
            EDID_SERIAL_NUMBER(data));
 
        edid->edid_week = EDID_WEEK(data);
@@ -651,7 +632,6 @@
        mhz = (max_dotclock + 999) / 1000;
 
        if (edid->edid_have_range) {
-
                if (mhz > edid->edid_range.er_max_clock)
                        edid->edid_range.er_max_clock = mhz;
        } else
diff -r e6ddfad4caa6 -r 4387465a460c sys/dev/videomode/pickmode.c
--- a/sys/dev/videomode/pickmode.c      Sat Apr 09 20:42:12 2011 +0000
+++ b/sys/dev/videomode/pickmode.c      Sat Apr 09 20:53:39 2011 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: pickmode.c,v 1.3 2011/04/09 18:22:31 jdc Exp $ */
+/* $NetBSD: pickmode.c,v 1.4 2011/04/09 20:53:39 christos Exp $ */
 
 /*-
  * Copyright (c) 2006 The NetBSD Foundation
@@ -29,7 +29,7 @@
  */ 
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pickmode.c,v 1.3 2011/04/09 18:22:31 jdc Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pickmode.c,v 1.4 2011/04/09 20:53:39 christos Exp $");
 
 #include <sys/param.h>
 #include <dev/videomode/videomode.h>
@@ -50,19 +50,17 @@
        DPRINTF("%s: looking for %d x %d at up to %d kHz\n", __func__, width,
            height, dotclock);
        for (i = 0; i < videomode_count; i++) {
-
                this = &videomode_list[i];
                if ((this->hdisplay != width) || (this->vdisplay != height) ||
                    (this->dot_clock > dotclock))
                        continue;
                if (best != NULL) {
-
                        if (this->dot_clock > best->dot_clock)
                                best = this;
                } else
                        best = this;
        }
-       if (best!= NULL)
+       if (best != NULL)
                DPRINTF("found %s\n", best->name);
 
        return best;
@@ -85,9 +83,7 @@
                        continue;
                DPRINTF("%s in %d hz, diff %d\n", this->name, mref, diff);
                if (best != NULL) {
-
                        if (diff < closest) {
-
                                best = this;
                                closest = diff;
                        }
@@ -96,7 +92,7 @@
                        closest = diff;
                }
        }
-       if (best!= NULL)
+       if (best != NULL)



Home | Main Index | Thread Index | Old Index