qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] ui/cocoa.m: add Speed menu
@ 2017-06-09 15:02 Programmingkid
  2017-06-13 18:03 ` Peter Maydell
  0 siblings, 1 reply; 3+ messages in thread
From: Programmingkid @ 2017-06-09 15:02 UTC (permalink / raw)
  To: Gerd Hoffmann, qemu-devel@nongnu.org qemu-devel

Programs running inside of QEMU can sometimes use more CPU time than is really
needed. To solve this problem, we just need to throttle the virtual CPU. This
feature will stop laptops from burning up. 

This patch adds a menu called Speed that has menu items from 100% to 1% that
represent the speed options. 100% is full speed and 1% is slowest.

Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
---
v2 changes:
Fixed missing space with 'if' and 'for' structures.
Fixed tab damage.
Use loop to create menu items.
Changed name of menu items to use percentages.
Use tags to determine selected menu item.

 ui/cocoa.m | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 26d4a1c..772629e 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -35,6 +35,7 @@
 #include "sysemu/blockdev.h"
 #include "qemu-version.h"
 #include <Carbon/Carbon.h>
+#include "qom/cpu.h"
 
 #ifndef MAC_OS_X_VERSION_10_5
 #define MAC_OS_X_VERSION_10_5 1050
@@ -828,6 +829,7 @@ QemuCocoaView *cocoaView;
 - (void)openDocumentation:(NSString *)filename;
 - (IBAction) do_about_menu_item: (id) sender;
 - (void)make_about_window;
+- (void)adjustSpeed:(id)sender;
 @end
 
 @implementation QemuCocoaAppController
@@ -1234,6 +1236,35 @@ QemuCocoaView *cocoaView;
     [superView addSubview: copyright_label];
 }
 
+/* Used by the Speed menu items */
+- (void)adjustSpeed:(id)sender
+{
+    int speed, menu_number, count, item;
+    NSMenu *menu;
+    NSArray *itemArray;
+
+    // uncheck all the menu items in the Speed menu
+    menu = [sender menu];
+    if (menu != nil)
+    {
+        count = [[menu itemArray] count];
+        itemArray = [menu itemArray];
+        for (item = 0; item < count; item++)  // unselect each item
+            [[itemArray objectAtIndex: item] setState: NSOffState];
+    }
+
+    // check the menu item
+    [sender setState: NSOnState];
+
+    // get the menu number
+    menu_number = [sender tag];
+
+    /* Calculate the speed */
+    speed = -1 * menu_number + 100;
+    cpu_throttle_set(speed);
+    COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%');
+}
+
 @end
 
 
@@ -1316,6 +1347,29 @@ int main (int argc, const char * argv[]) {
     [menuItem setSubmenu:menu];
     [[NSApp mainMenu] addItem:menuItem];
 
+    // Speed menu
+    menu = [[NSMenu alloc] initWithTitle:@"Speed"];
+
+    // The 100% menu item has to be checked at the start
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"100%" action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
+    [menuItem setTag: 100];
+    [menuItem setState: NSOnState];
+    [menu addItem: menuItem];
+
+    // Add the rest of the menu items
+    int p, percentage;
+    for (p = 9; p >= 0; p--)
+    {
+        percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item
+        menuItem = [[[NSMenuItem alloc]
+                   initWithTitle: [NSString stringWithFormat: @"%d%c", percentage, '%'] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
+        [menuItem setTag: percentage];
+        [menu addItem: menuItem];
+    }
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease];
+    [menuItem setSubmenu:menu];
+    [[NSApp mainMenu] addItem:menuItem];
+
     // Window menu
     menu = [[NSMenu alloc] initWithTitle:@"Window"];
     [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread
* [Qemu-devel] [PATCH v2] ui/cocoa.m: add Speed menu
@ 2016-12-29 19:37 Programmingkid
  0 siblings, 0 replies; 3+ messages in thread
From: Programmingkid @ 2016-12-29 19:37 UTC (permalink / raw)
  To: Peter Maydell, Eric Blake; +Cc: qemu-devel qemu-devel

Programs running inside of QEMU can sometimes use more CPU time than is really
needed. To solve this problem, we just need to throttle the virtual CPU. This
feature will stop laptops from burning up. 

This patch adds a menu called Speed that has menu items from 100% to 1% that
represent the speed options. 100% is full speed and 1% is slowest.

Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
---
v2 changes:
Fixed missing space with 'if' and 'for' structures.
Fixed tab damage.
Use loop to create menu items.
Changed name of menu items to use percentages.
Use tags to determine selected menu item.

 ui/cocoa.m | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 26d4a1c..772629e 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -35,6 +35,7 @@
 #include "sysemu/blockdev.h"
 #include "qemu-version.h"
 #include <Carbon/Carbon.h>
+#include "qom/cpu.h"
 
 #ifndef MAC_OS_X_VERSION_10_5
 #define MAC_OS_X_VERSION_10_5 1050
@@ -828,6 +829,7 @@ QemuCocoaView *cocoaView;
 - (void)openDocumentation:(NSString *)filename;
 - (IBAction) do_about_menu_item: (id) sender;
 - (void)make_about_window;
+- (void)adjustSpeed:(id)sender;
 @end
 
 @implementation QemuCocoaAppController
@@ -1234,6 +1236,35 @@ QemuCocoaView *cocoaView;
     [superView addSubview: copyright_label];
 }
 
+/* Used by the Speed menu items */
+- (void)adjustSpeed:(id)sender
+{
+    int speed, menu_number, count, item;
+    NSMenu *menu;
+    NSArray *itemArray;
+
+    // uncheck all the menu items in the Speed menu
+    menu = [sender menu];
+    if (menu != nil)
+    {
+        count = [[menu itemArray] count];
+        itemArray = [menu itemArray];
+        for (item = 0; item < count; item++)  // unselect each item
+            [[itemArray objectAtIndex: item] setState: NSOffState];
+    }
+
+    // check the menu item
+    [sender setState: NSOnState];
+
+    // get the menu number
+    menu_number = [sender tag];
+
+    /* Calculate the speed */
+    speed = -1 * menu_number + 100;
+    cpu_throttle_set(speed);
+    COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%');
+}
+
 @end
 
 
@@ -1316,6 +1347,29 @@ int main (int argc, const char * argv[]) {
     [menuItem setSubmenu:menu];
     [[NSApp mainMenu] addItem:menuItem];
 
+    // Speed menu
+    menu = [[NSMenu alloc] initWithTitle:@"Speed"];
+
+    // The 100% menu item has to be checked at the start
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"100%" action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
+    [menuItem setTag: 100];
+    [menuItem setState: NSOnState];
+    [menu addItem: menuItem];
+
+    // Add the rest of the menu items
+    int p, percentage;
+    for (p = 9; p >= 0; p--)
+    {
+        percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item
+        menuItem = [[[NSMenuItem alloc]
+                   initWithTitle: [NSString stringWithFormat: @"%d%c", percentage, '%'] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease];
+        [menuItem setTag: percentage];
+        [menu addItem: menuItem];
+    }
+    menuItem = [[[NSMenuItem alloc] initWithTitle:@"Speed" action:nil keyEquivalent:@""] autorelease];
+    [menuItem setSubmenu:menu];
+    [[NSApp mainMenu] addItem:menuItem];
+
     // Window menu
     menu = [[NSMenu alloc] initWithTitle:@"Window"];
     [menu addItem: [[[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"] autorelease]]; // Miniaturize
-- 
2.7.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-06-13 18:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-09 15:02 [Qemu-devel] [PATCH v2] ui/cocoa.m: add Speed menu Programmingkid
2017-06-13 18:03 ` Peter Maydell
  -- strict thread matches above, loose matches on Subject: below --
2016-12-29 19:37 Programmingkid

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).