Skip to contentSkip to conversation

hid.c: Add debug support

PATCH In review Programmingkid 2 participants 4 messages Mar 25, 2016 – Mar 27, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 hid.c: Add debug support

Add debug macros to the code for easier debugging.

---
 hw/input/hid.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/input/hid.c +11 -0
index 329a27b..42ca592 100644
@@ -37,6 +37,13 @@
 #define RELEASED -1
 #define PUSHED -2
 
+/* #define DEBUG_HID_CODE */
+#ifdef DEBUG_HID_CODE
+    #define DEBUG_HID(fmt, ...) printf(fmt, __VA_ARGS__)
+#else
+    #define DEBUG_HID(fmt, ...) (void)0
+#endif
+
 /* Translates a QKeyCode to USB HID value */
 static const uint8_t qcode_to_usb_hid[] = {
     [Q_KEY_CODE_SHIFT] = USB_HID_LEFT_SHIFT,
@@ -331,6 +338,7 @@ static void hid_keyboard_event(DeviceState *dev, QemuConsole *src,
         return;
     }
     keycode = qcode_to_usb_hid[qcode];
+    DEBUG_HID("keycode = 0x%x qcode:%d\n", keycode, qcode);
 
     count = 2;
     if (evt->u.key.data->down == false) { /* if key up event */
@@ -381,6 +389,9 @@ static void hid_keyboard_process_keycode(HIDState *hs)
     slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
     keycode = hs->kbd.keycodes[slot];
 
+    DEBUG_HID("keycode:0x%x status:%s\n", keycode, (status == PUSHED ? "Pushed"
+              : "Released"));
+
     /* handle Control, Option, GUI/Windows/Command, and Shift keys */
     if (keycode >= 0xe0) {
         process_modifier_key(status, keycode, &(hs->kbd.modifiers));
Signature · 1 line
2.7.2
#1 lore raw
Quoted 12 lines · Programmingkid

Add debug macros to the code for easier debugging.

---
 hw/input/hid.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/input/hid.c +0 -0
index 329a27b..42ca592 100644

hw/input/hid.c line 46

Quoted 10 lines
hw/input/hid.c +7 -0
@@ -37,6 +37,13 @@
 #define RELEASED -1
 #define PUSHED -2
 
+/* #define DEBUG_HID_CODE */
+#ifdef DEBUG_HID_CODE
+    #define DEBUG_HID(fmt, ...) printf(fmt, __VA_ARGS__)
+#else
+    #define DEBUG_HID(fmt, ...) (void)0
+#endif
+

This style of debug setup is discouraged these days as its prone to bitrot. It's better to define like this:

#define DEBUG_HID(fmt, ...) \

  if (DEBUG_HID_CODE) {     \
     printf(fmt, __VA_ARGS);\

}

This means you get the benefit of the compiler checking your format strings even if the code gets optimised away when DEBUG_HID_CODE isn't defined.

Quoted 12 lines
hw/input/hid.c +1 -0
 /* Translates a QKeyCode to USB HID value */
 static const uint8_t qcode_to_usb_hid[] = {
     [Q_KEY_CODE_SHIFT] = USB_HID_LEFT_SHIFT,
@@ -331,6 +338,7 @@ static void hid_keyboard_event(DeviceState *dev, QemuConsole *src,
         return;
     }
     keycode = qcode_to_usb_hid[qcode];
+    DEBUG_HID("keycode = 0x%x qcode:%d\n", keycode, qcode);
 
     count = 2;
     if (evt->u.key.data->down == false) { /* if key up event */

hw/input/hid.c static void hid_keyboard_process_keycode(HIDState *hs) · line 397

Quoted 9 lines
hw/input/hid.c +3 -0
@@ -381,6 +389,9 @@ static void hid_keyboard_process_keycode(HIDState *hs)
     slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
     keycode = hs->kbd.keycodes[slot];
 
+    DEBUG_HID("keycode:0x%x status:%s\n", keycode, (status == PUSHED ? "Pushed"
+              : "Released"));
+
     /* handle Control, Option, GUI/Windows/Command, and Shift keys */
     if (keycode >= 0xe0) {
         process_modifier_key(status, keycode, &(hs->kbd.modifiers));

--
Alex Bennée

#2 lore raw
Quoted 29 lines · Alex Bennée
Quoted 12 lines · Programmingkid

Add debug macros to the code for easier debugging.

---
hw/input/hid.c | 11 +++++++++++

1 file changed, 11 insertions(+)
hw/input/hid.c +0 -0
index 329a27b..42ca592 100644

hw/input/hid.c line 46

Quoted 10 lines
hw/input/hid.c +7 -0
@@ -37,6 +37,13 @@
 #define RELEASED -1
 #define PUSHED -2
 
+/* #define DEBUG_HID_CODE */
+#ifdef DEBUG_HID_CODE
+    #define DEBUG_HID(fmt, ...) printf(fmt, __VA_ARGS__)
+#else
+    #define DEBUG_HID(fmt, ...) (void)0
+#endif
+

This style of debug setup is discouraged these days as its prone to bitrot. It's better to define like this:

Quoted 8 lines

#define DEBUG_HID(fmt, ...) \

 if (DEBUG_HID_CODE) {     \
    printf(fmt, __VA_ARGS);\

}

This means you get the benefit of the compiler checking your format strings even if the code gets optimised away when DEBUG_HID_CODE isn't defined.

I don't like if conditions in the debug macro because they do take up cpu time. The (void)0 is just zero. I don't think it would take any cpu time away from the program.

#3 lore raw

It won't. The compiler will dead code away any branches where the test evaluates to a constant.

Quoted 44 lines · Programmingkid
Quoted 29 lines · Alex Bennée
Quoted 12 lines · Programmingkid

Add debug macros to the code for easier debugging.

---
hw/input/hid.c | 11 +++++++++++

1 file changed, 11 insertions(+)
hw/input/hid.c +0 -0
index 329a27b..42ca592 100644

hw/input/hid.c line 46

Quoted 10 lines
hw/input/hid.c +7 -0
@@ -37,6 +37,13 @@
 #define RELEASED -1
 #define PUSHED -2
 
+/* #define DEBUG_HID_CODE */
+#ifdef DEBUG_HID_CODE
+    #define DEBUG_HID(fmt, ...) printf(fmt, __VA_ARGS__)
+#else
+    #define DEBUG_HID(fmt, ...) (void)0
+#endif
+

This style of debug setup is discouraged these days as its prone to bitrot. It's better to define like this:

Quoted 8 lines

#define DEBUG_HID(fmt, ...) \

 if (DEBUG_HID_CODE) {     \
    printf(fmt, __VA_ARGS);\

}

This means you get the benefit of the compiler checking your format strings even if the code gets optimised away when DEBUG_HID_CODE isn't defined.

I don't like if conditions in the debug macro because they do take up cpu time. The (void)0 is just zero. I don't think it would take any cpu time away from the program.