From patchwork Wed Jun 14 03:17:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Programmingkid X-Patchwork-Id: 9785185 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 54FCC60325 for ; Wed, 14 Jun 2017 03:18:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1085C284B0 for ; Wed, 14 Jun 2017 03:18:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 04D37284C7; Wed, 14 Jun 2017 03:18:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 68EAE284B0 for ; Wed, 14 Jun 2017 03:18:26 +0000 (UTC) Received: from localhost ([::1]:46178 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKyp6-0002fh-Ur for patchwork-qemu-devel@patchwork.kernel.org; Tue, 13 Jun 2017 23:18:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56585) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKyoU-0002fR-CJ for qemu-devel@nongnu.org; Tue, 13 Jun 2017 23:17:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dKyoR-0006n6-81 for qemu-devel@nongnu.org; Tue, 13 Jun 2017 23:17:46 -0400 Received: from mail-it0-x244.google.com ([2607:f8b0:4001:c0b::244]:33870) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dKyoR-0006mb-1n for qemu-devel@nongnu.org; Tue, 13 Jun 2017 23:17:43 -0400 Received: by mail-it0-x244.google.com with SMTP id 201so14557248itu.1 for ; Tue, 13 Jun 2017 20:17:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:content-transfer-encoding:subject:date:message-id:cc:to :mime-version; bh=M6b9RTqNq2h035KgWYDPOI+FjttWK6hzKogvlTFSYgo=; b=QCRokHOMSfJZfS31yOPps8cwlVdbBoe3EE0n4jEUkpoY2TzQMrY1Surizl0zU87xG4 P1+nBiGW5v3le7dRHytc5SKH3jvE2TiIgU0Ww1F2904zclwdwxqyWr2nG5CPc4C0ZluY KV+Br/nH5Ls4rluGkZKY+y/N3yW8pPStMUALjporzB84GNIbujm1TD6DxNMGuttfzav5 Pf2NgoKug/dv/dmwXc7mnK2acTxhut4VP9YYTjoNf5qxUYIYWayZcbFDQxwQIw247BwG nQTxLdnUUvtRGjkiWpkzTxSRLCPMHTj9zicRsh1iSAZs/9uzX5cuOTGfsoxz3gV87+Db IOZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:content-transfer-encoding:subject:date :message-id:cc:to:mime-version; bh=M6b9RTqNq2h035KgWYDPOI+FjttWK6hzKogvlTFSYgo=; b=J/C7AHFC3RE4RUMDxKZOnFg+EF3SGoF86BbF2NC+IjBOHUtBosTmeOHCGf+Iz/ZM8T WCR1u7nVe8T+7gWkkEVm/YeKVuT6PMUxYmEn1dffjU7lhexY4Hme6Q66Rc5AgbOfGaP1 +nOwfwPIbWNnS6m2fK73h4qT1ePXzr59Y8Mkr+Nmd6oPTtfDrc91etsfD1Qv3BXe+jQj 4ZqiIGwpdTBab9y6Ne+d/u15NgCePsQxjC1IFyIi7kNWMjBkx300Bt3xv7LeAK7ubwqw xng+7a0N7k+tf6a10+IAmp+2zGPdW8MAWj3nb+r2Ml/zrIJ05HnswuAfn8ghhLpIL8k8 RbFw== X-Gm-Message-State: AKS2vOzN8CnRVIB9zj+SQBdChoOGPES2GXIDoHVWzTZHe2Az6gLrpFZA DPJeJVPDpyHpjw== X-Received: by 10.36.98.206 with SMTP id d197mr15014554itc.50.1497410260871; Tue, 13 Jun 2017 20:17:40 -0700 (PDT) Received: from [192.168.0.6] (d199-74-164-53.col.wideopenwest.com. [74.199.53.164]) by smtp.gmail.com with ESMTPSA id z33sm7433813ioi.37.2017.06.13.20.17.39 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 13 Jun 2017 20:17:40 -0700 (PDT) From: Programmingkid Date: Tue, 13 Jun 2017 23:17:38 -0400 Message-Id: To: Peter Maydell , Gerd Hoffmann Mime-Version: 1.0 (Apple Message framework v1084) X-Mailer: Apple Mail (2.1084) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4001:c0b::244 Subject: [Qemu-devel] [PATCH v3] ui/cocoa.m: add Speed menu X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "qemu-devel@nongnu.org qemu-devel" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- v3 changes: Changed the item deselection loop. Moved speed calculation equation to main function. Replaced "%c" with "%%". Changed how 100% menu item is made. 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 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/ui/cocoa.m b/ui/cocoa.m index 004ec27..a866a66 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -35,6 +35,7 @@ #include "sysemu/blockdev.h" #include "qemu-version.h" #include +#include "qom/cpu.h" #ifndef MAC_OS_X_VERSION_10_5 #define MAC_OS_X_VERSION_10_5 1050 @@ -857,6 +858,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 @@ -1263,6 +1265,34 @@ QemuCocoaView *cocoaView; [superView addSubview: copyright_label]; } +/* Used by the Speed menu items */ +- (void)adjustSpeed:(id)sender +{ + int throttle_pct; /* throttle percentage */ + NSMenu *menu; + + menu = [sender menu]; + if (menu != nil) + { + /* Unselect the currently selected item */ + for (NSMenuItem *item in [menu itemArray]) { + if (item.state == NSOnState) { + [item setState: NSOffState]; + break; + } + } + } + + // check the menu item + [sender setState: NSOnState]; + + // get the throttle percentage + throttle_pct = [sender tag]; + + cpu_throttle_set(throttle_pct); + COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(), '%'); +} + @end @@ -1345,6 +1375,31 @@ int main (int argc, const char * argv[]) { [menuItem setSubmenu:menu]; [[NSApp mainMenu] addItem:menuItem]; + // Speed menu + menu = [[NSMenu alloc] initWithTitle:@"Speed"]; + + // Add the rest of the Speed menu items + int p, percentage, throttle_pct; + for (p = 10; p >= 0; p--) + { + percentage = p * 10 > 1 ? p * 10 : 1; // prevent a 0% menu item + + if (percentage == 100) { + [menuItem setState: NSOnState]; + } + menuItem = [[[NSMenuItem alloc] + initWithTitle: [NSString stringWithFormat: @"%d%%", percentage] action:@selector(adjustSpeed:) keyEquivalent:@""] autorelease]; + + /* Calculate the throttle percentage */ + throttle_pct = -1 * percentage + 100; + + [menuItem setTag: throttle_pct]; + [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