<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 18, 2016 at 10:59 PM, Ken Thomases <span dir="ltr"><<a href="mailto:ken@codeweavers.com" target="_blank">ken@codeweavers.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">On Jul 18, 2016, at 1:57 AM, David Lawrie <<a href="mailto:david.dljunk@gmail.com">david.dljunk@gmail.com</a>> wrote:<br>
<br>
> +static CFStringRef copy_device_name(IOHIDDeviceRef device)<br>
> +{<br>
> +    CFTypeRef ref;<br>
> +    CFStringRef name = NULL;<br>
> +<br>
> +    if(device)<br>
<br>
</span>Maybe didn't notice this in the previous version, but put a space between "if" and the parenthesis.<br></blockquote><div><br></div><div>will do</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> +    {<br>
> +        assert(IOHIDDeviceGetTypeID() == CFGetTypeID(device));<br>
> +<br>
> +        ref = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));<br>
> +<br>
> +        if (ref && CFStringGetTypeID() == CFGetTypeID(ref))<br>
> +            name = CFStringCreateCopy(kCFAllocatorDefault,ref);<br>
<br>
</span>Please use a space after the comma in argument lists in function calls.<br></blockquote><div><br></div><div>will do</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> +        else if (ref)<br>
> +            name = CFCopyDescription(ref);<br>
> +        else<br>
> +            name = CFStringCreateCopy(kCFAllocatorDefault,CFSTR("(null)"));<br>
<br>
</span>You don't want to fall back to a string based on the vendor and product ID?<br></blockquote><div><br></div><div>I thought about it, but then<span style="font-size:13px"> </span>I wanted to try to hew as close to the behavior of debugstr_cf as possible since that is the output that the user sees already in trace logs. This label "(null)" is how debugstr handles that case. The alternative of course is changing both functions to try to get the venture/product ID - though I think the concern is if it doesn't have aProductKey it may not have a proper productId or vendorID either ... also there is a function get_os_x_device_name (in dinput only) which doesn't handle this case at all I don't think. What's your recommendation?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Also, the same spacing issue as above.<br></blockquote><div><br></div><div>will do</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> +    }<br>
> +    else<br>
> +        ERR("Invalid Device requested %p\n",device);<br>
<br>
</span>At this point, device is known to be NULL, so the log message can say that rather than the vague "invalid", and doesn't need to include its value.  Or did you mean to be checking something else in the "if"?<br></blockquote><div><br></div><div>Ah right, good point! :)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><br>
> +<br>
> +    return name;<br>
> +}<br>
> +<br>
> static long get_device_location_ID(IOHIDDeviceRef device)<br>
> {<br>
>     return get_device_property_long(device, CFSTR(kIOHIDLocationIDKey));<br>
> @@ -203,7 +227,18 @@ static void copy_set_to_array(const void *value, void *context)<br>
>     CFArrayAppendValue(context, value);<br>
> }<br>
><br>
> -static CFComparisonResult device_location_comparator(const void *val1, const void *val2, void *context)<br>
> +static CFComparisonResult device_name_comparator(IOHIDDeviceRef device1, IOHIDDeviceRef device2)<br>
> +{<br>
> +    CFStringRef name1 = copy_device_name(device1), name2 = copy_device_name(device2);<br>
> +    CFComparisonResult result = CFStringCompare(name1, name2, (kCFCompareForcedOrdering | kCFCompareNumerically));<br>
> +    if(name1)<br>
> +        CFRelease(name1);<br>
> +    if(name2)<br>
> +        CFRelease(name2);<br>
<br>
</span>If name1 or name2 might be NULL as these tests imply, then what does the preceding CFStringCompare() call do?  Probably crashes.  So, either assume/assure that copy_device_name() doesn't return NULL in normal operation or handle the possibility that it might more thoroughly.<br></blockquote><div><br></div><div>Actually I think it doesn't crash, though I should test it to be sure. However, maybe for completeness/robustness I should include if statements dealing with all the possible name-NULL combinations and return - something like:</div><div><br></div><div>if ((!name1 && !name2) || !name1)</div><div>   return lessThan;</div><div>else if (!name2)</div><div>   return greaterThan;</div><div><br></div><div>Then it wouldn't matter what CFStringCompare's behavior was if it crashes ... or I could set name equal to an empty string in copy_device_name which would have the same effect as the above few lines.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span class=""><font color="#888888"><br>
-Ken<br>
<br>
</font></span></blockquote></div><br></div><div class="gmail_extra">--David</div></div>